题解:P14806 [CCPC 2024 哈尔滨站] 一维星系

· · 题解

模拟题。

注意到:

  1. 星球位于相同位置后不会分开。
  2. 若两个星球互相穿过彼此两次,则两个星球将一直处于“震荡”状态,直到第三个星球与二者之一合并。

因此,我们称如下情况为一个“关键事件”:

  1. 两个星球 x,y 合并(由 pos_x\neq pos_y 变为 pos_x=pos_y)。
  2. 两个星球 x,y 位置交换(由 pos_y=pos_x+1 变为 pos_x=pos_y+1)。

显然一共会发生 O(n) 个关键事件,之后所有球都会处于“不动”或“震荡”状态。

使用优先队列维护所有关键事件,按时间顺序模拟这些事件。

具体地,使用星球的并查集维护星球合并,使用链表维护星球的相对顺序,当两个星球震荡时,默认两个星球的速度为 0,并在奇数时间交换二者下标。维护 to_i 表示 i 和 to_i 发生震荡。

因为事件靠前的关键事件发生可能导致事件靠后的关键事件失效,因此维护 xlst_i 表示 i 最后被更新的时间戳(相对时间),保证 xlst 不重复。当队首的 xlst 不等于当前 xlst,忽视队首,直接弹出。

当两个星球 x,y 合并时,清空 x,y 的震荡状态,更新并查集,在链表中合并 x,y,计算事件 (pre_{pre_x},pre_x),(pre_x,x),(x,nxt_x),(nxt_x,nxt_{nxt_x}) 的发生时间并入队。更新 (pre_{pre_x},pre_x) 和 (nxt_x,nxt_{nxt_x}) 的原因是可能本次修改破环了 pre_x 与 x 或 nxt_y 与 y 的震荡。注意由于我们合并了 x,y,因此上述事件对不区分 x,y。

当两个星球交换位置时,清空 x,y 的震荡状态,链表中交换 x,y,计算事件 (pre_y,y),(y,x),(x,nxt_x)(这里的 x,y 是交换后的)的发生时间并入队。当 x,y 还有继续交换的趋势时,标记 x,y 进入震荡状态。

维护 lst_i 表示 i 最后被更新的绝对时间,实现函数 getpos(i,t) 表示 i 在 lst_i 后不经历关键事件的前提下 t 时刻 pos_i 的值,这样我们无需统一处理相同时间发生的事件,按顺序处理并使用 getpos 更新当前处理的点坐标即可。

部分代码实现参考了 DaiRuiChen007 的 提交记录。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,q;
struct node{
    ll x,w,id;
}a[100005];
ll id[100005];
struct qry{
    ll id,t,p;
}qr[100005];
ll f[100005],pos[100005],dir[100005];
ll Lw[100005],Rw[100005];
ll getdir(ll i){
    if(Lw[i]==Rw[i]) return 0;
    if(Lw[i]>Rw[i]) return -1;
    return 1; 
}
ll sumw[100005];
ll fd(ll x){
    if(x==f[x]) return x;
    return f[x]=fd(f[x]);
}

ll pre[100005],nxt[100005];
void mergelist(ll x,ll y){
    if(pos[x]>pos[y]) swap(x,y);
    nxt[x]=nxt[y];
    pre[nxt[y]]=x;
    pre[y]=nxt[y]=0;
    Rw[x]-=sumw[y];
}
void swaplist(ll x,ll y){
    nxt[pre[x]]=y;
    pre[nxt[y]]=x;
    pre[y]=pre[x];
    pre[x]=y;
    nxt[x]=nxt[y];
    nxt[y]=x;
    Lw[y]-=sumw[x];
    Rw[y]+=sumw[x];
    Rw[x]-=sumw[y];
    Lw[x]+=sumw[y];
}

ll to[100005];//i 与 to_i 震荡
ll lst[100005];//上一次被更新的时间
ll xlst[100005],xtot;//无重复 lst
ll getpos(ll i,ll t){
    if(to[i]){
        if((t-lst[i])&1) return pos[to[i]];
        return pos[i];
    }
    return (ll)pos[i]+1ll*dir[i]*(t-lst[i]);
}
void delrev(ll x){
    if(to[x]){
        ll y=to[x];
        to[x]=to[y]=0;
        dir[y]=getdir(y);
        xlst[y]=++xtot;
    }
    dir[x]=getdir(x);
    xlst[x]=++xtot;
}

struct event{
    ll u,v,t;
    ll eu,ev;
    bool operator < (const event &b) const{
        return t>b.t;
    }
};
priority_queue<event> Q;

void addevent(ll u,ll v,ll t){
    if(!u||!v) return;
    ll pu=getpos(u,t),pv=getpos(v,t);
    if(pu>pv) swap(u,v),swap(pu,pv);
    if(pu==pv){
        Q.push({u,v,t,xlst[u],xlst[v]});
        return;
    }
    if(dir[u]!=1&&dir[v]!=-1) return;
    if(dir[u]==dir[v]) return;
    ll nt=0;
    if(dir[u]-dir[v]==1) nt=pv-pu;
    else nt=(pv-pu+1)/2;
    Q.push({u,v,t+nt,xlst[u],xlst[v]});
}

void upd(ll &x,ll t){
    if(!to[x]){
        pos[x]+=(t-lst[x])*dir[x];
        lst[x]=t;
    }else{
        ll tmp=t-lst[x];
        lst[x]=lst[to[x]]=t;
        if(tmp&1){
            if(pos[x]<pos[to[x]]) swaplist(x,to[x]);
            else swaplist(to[x],x);
            swap(pos[x],pos[to[x]]);
            x=to[x];
        }
    }
}

void merge(ll u,ll v){
    delrev(v);
    mergelist(u,v);
    f[v]=u;
    sumw[u]+=sumw[v];
    delrev(u);
}

void swapnode(ll u,ll v){
    swaplist(u,v);
    delrev(u);
    delrev(v);
    if(dir[u]==-1&&dir[v]==1) to[u]=v,to[v]=u,dir[u]=dir[v]=0;
}

void implm(event e){
    ll u=e.u,v=e.v,t=e.t;
    if(xlst[u]!=e.eu||xlst[v]!=e.ev||u!=fd(u)||v!=fd(v)) return;
    upd(u,t),upd(v,t);
    if(pos[u]==pos[v]){
//      cout<<"merge\n";
        merge(u,v);
        addevent(pre[u],u,t);
        addevent(pre[pre[u]],pre[u],t);//pre_u 可能与 pre_{pre_u} 震荡 
        addevent(u,nxt[u],t);
        addevent(nxt[u],nxt[nxt[u]],t);
    }else{
        if(pos[u]>pos[v]){
//          cout<<"swap\n";
            swapnode(u,v);
            addevent(pre[v],v,t);
            addevent(v,u,t);
            addevent(u,nxt[u],t);
        }else{
//          cout<<"nothing\n";
        }
    }
}

ll ans[100005];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>q;
    for(ll i=1;i<=n;i++) cin>>a[i].x>>a[i].w,a[i].id=i;
    for(ll i=1;i<=q;i++) cin>>qr[i].t>>qr[i].p,qr[i].id=i;
    sort(a+1,a+1+n,[&](node x,node y){
        return x.x<y.x;
    });
    sort(qr+1,qr+1+q,[&](qry x,qry y){
        return x.t<y.t;
    });
    for(ll i=1;i<=n;i++) id[a[i].id]=i;
    for(ll i=1;i<=n;i++){
        id[a[i].id]=i;
        ll j=i;
        while(j<=n&&a[j].x==a[i].x) j++;
        nxt[i]=j;
        pre[j]=i;
        j--;
        for(ll k=i;k<=j;k++) f[k]=i,sumw[i]+=a[k].w;
        pos[i]=a[i].x;
        i=j;
    }
    for(ll i=1;i;i=nxt[i]) Lw[i]=Lw[pre[i]]+sumw[pre[i]];
    for(ll i=fd(n);i;i=pre[i]) Rw[i]=Rw[nxt[i]]+sumw[nxt[i]];
    for(ll i=1;i;i=nxt[i]) dir[i]=getdir(i);
    for(ll i=1;i;i=nxt[i]){
        if(nxt[i]) addevent(i,nxt[i],0);
    }
    for(ll i=1;i<=q;i++) qr[i].p=fd(id[qr[i].p]);
    for(ll i=1;i<=q;i++){
        while(!Q.empty()&&Q.top().t<=qr[i].t){
            auto e=Q.top();
            Q.pop();
            implm(e);
        }
        ans[qr[i].id]=getpos(fd(qr[i].p),qr[i].t);
    }
    for(ll i=1;i<=q;i++) cout<<ans[i]<<"\n";
    return 0;
}