Nyoj116 soldier Kills (ii) (interval query and single-point update for segment trees)

Source: Internet
Author: User

    • The topic is
    • topic information
    • run result
    • line
    • discussion area
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
Source
[Zhang Yunzun] Original
Uploaded by
Zhang Yunzun

Only segment tree See also someone with a tree-like array and so will learn to learn haha.

#include <stdio.h>struct node{int Left,right;long Long Count;} tree[4*1000000];//contribution void build (int l,int r,int root) {tree[root].left=l;tree[root].right=r;if (l==r) {scanf ("%lld", &tree[root].count); return;} Else{int mid= (l+r)/2;build (l,mid,root*2); build (mid+1,r,root*2+1); tree[root].count=tree[root*2].count+tree[root* 2+1].count;}} query void search (int l,int r,int Root,long long &result) {if (tree[root].left==l&&tree[root].right==r) { Result=tree[root].count;return;} int mid= (tree[root].left+tree[root].right)/2;if (mid<l) search (L,r,root*2+1,result), else if (mid>=r) search (L, R,root*2,result); Else{long long Result1;search (L,MID,ROOT*2,RESULT1); search (mid+1,r,root*2+1,result); result= RESULT+RESULT1;}} update void update (int pos,int x,int root) {if (Tree[root].left==tree[root].right&&tree[root].right==pos) {tree[ Root].count+=x;return;} int mid= (tree[root].left+tree[root].right)/2;//Find the target soldier if (pos<=mid) update (pos,x,root*2); else if (Pos>mid) Update (pos,x,root*2+1);//When the target soldier is updatedThe parent node associated with it is updated tree[root].count=tree[root*2].count+tree[root*2+1].count;} int main () {int n,k;scanf ("%d%d", &n,&k), Build (1,n,1), for (int i=0;i<k;i++) {char str[10];int a,b;scanf ("%s%") D%d ", str,&a,&b), if (str[0]== ' Q ') {Long long Result;search (A,b,1,result);p rintf ("%lld\n ", result);} if (str[0]== ' A ') {update (a,b,1);}} return 0;}


Nyoj116 soldier Kills (ii) (interval query and single-point update for segment trees)

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.