[explanation] roll out of Mr. Luogu P5338 [TJOI2019]

Keywords: PHP less

Original question gate

This problem can balance the tree directly and energetically, so I want to talk about the method of line segment tree + tree array

It's also very violent to use the actual line tree + tree array

First, we use the tree array to maintain the number of teams per ac. In this way, we can quickly find out how many teams have more ac than the current team

We use \ (n \) dynamic open-point line tree, and the \ (i \) line tree maintains the penalty time of the team with the ac number of \ (i \). When the ac number of a team is \ (x \) penalty \ (t \), add one to the line tree \ (t \) of \ (x \). In this way, we can quickly find out how many teams have the same ac number as the current team and have less penalty time

When a team passes a question, it can be modified in line tree and tree array normally

#include <bits/stdc++.h>
#define M 1000005
#define N 150005
#define ML 1500005
#define uint unsigned int
#define getchar nc
using namespace std;
inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
    register int x=0,f=1;register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return x*f;
}
inline void write(register int x)
{
    if(!x)putchar('0');if(x<0)x=-x,putchar('-');
    static int sta[20];register int tot=0;
    while(x)sta[tot++]=x%10,x/=10;
    while(tot)putchar(sta[--tot]+48);
}
uint randNum(uint& seed,uint last,const uint mod)
{
    seed=17*seed+last;
    return seed%mod+1;
}
struct bit{
    int tr[N];
    inline void init()
    {
        memset(tr,0,sizeof(tr));
    }
    inline void update(register int pos,register int val)
    {
        for(register int i=pos;i<N;i+=i&(-i))
            tr[i]+=val;
    }
    inline int query(register int pos)
    {
        int res=0;
        for(register int i=pos;i;i-=i&(-i))
            res+=tr[i];
        return res;
    }
}tr1;
struct segt{
    struct node{
        int ls,rs,sum;
    }tr[M*40];
    int tot,root[N];
    inline void init()
    {
        memset(root,0,sizeof(root));
        tot=0;
    }
    inline void update(register int &rt,register int l,register int r,register int pos,register int val)
    {
        if(!rt)
        { 
            rt=++tot;
            tr[rt].ls=tr[rt].rs=tr[rt].sum=0;
        } 
        if(l==r)
        {
            tr[rt].sum+=val;
            return;
        }
        int mid=l+r>>1;
        if(pos<=mid)
            update(tr[rt].ls,l,mid,pos,val);
        else    
            update(tr[rt].rs,mid+1,r,pos,val);
        tr[rt].sum=tr[tr[rt].ls].sum+tr[tr[rt].rs].sum;
    }
    inline int query(register int &rt,register int l,register int r,register int L,register int R)
    {
        if(!rt)
            return 0;
        if(L<=l&&r<=R)
            return tr[rt].sum;
        int mid=l+r>>1,res=0;
        if(L<=mid)
            res+=query(tr[rt].ls,l,mid,L,R);
        if(R>mid)
            res+=query(tr[rt].rs,mid+1,r,L,R);
        return res;
    }
}tr2;
uint seed,last;
int T,n,m,tim[N],num[N];
int main()
{
    T=read();
    last=7;
    while(T--)
    {
        m=read(),n=read(),seed=read();
        memset(num,0,sizeof(num));
        memset(tim,0,sizeof(tim));
        tr1.init(),tr2.init();
        for(register int i=1;i<=m;++i)
            num[i]=1,tim[i]=1;
        tr1.update(1,m),tr2.update(tr2.root[1],1,ML,1,m);
        for(register int i=1;i<=n;++i)
        {
            int p=randNum(seed,last,m),v=randNum(seed,last,m);
            tr1.update(num[p],-1);
            tr2.update(tr2.root[num[p]],1,ML,tim[p],-1);
            ++num[p],tim[p]+=v;
            tr1.update(num[p],1);
            tr2.update(tr2.root[num[p]],1,ML,tim[p],1);
            int res=m-tr1.query(num[p]);
            res+=tr2.query(tr2.root[num[p]],1,ML,1,tim[p]-1);
            last=res;
            write(res),puts("");
        }
    }
    return 0;
}

Posted by kidbrax on Fri, 01 Nov 2019 23:17:55 -0700