hdu1754 I Hate It (line segment tree)

Source: Internet
Author: User

I Hate ItTime limit:9000/3000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 55291 Accepted Submission (s): 21599

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 Input
5 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5

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

Authorlinle
Analysis: A line tree entry basic problem, each update node stores the maximum value in the subtree that is rooted in the node.
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> Using namespace Std;const double eps = 1e-6;const double pi = ACOs ( -1.0); const int INF = 0x3f3f3f3f;const int MOD = 100000 0007; #define LL Long Long#define CL (A, B) memset (A,b,sizeof (a)) #define MAXN 800010struct node{int a,b,r;}    T[maxn];int n,m,ans;void Build (int x, int y, int num) {t[num].a = x;    t[num].b = y;    T[NUM].R = 0;    if (x = = y) return;    int mid = (x+y)/2;    Build (x, Mid, num*2); Build (Mid+1, y, num*2+1);}        void update (int x, int y, int num) {if (t[num].a = = t[num].b && t[num].b = = x) {t[num].r = y;    return;    } int mid = (t[num].a+t[num].b)/2;    if (x > Mid) Update (x, y, num*2+1);    else update (x, y, num*2); T[NUM].R = Max (T[NUM*2].R, T[NUM*2+1].R);} void query (int x, int y, int num) {if (t[num].a = = x && t[num].b = = y) {ans = max (ans, T[NUM].R);     return;     } int mid = (t[num].a+t[num].b)/2;     if (x >= mid+1) query (x, y, num*2+1);     else if (y <= mid) query (x, y, num*2);         else {query (x, Mid, num*2);     Query (mid+1, y, num*2+1);    }}int Main () {char ch;    int x,y,k;        while (scanf ("%d%d", &n,&m) ==2) {build (1, N, 1);            for (int i=1; i<=n; i++) {scanf ("%d", &k);        Update (I, k, 1);            } while (m--) {GetChar ();            scanf ("%c%d%d", &ch,&x,&y);            if (ch = = ' U ') {update (x, y, 1);                } else if (ch = = ' Q ') {ans =-99999;                Query (x, y, 1);            printf ("%d\n", ans); }}} return 0;}


hdu1754 I Hate It (line 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.