HDU 1754--i Hate It (Segment Tree Foundation)

Source: Internet
Author: User

I Hate It

Time limit:9000/3000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 44820 Accepted Submission (s): 17591

Problem 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), which represent 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 outputs the highest score in a row for each query operation.

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

Sample Output5659 HintHuge input,the C function scanf () would work better than CIN

Authorlinle

Sourcehdu 1754--i Hate It

Analysis: The first line of contact Tree topic, but also a line of tree entry questions,

Segment tree in the general data structure of the book rarely encountered, more practical can refer to the great God on the blog to write the explanation:

Metalseed of the line tree

AC Code:

1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 #defineMAXN 8001006 7 intTREE[MAXN];8 9 intMaxintAintb) {Ten     returna > B?a:b; One } A  - voidBuildintLeftintRightintroot) { -     if(left = =Right ) thescanf"%d",&Tree[root]); -     Else{ -         intMid = (left + right) >>1; -Build (left, mid, Root <<1); +Build (Mid +1, right, Root <<1|1); -Tree[root] = max (Tree[root <<1], Tree[root <<1|1]); +     } A } at  - voidUpdateintPintElementintLeftintRightintroot) { -     if(left = =Right ) -Tree[root] =element; -     Else{ -         intMid = (left + right) >>1; in         if(P <=mid) -Update (p, element, left, Mid, Root <<1); to         Else +Update (P, element, Mid +1, right, Root <<1|1); -Tree[root] = max (Tree[root <<1], Tree[root <<1|1]); the     } * } $ Panax Notoginseng intQueryintAintBintLeftintRightintroot) { -     if(A <= left&&b >=Right ) the         returnTree[root]; +     Else{ A         intsum =0, Mid = (left + right) >>1; the         if(A <=mid) +sum = max (sum, query (a, B, left, Mid, Root <<1)); -         if(b>mid) $sum = max (sum, query (a, B, mid +1, right, Root <<1|1)); $         returnsum; -     } - } the  - intMain ()Wuyi { the     intN, M, I, A, b; -     CharC; Wu      while(SCANF ("%d%d", &n, &m)! =EOF) { -Build1N1); About          for(i =0; I < m; i++){ $ GetChar (); -scanf"%c%d%d",&c,&a,&b); -             if(c = ='U') -Update (A, B,1N1); A             Else +printf"%d\n", query (A, B,1N1)); the         } -     } $     return 0; the}

HDU 1754--i Hate It (Segment Tree Foundation)

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.