[Tree line] [Splay] [Fenwick tree] JZOJ 3292

Description

In some poker games, such as Texas Hold'em, the dealer is luxurious. A generic term for professional licensing hand for the dealer. Before dealer licensing, first pin card (burn card). The so-called pin card, is to move into the current library card at that end of the top deck, which is used to prevent players guessing cards and affect the game.

Suppose a start, dealers come up with a new deck, the deck of N different cards, numbered sequentially from 1 to N. Because it is a new card, the card is properly arranged in order, starting from the top deck, followed by 1, 2, ...... until the N, N deck plate at the bottom. After all the cards issued order, dealer will licensing operation N times, before the i-th licensing, Ri times he will continuously operating pin card, given by the input Ri. Will the players finally get the order of the deck is what?

For example, assume that N = 4, when the beginning of the sequence constituting the library card is in {1, 2, 3, 4}.

Suppose R1 = 2, the two pins should be connected dealer cards, the deck 1 and 2 into the bottom, then 3 to the player. Library card is currently in the order of {4, 1, 2}.

We assumed that R2 = 0, the dealer does not require a pin card, directly to the player 4, the current library card in order to {1,2}.

Suppose R3 = 3, then the dealer order to pin 1, 2, 1, 2 and then distributed to the players. At present library, leaving only a card 1.

Assume R4 = 2, the dealer after repeated twice to pin 1, or the one sent to the players, because 1 is the only library card.
 

Input

Line 1, an integer N, the number of licenses.

Row 2 to row 1 + N, i + 1 in the first row, there is an integer Ri, 0≤Ri <N

Output

Line 1 to line N: i-th row is only an integer that represents the i-card players received numbers.
 

Sample Input

4
2
0
3
2

Sample Output

3
4
2
1
 

Data Constraint

analysis

This question is to ask, each time to find and eject k-th largest element

$ K = \ the number of elements remaining sum r_i $% +1

Then this operation is not difficult to think of a balanced tree weights segment tree

Delete point fucks it on the line

By the way, the game time to write a balanced tree, the results did not see it% can change the direct use of balanced trees put simulate the brand ...... more incredible is that for so long did not write a thing can actually write out the correct answer

This is a problem people are very insidious problem ah, Splay added a quick read and oxygen was 90, then head of a pumping want to use slower Fenwick tree ......

 

Segment tree 100pts

#include <iostream>
#include <cstdio>
using namespace std;
const int N=7e5+10;
struct Seg {
    int c[2],sz;
}t[4*N];
int rt=1,n,r;

void Update(int x) {
    t[x].sz=t[t[x].c[0]].sz+t[t[x].c[1]].sz;
}

void Build(int x,int l,int r) {
    if (l==r) {
        t[x].sz=1;
        return;
    }
    int mid=l+r>>1;
    Build(t[x].c[0]=x<<1,l,mid);
    Build(t[x].c[1]=(x<<1)+1,mid+1,r);
    Update(x);
}

void Find_Del(int x,int l,int r,int k,int from) {
    if (l==r) {
        printf("%d\n",l);
        t[x>>1].c[from]=0;
        return;
    }
    int mid=l+r>>1;
    if (t[x].c[0]&&t[t[x].c[0]].sz>=k) Find_Del(t[x].c[0],l,mid,k,0);
    else if (t[x].c[1]&&t[t[x].c[1]].sz>=k-t[t[x].c[0]].sz) Find_Del(t[x].c[1],mid+1,r,k-t[t[x].c[0]].sz,1);
    Update(x);
}

int main() {
    scanf("%d",&n);
    Build(rt,1,n);
    for (int i=1;i<=n;i++) {
        int R;
        scanf("%d",&R);r=(R+r)%(n-i+1);
        Find_Del(rt,1,n,r+1,666);
    }
}
View Code

Splay O(2) 90pts

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
using namespace std;
const int N=7e5+10;
struct Node {
    int c[2],f,id,sz;
}t[N];
int rt,cnt;
int n,r;
bool a[N];

int Read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}

bool Witch(int x) {return t[t[x].f].c[1]==x;}

void Update(int x) {
    t[x].sz=1+t[t[x].c[0]].sz+t[t[x].c[1]].sz;
}

void Rotate(int x) {
    int f=t[x].f,gf=t[f].f,lr=Witch(x);
    t[x].f=gf;if (gf) t[gf].c[Witch(f)]=x;
    t[t[f].c[lr]=t[x].c[lr^1]].f=f;
    t[t[x].c[lr^1]=f].f=x;
    Update(f);Update(x);
}

void Splay(int x,int goal) {
    for (;t[x].f!=goal;Rotate(x))
        if (t[t[x].f].f!=goal) Rotate(Witch(t[x].f)==Witch(x)?t[x].f:x);
    if (!goal) rt=x;
}

void Build(int &x,int l,int r) {
    int mid=l+r>>1;
    if (a[mid]) return;
    if (!x) x=++cnt;
    t[x].id=mid;t[x].sz=1;a[mid]=1;
    Build(t[x].c[0],l,mid);
    Build(t[x].c[1],mid+1,r);
    t[x].sz+=t[t[x].c[0]].sz+t[t[x].c[1]].sz;
    if (t[x].c[0]) t[t[x].c[0]].f=x;
    if (t[x].c[1]) t[t[x].c[1]].f=x;
}

int Get_Kth(int x,int k) {
    if (t[t[x].c[0]].sz+1>k) return Get_Kth(t[x].c[0],k);
    else {
        if (t[t[x].c[0]].sz+1==k) return x;
        return Get_Kth(t[x].c[1],k-t[t[x].c[0]].sz-1);
    }
}

void Del(int x) {
    Splay(x,0);
    int oldrt=rt;
    printf("%d\n",t[x].id);
    if (!t[x].c[0]&&!t[x].c[1]) {
        rt=0;return;
    }
    if (!t[x].c[0]) {
        rt=t[x].c[1];t[rt].f=0;t[oldrt].c[1]=0;
        return;
    }
    if (!t[x].c[1]) {
        rt=t[x].c[0];t[rt].f=0;t[oldrt].c[0]=0;
        return;
    }
    int p=t[x].c[0];
    while (t[p].c[1]) p=t[p].c[1];
    Splay(p,0);
    rt=p;t[t[rt].c[1]=t[oldrt].c[1]].f=rt;
    t[oldrt].f=t[oldrt].c[1]=0;
    Update(rt);
}

int main() {
    n=Read();
    Build(rt,1,n);
    for (int i=1;i<=n;i++) {
        int R;
        R=Read();(r+=R)%=cnt;
        Del(Get_Kth(rt,r+1));cnt--;
    }
    cnt++;
}
View Code

Fenwick tree O (2) about 60pts

#include <iostream>
#include <cstdio>
using namespace std;
#define lowbit(x) x&-x
const int N=7e5+10;
int t[N];
int n,R;

void Add(int x) {
    for (;x<=n;x+=lowbit(x)) t[x]++;
}

void Dec(int x) {
    for (;x<=n;x+=lowbit(x)) t[x]--;
}

int Sum(int x) {
    int ans=0;
    for (;x;x-=lowbit(x)) ans+=t[x];
    return ans;
}

int main() {
    scanf("%d",&n);
    for (int i=1;i<=n;i++) Add(i);
    for (int i=1;i<=n;i++) {
        int r;
        scanf("%d",&r);R=(R+r)%(n-i+1);
        int j=R+1;
        while (Sum(j)!=R+1) j+=R+1-Sum(j);
        printf("%d\n",j);
        Dec(j);
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/mastervan/p/11123216.html