HDOJ 1754 I Hate It

来源:互联网 发布:东方网域名 编辑:程序博客网 时间:2024/06/05 00:10


一个简易的splay。。。。

I Hate It
Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]  

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 
 

Source

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

[Submit]   [Go Back]   [Status]  



#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;const int maxn=222222;int sz[maxn],ch[maxn][2],pre[maxn],m[maxn],v[maxn];int id,root;int num[maxn],n,Q;void push_up(int x){    sz[x]=1+sz[ch[x][0]]+sz[ch[x][1]];    m[x]=max(v[x],max(m[ch[x][0]],m[ch[x][1]]));}void Rotate(int x,int d){    int y=pre[x];    ///push_down(y);    ///push_down(x);    pre[ch[x][d]]=y;    ch[y][!d]=ch[x][d];    pre[x]=pre[y];    if(pre[x]) ch[pre[y]][ch[pre[y]][1]==y]=x;    pre[y]=x;    ch[x][d]=y;    push_up(y);}void Splay(int x,int goal){    ///push_down(x);    while(pre[x]!=goal)    {        if(pre[pre[x]]==goal)        {            Rotate(x,ch[pre[x]][0]==x);        }        else        {            int y=pre[x],d=ch[pre[y]][0]==y;            if(ch[y][d]==x) Rotate(x,!d);            else Rotate(y,d);            Rotate(x,d);        }        ///push_down(x);    }    push_up(x);    if(!goal) root=x;}void RotateTo(int k,int goal){    int x=root;    ///push_down(x);    while(sz[ch[x][0]]!=k)    {        if(k<sz[ch[x][0]])        {            x=ch[x][0];        }        else        {            k-=(sz[ch[x][0]]+1);            x=ch[x][1];        }        ///push_down(x);    }    Splay(x,goal);}void newNode(int& x,int c){    x=++id;    sz[x]=1; m[x]=v[x]=c;    ch[x][0]=ch[x][1]=pre[x]=0;}void makeTree(int& x,int l,int r,int f){    if(l>r) return ;    int m=(l+r)>>1;    newNode(x,num[m]);    makeTree(ch[x][0],l,m-1,x);    makeTree(ch[x][1],m+1,r,x);    pre[x]=f;    push_up(x);}void init(){    ch[0][1]=ch[0][0]=pre[0]=0;    sz[0]=v[0]=m[0]=0;    id=root=0;    newNode(root,-1);    newNode(ch[root][1],-1);    pre[id]=root;    sz[root]=2;    makeTree(ch[ch[root][1]][0],0,n-1,ch[root][1]);    push_up(ch[root][1]);    push_up(root);}int main(){while(scanf("%d%d",&n,&Q)!=EOF){    for(int i=0;i<n;i++) scanf("%d",num+i);    init();    while(Q--)    {        char str[3];        int a,b;        scanf("%s%d%d",str,&a,&b);        if(str[0]=='Q')        {            RotateTo(a-1,0);            RotateTo(b+1,root);            printf("%d\n",m[ch[ch[root][1]][0]]);        }        else if(str[0]=='U')        {            RotateTo(a,0);            v[root]=b;            if(b>m[root]) m[root]=b;        }    }}    return 0;}





0 0
原创粉丝点击