# HDU 1754 I hate it

225人阅读 评论(0)

/*B - I Hate It
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit

Status
Description

Input

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
*/

#include <cstdio>
#include <algorithm>
#include <ctime>
using namespace std;

int N,M;
const int SIZE = 200010,INTMIN = -2147483647;
int g_students[SIZE],g_segTree[SIZE*4];//定义四倍空间节点

//node对应区间[begin,end]。node是g_segTree,begin~end是g_students
void Build(int node,int begin,int end){
if(begin==end){
g_segTree[node] = g_students[begin];
return ;
}
int mid = (begin + end) >> 1;
Build(2*node,begin,mid);
Build(2*node+1,mid+1,end);
g_segTree[node] = max(g_segTree[2*node],g_segTree[2*node+1]);
return ;
}

int Query(int node,int begin,int end,int q_left,int q_right){
if(q_left>end || q_right<begin) return INTMIN ;
if(q_left<=begin && end <= q_right) return g_segTree[node];
int mid = begin + ((end-begin)>>1);
int lson = Query(node*2,begin,mid,q_left,q_right);
int rson = Query(node*2+1,mid+1,end,q_left,q_right);
return max(lson,rson);
}
/*

*/
void Update(int node,int begin,int end,int indexer,int data){
if(begin==end && indexer==begin){//单节点更新，
g_segTree[node] = data;
return ;
}
//找到待更新的子树
int mid = (begin+end) >> 1;//区间分割
if(indexer <= mid) Update(2*node,begin,mid,indexer,data);
else Update(2*node+1,mid+1,end,indexer,data);
g_segTree[node] = max(g_segTree[2*node],g_segTree[2*node+1]);
return ;
}

int main(){
while(scanf("%d %d",&N,&M)!=EOF){
getchar();
for(int i=1;i<=N;i++) scanf("%d",g_students+i);
Build(1,1,N);
char C;
int A,B;
while(M--){
getchar();
scanf("%c %d %d",&C,&A,&B);
if(C=='Q'){
//查询区间为[A,B],A应该<=B
if(A>B) swap(A,B);//但这一句只能写到if内,，不能写在上面，因为对于Update操作，
int res = Query(1,1,N,A,B);//A是节点，B是待更新的成绩，A，B大小关系不确定是理所应当的
printf("%d\n", res);
}//注意
else if(C=='U') Update(1,1,N,A,B);
}
}
return 0;
}

个人资料
等级：
访问量： 5万+
积分： 1260
排名： 3万+
最新评论