树状数组的区间修改,单点查询
2014-10-17 21:49
369 查看
hdu 1556 Color the ball
要想区间修改的话,那么节点就必须往上更新,查询时往上累加。(区间修改,单点查询)
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int maxn=100000+5; int C[maxn]; int n; int lowbit(int x) { return (-x)&x; } void update(int x,int y) { for(int i=x; i>0;i-=lowbit(i)) C[i]+=y; } int query(int x) { int s=0; for(int k=x; k<=n; k+=lowbit(k)) s+=C[k]; return s; } int main() { int a,b; while(cin>>n) { for(int i=1; i<=n; i++) C[i]=0; for(int h=0;h<n;h++) { scanf("%d%d",&a,&b); update(b,1); update(a-1,-1); } for(int j=1;j<=n;j++) if(j==n) printf("%d\n",query(j)); else printf("%d ",query(j)); //printf("%d%c",query(i),i==n?'\n':' '); } return 0; }
csu 1335 高桥和低桥(树状数组+二分)
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; const int N = 1e5+10; int c ,m,n,k,a ; int x ,y ; int lowbit(int k) { return k&(-k); } void add(int k,int he) { while(k>0) { c[k]+=he; k-=lowbit(k); } } int Q(int k) { int query=0; while(k<=n) { query+=c[k]; k+=lowbit(k); } return query; } int main() { #ifndef ONLINE_JUDGE freopen("in.cpp","r",stdin); #endif // ONLINE_JUDGE int t,from,to,he,kkk=1; while(~scanf("%d%d%d",&n,&m,&k)) { memset(c,0,sizeof(c)); for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); int cur=1; int s,t; for(int i=1;i<=m;i++) { scanf("%d %d",&s,&t); int from=upper_bound(a,a+n,cur)-a; int to=upper_bound(a,a+n,s)-a; add(from,-1); add(to,+1); cur=t; } int ans =0 ; for(int i=1;i<=n;i++) { int kk =Q(i); if(kk>=k) ans = ans+1; } printf("Case %d: %d\n",kkk++,ans); } return 0; }
相关题: hdu A Simple Problem with Integers
相关文章推荐
- P3374 【模板】树状数组 1 单点修改与区间查询
- 【算法系列学习】线段树vs树状数组 单点修改,区间查询 [kuangbin带你飞]专题七 线段树 A - 敌兵布阵
- 树状数组单点修改区间查询
- 树状数组(区间修改,单点查询)
- 树状数组模板1——单点修改区间查询
- 【codevs1081】【树状数组】区间修改 单点查询
- 树状数组模板(max,min,区间和,区间修改,单点查询)
- hdu1166敌兵布阵 (树状数组 单点修改+区间查询)
- 二维树状数组(区间修改,单点查询)
- (模板)树状数组 (区间修改,单点查询)
- 树状数组-单点修改区间查询-区间修改单点查询-区间修改区间查询
- 树状数组单点更新和区间更新,二维数组poj2155(区间更新,单点查询)(已加入区间修改区间查询)
- nyoj 123 士兵杀敌(四) 树状数组【单点查询+区间修改】
- 树状数组 区间修改,单点查询;
- CS Academy Round #30 (Div. 2 only) C.Constant Sum(树状数组,区间修改,单点查询模板)
- 【原创】【数据结构】一维树状数组的基本操作(单点修改,区间查询) (HDU1166 敌兵布阵)
- POJ 2155-Matrix(二维树状数组-区间修改 单点查询)
- 树状数组的区间修改单点查询及Color the ball的AC代码
- POJ-2763 Housewife Wind (树链剖分 入门题 树状数组 单点修改 区间查询)
- 【树状数组】【单点修改区间求和】【区间修改单点查询】【单点修改区间最大值查询】