POJ 3580. SuperMemo

Description

Description

Input

The first line contains n (n ≤ 100000).

The following n lines describe the sequence.

Then follows M (M ≤ 100000), the numbers of operations and queries.

The following M lines describe the operations and queries.

Output

For each “MIN” query, output the correct answer.

Sample Input

5
1
2
3
4
5
2
ADD 2 4 1
MIN 4 5

Sample Output

5

Source

POJ Founder Monthly Contest – 2008.04.13, Yao Jinyu

Solution

  • Splay操作大杂烩!

  • 其它没什么可说的, REVOLVE 操作的话就先算出循环完的位置,

  • 直接把后面剪下来接到前面即可(相当于 CUT 操作)。

Code

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+3,inf=1e9;
int root,tot;
int a[N],fa[N],key[N],size[N],s[N][2],mn[N],c[N];
bool rev[N];
inline int read()
{
    int X=0,w=1; char ch=0;
    while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();
    return X*w;
}
inline void write(int x)
{
    if(x<0) x=-x,putchar('-');
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
inline int min(int x,int y)
{
    return x<y?x:y;
}
inline bool pd(int x)
{
    return x==s[fa[x]][1];
}
inline void newnode(int &v,int val,int p)
{
    v=++tot;
    mn[v]=key[v]=val;
    size[v]=1,fa[v]=p;
    s[v][0]=s[v][1]=c[v]=rev[v]=0;
}
inline void add(int v,int val)
{
    if(v) mn[v]+=val,c[v]+=val,key[v]+=val;
}
inline void update(int v)
{
    mn[v]=key[v];
    if(s[v][0]) mn[v]=min(mn[v],mn[s[v][0]]);
    if(s[v][1]) mn[v]=min(mn[v],mn[s[v][1]]);
    size[v]=size[s[v][0]]+size[s[v][1]]+1;
}
inline void down(int v)
{
    if(rev[v])
    {
        rev[s[v][0]]^=1,rev[s[v][1]]^=1;
        swap(s[v][0],s[v][1]);
        rev[v]=0;
    }
    if(c[v])
    {
        add(s[v][0],c[v]),add(s[v][1],c[v]);
        c[v]=0;
    }
}
inline void build(int &v,int l,int r,int p)
{
    if(l>r) return;
    int mid=(l+r)>>1;
    newnode(v,a[mid],p);
    build(s[v][0],l,mid-1,v);
    build(s[v][1],mid+1,r,v);
    update(v);
}
inline void rotate(int x)
{
    down(x);
    int y=fa[x],w=pd(x);
    if(s[y][w]=s[x][w^1]) fa[s[x][w^1]]=y;
    if(fa[x]=fa[y]) s[fa[y]][pd(y)]=x;
    s[fa[y]=x][w^1]=y;
    update(y);
}
inline void splay(int x,int k)
{
    for(int y;(y=fa[x])!=k;rotate(x))
        if(fa[y]!=k) rotate(pd(x)==pd(y)?y:x);
    update(x);
    if(!k) root=x;
}
inline int kth(int v,int k)
{
    down(v);
    if(size[s[v][0]]+1==k) return v;
    if(k<=size[s[v][0]]) return kth(s[v][0],k);
    return kth(s[v][1],k-size[s[v][0]]-1);
}
inline void change(int l,int r,int val)
{
    splay(kth(root,l-1),0);
    splay(kth(root,r+1),root);
    add(s[s[root][1]][0],val);
    update(s[root][1]),update(root);
}
inline int min_num(int l,int r)
{
    splay(kth(root,l-1),0);
    splay(kth(root,r+1),root);
    return mn[s[s[root][1]][0]];
}
inline void insert(int x,int val)
{
    splay(kth(root,x),0);
    splay(kth(root,x+1),root);
    newnode(s[s[root][1]][0],val,s[root][1]);
    update(s[root][1]),update(root);
}
inline void delete_num(int x)
{
    splay(kth(root,x-1),0);
    splay(kth(root,x),root);
    fa[s[root][1]=s[s[root][1]][1]]=root;
    update(s[root][1]),update(root);
}
inline void flip(int l,int r)
{
    splay(kth(root,l-1),0);
    splay(kth(root,r+1),root);
    rev[s[s[root][1]][0]]^=1;
}
inline void cut(int l,int r,int k)
{
    splay(kth(root,r-k),0);
    splay(kth(root,r+1),root);
    int rt=s[s[root][1]][0];s[s[root][1]][0]=0;//delete
    splay(kth(root,l-1),0);
    splay(kth(root,l),root);
    s[fa[rt]=s[root][1]][0]=rt;
    update(s[root][1]),update(root);//insert
}
inline void print(int v)
{
    down(v);
    if(s[v][0]) print(s[v][0]);
    if(key[v]<inf) write(key[v]),putchar(' ');
    if(s[v][1]) print(s[v][1]);
}
int main()
{
    int n=read();
    a[1]=a[n+2]=inf;
    for(int i=2;i<=n+1;i++) a[i]=read();
    build(root,1,n+2,0);
    int m=read();
    while(m--)
    {
        char ch=getchar();
        while(ch!='A' && ch!='M' && ch!='R' && ch!='I' && ch!='D') ch=getchar();
        if(ch=='A')
        {
            int x=read()+1,y=read()+1;
            change(x,y,read());
        }else
        if(ch=='M')
        {
            int x=read()+1,y=read()+1;
            write(min_num(x,y)),putchar('\n');
        }else
        if(ch=='I')
        {
            int x=read()+1;
            insert(x,read());
        }else
        if(ch=='D') delete_num(read()+1); else
        {
            while(ch!='S' && ch!='O') ch=getchar();
            int x=read()+1,y=read()+1;
            if(ch=='S') flip(x,y); else
            {
                int z=read()%(y-x+1);
                if(z) cut(x,y,z);
            }
        }
    }
    //print(root);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值