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 ...
随机推荐
- 使用httpClient调用接口,参数用map封装或者使用JSON参数,并转换返回结果
这里接口用表存起来,标记请求方式,然后接受参数,消息或者请求参数都可以, 然后先是遍历需要调用的接口,封装参数,再分别调用get与post即可,没有微服务还是得自己写 //消息转发-获取参数中对应参数 ...
- 洛谷 P3307: bzoj 3202: [SDOI2013] 项链
题目传送门:洛谷P3307.这题在bzoj上是权限题. 题意简述: 这题分为两个部分: ① 有一些珠子,每个珠子可以看成一个无序三元组.三元组要满足三个数都在$1$到$m$之间,并且三个数互质,两个珠 ...
- [转载]如何在C++03中模拟C++11的右值引用std::move特性
本文摘自: http://adamcavendish.is-programmer.com/posts/38190.htm 引言 众所周知,C++11 的新特性中有一个非常重要的特性,那就是 rvalu ...
- nodejs 接收上传的图片
1.nodejs接收上传的图片主要是使用formidable模块,服务器是使用的express搭建. 引入formidable var formidable = require('./node_mod ...
- CentOS7 修改网卡名称
vi /etc/sysconfig/grub 增加net.ifnames=0 biosdevname=0 执行:grub2-mkconfig -o /boot/grub2/grub.cfg
- vue总结 03过滤器
过滤器 Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持).过滤器应该被添加在 JavaS ...
- python图片处理和matlab图片处理的区别
作者:波布兰链接:https://www.zhihu.com/question/28218420/answer/39904627来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- MySQL学习笔记:limit
MySQL支持用limit语句来设置查询的行数范围,同Hive. LIMIT a, b; a:从a+1开始: b:取b行数据: 1.取出第一行记录: ,; 2.取出第二行记录: ,; 3.取出从11开 ...
- vector 测试
vector 测试 */--> div.org-src-container { font-size: 85%; font-family: monospace; } pre.src { backg ...
- java8 - Optional
mport java.util.Optional; import org.junit.Test; /* * 一.Optional 容器类:用于尽量避免空指针异常 * Optional.of(T t) ...