树状数组 简单题 cf 961E
题目链接 : https://codeforces.com/problemset/problem/961/E
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 x episode y and season y episode 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.
5 1 2 3 4 5
0
3 8 12 7
3
3 3 2 1
2
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).
题目大意 : (首先这个题有一个电视剧) 输入n (代表一共有 多少季)之后有n个数每个数 代表这季有多少集,如果 x 季 y集 和 y 季 x集能
同时被找到就说明有一个错误。例如 n = 2 a1=2 a2=3 有 1 季 2 集 和 2 季 1 集 说明有一个错误则输出 1 .
思路 : 保证 ax>=y x<y ay>=x 这三个条件,如果只是前两个的话树状数组完全可以满足,但如果加上第三个条件的话,树状数组没法满足
所以想到先把第三个条件预处理出来存到vector中,然后依次解决.vector中压入v [ min( i-1, a [ i ] ) ] . push_back( i ),为什么要这么
写呢首先a[ i ] =min ( a[ i ] , n ),这个不难理解因为如果a [ i ] > n 的话比n 大的没有意义因为最多就到n季。然后为什么vector中要
以 min ( i - 1 , a [ i ] ) 为一维呢,这里是指当前 i 能到达的极限,极限有两种情况{ 1.如果ai大的话可以通过之后的 i 来查找当前剩
余的ai,2.如果 i 大的话说明当前这个值最多能达到ai(可以手动推一下) } 那为什么是 i -1 呢?因为如果 i = 5 , a5=3 的话 i 要从 4
开始查因为和自己查没有意义还会出错.
这里要压入 i 是为了之后找 ax>=i 因为在树状数组求逆序数是 sum(MAX_N)-sum ( ai ) , 这个是求ax>ai,而我们要求的是 ax> = i
所以我们变成了 sum(MAX_N)-sum(i),这里 x < i 一直成立.
还有就是ai的范围是1e9 太大了,在这道题中当 ai>n 和ai=n 是等价的,因为一共就n那么大,如果ai比n大的话找不到更大的和ai对应
所以是等价的.
#include<bits/stdc++.h>
using namespace std;
int x[];
int szsz[];
int m,n;
vector <int> vic[];
int lowbit(int a){
return a&(-a);
}
void add(int a){
for(int i=a;i<=;i+=lowbit(i)){
szsz[i]+=;
}
}
int qiuhe(int a){
int ans=;
for(int i=a;i>=;i-=lowbit(i)){
ans+=szsz[i];
}
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&x[i]);
x[i]=min(n,x[i]);
vic[min(i-,x[i])].push_back(i);
}
memset(szsz,,sizeof(szsz));
long long sum=;
for(int i=;i<=n;i++){
add(x[i]);
for(int j=;j<vic[i].size();j++){
sum+=qiuhe(n)-qiuhe(vic[i][j]-);
}
}
printf("%lld\n",sum);
return ;
}
树状数组 简单题 cf 961E的更多相关文章
- st表树状数组入门题单
预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...
- HDU 1166 敌兵布阵(线段树/树状数组模板题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- bzoj1103树状数组水题
(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) #i ...
- 树状数组训练题1:弱弱的战壕(vijos1066)
题目链接:弱弱的战壕 这道题似乎是vijos上能找到的最简单的树状数组题了. 原来,我有一个错误的思想,我的设计是维护两个树状数组,一个是横坐标,一个是纵坐标,然后读入每个点的坐标,扔进对应的树状数组 ...
- UESTC 1584 Washi与Sonochi的约定【树状数组裸题+排序】
题目链接:UESTC 1584 Washi与Sonochi的约定 题意:在二维平面上,某个点的ranked被定义为x坐标不大于其x坐标,且y坐标不大于其y坐标的怪物的数量.(不含其自身),要求输出n行 ...
- 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)
思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...
- 【树状数组 思维题】luoguP3616 富金森林公园
树状数组.差分.前缀和.离散化 题目描述 博艾的富金森林公园里有一个长长的富金山脉,山脉是由一块块巨石并列构成的,编号从1到N.每一个巨石有一个海拔高度.而这个山脉又在一个盆地中,盆地里可能会积水,积 ...
- Lightoj 1112 - Curious Robin Hood 【单点改动 + 单点、 区间查询】【树状数组 水题】
1112 - Curious Robin Hood PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
随机推荐
- E20180506-hm
更新: 2019/02/19 原来忘记分类,把此博文归入单词类 criterion n. 规范; (批评.判断等的) 标准,准则; criteria n. (批评.判断等的) 标准,准则( crit ...
- SCUTOJ - 362 - CC的族谱 - 树上倍增
https://scut.online/p/362 和LCA差不多,注意开大点不怕浪费. #include<bits/stdc++.h> using namespace std; type ...
- hoj2662 Pieces Assignment
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- [Xcode 实际操作]七、文件与数据-(15)单例模式的使用
目录:[Swift]Xcode实际操作 本文将演示单例对象的使用. 在项目名称上点击鼠标右键,弹出右键菜单,选择[New File]新建文件命令, 在弹出的模板选项窗口中,选择[Swift]文件选项, ...
- pytest入门学习(2)
pytest的hello world pyt1.py def func(x): print (x+1); return x+1; def test_answer(): assert func(3) = ...
- bzoj1475:方格取数
传送门 最小割,这也是个经典题了,当初学最小割时没学会,这次算是理解了,首先二分图染色,将整个图分成黑色点和白色点,由于相邻的格子不能同时选,一个黑点一定对应四个白点,也就是我们只能选择这个黑点或者四 ...
- 洛谷P2599||bzoj1413 [ZJOI2009]取石子游戏
bzoj1413 洛谷P2599 根本不会啊... 看题解吧 #include<cstdio> #include<algorithm> #include<cstring& ...
- 基于阿里云SLB/ESS/EIP/ECS/VPC的同城高可用方案演练
今天基于阿里云SLB/ESS/EIP/ECS/VPC等产品进行了一次同城高可用方案演练: 基本步骤如下: 1. 在华东1创建VPC网络VPC1,在华东1可用区B和G各创建一个虚拟交换机vpc1_swi ...
- c 语言写的高级Oracle®数据库调优及监控工具
http://www.lab128.com.cn/lab128_why.html ###另外一款ORALCE Monitor tool freee https://www.myorasql.com/ ...
- 简单总结ConcurrentHashMap
一.HashTable hashTable是一个线程安全的容器,是线程安全版本的HashMap.但它的底层是和HashMap一样的,只是在方法上都加上了synchronized关键字. 这样子有什么后 ...