"Tree-like array (binary index tree)" Light courtyard warm-up-candy, NYOJ-116 soldiers Kill (ii)

Source: Internet
Author: User

Conceptreprinted connection: The tree-like array speaks very well. These two problems are very similar, the query interval of the accumulation and update nodes. ADD (X,d) and Query (l,r) Operation"Topic Link: Candy"Alas, but also now found that the problem with this knowledge, the first data structure of a is about the tree-like array, forget to forget.

  

Problem c:candytime limit:3 Sec Memory limit:128 MB
submit:252 solved:63

Submitstatusweb Board Description

Kimi have a lot of candies, and divides them into piles, where the ith pile contains ai candies. Each time Kimi would choose an interval [l,R], and calculate the total amount of Al, a l+1,..., A R. It's a hard task, and your ' re required to solve it.

Input

An integer t(T≤10) would exist in the first line of input, indicating the number of test cases. Each test case is begins with the number of Pilesn(1≤n≤105). The second line contains N integers ai(1≤ai≤100), where Ai< /c10> stands for the number of candies in the ith pile. The next line is the number of queries m(1≤m≤105). The next M lines, each with a integers l,R(1≤lrN ), describe the queried intervals.

Output

For each test case, output the total amount of candies in the queried interval.

Sample Input151 2 4 5 931 5Sample Output31114
1#include <cstdio>2#include <cstring>3 Const intMAXN =1000008;4 intA[MAXN];5 intm;6 intLowbit (intR) {7     returnR & (-R);8 }9 intSumintx) {Ten     intRET =0; One      while(X >0){ ARET + =A[x]; -X-=lowbit (x); -     } the     returnret; - } - voidAddintIintx) { -      while(I <=m) { +A[i] + =x; -i + =lowbit (i); +     } A } at intMain () { -     intN; -scanf"%d",&n); -      while(n--){ -Memset (A,0,sizeof(a));  -scanf"%d",&m); in         inti,j;  -          for(i =1; I <= m;i++){ to             intx; +scanf"%d",&x); - Add (i,x); the         } *         intT; $scanf"%d",&t);Panax Notoginseng          while(t--){ -             intnum1,num2; thescanf"%d%d",&num1,&num2);  +printf"%d\n", SUM (num2)-SUM (NUM1-1)); A         } the     } +}

"Title Link: nyoj-Soldier kills (ii)"

Similar simple link: soldier Kill (a) only involves the query interval and

Soldier Kills (ii) time limit:MS | Memory limit:65535 KB 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.

South General after a certain inquiry 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
1  2#include <stdio.h>3 #defineSize 10000084 5 intA[size];6 intN;7 intLowbit (intR)8 {9     returnR & (-R);Ten } One intSumintx) A { -     intRET =0; -      while(x>0) the     { -ret+=a[x];x-=lowbit (x); -     } -     returnret; + } - voidAddintIintx) + { A      while(i<=N) at     { -a[i]+=x; -i+=lowbit (i); -     } - } -  in intMain () - { to     intm,m,n,x; +scanf"%d%d",&n,&M); -      for(intI=1; i<=n;i++) the     { *scanf"%d",&x); $ Add (i,x);Panax Notoginseng     } -      while(m--) the     { +         Charac[5]; Ascanf"%s%d%d",ac,&m,&n); the         if(ac[0]=='Q') +         { -printf"%d\n", SUM (n)-sum (M-1)); $         } $         Else -         { - Add (m,n); the         } -     }Wuyi     return 0; the}

Keep working on it ~

"Tree-like array (binary index tree)" Light courtyard warm-up-candy, NYOJ-116 soldiers Kill (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.