CF961E Tufurama【主席树】
CF961E Tufurama
题意翻译
题目描述
有一天Polycarp决定重看他最喜爱的电视剧《Tufurama》。当他搜索“在线全高清免费观看Tufurama第3季第7集”却只得到第7季第3集的结果时,他很惊讶。这让Polycarp感到疑惑——如果有天他决定重看整个系列却无法找到正确的剧集观看,那该怎么办呢?Polycarp现在想统计一下他被迫用不同方案搜索同一剧集的次数。
电视连续剧有n 季(从1 到n 编号),第i 季有ai 集(从1 到ai 编号)。Polycarp认为如果有一对x 和y (x<y),使第x 季第y 集、第y 季第x 集存在,那么其中一个搜索就会包含错误的内容。请帮助Polycarp统计这样的数对的数量吧!
输入输出格式
输入格式
第一行,一个整数n(1≤n≤2⋅105) ,表示季数。
第二行,n 个用空格隔开的整数a1,a2,...,an(1≤ai≤109) ,表示每一季的集数。
输出格式
只有一行,一个整数,表示x 和y (x<y ),使第x 季第y 集、第y 季第x 集存在的数对的数量。
说明
在样例2中可能的对数:
- x=1,y=2 (第1季第2集
第2季第1集)
- x=2,y=3 (第2季第3集
第3季第2集)
- x=1,y=3 (第1季第3集
第3季第1集)
在样例3中:
- x=1,y=2 (第1季第2集
第2季第1集)
- x=1,y=3 (第1季第3集
第3季第1集)
感谢@月见之兔 提供的翻译
题目描述
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method.
TV series have n seasons (numbered 1 through n ), the i -th season has ai episodes (numbered 1 through ai ). Polycarp thinks that if for some pair of integers x and y ( x<y ) exist both season xepisode y and season yepisode x then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs!
输入输出格式
输入格式:
The first line contains one integer n (1<=n<=2⋅105) — the number of seasons.
The second line contains n integers separated by space a1,a2,...,an (1<=ai<=109) — number of episodes in each season.
输出格式:
Print one integer — the number of pairs x and y ( x<y ) such that there exist both season x episode y and season y episode x .
输入输出样例
说明
Possible pairs in the second example:
- x=1 , y=2 (season 1 episode 2
season 2 episode 1);
- x=2 , y=3 (season 2 episode 3
season 3 episode 2);
- x=1 , y=3 (season 1 episode 3
season 3 episode 1).
In the third example:
- x=1 , y=2 (season 1 episode 2
season 2 episode 1);
- x=1 , y=3 (season 1 episode 3
season 3 episode 1).
Solution
哭哭 多久没遇到这种不用看题解就能a掉的紫题了QAQ
一句话题意:求有多少对$<i,j>$,满足$a[i]>=j,a[j]>=i,i<j$
然后把关系再理一下,就变成了,对于每一个$i$,求在区间$[i+1,min(n,a[i])]$内有多少个$j$满足$a[j]>=i$
然后就是主席树叻~
倒着遍历维护每一个版本,查询区间就是用区间两个端点的版本满足条件的个数相减得到答案。
要离散化~
Code
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std; const int N = ; int n, a[N], b[ * N]; struct Node {
Node *ls, *rs;
int pos, val;
void update() {
val = ls -> val + rs -> val;
}
} pool[*N], *root[N], *tail = pool, *zero; Node *newnode() {
Node *nd = ++ tail;
nd -> ls = zero;
nd -> rs = zero;
nd -> pos = ;
nd -> val = ;
return nd;
} Node *build(int l, int r) {
Node *nd = newnode();
if(l == r) return nd;
int mid = (l + r) >> ;
nd -> ls = build(l, mid);
nd -> rs = build(mid + , r);
nd -> update();
return nd;
} int query(Node *nd, int l, int r, int L, int R) {
if(l >= L && r <= R) return nd -> val;
int ans = ; int mid = (l + r) >> ;
if(L <= mid) ans += query(nd -> ls, l, mid, L, R);
if(R > mid) ans += query(nd -> rs, mid + , r, L, R);
return ans;
} Node *insert(Node *nd, int l, int r, int pos) {
Node *nnd = newnode();
if(l == r) {
nnd -> val = nd -> val + ; return nnd;
}
int mid = (l + r) >> ;
if(pos <= mid) {
nnd -> rs = nd -> rs;
nnd -> ls = insert(nd -> ls, l, mid, pos);
} else {
nnd -> ls = nd -> ls;
nnd -> rs = insert(nd -> rs, mid + , r, pos);
}
nnd -> update();
return nnd;
} int main() {
zero = ++ tail;
zero -> ls = zero; zero -> rs = zero; zero -> pos = ; zero -> val = ;
scanf("%d", &n);
int t = ;
for(int i = ; i <= n; i ++) {
scanf("%d", &a[i]);
b[++ t] = a[i]; b[++ t] = i;
}
sort(b + , b + + t);
int m = unique(b + , b + + t) - b - ;
root[n + ] = build(, m);
long long ans = ;
for(int i = n; i >= ; i --) {
int pos = lower_bound(b + , b + + m, a[i]) - b;
int posi = lower_bound(b + , b + + m, i) - b;
int R = min(n, pos);
root[i] = insert(root[i + ], , m, pos);
if(R < i) continue;
int tmp1 = query(root[i + ], , m, posi, m);
int tmp2 = query(root[R + ], , m, posi, m);
ans += tmp1 - tmp2;
}
printf("%I64d", ans);
}
CF961E Tufurama【主席树】的更多相关文章
- CF961E Tufurama 主席树
对原问题进行转化 考虑对每个$i$,询问在$j \in [i + 1, a[i]]$中满足$a[j] \geqslant i$的个数 这样子可以做到不重不漏 个数满足差分的性质,使用主席树来维护即可 ...
- 2018.12.05 codeforces 961E. Tufurama(主席树)
传送门 一眼主席树sbsbsb题(%%%树状数组大佬们). 简化题意:求满足x<y,y≤ax,x≤ayx<y,y\le a_x,x\le a_yx<y,y≤ax,x≤ay的(x, ...
- 【树状数组】CF961E Tufurama
挺巧妙的数据结构题(不过据说这是一种套路? E. Tufurama One day Polycarp decided to rewatch his absolute favourite episode ...
- EC Round 41 (Rated for Div. 2)主席树 E. Tufurama
简单分析一下,对于x<y,求a[x]>=y 同时a[y]>=x 再简化一下,求1-a[y]区间内大于>=y的个数...主席树牛逼 #include<iostream> ...
- [CF961E] Tufurama
Description: 有一天Polycarp决定重看他最喜爱的电视剧<Tufurama>.当他搜索"在线全高清免费观看Tufurama第3季第7集"却只得到第7季第 ...
- bzoj3207--Hash+主席树
题目大意: 给定一个n个数的序列和m个询问(n,m<=100000)和k,每个询问包含k+2个数字:l,r,b[1],b[2]...b[k],要求输出b[1]~b[k]在[l,r]中是否出现. ...
- bzoj1901--树状数组套主席树
树状数组套主席树模板题... 题目大意: 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[ ...
- BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
3626: [LNOI2014]LCA Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2050 Solved: 817[Submit][Status ...
- BZOJ 1146: [CTSC2008]网络管理Network [树上带修改主席树]
1146: [CTSC2008]网络管理Network Time Limit: 50 Sec Memory Limit: 162 MBSubmit: 3522 Solved: 1041[Submi ...
随机推荐
- HADOOP百度云资料
百度云下载地址: 链接:http://pan.baidu.com/s/1pL56hkv 密码:u4h3 解压密码:www.mukedaba.com
- j-linkV8固件更新(任意版本)
在使用j-link v8调试程序时,容易出现固件丢失或出错的情况,导致电脑不能识别,j-link上面的灯不亮.我今天刚刚遇到了这个情况,于是就拆开外壳,在网上搜索资料,发现刷固件相关的还真多,但是有一 ...
- 制作macOS10.12系列的系统镜像文件
制作macOS10.12系列的系统镜像文件步骤,过程也比较简单,十来个命令.以10.12.6为例,首先,在苹果商店下载系统安装包APP,或者网上下载后把安装APP复制到 应用程序 文件夹. 然后打 ...
- gitlab备份与还原
1.备份 登录原服务器,执行命令: gitlab-rake gitlab:backup:create 备份后文件在如下目录,下载该文件 /var/opt/gitlab/backups 2.还原 先安装 ...
- OKR.2019
转眼又一年过去了,回顾审视一年的得失,规划下一年的奋斗目标.Review And Planning,让全新的2019迎来全新的自己. O1 学习软件开发技术知识 KR1.1 阅读<CLR via ...
- Linux网络综合命令——IP
1.作用 ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具,例如ifconfig.route等,使用权限为超级用户.几乎所有的Linux发行版本都支持该命令. ...
- [android] The_connection_to_adb_is_down__and_a_severe_error_has_occured解决方案
初学安卓,这是我碰到的第一个问题,从网上找了些解决方法,同时也把问题解决了. 方案一 1.先把eclipse关闭. 2.在管理器转到你的android SDK 的platform-tools下, 如图 ...
- 面试题:输入两个整数 n 和 m,从数列1,2,3…….n 中 随意取几个数, 使其和等于 m
问题: 2010年中兴面试题 编程求解: 输入两个整数 n 和 m,从数列1,2,3…….n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来. 思路: 类似这种组合问题一般都是使 ...
- hdu 5137 去掉一个点 使得最短路最大(2014广州现场赛 K题)
题意:从2~n-1这几个点中任意去掉一个点,使得从1到n的最短路径最大,如果任意去掉一个点1~n无通路输出Inf. Sample Input4 51 2 31 3 71 4 502 3 43 4 23 ...
- Iterator 迭代器
意图 提供一种方法顺序访问一个聚合对象中各个元素 , 而又不需暴露该对象的内部表示. 动机 一个聚合对象, 如列表(list), 应该提供一种方法来让别人可以访问它的元素,而又不需暴露它的内部结构 迭 ...