NYOJ116 soldier kills (ii) "Tree-like array"

Source: Internet
Author: User

soldier Kills (ii)time limit: Ms | Memory limit: 65535 KBDifficulty: 5Describe
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 6
1 2 3) 4 5
QUERY 1 3
ADD 1 2
QUERY 1 3
ADD 2 3
QUERY 1 2

QUERY 1 5


Sample output

6
8
8

20


Topic: give you a range [1,n], each point on a value. Next is the M command.

Query X Y indicates how much and how many times the interval [x, y] is asked. Add x y indicates an increase in the X point

Y.

Idea: Use a line tree or a tree-like array to do it. Try it this time with a tree-like array.

Query (n), which indicates the and of the first n items to ask. Add (P,val) indicates that the first point of P increases Val.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    Std;int c[1000010],lowbit[1000010],num[1000010],n,m;int query (int p) {int SUM = 0;        while (P > 0) {SUM + = c[p];    P-= lowbit[p]; } return SUM;}        void Add (int p,int val) {while (P <= N) {C[p] + = val;    p + = lowbit[p];    }}int Main () {scanf ("%d%d", &n,&m);        for (int i = 1; I <= N; i++) {scanf ("%d", &num[i]);    Num[i] + = num[i-1];    } for (int i = 1; I <= N; i++) Lowbit[i] = i& (i^ (i-1));    for (int i = 1; I <= N; i++) c[i] = Num[i]-num[i-lowbit[i]];        while (m--) {char a[10];        Cin >> A;            if (a[0] = = ' Q ') {int x, y;            scanf ("%d%d", &x,&y);        cout << Query (Y)-query (X-1) << Endl;            } else {int x, y;            scanf ("%d%d", &x,&y);    Add (x, y);    }} return 0;} 



NYOJ116 soldier kills (ii) "Tree-like array"

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.