NYOJ 116 soldier Kills (ii) "line segment tree single point update + sum"

Source: Internet
Author: User

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.

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

A couple of days without the water.
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define MAX 1000100using namespace Std;int n,m;int sum[max<<2];void pushup (int o) {sum[o]=sum[o<<1]+sum[o<<1|1] ;}        void Gettree (int o,int l,int R) {if (l==r) {scanf ("%d", &sum[o]);    return;    } int mid= (L+R) >>1;    Gettree (O<<1,l,mid);    Gettree (O&LT;&LT;1|1,MID+1,R); Pushup (o);}        void update (int o,int l,int r,int pos,int val) {if (l==r) {sum[o]+=val;    return;    } int mid= (L+R) >>1;    if (pos<=mid) update (o<<1,l,mid,pos,val);    else update (o<<1|1,mid+1,r,pos,val); Pushup (o);}    int find (int o,int l,int r,int l,int R) {if (l<=l&&r>=r) return sum[o];    int mid= (L+R) >>1;    int ans=0;    if (r<=mid) ans+=find (o<<1,l,mid,l,r);    else if (l>mid) ans+=find (o<<1|1,mid+1,r,l,r); else {ans+=find (O<<1,l,mid,L,MID);    Ans+=find (O&LT;&LT;1|1,MID+1,R,MID+1,R); } return ans; int main () {int N,m,a,b;char op[20];scanf ("%d%d", &n,&m), Gettree (1,1,n), while (m--) {scanf ("%s", op); scanf ("%d    %d ", &a,&b), if (op[0]== ' Q ') printf ("%d\n ", Find (1,1,n,a,b)); else update (1,1,N,A,B);} return 0;}

  

NYOJ 116 soldier Kills (ii) "line segment tree single point update + 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.