HDU1754 (single point update of line segment tree)

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 93183    Accepted Submission(s): 35335


Problem Description
A habit of comparison is popular in many schools. Teachers like to ask what is the highest score from so-and-so to so-and-so.
This disgusts many students.

Whether you like it or not, what you need to do now is to write a program according to the teacher's request to simulate the teacher's inquiry. Of course, teachers sometimes need to update a student's grades.
 

Input
This question contains multiple sets of tests, please process until the end of the file.
In 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 grades of the N students, where the ith number represents the grades of the student whose ID is i.
Next there are M lines. Each line has a character C (only take 'Q' or 'U'), and two positive integers A, B.
When C is 'Q', it means that this is a query operation, which asks the students whose IDs are from A to B (including A and B), which is the highest grade.
When C is 'U', it means that this is an update operation, which requires changing the grade of the student whose ID is A to B.
 

Output
For each query operation, output the highest score in one line.
 

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

Sample Output
 
  
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin

Problem solving instructions: Naked problem

ac code:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
const int MAXN=2*1e5+10;
int father[MAXN];
int mx;
struct po{
	int value;
	int l, r;
	int mid(){
		return (l+r)/2;
	}
}node[MAXN<<2];
void build(int i,int left,int right){
        node[i].l=left;
     	node[i].r=right;
     	node[i].value=0;
	    if(left==right){
	    	father[left]=i;
	    	return ;
		}
        int midd=node[i].mid();
        build(i<<1,left,midd);
        build((i<<1)+1,midd+1,right);
}
void update(int tr){
	if(tr==1)return ;
	int fi = tr / 2;
	int a=node[fi<<1].value;
	int b=node[(fi<<1)+1].value;
	node[fi].value=max(a,b);
	update(fi);
}
void query(int i,int left,int right){
	if(left==node[i].l&&right==node[i].r){
		mx=max(node[i].value,mx);
		return;
	}
	i<<=1;
	if(left<=node[i].r){
		if(right<=node[i].r)query(i,left,right);
		else query(i,left,node[i].r);
	}
	i++;
	if(right>=node[i].l){
		if(left>=node[i].l)query(i,left,right);
		else query(i,node[i].l,right);
	}
}
int main(){
	int n,m,ip;
	ios::sync_with_stdio(false);
	while(cin>>n>>m){
	   build(1,1,n);
	   for(int i=1;i<=n;i++){
		   cin >> ip;
		   node[father[i]].value=ip;
		   update(father[i]);
	   }
	   for(int i=1;i<=m;i++){
	      string op;
	      cin>>on;
	      int ip1,ip2;
	      if(op=="Q"){
	      	  mx=0;
	   	      cin>>ip1>>ip2;
	   	      query(1,ip1,ip2);
	   	      cout<<mx<<endl;
	        }
	       else{
	   	      cin>>ip1>>ip2;
	   	      node[father[ip1]].value=ip2;
	   	      update(father[ip1]);
	       }
       }
	}
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326077147&siteId=291194637