Title Link: Nyoj 116 soldier kills
Soldier Kills (ii) time limit: +Ms | Memory Limit:65535KB Difficulty:5
-
-
Describe
-
South Generals have n soldiers, numbered 1 to N, each of these soldiers is known for their kills.
The staff is the South General's military advisers, the South General often want to know the number m to nth soldier's total kills, please help the handyman to answer the South General.
The South General's inquiry after the soldier I may also kill the Q person, after the South General asked again, need to consider the new kill number.
-
Input
-
Only one set of test data
The first line is two integer n,m, where N represents the number of soldiers (1<n<1000000), and M represents the number of instructions. (1<m<100000)
The next line is n integers, and AI represents the number of soldier kills. (0<=ai<=100)
The subsequent M-line is an instruction that contains a string and two integers, first a string, and if the string query indicates that the South General has a query operation, followed by the two integer m,n, indicating the start and end of the query soldier number; If the string add is followed by the two integer I , A (1<=i<=n,1<=a<=100), which indicates that the number of new kills for the first soldier is a.
-
Output
-
For each query, output an integer r that represents the total number of kills of soldier number m to nth soldier, one row per set of outputs
-
Sample input
-
5 2 3 4 5QUERY 1 3ADD 1 2QUERY 1 3ADD 2 3QUERY 1 2QUERY 1 5
-
Sample output
-
68820
The first line of self-completed tree segment!! Remember.
"Source Code"
#include <iostream> #include <cstdio> #include <cstring> #define L (m) m<<1#define R (m) m<< 1|1using namespace Std;int n,m;const int maxn = 1000000+1;int num[maxn];struct node{int l,r,sum;} tree[maxn<<2]; Open 4 times-fold array void Build (int m,int l,int r) {tree[m].l=l; tree[m].r=r;//Assign initial value if (TREE[M].L==TREE[M].R) {tree[m].sum=num[l]; return;//Don't forget return}int mid = (TREE[M].L+TREE[M].R) >>1; Build (L (M), L,mid); Recursive constructs left and right subtree build (R (m), mid+1,r); tree[m].sum = Tree[l (M)].sum+tree[r (m)].sum; Backtracking, adding the sum of the child nodes to the parent node}void Update (int m,int a,int x) {if (tree[m].l==a && tree[m].r==a) {tree[m].sum+=x; return;// This return forgot to write for a long time wrong}int mid = (TREE[M].L+TREE[M].R) >>1;//cout<< "bug" <<endl;if (mid>=a) Update (L ( m), a,x); Elseupdate (R (M), a,x); Tree[m].sum=tree[l (m)].sum+tree[r (m)].sum; int Query (int m,int l,int R) {if (tree[m].l==l && tree[m].r==r) {return tree[m].sum;} int mid = (TREE[M].L+TREE[M].R) >>1;if (mid>=r)//This can also be written as if else if Else return query (L (m), l,r), if (mid<l) return query (R (m), l,r), return query (L (m), L,mid) +query (R (M), mid+1,r);} int main () {while (scanf ("%d%d", &n,&m)!=eof) {memset (tree,0,sizeof (tree)), and for (int i=1;i<=n;i++) scanf ("%d ", &num[i]); Build (1,1,n); char cmd[10]; int A, b;for (int i=0;i<m;i++) {scanf ("%s", cmd), if (cmd[0]== ' Q ') {scanf ("%d%d", &a,&b);p rintf ("%d\n", Query ( 1,a,b));} ELSE{SCANF ("%d%d", &a,&b); Update (1,A,B);}}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
NYOJ 116 soldier Kills (segment tree, interval, and)