NY 123 soldier Kill (iv)--segment tree (interval update, single point query)

Source: Internet
Author: User

Soldier Kills (iv) time limit: -Ms | Memory Limit:65535KB 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 10ADD 1 3 10QUERY 3ADD 2 6 50QUERY 3
Sample output
1060
Source
Interval update, check Order # include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;const int N = 1e6+5;struct tree{int l,r;int num;} t[n*4];void Build (int l,int r,int root) {t[root].l=l;t[root].r=r;t[root].num=0;if (l==r) return;int mid= (r+l)/2;build ( L,mid,2*root); build (mid+1,r,2*root+1);} void update (int l,int r,int val,int root) {if (l<=t[root].l&&t[root].r<=r) {T[root].num+=val;return;} int mid= (T[ROOT].L+T[ROOT].R)/2;if (r<=mid) update (l,r,val,2*root); else if (l>mid) update (L,R,VAL,2*ROOT+1); Else{update (l,mid,val,2*root); update (mid+1,r,val,2*root+1);}} int ans;void find (int id,int root) {ans+=t[root].num;if (T[ROOT].L==T[ROOT].R) {//ans=t[root].num;return;} int mid= (T[ROOT].L+T[ROOT].R)/2;if (id<=mid) find (Id,2*root); Elsefind (id,2*root+1);} int main () {int t,n;int X,y,z;char s[10];while (~scanf ("%d%d", &t,&n)) {build (1,n,1); while (t--) {scanf ("%s", s); if (s[0]== ' A ') {scanf ("%d%d%d", &x,&y,&z); update (x,y,z,1);} else if (s[0]== ' Q ') {scanf ("%d", &x); Ans=0;find (x,1);p rintf ("%d\n", ans);}} return 0;}


NY 123 soldier Kill (iv)--segment tree (interval update, single point query)

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.