hdu 1754 I Hate It(splay入门题)

来源:互联网 发布:蜡笔小新的爷爷 知乎 编辑:程序博客网 时间:2024/05/22 06:19

I Hate It

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


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专场
 

Recommend
lcy
 
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1754
分析:这题是区间的有关操作,可以用splay来做,询问一个区间的话,只要把那个区间的左边界-1转到根,把右边界+1转到根的右儿子,这样这个区间就在根的右儿子的左儿子处。。。更新一个节点是,把该节点转到根节点,然后直接更新。。。效率貌似不高啊500ms++

这题其实用线段树或树状数组会简单许多,线段树的话就是单点更新,加上区间求最值。。。代码在后面
代码:
#include<cstdio>#include<iostream>using namespace std;const int mm=222222;int i,n,m,num[mm];struct splaytree{    int c[mm][2],p[mm],s[mm],v[mm],m[mm];    int id,root;    void rotate(int x,int f)    {        int y=p[x];        c[y][!f]=c[x][f];        p[c[x][f]]=y;        p[x]=p[y];        if(p[x])c[p[y]][c[p[y]][1]==y]=x;        c[x][f]=y;        p[y]=x;        updata(y);    }    void splay(int x,int goal)    {        while(p[x]!=goal)            if(p[p[x]]==goal)rotate(x,c[p[x]][0]==x);            else            {                int y=p[x],f=(c[p[y]][0]==y);                if(c[y][f]==x)rotate(x,!f);                else rotate(y,f);                rotate(x,f);            }        updata(x);        if(!goal)root=x;    }    void select(int k,int goal)    {        int x=root;        while(s[c[x][0]]!=k)        {            if(s[c[x][0]]>k)x=c[x][0];            else k-=(s[c[x][0]]+1),x=c[x][1];        }        splay(x,goal);    }    void make(int &x,int l,int r,int f)    {        if(l>r)return;        int m=(l+r)>>1;        newnode(x,num[m]);        make(c[x][0],l,m-1,x);        make(c[x][1],m+1,r,x);        p[x]=f;        updata(x);    }    void newnode(int &x,int a)    {        x=++id;        c[x][0]=c[x][1]=p[x]=0;        m[x]=v[x]=a;        s[x]=1;    }    void updata(int x)    {        s[x]=1+s[c[x][0]]+s[c[x][1]];        m[x]=max(v[x],max(m[c[x][0]],m[c[x][1]]));    }    void prepare()    {        c[0][0]=c[0][1]=p[0]=0;        s[0]=v[0]=m[0]=0;        id=root=0;        newnode(root,-1);        newnode(c[root][1],-1);        p[id]=root;        s[root]=2;        make(c[c[root][1]][0],0,n-1,c[root][1]);        updata(c[root][1]);        updata(root);    }    void change()    {        int a,b;        scanf("%d%d",&a,&b);        select(a,0);        v[root]=b;        if(b>m[root])m[root]=b;    }    void answer()    {        int a,b;        scanf("%d%d",&a,&b);        select(a-1,0);        select(b+1,root);        printf("%d\n",m[c[c[root][1]][0]]);    }} spt;int main(){    char op[2];    while(scanf("%d%d",&n,&m)!=-1)    {        for(i=0; i<n; ++i)scanf("%d",&num[i]);        spt.prepare();        while(m--)        {            scanf("%s",op);            if(op[0]=='U')spt.change();            else spt.answer();        }    }    return 0;}

线段树:
#include<cstdio>#include<cstring>#include<iostream>#include<cmath>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;const int mm=888888;int ans[mm];char op[55];int i,j,n,m;void pushup(int rt){    ans[rt]=max(ans[rt<<1],ans[rt<<1|1]);}void build(int l,int r,int rt){    if(l==r)    {        scanf("%d",&ans[rt]);        return;    }    int m=(l+r)>>1;    build(lson);    build(rson);    pushup(rt);}void updata(int p,int val,int l,int r,int rt){    if(l==r)    {        ans[rt]=val;        return;    }    int m=(l+r)>>1;    if(p<=m)updata(p,val,lson);    else updata(p,val,rson);    pushup(rt);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&R>=r)return ans[rt];    int m=(l+r)>>1,ret=0;    if(L<=m)ret=max(ret,query(L,R,lson));    if(R>m)ret=max(ret,query(L,R,rson));    return ret;}int main(){    while(scanf("%d%d",&n,&m)!=-1)    {        build(1,n,1);        while(m--)        {            scanf("%s%d%d",op,&i,&j);            if(op[0]=='U')updata(i,j,1,n,1);            if(op[0]=='Q')printf("%d\n",query(i,j,1,n,1));        }    }    return 0;}