HDU 1754 I Hate It (Single Point update of Line Segment tree), hdu1754
Problem Description many schools have a popular habit. Teachers like to ask, from xx to xx, what is the highest score.
This made many students very disgusted.
Whether you like it or not, what you need to do now is to write a program to simulate the instructor's inquiry according to the instructor's requirements. Of course, teachers sometimes need to update their scores.
Input this question contains multiple groups of tests, please process until the end of the file.
In the first row 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 separated from 1 to N.
The second row contains N integers, indicating the initial score of the N students. The number of I represents the score of the students whose ID is I.
Next there are M rows. Each line has A character C (only 'q' or 'U'), and two positive integers A and B.
When C is 'Q', it indicates that this is A query operation. It asks the students whose ID ranges from A to B (including A and B) about the highest score.
When C is 'U', it indicates that this is an update operation. You must change the score of students whose ID is A to B.
Output outputs the highest score in one row for each query operation.
Sample Input
5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
Sample Output
5659HintHuge input,the C function scanf() will work better than cin
I feel that there is nothing to say. This is my getting started Question on the Line Segment tree. It feels very effective.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)using namespace std;#define N 200005struct stud{int le,ri;int va;}f[N*4];int a[N];void pushup(int pos){ f[pos].va=max(f[L(pos)].va,f[R(pos)].va);}void build(int pos,int le,int ri){ f[pos].le=le; f[pos].ri=ri; if(le==ri) { f[pos].va=a[le]; return ; } int mid=MID(le,ri); build(L(pos),le,mid); build(R(pos),mid+1,ri); pushup(pos);}void update(int pos,int le,int ri){ if(f[pos].le==le&&f[pos].ri==le) { f[pos].va=ri; return ; } int mid=MID(f[pos].le,f[pos].ri); if(mid>=le) update(L(pos),le,ri); else update(R(pos),le,ri); pushup(pos);}int query(int pos,int le,int ri){ if(f[pos].le>=le&&f[pos].ri<=ri) { return f[pos].va; } int mid=MID(f[pos].le,f[pos].ri); if(mid>=ri) return query(L(pos),le,ri); else if(mid<le) return query(R(pos),le,ri); else return max(query(L(pos),le,mid),query(R(pos),mid+1,ri));}int main(){ int n,m,i; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); char c[10]; int le,ri; while(m--) { scanf("%s%d%d",c,&le,&ri); if(c[0]=='Q') printf("%d\n",query(1,le,ri)); else update(1,le,ri); } } return 0;}