splay 求助
查看原帖
splay 求助
420129
Nt_Tsumiki楼主2022/10/5 18:42
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>

#define PII std::pair<int,int>
#define INF 100000001
#define int unsigned int

namespace Splay {
    struct Node {
        int fa,siz,num,ch[2];
        PII val;
    }t[1000010];

    int cnt,rt,loc[1000010];

    void upd(int x) { t[x].siz=t[x].num+t[t[x].ch[0]].siz+t[t[x].ch[1]].siz; }

    int get(int x) { return x==t[t[x].fa].ch[1]; }
 
    void clear(int x) { t[x].fa=t[x].siz=t[x].num=t[x].ch[0]=t[x].ch[1]=0,t[x].val={0,0}; }

    void rotate(int x) {
        int y=t[x].fa,z=t[y].fa,tmp=get(x);
        t[y].ch[tmp]=t[x].ch[tmp^1];
        if (t[x].ch[tmp^1]) t[t[x].ch[tmp^1]].fa=y;
        t[x].ch[tmp^1]=y;
        t[y].fa=x;
        t[x].fa=z;
        if (z) t[z].ch[y==t[z].ch[1]]=x;
        upd(y);
        upd(x);
    }

    void splay(int x,int goal=0) {
        for (int f=t[x].fa;f=t[x].fa,f!=goal;rotate(x)) 
            if (t[f].fa!=goal) rotate(get(f)==get(x)?f:x);
        if (!goal) rt=x;
    }

    int ins(int x,int k) {
        if (!rt) {
            t[rt=++cnt].val={x,k};
            t[rt].num++;
            upd(rt);
            return rt;
        }
        int y=rt,f=0;
        while (1) {
            if (t[y].val.first==x and t[y].val.second==k) {
                t[y].num++;
                upd(y);
                upd(f);
                splay(y);
                return y;
            }
            f=y,y=t[y].ch[x==t[y].val.first?k>t[y].val.second:x>t[y].val.first];
            if (!y) {
                t[++cnt].val={x,k};
                t[cnt].num++;
                t[cnt].fa=f;
                t[f].ch[x==t[f].val.first?k>t[f].val.second:x>t[f].val.first]=cnt;
                upd(cnt);
                upd(f);
                splay(cnt);
                return cnt;
            }
        }
    }

    int rk() { return t[t[rt].ch[1]].siz; }

    int pre(int x) {
        int y=t[x].ch[0];
        if (!y) return y;
        while (t[y].ch[1]) y=t[y].ch[1];
        splay(y);
        return y; 
    }

    void del(int x) {
        splay(x);
        if (t[rt].num>1) {
            t[rt].num--;
            upd(rt);
        } else if (!t[rt].ch[0] and !t[rt].ch[1]) {
            clear(rt);
            rt=0;
        } else if (!t[rt].ch[0]) {
            int tmp=rt;
            rt=t[rt].ch[1];
            t[rt].fa=0;
            clear(tmp);
        } else if (!t[rt].ch[1]) {
            int tmp=rt;
            rt=t[rt].ch[0];
            t[rt].fa=0;
            clear(tmp);
        } else {
            int t1=rt,t2=pre(rt);
            t[rt].ch[1]=t[t1].ch[1];
            t[t[rt].ch[1]].fa=rt;
            clear(t1);
            upd(rt);
        }
    }
}
using namespace Splay;
using namespace std;
int n,m,seed,last=7,ria,rib,tt;

int randNum( int& seed , int last , const int m){ 
    seed = seed * 17 + last ; return seed % m + 1; 
}

void work(int x) {
    if (!x) return ;
    work(t[x].ch[0]);
    printf("%d %d\n",t[x].val.first,t[x].val.second);
    work(t[x].ch[1]);
}

signed main() {
    scanf("%u",&tt);
    while (tt--) {
        rt=cnt=0;
        last=7;
        memset(loc,0,sizeof loc);
        scanf("%u%u%u",&m,&n,&seed);
        ins(0,INF),ins(INF,0);
        for (int i=1;i<=n;i++) {
            ria=randNum(seed,last,m);
            rib=randNum(seed,last,m);
            // printf("%d %d\n",ria,rib);
            if (!loc[ria]) {
                loc[ria]=ins(1,-rib);
            } else {
                int t1=t[loc[ria]].val.first,t2=t[loc[ria]].val.second;
                del(loc[ria]);
                loc[ria]=ins(t1+1,t2-rib);
            }
            last=rk()-1;
            printf("%u\n",last);
            // work(rt);
        }
    }
    return 0;
}

全 T

2022/10/5 18:42
加载中...