Test instructions: N enemy soldiers of the camp, multiple operations: Add a, x: The first camp to increase the X-man; Sub a,x: The first camp reduces the x;query A, a: The total number of query intervals [a, b].
Analysis:
Single point update, interval query. Write directly.
Code:
#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;typedef long ll; const int Maxn=50000;int t,n;ll sum[maxn<<2+10];void pushup (int rt) {Sum[rt]=sum[rt<<1]+sum[rt<<1|1 ];} void creat (int l,int r,int RT) {if (l==r) {scanf ("%d", &sum[rt]); return;} int mid= (L+R) >>1;creat (l,mid,rt<<1), creat (mid+1,r,rt<<1|1);p ushup (RT);} void upd (int a,int x,int l,int r,int RT) {if (l==r) {Sum[rt]+=x;return;} int mid= (L+R) >>1;if (a<=mid) upd (a,x,l,mid,rt<<1), Else upd (a,x,mid+1,r,rt<<1|1);p ushup (RT);} ll query (int a,int b,int l,int r,int RT) {if (a<=l&&b>=r) return sum[rt];int mid= (l+r) >>1;ll ret;if (a& Lt;=mid) Ret=query (a,b,l,mid,rt<<1), if (B>mid) ret+=query (a,b,mid+1,r,rt<<1|1); return ret;} int main () {scanf ("%d", &t), int cas=1;while (t--) {scanf ("%d", &n), creat (1,n,1);p rintf ("Case%d:\n", cas++); Char Q[20];while (scanf ("%s", Q)!=eof) {if (q[0]== ' E ') break;ll a,b;scanf ("%d%d", &a,&b); if (q[0]== ' A ' | | q[0]== ' s ') {if (q[0]== ' s ') b*=-1;upd (a,b,1,n,1);} else printf ("%i64d\n", Query (a,b,1,n,1));}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1166 enemy soldiers-line tree-(single point update, interval query)