splay poj 3580

//16:38
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 0x7FFFFFFF
#define maxn 1111111
using namespace std;
int n,que,ll,rr,v,T,id,root,tot,a[maxn],fa[maxn],size[maxn],key[maxn],minv[maxn],del[maxn],rev[maxn],ch[maxn][2];
char opt[11];
void init()
{
    size[0]=fa[0]=0;
    minv[0]=INF;
}
void update(int x)
{
     size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
     minv[x]=key[x];
     if (ch[x][0]) minv[x]=min(minv[ch[x][0]],minv[x]);
     if (ch[x][1]) minv[x]=min(minv[ch[x][1]],minv[x]);
}
void pushdown(int x)
{
     if (del[x])
     {
        if (ch[x][0]) { del[ch[x][0]]+=del[x]; minv[ch[x][0]]+=del[x]; key[ch[x][0]]+=del[x]; }
        if (ch[x][1]) { del[ch[x][1]]+=del[x]; minv[ch[x][1]]+=del[x]; key[ch[x][1]]+=del[x]; }
        del[x]=0;
     }
     if (rev[x])
     {
        if (ch[x][0]) rev[ch[x][0]]^=1;
        if (ch[x][1]) rev[ch[x][1]]^=1;
        swap(ch[x][0],ch[x][1]);
        rev[x]=0;
     }
}
void rotate(int x,int opt) //RR 0 LR 1
{
    int y=fa[x];
    pushdown(y); pushdown(x);
    ch[y][opt]=ch[x][opt^1]; if (ch[x][opt^1]) fa[ch[x][opt^1]]=y;
    fa[x]=fa[y]; if (fa[y]) ch[fa[y]][y==ch[fa[y]][1]]=x;
    fa[y]=x;
    ch[x][opt^1]=y;
    update(y); update(x);
}
void splay(int x,int y)
{
    while (fa[x]!=y)
    {
        if (fa[fa[x]]==y) rotate(x,x==ch[fa[x]][1]);
           else {
                    if (fa[x]==ch[fa[fa[x]]][0])
                    {
                       if (x==ch[fa[x]][0]) { rotate(fa[x],0); rotate(x,0); }
                          else { rotate(x,1); rotate(x,0); }
                    }
                    else
                    {
                       if (x==ch[fa[x]][1]) { rotate(fa[x],1); rotate(x,1); }
                          else { rotate(x,0); rotate(x,1); }
                    }
                }
    }
    if (!y) root=x;
}
int getpos(int x,int rem)
{
    pushdown(x);
    if (size[ch[x][0]]>=rem) return getpos(ch[x][0],rem);
    if (size[ch[x][0]]+1==rem) return x;
    return getpos(ch[x][1],rem-size[ch[x][0]]-1);
}
void add(int ll,int rr,int d)
{
    int x=getpos(root,ll),y=getpos(root,rr+2);
    splay(x,0);
    splay(y,x);
    del[ch[y][0]]+=d; minv[ch[y][0]]+=d; key[ch[y][0]]+=d;
    update(y);
    update(x);
}
void reverse(int ll,int rr)
{
    int x=getpos(root,ll),y=getpos(root,rr+2);
    splay(x,0);
    splay(y,x);
    rev[ch[y][0]]^=1;
}
void revolve(int ll,int rr,int T)
{
    T%=(rr-ll+1);
    if (!T) return;
    T=rr-T;
    reverse(ll,T); reverse(T+1,rr); reverse(ll,rr);
}
void insert(int id,int v)
{
    int x=getpos(root,id+1),y=getpos(root,id+2);
    splay(x,0); splay(y,x);
    key[++tot]=v; minv[tot]=v; size[tot]=1;
    fa[tot]=y;
    ch[y][0]=tot;
    update(y); update(x);
    splay(tot,0);
}
void remove(int id)
{
    int x=getpos(root,id),y=getpos(root,id+2);
    splay(x,0); splay(y,x);
    ch[y][0]=0; update(y); update(x);
}
int query(int ll,int rr)
{
    int x=getpos(root,ll),y=getpos(root,rr+2);
    splay(x,0); splay(y,x);
    return minv[ch[y][0]];
}
void addnode(int now,int &x,int fr)
{
    key[++tot]=a[now]; minv[tot]=key[tot];
    size[tot]=1;
    fa[tot]=fr;
    x=tot;
}
void build(int ll,int rr,int &x,int fr)
{
     if (ll>rr) return;
     int mid=(ll+rr)/2;
     addnode(mid,x,fr);
     build(ll,mid-1,ch[x][0],x);
     build(mid+1,rr,ch[x][1],x);
     update(x);
}
int main()
{
    init();
    scanf("%d",&n);
    tot=2; root=1;
    ch[1][1]=2;
    fa[2]=1;
    size[1]=2; size[2]=1; minv[1]=minv[2]=INF;
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    build(1,n,ch[2][0],2);
    update(2); update(1);
    scanf("%d",&que);
    for (int i=1;i<=que;i++)
    {
        scanf("%s",opt);
        switch (opt[0])
        {
           case ‘A‘:
                scanf("%d %d %d",&ll,&rr,&v);
                add(ll,rr,v);
                break;
           case ‘R‘:
                if (opt[3]==‘E‘)
                {
                   scanf("%d %d",&ll,&rr);
                   reverse(ll,rr);
                }
                else
                {
                   scanf("%d %d %d",&ll,&rr,&T);
                   revolve(ll,rr,T);
                }
                break;
           case ‘I‘:
                scanf("%d %d",&id,&v);
                insert(id,v);
                break;
           case ‘D‘:
                scanf("%d",&id);
                remove(id);
                break;
           case ‘M‘:
                scanf("%d %d",&ll,&rr);
                printf("%d\n",query(ll,rr));
                break;
        }
    }
    return 0;
}
时间: 2024-10-10 05:34:09

splay poj 3580的相关文章

poj 3580 SuperMemo (Splay)

poj 3580 好恶心的题目,真是各种操作都来了个遍 ... 不过Splay树真是一种神奇的东西,通过旋转就能实现各种操作,而且方法也都相差不大 . 题意: 给出一个数字序列,有6种操作: (1) ADD x y d: 第x个数到第y个数加d . (2) REVERSE x y : 将区间[x,y]中的数翻转 . (3) REVOLVE x y t :将区间[x,y]旋转t次,如1 2 3 4 5 旋转2次后就变成4 5 1 2 3 . (4) INSERT x p :在第x个数后面插入p .

POJ 3580 Splay

G - SuperMemo Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3580 Appoint description:  System Crawler  (2014-11-27) Description Your friend, Jackson is invited to a TV show called SuperMemo in

poj 3580 SuperMemo splay树模板题

题意: 给一个序列,对其进行各种操作.在对序列仅对操作的处理上,splay是比线段树强大的,虽然均摊复杂度均为logN,但它能支持1:在某个位置插入一些连续的数,2:在某个位置删除一些连续的数.只是splay随便一些200+行. 分析: 网上各种模板介绍漫天飞,这个还算简洁明了. 代码: //poj 3580 #include <stdio.h> #define maxN 200000 int N,T,node; int a[maxN],size[maxN],left[maxN],right[

POJ 3580 SuperMemo

裸Splay区间操作: 内存池+区间加减+区间翻转+插入+删除+维护最值 SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8552   Accepted: 2801 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participa

SuperMemo(POJ 3580)

SuperMemo Time Limit: 5000MS   Memory Limit: 65536K       Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game. At first, the host tells the parti

POJ 3580 - SuperMemo - [伸展树splay]

题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game. At first, the host tells the participant a sequence of numbers, {A1, A2, ... An}. Then the h

POJ 3580(SuperMemo-Splay区间加)[template:Splay V2]

SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11384   Accepted: 3572 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game

Splay树(多操作)——POJ 3580 SuperMemo

对应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11309   Accepted: 3545 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a

又一道Splay吐血题 [POJ 3580] SuperMemo

SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9878   Accepted: 3177 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game.