/*cin慢是有原因的,其实默认的时候,
cin与stdin总是保持同步的,也就是说这两种方法可以混用,
而不必担心文件指针混乱,同时cout和stdout也一样,
两者混用不会输出顺序错乱。正因为这个兼容性的特性,
导致cin有许多额外的开销,如何禁用这个特性呢?
只需一个语句std::ios::sync_with_stdio(false);
这样就可以取消cin于stdin的同步了
*/
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
Output
对于每一次询问操作,在一行里面输出最高成绩。
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
看到博客中最详细的解释
http://blog.csdn.net/metalseed/article/details/8039326
题目:给你n个学生,m次查询操作.....中文题,题意应该不影响
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int MAXNODE=1<<19; 4 const int MAX=2e6+10; 5 struct NODE{ 6 int value; 7 int left,right; 8 }node[MAXNODE]; 9 int father[MAX]; 10 void buildTree(int i,int left,int right){ 11 node[i].left=left; 12 node[i].right=right; 13 node[i].value=0; 14 if(left==right){ 15 father[left]=i; 16 return ; 17 } 18 buildTree(i<<1,left,(int)(floor(left+right)/2.0)); 19 buildTree((i<<1)+1,(int)(floor(left+right)/2.0)+1,right); 20 } 21 void UpdateTree(int ri){ 22 if(ri==1) return; 23 int fi=ri/2; 24 int a=node[fi<<1].value; 25 int b=node[(fi<<1)+1].value; 26 node[fi].value=max(a,b); 27 UpdateTree(ri/2); 28 } 29 int Max; 30 void Query(int i,int l,int r){ 31 if(node[i].left==l&&node[i].right==r){ 32 Max=max(Max,node[i].value); 33 return ; 34 } 35 i=i<<1; 36 if(l<=node[i].right){ 37 if(r<=node[i].right) Query(i,l,r); 38 else Query(i,l,node[i].right); 39 } 40 i++; 41 if(r>=node[i].left){ 42 if(l>=node[i].left) Query(i,l,r); 43 else Query(i,node[i].left,r); 44 } 45 } 46 int main(){ 47 int n,m,q; 48 ios::sync_with_stdio(false); 49 while(cin>>n>>m){ 50 buildTree(1,1,n); 51 for(int i=1;i<=n;i++){ 52 cin>>q; 53 node[father[i]].value=q; 54 UpdateTree(father[i]); 55 } 56 string op; 57 int a,b; 58 while(m--){ 59 cin>>op>>a>>b; 60 if(op[0]=='Q'){ 61 Max=0; 62 Query(1,a,b); 63 cout<<Max<<endl; 64 } 65 else { 66 node[father[a]].value=b; 67 UpdateTree(father[a]); 68 } 69 } 70 } 71 return 0; 72 }
仅仅输入不一样
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int MAXNODE=1<<19; 4 const int MAX=2e6+10; 5 struct NODE{ 6 int value; 7 int left,right; 8 }node[MAXNODE]; 9 int father[MAX]; 10 void buildTree(int i,int left,int right){ 11 node[i].left=left; 12 node[i].right=right; 13 node[i].value=0; 14 if(left==right){ 15 father[left]=i; 16 return ; 17 } 18 buildTree(i<<1,left,(int)(floor(left+right)/2.0)); 19 buildTree((i<<1)+1,(int)(floor(left+right)/2.0)+1,right); 20 } 21 void UpdateTree(int ri){ 22 if(ri==1) return; 23 int fi=ri/2; 24 int a=node[fi<<1].value; 25 int b=node[(fi<<1)+1].value; 26 node[fi].value=max(a,b); 27 UpdateTree(ri/2); 28 } 29 int Max; 30 void Query(int i,int l,int r){ //自上而下,一直分到叶子 31 if(node[i].left==l&&node[i].right==r){ 32 Max=max(Max,node[i].value); 33 return ; 34 } 35 i=i<<1; 36 if(l<=node[i].right){ 37 if(r<=node[i].right) Query(i,l,r); 38 else Query(i,l,node[i].right); 39 } 40 i++; 41 if(r>=node[i].left){ 42 if(l>=node[i].left) Query(i,l,r); 43 else Query(i,node[i].left,r); 44 } 45 } 46 int main(){ 47 int n,m,q; 48 while(scanf("%d%d",&n,&m)){ 49 buildTree(1,1,n); 50 for(int i=1;i<=n;i++){ 51 scanf("%d",&q); 52 node[father[i]].value=q; 53 UpdateTree(father[i]); 54 } 55 char op[10]; 56 int a,b; 57 while(m--){ 58 scanf("%s%d%d",op,&a,&b); 59 if(op[0]=='Q'){ 60 Max=0; 61 Query(1,a,b); 62 printf("%d\n",Max); 63 } 64 else { 65 node[father[a]].value=b; 66 UpdateTree(father[a]); 67 } 68 } 69 } 70 return 0; 71 }