2015 多校联赛 ——HDU5372(树状数组)
来源:互联网 发布:金利合软件 编辑:程序博客网 时间:2024/05/24 00:08
Sample Input
30 00 30 150 10 01 10 10 0
Sample Output
Case #1:000Case #2:0102
有0,1两个操作,0 x代表添加从x 到 x + i(带表第i次添加)的线段,每次添加时问被其覆盖的线段有多少。
1 x代表删除第i次添加的。
思路:每一次添加后,求出小于x的左节点个数x1,小于等于y的右节点个数x2。 x2- x1即可
改变单个节点,所以树状数组更加合适
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;#define N 200050#define mod 258280327int le[N],re[N],lr[N],rr[N];int oper[N],Left[N],Right[N],id[N];int c[N];int tot;int lowbit(int x){ return x&-x;}void update(int *c,int n,int k,int v){ while(k <= n) { c[k] += v; k += lowbit(k); }}int query(int *c ,int k){ int ans = 0; while(k > 0) { ans += c[k]; k -= lowbit(k); } return ans;}int main(){ //freopen("4.txt","r",stdin); int cas=1,L=1,l,ans,nul,nur,n,Q; while(scanf("%d",&n)!=EOF) { Q = nul = nur = 0; for(int i = 1; i <= n; i++) { scanf("%d%d",&oper[i],&l); if(oper[i] == 0) { id[++Q] = i; le[i] = l; re[i] = l+L; Left[nul++] = le[i]; Right[nur++] = re[i]; L++; } else { le[i] = l; } } printf("Case #%d:\n",cas++); memset(lr,0,sizeof(lr)); memset(rr,0,sizeof(rr)); sort(Left, Left + nul); nul = unique(Left, Left + nul) - Left; sort(Right, Right + nur); nur = unique(Right, Right + nur) - Right; for(int i = 1; i <= n; i++) { if(oper[i] == 0) { int x1 = lower_bound(Left,Left+nul,le[i]) - Left + 1; int x2 = lower_bound(Right,Right+nur,re[i]) - Right + 1; ans = query(rr,x2)-query(lr,x1-1); update(rr,nur,x2,1); update(lr,nul,x1,1); printf("%d\n",ans); } else { int v = id[le[i]]; int x1 = lower_bound(Left,Left+nul,le[v]) - Left + 1; int x2 = lower_bound(Right,Right+nur,re[v]) - Right + 1; update(lr,nul,x1,-1); update(rr,nur,x2,-1); } } } return 0;}
0 0
- 2015 多校联赛 ——HDU5372(树状数组)
- hdu5372(树状数组)
- hdu5372(2015多校7)--Segment Game(树状数组)
- hdu5372 Segment Game 树状数组
- hdu5372 离散化+树状数组
- hdu5372 Segment Game (树状数组)
- 2015 多校联赛 ——HDU5289(二分+ST)
- 2015多校联赛 ——HDU5288(数学)
- 2015 多校联赛 ——HDU5299(树删边)
- 2015 多校联赛 ——HDU5305(搜索)
- 2015 多校联赛 ——HDU5303(贪心)
- 2015 多校联赛 ——HDU5301(技巧)
- 2015 多校联赛 ——HDU5319(模拟)
- 2015 多校联赛 ——HDU5323(搜索)
- 2015 多校联赛 ——HDU5316(线段树)
- 2015 多校联赛 ——HDU5325(DFS)
- 2015 多校联赛 ——HDU5302(构造)
- 2015 多校联赛 ——HDU5335(Walk out)
- 比较优秀的软件记录
- c++线程的创建
- CString类型的字符串写入文件,所遇到的问题
- zip, tar, tar.gz, tar.bz2, jar,7z等格式文件的压缩和解压方法
- 线程同步的五种方法
- 2015 多校联赛 ——HDU5372(树状数组)
- Yandex.Algorithm Online Round 3 Sunday, June 15, 2014
- HDU 2029
- JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
- 在数组中找出两个数a、b,使得a加b等于给定的c
- 0000002_java学习之路_验证码的使用
- ARM CMSIS DAP源码分析(二)
- oracle 分组后,求和
- 一个不错的关于mysql和posgresql比较的帖子