题解:P14806 [CCPC 2024 哈尔滨站] 一维星系
george0929 · · 题解
模拟题。
注意到:
- 星球位于相同位置后不会分开。
- 若两个星球互相穿过彼此两次,则两个星球将一直处于“震荡”状态,直到第三个星球与二者之一合并。
因此,我们称如下情况为一个“关键事件”:
- 两个星球
x,y 合并(由pos_x\neq pos_y 变为pos_x=pos_y )。 - 两个星球
x,y 位置交换(由pos_y=pos_x+1 变为pos_x=pos_y+1 )。
显然一共会发生
使用优先队列维护所有关键事件,按时间顺序模拟这些事件。
具体地,使用星球的并查集维护星球合并,使用链表维护星球的相对顺序,当两个星球震荡时,默认两个星球的速度为
因为事件靠前的关键事件发生可能导致事件靠后的关键事件失效,因此维护
当两个星球
当两个星球交换位置时,清空
维护 getpos(i,t) 表示 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;
}