BZOJ 3595: [Scoi2014] Fang Bobo the Oj Splay + + cards often dynamic knicks

Description

Fang Bobo is doing his Oj. Now he User Rankings problems in the handling of Oj.
N registered users on Oj, numbered from 1 to "the beginning they would do according to Fang Bobo mood ranked by number of these users the following four actions, modify the user's rank and serial number:
1. Operation format 1 xy, z means the number of users numbers to V, the same ranking, the user needs to output position in the queue after this operation, the data to ensure that x does appear in the queue, while 1, is not currently a ranking the number.
2. operation format is 2 x, means that the number of users x raised to the first rank, you need to perform before the operation output number z user ranking after you perform this operation.
3. operation format to 3 x, means that the number of users ranking z down to the last one, you need to perform before the operation output number z user ranking after you perform this operation.
4. operation format is 4 k, means that the current inquiry ranked as the foot of a user ID, after you perform this operation requires output current user's number.
but at the same time in order to prevent other people listening their work, Fang Bobo for his operation is encrypted, soon Modes of operation formats are changed to:
. 1 X + a Y + a
2 X + a
. 3 X + a
. 4 K + a
where on a once operation of the output obtained, beginning a = 0.
For example:
one obtained in the 5 is output
which is input in one operation: 11,315
because this input is encrypted after the operation, so you should handle 1810 is
now intercepts all operations butoxy Fang Bobo you hope you can give results.

Input

The first input line contains two space-separated integers n and m, the number of users and represents the initial operand.
Thereafter m rows, each row is a query, query format as shown in FIG.

Output

Output comprising m rows. Each row contains an integer, wherein the integer represents the i-th row of the i-th output operation.

 

And NOIP2017 day2t3 quite like, super card often. With our own strength independent code out the problem still super happy 

regular version:

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define maxn 200004  
#define lson(x) ch[x][0]
#define rson(x) ch[x][1] 
#define get(x) (ch[f[x]][1]==x) 
using namespace std;
inline void setIO(string s)
{
    string in=s+".in",out=s+".out"; 
    freopen(in.c_str(),"r",stdin); 
    freopen(out.c_str(),"w",stdout); 
}
int n,Q,lastans,root;  
int ch [maxn] [2] f [maxn] L [maxn] R [maxn], you [maxn], sta [maxn]; 
inline int de()
{
    int x; 
    scanf("%d",&x); 
    return x - lastans; 
}
// right endpoint small -> large    
struct Node
{
    int l,r,id;  
    Node(int l=0,int r=0,int id=0):l(l),r(r),id(id){} 
    bool operator<(Node b)const { return b.r > r; } 
}; 
set<Node>S; 
set<Node>::iterator it;  
struct Splay
{ 
    int cnt; 
    inline void init() 
    { 
        cnt=0; 
        for(int i=1;i<maxn-100;++i) sta[++cnt]=i;  
    }
    inline int newnode() 
    { 
        return sta[cnt--];  
    }
    // memory recovery   
    inline void erase(int x) 
    {
        ch [x] [0] = ch [x] [1] = f [x] = L [x] = R [x] = siz [x] = 0; 
        is [cnt ++] = x;       
    }
    inline void pushup(int x)
    {
        [x] = [end (x)] + [NB (x)] + r [x] -L [x] +1; 
    }     
    inline void rotate(int x)
    {
        int old=f[x],fold=f[old],which=get(x); 
        ch[old][which]=ch[x][which^1], f[ch[old][which]]=old; 
        ch[x][which^1]=old, f[old]=x, f[x]=fold; 
        if(fold) ch[fold][ch[fold][1]==old]=x; 
        pushup(old), pushup(x);     
    }
    // u -> x      
    inline void splay(int x,int &u)
    {
        you are a = f [u]; 
        for(int fa;(fa=f[x])!=a;rotate(x)) 
        {     
            if(f[fa]!=a) 
                rotate(get(fa)==get(x)?fa:x);             
        }
        u = x;   
    }
    // Find the k-th largest number
    inline int find(int kth)
    {
        int p = root;    
        while(1)
        {
            if(siz[lson(p)] >= kth) p=ch[p][0]; 
            else 
            {
                KTH - = (you [lson (p)]); 
                if(kth > R[p] - L[p] + 1) kth -= (R[p] - L[p] + 1), p = rson(p); 
                else 
                {
                    return L[p] + kth - 1; 
                }
            }
        } 
    }  
    inline void del(int x)
    {
        int p=x; 
        // root = x 
        splay(x,root); 
        if(!ch[x][0] && !ch[x][1]) root = 0;  
        else if(!ch[x][0])  root = ch[x][1], f[ch[x][1]]=0; 
        else if(!ch[x][1]) root = ch[x][0], f[ch[x][0]]=0;      
        else 
        { 
            p = ch[x][0]; 
            while(rson(p)) p = rson(p); 
            splay(p, ch[x][0]);    
            rson(p) = ch[x][1], f[ch[x][1]] = p, f[p] = 0; 
            pushup(p);     
            root=p;   
        }
        S.erase(Node(L[x], R[x],x)); 
        erase(x); 
    }    
    // insert after the first kth great location.    
    inline int insert(int kth,int l,int r)
    {
        if(!root)
        {
            root=newnode(), L[root]=l,R[root]=r;      
            pushup(root);  
            S.insert(Node(L[root], R[root], root)); 
            return root; 
        }
        int p=root, fa=0, cur=0, tmp=0; 
        while(p) 
        {
            four = p; 
            if(kth >= (tmp=siz[lson(p)] + R[p]-L[p]+1)) kth -= tmp, p=rson(p), cur=1; 
            else p=lson(p), cur=0;  
        }
        p = newnode(); 
        L[p]=l,R[p]=r; 
        ch [four] [cur] = p, f [k] = four; 
        pushup(p); 
        splay(p, root);
        S.insert(Node(L[p], R[p], p));                
        return p;        
    }
    // For point x, the split l 
    inline int split(int x,int l)
    {
        splay(x, root);     
        int tmp = siz[lson(x)],l1=L[x], r1=R[x], oo=0;  
        // this point already exists alone, without deleting     
        if(l==l1&&l==r1) return x;      
        if(l==l1) 
        {  
            of (x);    
            oo = insert(tmp,l,l); 
            insert(tmp+1,l+1,r1); 
        }
        else if(l==r1)
        {
            of (x);          
            insert(tmp,l1,r1-1); 
            oo = insert(tmp+r1-l1,r1,r1); 
        }
        else 
        { 
            of (x);   
            // printf("---------\n");
            insert(tmp,l1,l-1);  // printf("%d %d\n",l1,l-1); 
            oo = insert(tmp+l-l1,l,l);  // printf("%d %d\n",l,l); 
            insert(tmp+l-l1+1,l+1,r1);  //printf("%d %d\n",l+1,r1); 
            // for(it=S.begin();it!=S.end();it++) printf("%d %d\n",(*it).l,(*it).r); 
        }   
        return oo; 
    }
}tr; 
int main ()
{
    // setIO("input"); 
    scanf("%d%d",&n,&Q);
    tr.init(); 
    tr.insert(0,1,n);     
    S.insert(Node(1,n,root));     
    while(Q--)
    {
        int opt; 
        scanf("%d",&opt); 
        if(opt==1)
        {
            // x -> y 
            int x = from (), y = (), z;  
            it=S.lower_bound(Node(0, x, 0));         
            z = tr.split((*it).id, x);          
            S.erase(Node(L[z], R[z], z)); 
            S.insert(Node(y,y,z));                       
            tr.splay(z, root), L[z]=R[z]=y, tr.pushup(z), printf("%d\n",lastans=siz[lson(z)]+1);  
        }
        if(opt==2)
        { 
            int x = a (), z, l, r; 
            it=S.lower_bound(Node(0, x, 0));   
            z = tr.split((*it).id, x);      
            tr.splay(z, root), printf("%d\n",lastans=siz[lson(z)]+1); 
            l = L[z], r=R[z];     
            tr.del(z), tr.insert(0, l, r);      
        }
        if(opt==3)
        {
            int x = a (), z, l, r, kk;   
            // for(it=S.begin();it!=S.end();it++) printf("%d %d::\n",(*it).l,(*it).r); 
            it = S.lower_bound(Node(0, x, 0));        
            z = tr.split((*it).id, x); 
            tr.splay(z, root), printf("%d\n",lastans=siz[lson(z)]+1); 
            l = L[z], r = R[z]; 
            tr.del (z); 
            tr.insert(siz[root], l, r);      
        }
        if(opt==4)
        {
            int k = a (); 
            printf("%d\n",lastans=tr.find(k)); 
        }
    }
    return 0; 
}

  

Chang card version

#include<bits/stdc++.h>
#define maxn 200004  
#define lson(x) ch[x][0]
#define rson(x) ch[x][1] 
#define get(x) (ch[f[x]][1]==x) 
#define rg register
#define O2 __attribute__((optimize("-O2")))
using namespace std;
char *p1,*p2,buf[100000];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int rd() {int x=0; char c=nc(); while(c<48) c=nc(); while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x;}
O2 inline void setIO(rg string s)
{
    string in=s+".in",out=s+".out"; 
    freopen(in.c_str(),"r",stdin); 
    freopen(out.c_str(),"w",stdout); 
}
int n,Q,lastans,root;  
int ch [maxn] [2] f [maxn] L [maxn] R [maxn], you [maxn], sta [maxn]; 
O2 inline int ()
{
    rg int x=rd(); 
    return x - lastans; 
}
// right endpoint small -> large    
struct Node
{
    int l,r,id;  
    Node(rg int l=0,rg int r=0,rg int id=0):l(l),r(r),id(id){} 
    bool operator<(Node b)const { return b.r > r; } 
}; 
set<Node>S; 
set<Node>::iterator it;  
struct Splay
{ 
    int cnt; 
    O2 inline void init() 
    { 
        cnt=0; 
        for(rg int i=1;i<maxn-100;++i) sta[++cnt]=i;  
    }
    O2 inline int newnode() 
    { 
        return sta[cnt--];  
    }
    // memory recovery   
    O2 inline void erase(int x) 
    {
        ch [x] [0] = ch [x] [1] = f [x] = L [x] = R [x] = siz [x] = 0; 
        is [cnt ++] = x;       
    }
    O2 inline void pushup(int x)
    {
        [x] = [end (x)] + [NB (x)] + r [x] -L [x] +1; 
    }     
    O2 inline void rotate(int x)
    {
        rg int old=f[x],fold=f[old],which=get(x); 
        ch[old][which]=ch[x][which^1], f[ch[old][which]]=old; 
        ch[x][which^1]=old, f[old]=x, f[x]=fold; 
        if(fold) ch[fold][ch[fold][1]==old]=x; 
        pushup(old), pushup(x);     
    }
    // u -> x      
    O2 inline void splay(int x,int &u)
    {
        rg int a=f[u]; 
        for(rg int fa;(fa=f[x])!=a;rotate(x)) 
        {     
            if(f[fa]!=a) 
                rotate(get(fa)==get(x)?fa:x);             
        }
        u = x;   
    }
    // Find the k-th largest number
    O2 inline int find(int kth)
    {
        rg int p = root;    
        while(1)
        {
            if(siz[lson(p)] >= kth) p=ch[p][0]; 
            else 
            {
                KTH - = (you [lson (p)]); 
                if(kth > R[p] - L[p] + 1) kth -= (R[p] - L[p] + 1), p = rson(p); 
                else 
                {
                    return L[p] + kth - 1; 
                }
            }
        } 
    }  
    O2 inline void del(int x)
    {
        rg int p=x; 
        // root = x 
        splay(x,root); 
        if(!ch[x][0] && !ch[x][1]) root = 0;  
        else if(!ch[x][0])  root = ch[x][1], f[ch[x][1]]=0; 
        else if(!ch[x][1]) root = ch[x][0], f[ch[x][0]]=0;      
        else 
        { 
            p = ch[x][0]; 
            while(rson(p)) p = rson(p); 
            splay(p, ch[x][0]);    
            rson(p) = ch[x][1], f[ch[x][1]] = p, f[p] = 0; 
            pushup(p);     
            root=p;   
        }
        S.erase(Node(L[x], R[x],x)); 
        erase(x); 
    }    
    // insert after the first kth great location.    
    O2 inline int insert(rg int kth,rg int l,rg int r)
    {
        if(!root)
        {
            root=newnode(), L[root]=l,R[root]=r;      
            pushup(root);  
            S.insert(Node(L[root], R[root], root)); 
            return root; 
        }
        rg int p=root, fa=0, cur=0, tmp=0; 
        while(p) 
        {
            four = p; 
            if(kth >= (tmp=siz[lson(p)] + R[p]-L[p]+1)) kth -= tmp, p=rson(p), cur=1; 
            else p=lson(p), cur=0;  
        }
        p = newnode(); 
        L[p]=l,R[p]=r; 
        ch [four] [cur] = p, f [k] = four; 
        pushup(p); 
        splay(p, root);
        S.insert(Node(L[p], R[p], p));                
        return p;        
    }
    // For point x, the split l 
    O2 inline int split(rg int x,rg int l)
    {
        splay(x, root);     
        rg int tmp = siz[lson(x)],l1=L[x], r1=R[x], oo=0;  
        // this point already exists alone, without deleting     
        if(l==l1&&l==r1) return x;      
        if(l==l1) 
        {  
            of (x);    
            oo = insert(tmp,l,l); 
            insert(tmp+1,l+1,r1); 
        }
        else if(l==r1)
        {
            of (x);          
            insert(tmp,l1,r1-1); 
            oo = insert(tmp+r1-l1,r1,r1); 
        }
        else 
        { 
            of (x);   
            // printf("---------\n");
            insert(tmp,l1,l-1);  // printf("%d %d\n",l1,l-1); 
            oo = insert(tmp+l-l1,l,l);  // printf("%d %d\n",l,l); 
            insert(tmp+l-l1+1,l+1,r1);  //printf("%d %d\n",l+1,r1); 
            // for(it=S.begin();it!=S.end();it++) printf("%d %d\n",(*it).l,(*it).r); 
        }   
        return oo; 
    }
}tr; 
O2 int main()
{  
    n = rd (), Q = rd ();
    tr.init(); 
    tr.insert(0,1,n);     
    S.insert(Node(1,n,root));     
    while(Q--)
    {
        rg int opt=rd(); 
        if(opt==1)
        {
            // x -> y 
            int x = rg of (), y = (), z;  
            it=S.lower_bound(Node(0, x, 0));         
            z = tr.split((*it).id, x);          
            S.erase(Node(L[z], R[z], z)); 
            S.insert(Node(y,y,z));                       
            tr.splay(z, root), L[z]=R[z]=y, tr.pushup(z), printf("%d\n",lastans=siz[lson(z)]+1);  
        }
        if(opt==2)
        { 
            rg int x = a (), z, l, r; 
            it=S.lower_bound(Node(0, x, 0));   
            z = tr.split((*it).id, x);      
            tr.splay(z, root), printf("%d\n",lastans=siz[lson(z)]+1); 
            l = L[z], r=R[z];     
            tr.del(z), tr.insert(0, l, r);      
        }
        if(opt==3)
        {
            rg int x = a (), z, l, r, kk;      
            it = S.lower_bound(Node(0, x, 0));        
            z = tr.split((*it).id, x); 
            tr.splay(z, root), printf("%d\n",lastans=siz[lson(z)]+1); 
            l = L[z], r = R[z]; 
            tr.del (z); 
            tr.insert(siz[root], l, r);      
        }
        if(opt==4)
        {
            rg of int k = (); 
            printf("%d\n",lastans=tr.find(k)); 
        }
    }
    return 0; 
}

  

Guess you like

Origin www.cnblogs.com/guangheli/p/11082657.html