Link to original topic
I Hate ItTime
limit:9000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 45627 Accepted Submission (s): 17904
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
Problem Solving Report:
Because the topic requires multiple queries and updates to the data, a simple array query will definitely be tle. Therefore, the use of statistical methods to achieve, the most classic is definitely using a line tree to achieve. Achievements, interval query, point update. So simple question has been tle, finally found unexpectedly is not add!=eof, has been stuck in while inside, Fall!!!
/* * Pointer implementation of Segment tree */#include <iostream> #include <cstdio>using namespace Std;int n,m,l,r; char c[10];const int maxn=200010;int stu[maxn];struct node{int a,b,max; node* l; node* R; Node (int x,int y): A (x), B (Y), L (null), R (NULL) {}}*root;inline int max (int a,int b) {return a>b?a:b;} void build (int a,int b,node*& RT) {if (b==a) {rt=new Node (A, b); Rt->max=stu[a];return;} RT = New Node (A, b); int mid= (b-a+1)/2+a-1; Build (A,mid,rt->l); Build (Mid+1,b,rt->r); Rt->max = MAX (Rt->l->max,rt->r->max);} int query (int a,int b,node* RT) {if (a = = Rt->a && b== rt->b) return rt->max; int mid=rt->l->b; if (A<=mid && b>mid) return max (query (a,mid,rt->l), query (Mid+1,b,rt->r)); else{if (b <= mid) return query (A,B,RT->L); else return query (A,B,RT->R); }}void Update (const int& A,CONST int& score,node* RT) {if (a = = Rt->a && a== rt->b) { Rt->max = score; } else{if (a<= rt->l->b) update (A,SCORE,RT->L); else update (A,SCORE,RT->R); Rt->max = MAX (Rt->l->max,rt->r->max); }}void destroy (node* RT) {if (rt->l = = NULL) Delete rt; else {destroy (RT->L); Destroy (RT->R); Delete RT; }}int Main () {while (scanf ("%d%d", &n,&m)!=eof) {for (int i=1;i<=n;++i) scanf ("%d", &stu[i]); Build (1,n,root); for (int i=0;i<m;++i) {scanf ("%s%d%d", C,&l,&r); if (c[0]== ' Q ') printf ("%d\n", Query (L,r,root)); else if (c[0]== ' U ') update (l,r,root); } destroy (root); Remember to delete it, or it will be MLE} return 0;}
/* * Array implementation of Segment tree */#include <cstdio> #include <iostream>using namespace std;struct node{int l,r,max; void set (int a,int b,int c=-1) {l=a; R=b; Max=c;}}; int n,m,a,b; char c[5];const int maxn=200010;int STU[MAXN]; Node segtree[4*maxn];inline int max (int a,int b) {return a>b?a:b;} inline int lchild (int p) {return p<<1; }inline int rchild (int p) {return (p<<1) | void Build_segment_tree (int cur,int l,int R) {if (l==r) {segtree[cur].set (l,r,stu[l]); return;} Segtree[cur].set (L,R); Build_segment_tree (Lchild (cur), L, (l+r) >>1); Build_segment_tree (Rchild (cur), ((l+r) >>1) +1,r); If Max is a macro definition, do not directly use the function as an argument, otherwise you will call Segtree[cur] more than once. Max=max (Segtree[lchild (cur)]. Max,segtree[rchild (cur)]. MAX);} int query (int cur,const int& l,const int& R) {if (R<segtree[cur]. L | | Segtree[cur]. R<L) return-1; if (L<=segtree[cur]. L && Segtree[cur]. R<=R) return segtree[cur]. MAX; If Max is a macro definition, do not directly use the function as an argument, or you will call int mid= (Segtree[cur] multiple times. L +Segtree[cur]. R) >>1, ans=-1; if (l<=mid) Ans=max (Ans,query (lchild (cur), l,r)); if (mid<r) Ans=max (Ans,query (rchild (cur), l,r)); return ans;} void update (int cur,const int& s,const int& score) {if (Segtree[cur]. L==segtree[cur]. R) {Segtree[cur]. Max=score;return;} if (s<= (segtree[cur). L+segtree[cur]. R) >>1) update (lchild (cur), s,score); else Update (rchild (cur), s,score); If Max is a macro definition, do not directly use the function as an argument, otherwise you will call Segtree[cur] more than once. Max=max (Segtree[lchild (cur)]. MAX, Segtree[rchild (cur)]. MAX);} int main () {while (cin>>n>>m) {for (int i=1;i<=n;++i) scanf ("%d", &stu[i]); Build_segment_tree (1,1,n); for (int i=0;i<m;++i) {scanf ("%s%d%d", c,&a,&b); if (c[0]== ' Q ') {printf ("%d\n", Query (1,a,b));} else update (1,A,B); }} return 0;}
"HDU 1754" I Hate it--Segment Tree (array and pointer implementations)