HDU 5372 Segment Game - 离散化&树状数组
来源:互联网 发布:edius有mac版吗 编辑:程序博客网 时间:2024/06/08 10:25
题目描述
表示看错题目的一小部分,wa了两个小时!!!
题目大意:
有 n 个操作,每次要么插入一条线段,要么删除一条已存在的线段,其中第 i 次插入的线段的长度为 i。
对于每次插入,输出当前插入的线段能完整覆盖多少条线 段。
1 ≤n≤ 700000。
Source:2015 Multi-University Training Contest 7
分析(From Claris):
- 其实就是询问左端点在 l 右侧且右端点在 r 左侧的线段数。
- CDQ 分治?树套树?O(nlog2n) 的复杂度并不能承受。
- ans = 左端点在 l 右侧的线段数 − 左端点在 l 右侧且右端点 在 r 右侧的线段数。
- 因为线段长度递增,所以之前线段如果右端点在 r 右侧,那 么左端点一定在 l 右侧。
- 故 ans = 左端点在 l 右侧的线段数 − 右端点在 r 右侧的线 段数。两棵树状数组维护即可。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MAXN 200000int n,b[MAXN*2+10],cntb,qua[MAXN+10][3],add[MAXN+10],cntad,c1[MAXN*2+10],c2[MAXN*2+10],sum1,sum2;void Init(){ memset(b,0,sizeof b); memset(c1,0,sizeof c1); memset(c2,0,sizeof c2); cntad=cntb=0; sum1=sum2=0;}void read(){ for(int i=1;i<=n;i++){ scanf("%d%d",&qua[i][0],&qua[i][1]); if(!qua[i][0]){ add[++cntad]=i; b[++cntb]=qua[i][1]; b[++cntb]=qua[i][1]+cntad; } } sort(b+1,b+cntb+1); cntb=unique(b+1,b+cntb+1)-(b+1); for(int i=1,k=0;i<=n;i++){ if(qua[i][0]) continue; ++k; int tmp=qua[i][1]; qua[i][1]=lower_bound(b+1,b+cntb+1,tmp)-b; qua[i][2]=lower_bound(b+1,b+cntb+1,tmp+k)-b; }}int lowbit(int x){ return x&(-x);}void Update(int *c,int x,int d){ while(x<=cntb){ c[x]+=d; x+=lowbit(x); }}int Getsum(int *c,int x){ int ret=0; while(x){ ret+=c[x]; x-=lowbit(x); } return ret;}int main(){ int cas=0; while(scanf("%d",&n)==1){ printf("Case #%d:\n",++cas); Init(); read(); for(int i=1;i<=n;i++){ if(!qua[i][0]){ printf("%d\n",(sum1-Getsum(c1,qua[i][1]-1))-(sum2-Getsum(c2,qua[i][2]))); Update(c1,qua[i][1],1); sum1++; Update(c2,qua[i][2],1); sum2++; } else{ int id=add[qua[i][1]]; Update(c1,qua[id][1],-1); sum1--; Update(c2,qua[id][2],-1); sum2--; } } }}
0 0
- Segment Game (hdu 5372 树状数组+离散化)
- hdu 5372 Segment Game(树状数组+离散化)
- hdu 5372 Segment Game (树状数组+离散化)
- HDU 5372 Segment Game (树状数组+离散化)
- HDU 5372 Segment Game (MUT#7 树状数组+离散化)
- HDU 5372 Segment Game - 离散化&树状数组
- 树状数组,离散化,突破口(Segment Game,HDU 5372)
- HDOJ 5372 Segment Game 树状数组+离散化
- HDU 5372 Segment Game 树状数组
- 【树状数组】 HDU 5372 Segment Game
- hdu 5372 Segment Game(树状数组)
- HDU 5372 - Segment Game(树状数组)
- 【HDU 5372】Segment Game(树状数组)
- HDU 5372 Segment Game 树状数组
- HDU 5372 Segment Game(树状数组)
- [HDU 5372] Segment Game (树状数组)
- [树状数组] hdu 5372 Segment Game
- hdu-5372 Segment Game(离散树状数组)(2015多校contest 7)
- python的元组和列表使用之一
- 判断浏览器的方法
- web学习博客2016/2/19
- iOS屏幕适配
- 详说 Cookie, LocalStorage 与 SessionStorage
- HDU 5372 Segment Game - 离散化&树状数组
- 资料整理工具推荐mybase,本人使用
- c语言入门之项目2.0——成绩等级
- markdown学习3:使用Sublime Text作为Markdown编辑器
- WF4.0持久化
- sql语句批量替换字段数据
- 有关l2,1范数作用的理解
- css之几个细节
- codeforces 618d 搜索