标签:recommend 两种 accept uil dbr while -- color ane
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 97419 Accepted Submission(s): 41227
题目大意:字面意思。。。
解题思路:就是更新和查询两种操作。直接套模板即可。
#include <cstdio> #include<algorithm> using namespace std; const int N=50005; const int maxn=4*N; int a[maxn],sum[maxn]; int n,m,ql,qr,pos,value; void pushup(int num) { sum[num] = sum[num*2]+sum[num*2+1]; } void build(int num,int l,int r) { if(l==r) { sum[num] = a[l]; return ; } int mid = (l+r)/2; build(num*2,l,mid); build(num*2+1,mid+1,r); pushup(num); } void update1(int num,int l,int r)//+ { if(l==r) { sum[num] += value; return ; } int mid = (l+r)/2; if(pos<=mid) update1(num*2,l,mid); else update1(num*2+1,mid+1,r); pushup(num); } int query(int num,int l,int r) { if(ql<=l&&qr>=r) { return sum[num]; } int mid=(l+r)/2; int ans=0; if(mid>=ql) ans += query(num*2,l,mid); if(mid<qr) ans += query(num*2+1,mid+1,r); return ans; } int main() { int T,Case=1; scanf("%d",&T); while(T--) { printf("Case %d:\n",Case++); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } build(1,1,n); char s[10]; while(1) { scanf("%s",s); if(s[0]==‘A‘) { scanf("%d %d",&pos,&value); update1(1,1,n); } else if(s[0]==‘S‘) { scanf("%d %d",&pos,&value); value = -value; update1(1,1,n); } else if(s[0]==‘Q‘) { scanf("%d %d",&ql,&qr); printf("%d\n",query(1,1,n)); } else if(s[0]==‘E‘) { break; } } } }
标签:recommend 两种 accept uil dbr while -- color ane
原文地址:http://www.cnblogs.com/WWkkk/p/7366796.html