poj 3580 SuperMemo

题目链接:http://poj.org/problem?id=3580

题目思路:splay,里面有一个旋转操作,其实就是一个区间交换。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
#include<stack>
#include<list>
#include<iostream>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
#define Max 110
#define M 1010000
#define keytree ch[ch[root][1]][0]
int max(int a,int b)
{
	return a>b?a:b;
}
int min(int a,int b)
{
	return a<b?a:b;
}
int p[M],ch[M][2],v[M],s[M],a[M],mi[M],add[M],rev[M];
int top1,root,n,m;
void dadd(int x,int val)
{
    if(!x) return;
    v[x]+=val;
    mi[x]+=val;
    add[x]+=val;
}
void drev(int x)
{
    if(!x) return;
    swap(ch[x][0],ch[x][1]);
    rev[x]^=1;
}
void up(int x)
{
    int l=ch[x][0],r=ch[x][1];
    s[x]=s[l]+s[r]+1;
    mi[x]=min(v[x],min(mi[l],mi[r]));
}
void down(int x)
{
    if(rev[x])
    {
        drev(ch[x][0]);
        drev(ch[x][1]);
    }
    if(add[x])
    {
        dadd(ch[x][0],add[x]);
        dadd(ch[x][1],add[x]);
    }
    add[x]=rev[x]=0;
}
void newnode (int &x,int val,int pre)
{
    x=++top1;
    s[x]=1;
    ch[x][0]=ch[x][1]=rev[x]=add[x]=0;
    v[x]=mi[x]=val;
    p[x]=pre;
}
void build(int &x,int l,int r,int pre)
{
    if(l>r) return;
    int mid=(l+r)>>1;
    newnode(x,a[mid],pre);
    build(ch[x][0],l,mid-1,x);
    build(ch[x][1],mid+1,r,x);
    up(x);
}
void init()
{
    top1=p[0]=ch[0][0]=ch[0][1]=s[0]=rev[0]=add[0]=0;
    v[0]=mi[0]=inf;
    newnode(root,-inf,0);
    newnode(ch[root][1],inf,root);
    s[root]=2;
    build(keytree,1,n,ch[root][1]);
    up(ch[root][1]);
    up(root);
}
void rot(int x,int f)
{
    int y=p[x];
    p[ch[x][f]]=y;
    ch[y][!f]=ch[x][f];
    p[x]=p[y];
    if(p[y]) ch[p[y]][ch[p[y]][1]==y]=x;
    p[y]=x;
    ch[x][f]=y;
    up(y);
}
void splay(int x,int goal)
{
    while(p[x]!=goal)
    {
        if(p[p[x]]==goal) rot(x,ch[p[x]][0]==x);
        else
        {
            int y=p[x],f=ch[p[y]][0]==y;
            if(ch[y][f]==x)
                rot(x,!f);
            else
                rot(y,f);
            rot(x,f);
        }
    }
    if(!goal) root=x;
    up(x);
}
void rotto(int k,int goal)
{
    int x=root;
    down(x);
    while(s[ch[x][0]]!=k)
    {
        if(s[ch[x][0]]>k)
            x=ch[x][0];
        else
        {
            k-=s[ch[x][0]]+1;
            x=ch[x][1];
        }
        down(x);
    }
    splay(x,goal);
}
void insert(int pos,int num)
{
    rotto(pos,0);
    rotto(pos+1,root);
    newnode(keytree,num,ch[root][1]);
    up(ch[root][1]);
    up(root);
}
void del(int pos)
{
    rotto(pos-1,0);
    rotto(pos+1,root);
    keytree=0;
    up(ch[root][1]);
    up(root);
}
void Rev(int l,int r)
{
    rotto(l-1,0);
    rotto(r+1,root);
    drev(keytree);
}
void Add(int l,int r,int c)
{
    rotto(l-1,0);
    rotto(r+1,root);
    dadd(keytree,c);
    up(ch[root][1]);
    up(root);
}
void revolve(int l,int r,int num)
{
    num%=(r-l+1);
    num=(num+(r-l+1))%(r-l+1);
    if(!num) return;
    rotto(r-num,0);
    rotto(r+1,root);
    int x=keytree;
    keytree=0;
    up(ch[root][1]);
    up(root);
    rotto(l-1,0);
    rotto(l,root);
    keytree=x;
    p[keytree]=ch[root][1];
    up(ch[root][1]);
    up(root);
}
void getmin(int l,int r)
{
    rotto(l-1,0);
    rotto(r+1,root);
    printf("%d\n",mi[keytree]);
}
int main()
{
    char op[10];
    int l,r,c,pos,num,i;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        init();
        scanf("%d",&m);
       // printf("a");
        while(m--)
        {
            scanf("%s",op);
            if(op[0]=='I')
            {
                scanf("%d%d",&pos,&num);
                insert(pos,num);
            }
            else if(op[0]=='D')
            {
                scanf("%d",&pos);
                del(pos);
            }
            else if(op[0]=='A')
            {
                scanf("%d%d%d",&l,&r,&c);
                if(l>r) swap(l,r);
                Add(l,r,c);
            }
            else if(op[0]=='R'&&op[3]=='E')
            {
                scanf("%d%d",&l,&r);
                if(l>r) swap(l,r);
                Rev(l,r);
            }
            else if(op[0]=='M')
            {
                scanf("%d%d",&l,&r);
                if(l>r) swap(l,r);
                getmin(l,r);
            }
            else
            {
                scanf("%d%d%d",&l,&r,&c);
                if(l>r) swap(l,r);
                revolve(l,r,c);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值