NY 116 soldier Kill (ii)--segment Tree (single point update, Interval sum)

Source: Internet
Author: User

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 
<span style= "color: #330033;" >//single point update, Interval summation # include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;const    int N = 1e6+5;struct tree{int l,r; int num;}    t[4*n];void Build (int l,int r,int root) {t[root].l=l;    T[root].r=r;    t[root].num=0;    if (l==r) {return;    } int mid= (L+R)/2;    Build (L,mid,2*root); Build (mid+1,r,2*root+1);}        void update (int val,int id,int root) {if (t[root].l==t[root].r&&t[root].l==id) {t[root].num+=val;    Return    } int mid= (T[ROOT].L+T[ROOT].R)/2;    if (id<=mid) update (val,id,2*root);    else update (VAL,ID,2*ROOT+1); T[root].num=t[2*root].num+t[2*root+1].num;}        int ans;void find (int l,int r,int root) {if (t[root].l==l&&t[root].r==r) {ans+=t[root].num;    Return    } int mid= (T[ROOT].L+T[ROOT].R)/2;    if (R<=mid) find (L,r,2*root);    else if (l>mid) find (l,r,2*root+1);        else{find (L,mid,2*root); FinD (mid+1,r,2*root+1);    }}int Main () {int n,m,i,j;        while (~SCANF ("%d%d", &n,&m)) {build (1,n,1);            for (i=1;i<=n;i++) {int x;            scanf ("%d", &x);        Update (x,i,1);        } Char s[10];            while (m--) {int x,y;scanf ("%s", s);            scanf ("%d%d", &x,&y);            if (s[0]== ' A ') update (y,x,1);                else{ans=0;                Find (x,y,1);            printf ("%d\n", ans); }}} return 0;} </span>


NY 116 soldier Kill (ii)--segment Tree (single point update, Interval sum)

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.