标签:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <stack> #include <queue> #define maxn 1000007 #define INF 0x3f3f3f3f typedef long long LL; using namespace std; #define lson rt<<1 #define rson rt<<1|1 int A[maxn]; struct node { int l, r, s; } tree[maxn<<2];///4倍 void build(int l ,int r, int rt)///建立一棵树 { tree[rt].l=l; tree[rt].r=r; if(l==r) { tree[rt].s=A[l]; return; } int mid=(l+r)>>1; build(l, mid, lson); build(mid+1, r, rson); tree[rt].s=tree[lson].s+tree[rson].s; } ///节点和的更新函数,如果有节点的话,节点要么在左边,要么在右边 void update(int pos, int val, int rt) { tree[rt].s+=val; if(tree[rt].l==tree[rt].r) return; int mid=(tree[rt].l+tree[rt].r)>>1; if(pos<=mid)update(pos, val, lson); else update(pos, val, rson); } ///查询函数 int query(int l, int r, int rt) { if(tree[rt].l==l&&tree[rt].r==r) return tree[rt].s; int mid=(tree[rt].l+tree[rt].r)>>1; if(r<=mid)return query(l, r, lson);///求和区间在左儿子那一边 else if(l>mid)return query(l, r, rson);///求和区间在右儿子那一边 else { int lsum, rsum;///求和左右两边都有,还是要分成左边和+右边和 lsum=query(l, mid, lson); rsum=query(mid+1, r, rson); return 买+rsum; } } int main() { int T, cas=1; scanf("%d", &T); while(T--) { int n; scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", &A[i]); build(1, n, 1); int a, b; char op[20]; printf("Case %d:\n", cas++); while(scanf("%s", op), strcmp(op, "End"))///在这里被挖了一下, scanf("%s%d%d", op, &a, &b);这样的话,只有End无法结束,弄忘了 { scanf("%d%d", &a, &b); int sign=1; if(op[0]==‘Q‘) { printf("%d\n", query(a, b, 1)); continue; } if(op[0]==‘S‘)sign*=-1; update(a, b*sign, 1);///要记得你的sign的作用 } } return 0; }
标签:
原文地址:http://www.cnblogs.com/w-y-1/p/5738409.html