Nyoj116 soldier kills (ii) line segment tree Single point update

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
688
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace STD; #define MAXN 1000001#define inf 0x3f3f3f3fint ll[maxn<<2],rr[maxn<<2],sum[maxn<<2];int A[MAXN]    ; void build (int l,int r,int i) {ll[i]=l;    Rr[i]=r;        if (Ll[i]==rr[i]) {sum[i]=a[l];    return;    } int m= (L+R) >>1,ls=i<<1,rs=ls|1;    Build (L,m,ls);    Build (M+1,R,RS); SUM[I]=SUM[LS]+SUM[RS];}        void update (int k,int V,int i) {if (Ll[i]==rr[i]) {sum[i]+=v;    Return    } int m= (Ll[i]+rr[i]) >>1,ls=i<<1,rs=ls|1;    if (k<=m) {update (K,V,LS);    } else{Update (K,V,RS); } Sum[i]=sum[ls]+sum[rs];}    int finds (int l,int r,int i) {if (ll[i]==l&&rr[i]==r) {return sum[i];    } int m= (Ll[i]+rr[i]) >>1,ls=i<<1,rs=ls|1;    if (r<=m) {return finds (L,R,LS);    } else if (l>m) {return finds (L,R,RS); } else{return finds (L,m,ls) +fiNDS (M+1,R,RS);    }}int Main () {int n,m;    int x, y;    int a,i;    Char q[10];    Freopen ("In.txt", "R", stdin);        while (~SCANF ("%d%d", &n,&m)) {for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);        } build (1,n,1);            for (int i=1;i<=m;i++) {scanf ("%s", q);                if (!strcmp (q, "QUERY")) {scanf ("%d%d", &x,&y);            printf ("%d\n", Finds (x,y,1));                } else {scanf ("%d%d", &a,&i);            Update (a,i,1); }        }    }}


Nyoj116 soldier kills (ii) line segment tree Single point update

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.