树状数组 简单题 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的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
随机推荐
- qq开放平台可以应用到网页游戏的api整理
创建角色界面api整理 一.需求描述 1. 创建角色名称可以用qq空间昵称代替 2. 如果玩家是在新区玩的话,赠送老玩家支持礼包 3. 可以看到,好友xxx也在玩,而且到了多少等级,如果加为好友 ...
- 【废弃中】JavaScript 内置Object
创建: 2017/09/24 更新: 2018/01/22 增加window对象内容的链接 更改标题: [JavaScript 主要的自带Object] -> [JavaScript 内置Obj ...
- Ogre 整体框架入门
ogre 是面向对象的3d图形引擎. root 是引擎的一个界面类,包含很多快捷的调用其他类的接口. 在ogre中,广泛的使用了单件模式,同时最大的保证了你不需要自己管理资源,除了是你自己new的对象 ...
- 自定义Mybatis返回类型及注意事项
一.自定义返回拦截器package com.yaoex.crm.service.util; import org.apache.ibatis.session.ResultContext;import ...
- assembly x86(nasm)画三角形等图形的实现
参考了一位大佬的博客 https://blog.csdn.net/qq_40298054/article/details/84496944传送门 https://blog.csdn.net/qq_40 ...
- WKWebView简单使用
#import <WebKit/WebKit.h> @interface SchoolOverviewsViewController ()<WKUIDelegate,WKNaviga ...
- 深度学习环境搭建(Ubuntu16.04+GTX1080Ti+CUDA8.0+Cudnn6.0+TensorFlow+Caffe2(Pytorch))
OS System:Ubuntu16.04 GPU Device:GTX1080Ti Softwares:CUDA8.0.Cudnn6.0.TensorFlow(1.4.0).Caffe2(1.0.0 ...
- C# 面向对象之面向接口
接口的定义 与类不同的是接口用interface关键字 (1)接口中所有成员不能添加任何修饰符,默认为public,如果显示指定修饰符将会出现编译错误; (2)接口中不能包含字段.运算符重载.实例构造 ...
- POJ-325Corn Fields
链接:https://vjudge.net/problem/POJ-3254#author=freeloop 题意: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ ...
- Bear and Tower of Cubes Codeforces - 680D
https://codeforces.com/contest/680/problem/D 一道2D,又是搞两个小时才搞出来...不过好在搞出来了 官方题解:可以证明对于m,设a是满足a^3<=m ...