Acm--i Hate It (Evolutionary version of Segment tree)

Source: Internet
Author: User

Description

Many schools are popular for a comparative habit. Teachers like to ask, from XXX to XXX, the highest score is how much.
This makes many students very disgusted.

whether you like it or not, now you need to do is to follow the teacher's request, write a program, mock teacher's inquiry. Of course, teachers sometimes need to update a student's grades.

Input

This topic contains multiple sets of tests, please handle to the end of the file.
On the first line of each test, there are two positive integers N and M (0<n<=200000,0<m<5000), representing the number of students and the number of operations respectively.
Student ID numbers are numbered from 1 to N, respectively.
The second line contains n integers representing the initial scores of the N students, of which the number of I represents the student's score for ID i.
Then there's M-line. Each line has a character C ( only ' Q ' or ' U ') , and two positive integers, A/b.
When C is ' Q ', it indicates that this is a query operation, which asks for the highest number of students whose IDs are from a to B (including A, a).
when C is ' U ', it indicates that this is an update operation that requires the student with ID A to change the grade to B.

Output

for each query operation, output the highest score in one line.

Sample Input

5 6
1 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5

Sample Output

5659

Hint

Cin


Explanation: The main meaning of the problem in how to spend less time to achieve a student's highest score and How to update a student's performance. so this problem, have the following ideas:

1. When constructing each segment tree, in addition to the bottom row, each Fulcrum stores the maximum (not and) of the left child and right child below, so that a student is read
The maximum score will be much faster.

2. Because each time with a new student performance, the above fulcrum must also make corresponding changes, so the use of recursive thinking from the bottom up to update the results.

Here's the code:
1#include <cstdio>2#include <cstring>3#include <iostream>4 #defineMax (A > B) (A:B)5 #defineMin (a > B) (b:a)6 7 using namespacestd;8 9 Const intmaxn=200010;Ten  One structStudent A { -     intLeft,right; -     intNSum; the}segtree[maxn*3]; - intNUM[MAXN]; -  -  +  -  + voidBuild (intIintLeftintRight//recursive algorithm to construct a segment tree A { atsegtree[i].left=Left ; -segtree[i].right=Right ; -     if(left==Right ) -     { -segtree[i].nsum=Num[left]; -         return; in     } -     intMid= (left+right) >>1;//except 2 toBuild (i<<1, Left,mid);//i*2 a +Build (i<<1|1, mid+1, right);//i*2+1 b -Segtree[i].nsum= (segtree[i<<1].nsum>segtree[i<<1|1].nsum?segtree[i<<1].nsum:segtree[i<<1|1].nsum);//Take maximum value the } *  $ Panax Notoginseng voidUintIintTleftintb//Update Results - { the      +     if(segtree[i].left==tleft&&segtree[i].right==tleft) A     { thesegtree[i].nsum=b; +         return; -     } $     intMid= (segtree[i].left+segtree[i].right) >>1; $  -     if(tleft<=mid) -U (i<<1, tleft,b); the     Else -U (i<<1|1, tleft,b);Wuyi  theSegtree[i].nsum= (segtree[i].nsum>b?segtree[i].nsum:b); -  Wu } -  About  $ intQ (intIintLeftintRight ) - { -     if(segtree[i].left==left&&segtree[i].right==Right ) -     { A         returnsegtree[i].nsum; +     } the     intMiddle= (segtree[i].left+segtree[i].right) >>1; -     if(left>middle) $     { the         returnQ (i<<1|1, left,right); the     } the     Else if(right<=middle) the     { -         returnQ (i<<1, left,right); in     } the     Else the     { About         intA=q (i<<1, left,middle); the         intB=q (i<<1|1, middle+1, right); the         returnA>b?a:b; the     } + } -  the Bayi intMain () the { the     intn,t,x,y; -     CharC; -      while(SCANF ("%d%d", &n,&t)! =EOF) { the          for(intI=1; i<= N; i++) scanf ("%d",&num[i]); theBuild (1,1, n); the          while(t--){ theGetChar ();//absorbs the extra line-break keys -scanf"%c%d%d",&c,&x,&y); the  the             if(C = ='U') theU1, x, y);94             Else  theprintf"%d\n", Q (1, x, y));  the         }     the     }    98     return 0; About}




Acm--i Hate It (Evolutionary version of Segment tree)

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.