HDU more than 1754 students steal grades ask highest score-line tree-(single point substitution, interval maximum)

Source: Internet
Author: User

Test instructions: N students have initial scores, now have M operations, (Q,A,B): Ask the interval number for [A, b] students of the best results; (U,A,B): The student who modified the number A is a B, want you to perform this m operation.

Analysis:

Single point update, interval query. is a single point of substitution, interval query. The two typical uses of a line segment.

Code:

#include <iostream> #include <cstdio> #include <algorithm> using namespace std;const int maxn=200000; int n,m;int mx[maxn*4+10];void pushup (int rt) {Mx[rt]=max (mx[rt<<1],mx[rt<<1|1]);} void creat (int l,int r,int RT) {if (l==r) {//L==r leaf node, which represents a specific student//mx[rt]=0;//If you first build and then use the UPD to update the initial value, then you should first assign the initial values here, or the program Pushup () The operation will be problematic. Assign any value to the line, because the scanf ("%d", &mx[rt]) is updated when the initial value is entered; In this case, the direct initialization is also the return;} int mid= (L+R) >>1; Creat (l,mid,rt<<1); creat (mid+1,r,rt<<1|1);p ushup (RT);} void upd (int a,int x,int l,int r,int RT) {if (l==r) {//leaf node, if student A is found, then update his score Mx[rt]=x;return;} int mid= (L+R) >>1;  if (a<=mid) upd (a,x,l,mid,rt<<1);        If a is in the left interval, go to the left section to find him, and then update his score else UPD (a,x,mid+1,r,rt<<1|1);     If in the right interval, go to the right interval to find him pushup (RT);   Find student a update his score to update the maximum range of the interval}int query (int a,int b,int l,int r,int RT) {if (a<=l&&b>=r) return MX[RT];        If the query interval contains the current interval, then return the interval maximum value int mid= (L+R) >>1; Otherwise, the maximum value is found in the left sub-interval of the current interval, and the right sub-range is searched for the maximum value and then returns the maximum value int ret=0;if (A&LT;=MID) Ret=max (ret, query (a,b,l,mid,rt<<1)), if (B>mid) Ret=max (Ret,query (a,b,mid+1,r,rt<<1|1)); return ret; } int main () {while (scanf ("%d%d", &n,&m)!=eof) {creat (1,n,1);//for (int i=1;i<=n;i++) {////build again update the initial value with update operation can also, However, we must first assign the initial value//int a;//scanf ("%d", &a),//UPD (i,a,1,n,1),//}while (m--) {char a[10]; int b,c;scanf ("%s%d%d", &a , &b,&c); You cannot use%c because%c is an if (a[0]== ' Q ') printf ("%d\n", Query (b,c,1,n,1)) that does not filter spaces and newline characters, and Else upd (b,c,1,n,1);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU more than 1754 students steal grades ask highest score-line tree-(single point substitution, interval maximum)

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.