树状数组 简单题 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的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
随机推荐
- LeetCode: 521 Longest Uncommon Subsequence I(easy)
题目: anysubsequence of the other strings. A subsequence is a sequence that can be derived from one se ...
- JAVA基础--JAVA API集合框架(其他集合类,集合原理)15
一.ArrayList介绍 1.ArrayList介绍 ArrayList它是List接口的真正的实现类.也是我们开发中真正需要使用集合容器对象. ArrayList类,它是List接口的实现.肯定拥 ...
- KM算法萌新讲解篇
KM算法 首先了解问题:也就是最大权值匹配: 二分图里,边带了权值,求整幅图里匹配最大/最小的权值 因为接触匈牙利算法的时候看的是找对象系列的博文,所以也自己写一发找对象的博文吧: 算法背景: 信 ...
- unity2d 动画
1 资源test.jpg(如下)放入Resources文件夹 2 切割图片 点击图片,在inspector中,选择Texture Type为Sprite(2D and UI),然后点击Sprite E ...
- hdu1848(sg函数打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 题意:中文题诶- 思路:直接sg函数打表就好了 代码: #include <iostrea ...
- ISO 8 自适应cell
原文网址: http://www.cocoachina.com/ios/20141218/10687.html 在使用 table view 的时侯经常会遇到这样的需求:table view 的 ce ...
- 基于 Laravel 开发 ThinkSNS+ 中前端的抉择(webpack/Vue)踩坑日记
在上一篇文章< ThinkSNS+基于Laravel master分支,从1到 0,再到0.1>,简单的介绍了 ThinkSNS+ ,这里分享在开发过程中,前端选择的心理活动. Larav ...
- 笔记-JavaWeb学习之旅7
JavaScript基础 概念:一门客户端脚本语言,运行在客户端浏览器中,每一个浏览器都有JavaScript的解析引擎,是一个脚本语言,不需要编译,直接就可以被浏览器解析执行. JavaScript ...
- RPC跟MQ之间的差异比较
在阿里的平台技术部参与开发了Dubbo(远程调用服务)和Napoli(消息解决方案),又给网站应用支持这2个产品很长一段时间,了解了这2个产品的实现及应用对这两个产品的用法. 大部分情况下,“给定场景 ...
- error: unrecognized command line option "-std=c11" 解决办法
今天在安装php版本 grpc扩展的时候报错如下: cc1: error: unrecognized command line option "-std=c11" cc1: war ...