I Hate It(更新点,查找最大值)

来源:互联网 发布:js动态为div添加style 编辑:程序博客网 时间:2024/06/06 09:11

I Hate It

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


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。
 

Output
对于每一次询问操作,在一行里面输出最高成绩。
 

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

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

Author
linle
 

Source
2007省赛集训队练习赛(6)_linle专场




#include<cstdio>#include<iostream>#include<cstring>#define MAXN 200010#define max(a,b) (a > b ? a : b)#define min(a,b) (a > b ? b : a)using namespace std;int a[MAXN];struct Node{    int L,R,max;}S[MAXN * 4];int build(int root, int L, int R){    S[root].L = L;    S[root].R = R;    if(L==R)return S[root].max = a[L];        int mid=(L+R)/2;    int a= build(root*2, L , mid);    int b= build(root*2+1, mid+1 ,R);        return S[root].max = max(a,b);    }int update(int root, int P, int val){    if(P < S[root].L || P> S[root].R)return S[root].max; // 若 P 不存在于 root 所管理的区间内          if(P == S[root].L && P== S[root].R)return S[root].max = val;// 若 P为一个叶子节点          int a = update(root*2,P,val);    int b = update(root*2+1,P,val);         return S[root].max = max(a,b);    }int query(int root, int L, int R){    if(L > S[root].R || R < S[root].L)return 0;//若此区间与 root 所管理的区间无交集        if(L<= S[root].L && R>= S[root].R)return S[root].max;// 若此区间包含 root 所管理的区间         int a = query(root*2, L, R);    int b = query(root*2+1 , L, R);        return max(a,b);        }int main(){    int n,t,x,y;    char C;    while(scanf("%d%d",&n,&t)!=EOF){        for(int i=1; i<= n; i++)scanf("%d",&a[i]);        build(1,1,n);        while(t--){            getchar();            scanf("%c%d%d",&C,&x,&y);            if(C == 'U')update(1,x,y);            else printf("%d\n",query(1,x,y));            }        }        return 0;}

0 0
原创粉丝点击