Nyoj 123 tree-like array (interval update, single-point access)/(insertion line Ask)

Source: Internet
Author: User

Soldier Kills (iv)
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 5
Describe
South generals have millions of troops, now known to have a total of M soldiers, numbered 1~m, each time there is a task, there will always be a number of people together Qingzhan (numbers close to each other often in a piece, familiar with each other), eventually they get the military exploit, also would be divided into everyone, so, sometimes, It is a difficult thing to calculate which one of them is military exploit, the task of the Military Adviser is to ask a person's military exploit in the south when he has a quick report of his military exploit, please write a program to help the handyman.

Let's assume that everyone's military exploit at the start is 0.

Input
There is only one set of test data.
Each line is two integers t and M representing a total of T-bars, M soldiers. (1<=t,m<=1000000)
The subsequent T-line, each line is an instruction.
Directives are divided into two types:
A shape like
ADD 100 500 55 indicates that the 100th person to No. 500 person Qingzhan, eventually each person obtains the average 55 military exploit, each person obtains the military exploit number not to exceed 100, does not have less than 100.
The second form is as follows:
QUERY 300 indicates how much the South General is asking about the No. 300 person's military exploit.
Output
For each query that outputs this person's military exploit, the output for each query is one row.
Sample input
4 10
ADD 1 3 10
QUERY 3
ADD 2 6 50
QUERY 3

 #include <stdio.h> int c[1000010],n; int lowbit (int x)//The number represented by the position of the lowest bit 1 {return x& (-
x);
        } void Update (int p,int q)//single point update, {while (p<=n) {c[p]+=q;
    P+=lowbit (P);
    }} int S (int x)//s (i) is the number of the first I and {int sum=0;
        while (x>0) {sum+=c[x];
    X-=lowbit (x);
} return sum;
    } int main () {int T;
    Char s[10];
    scanf ("%d%d", &t,&n);
        while (t--) {scanf ("%s", s);
            if (s[0]== ' A ') {int l,r,num;
            scanf ("%d%d%d", &l,&r,&num);
            Update (L,num);
        Update (r+1,-num);//L plus num, section r+1-num, because S (x) calculates the first x sum, which is equivalent to S (l~r) are added num, very good.
            } else {int x;
            scanf ("%d", &x);
        printf ("%d\n", S (x));

}} return 0; }

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.