Tufurama CodeForces - 961E
Tufurama CodeForces - 961E
题意:有一部电视剧有n季,每一季有ai集。问有多少对i,j存在第i季第j集也同时存在第j季第i集。
思路:核心问题还是统计对于第i季,你要统计第i行(存在多少数量,要大于i)。
线段树的维护相对而言比较暴力,树状数组的话,一开始全是1,一旦某个数过小,就会导致不构成贡献,移除就好。
线段树
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 2e5+;
const int mod = 1e9+;
int a[maxn];
vector<int>stk[maxn<<];
void build(int x,int l,int r)
{
if(l == r)
{
stk[x].push_back(a[l]);
return;
}
for(int i = l;i<=r;i++)
stk[x].push_back(a[i]);
sort(stk[x].begin(),stk[x].end());
int mid = (l+r)/;
build(x<<,l,mid);
build(x<<|,mid + ,r);
}
int query(int L,int R,int l,int r,int i)
{
if(L > R)
return ;
if(L<=l && r<=R)
return stk[i].size() - (lower_bound(stk[i].begin(),stk[i].end(),L-) - stk[i].begin());
int ans = ;
int mid = (l+r)/;
if(L <= mid)
ans += query(L,R,l,mid,i<<);
if(R > mid)
ans += query(L,R,mid+,r,i<<|);
return ans;
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
build(,,n);
ll ans = ;
for(int i=;i<n;i++)
ans += query(i+,min(a[i],n),,n,);
printf("%lld\n",ans);
}
树状数组
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = 2e5+;
const int mod = 1e9+; const int N=2e5+;
int a[N],f[N];
struct node{
int x,y;
}p[N];
bool cmp(node a,node b)
{
if(a.y==b.y) return a.x<b.x;
return a.y<b.y;
}
void add(int x,int y)
{
while(x<N){
f[x]+=y;
x+=x&-x;
}
}
int query(int x)
{
int ans=;
while(x){
ans+=f[x];
x-=x&-x;
}
return ans;
}
int main ()
{
int n;scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),a[i]=min(a[i],n),p[i].x=i,p[i].y=a[i],add(i,);
sort(p+,p++n,cmp);
long long ans=;
int now=;
for(int i=;i<=n;i++){
while(now<=n && p[now].y<i) add(p[now++].x,-);
ans+=query(a[i]);
if(a[i]>=i) ans--;
}
printf("%lld\n",ans/);
return ;
}
Tufurama CodeForces - 961E的更多相关文章
- Tufurama CodeForces - 961E (cdq分治)
题面 One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series " ...
- Codeforces 961E - Tufurama
961E - Tufurama 思路: 线段树或者分块 遍历 1 - n - 1,求 区间[i + 1, min(a[i], n)]大于等于 i 的个数,累加起来 线段树: #include<b ...
- 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, ...
- Codeforces 961E - Tufurama 树状数组
转自:https://blog.csdn.net/my_sunshine26/article/details/79831362 题目大意: i从1开始 基本思路: 完全没思路,所以上来就二分,果不其然 ...
- Codeforces 961E 主席树
题意: 给出一个n个数的序列,求有几对(i,j)满足a[i]>=j&&a[j]>=i,(i,j)和(j,i)只能算一对. 考虑第i个数会有几个j(j<i)满足条件,首 ...
- 【树状数组】CF961E Tufurama
挺巧妙的数据结构题(不过据说这是一种套路? E. Tufurama One day Polycarp decided to rewatch his absolute favourite episode ...
- Educational Codeforces Round 41 E. Tufurama (961E)
[题解] 第一眼看题飞快地想到一种做法,然后假掉了. 这道题其实是主席树的模板题来着.但是也有别的水法. 我们可以发现每个位置的查询区间是[1,min(a[i],i-1)],所以我们可以把查询区间按照 ...
- Educational Codeforces Round 41 967 E. Tufurama (CDQ分治 求 二维点数)
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama (CDQ分治 求 二维点数) time limit per test 2 ...
- Codeforces 961 E Tufurama
Discription One day Polycarp decided to rewatch his absolute favourite episode of well-known TV seri ...
随机推荐
- IIS下FTP服务器的PASV端口范围修改方法
应该有不少使用IIS自带的FTP服务器,IIS的FTP里的PASV模式下默认端口范围1024 - 65535,连接时会从中随机选择到响应.这样的超大范围就给服务器安全带来的隐患. 虽然可以通过一些方法 ...
- easyUI 鼠标悬停某行提示
最近参与公司的电子档案系统的开发,需求是需要用户鼠标悬停某一行时,需要根据后台业务数据进行提示. 档案系统开发采用的框架是struts2+mybatis+spring+easyUI开发,而前端的e ...
- 浅析System.Console.WriteLine()
浅析System.Console.WriteLine() Writeline()函数的功能向 StreamWriter 类写入指定字符串和一行字符,共有19个重载,其与Write()函数的主要区别在于 ...
- Json Web Token 简介
1.Json Web Token是干什么 简称JWT,在HTTP通信过程中,进行身份认证. 我们知道HTTP通信是无状态的,因此客户端的请求到了服务端处理完之后是无法返回给原 ...
- 会话跟踪之Cookie
一:简介 Cookie主要用来存储用户信息,追踪用户会话.web客户端访问服务端直接采用的协议是Http,Http协议是一种无状态协议,无状态表现在不能够保留用户访问状态,无法记录用户信息.每一次的请 ...
- tas5717/5719功放问题点总结
问题一 AMP output channel invert problem and modification 原因:如果硬件设计时,耳机或者喇叭的输出通道反了,就要对其进行左右对换. 对策:如果通道反 ...
- 初学者:Git常用命令总结
git init 在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个repo,并在当前文件夹下创建一个.git文件夹. git clone 获取一个u ...
- Java interface和abstract小记
一.abstract 用abstract修饰的类叫做抽象类,用abstract修饰的方法叫抽象方法. 含有抽象方法的类必须被声明为抽象类,抽象类必须被继承,抽象方法必须被重写. 抽象类不能被实例化. ...
- 手机端@media screen布局自适应
@media only screen and (min-width: 310px) and (max-width: 360px) { }@media only screen and (min-widt ...
- jquery实现加载更多效果
情况是当滑动条滑动到最底部的时候,数据显示出一部分的更多 思路:获取到浏览器屏幕的高度client,文档的高度h和滑动距离顶部的距离scroll,当h<=client+scroll的时候就是滑动 ...