hdu1166依旧是大山跑线段树题
2012-06-03 21:42
211 查看
也是不知不觉就会re 还不知道为什么 神经病题!害我做了这么长时间!!!
[align=left]Sample Input[/align]
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
[align=left]Sample Output[/align]
Case 1:
6
33
59
[align=left]Sample Input[/align]
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
[align=left]Sample Output[/align]
Case 1:
6
33
59
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define maxn 50001 int data[maxn]; int sum,n; struct Tree { int l,r; long long sum; }tree[3*maxn]; void build(int l,int r,int pos) { tree[pos].l=l; tree[pos].r=r; if(l==r) { tree[pos].sum=data[l]; return; } int mid=(l+r)>>1; build(l,mid,2*pos); build(mid+1,r,2*pos+1); tree[pos].sum=tree[pos*2].sum+tree[pos*2+1].sum; } void Update(int pos,int a,int b) { if(tree[pos].l==a&&tree[pos].r==a) { tree[pos].sum+=b; return; } int mid=(tree[pos].l+tree[pos].r)>>1; if(a<=mid) Update(pos*2,a,b); if(a>mid) Update(pos*2+1,a,b); tree[pos].sum+=b; } void Search(int pos,int a,int b) { if(tree[pos].l==a&&tree[pos].r==b) { sum+=tree[pos].sum; return; } int mid=(tree[pos].l+tree[pos].r)>>1; if(b<=mid) { Search(pos*2,a,b); } else if(a>=mid+1) { Search(pos*2+1,a,b); } else { Search(pos*2,a,mid); Search(pos*2+1,mid+1,b); } } int main() { int t,i,a,b,ans=0; string str; cin>>t; while(t--) { ans++; printf("Case %d:\n",ans); cin>>n; data[0]=0; for(i=1;i<=n;i++) scanf("%d",&data[i]); build(1,10,1); while(1){ cin>>str; getchar(); if(str=="End") break; if(str=="Add") { scanf("%d%d",&a,&b); Update(1,a,b); } if(str=="Sub") { scanf("%d%d",&a,&b); Update(1,a,-b); } if(str=="Query") { scanf("%d%d",&a,&b); sum=0; Search(1,a,b); printf("%d\n",sum); } } } return 0; }
相关文章推荐
- 线段树的总结与习题acm杭电HDU1166
- 【单点更新,区间查询,线段树】【HDU1166】【敌兵布阵】
- hdu1166(线段树单点更新&区间求和模板)
- 线段树(单点更新)HDU1166、HDU1742
- hdu1166_敌兵布阵(线段树入门)
- 线段树hdu1166
- hdu1166 线段树
- hdu1166敌兵布阵(线段树)
- HDU1166:敌兵布阵(线段树以及单点更新)
- 【HDU1166】敌兵布阵,线段树练习
- HDU1166_敌兵布阵_线段树单点修改区间查询
- hdu1754 I Hate It && hdu1166 敌兵布阵 ——线段树复习
- hdu1166 敌兵布阵(线段树)
- hdu1166 敌兵布阵 线段树入门
- HDU1166 敌兵布阵 解题报告--线段树
- 线段树—hdu1166
- 【单点更新,区间查询,线段树】【HDU1166】【敌兵布阵】
- 线段树入门题 hdu1166 敌兵布阵
- 线段树 hdu1166 (敌兵布阵)
- hdu1166之线段树