QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#709252 | #9524. 1D Galaxy | qzez | WA | 2ms | 9932kb | C++14 | 3.6kb | 2024-11-04 13:18:59 | 2024-11-04 13:19:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e5+10;
const ll INF=1e18;
int n,m,fa[N];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
struct ques{
int t,i,id;
}o[N];
ll w[N],a[N];
int cur[N],oth[N];
int pre[N],nex[N];
ll pw[N],sw[N];
int d[N];
int tlas[N];
void getd(int i){
if(pw[i]>sw[i])d[i]=-1;
else if(pw[i]<sw[i])d[i]=1;
else d[i]=0;
}
void swp(int y){
int x=pre[y];
int p=pre[x],q=nex[y];
nex[p]=y,pre[y]=p;
nex[y]=x,pre[x]=y;
nex[x]=q,pre[q]=x;
pw[y]=pw[pre[y]]+w[pre[y]];
pw[x]=pw[pre[x]]+w[pre[x]];
sw[x]=sw[nex[x]]+w[nex[x]];
sw[y]=sw[nex[y]]+w[nex[y]];
}
int refind(int x,int t){
if(oth[x]){
int y=oth[x];
if((t-tlas[x])&1){
swp(a[x]<a[y]?y:x);
tlas[x]=tlas[y]=t;
return y;
}else{
tlas[x]=tlas[y]=t;
return x;
}
}else{
a[x]+=(t-tlas[x])*d[x],tlas[x]=t;
return x;
}
}
ll geta(int x,int t){
if(oth[x]){
int y=oth[x];
if((t-tlas[x])&1)return a[y];
else return a[x];
}else{
return a[x]+(t-tlas[x])*d[x];
}
}
void remove(int y){
int x=pre[y];
fa[y]=x;
w[x]+=w[y];
nex[x]=nex[y];
pre[nex[y]]=x;
sw[x]=sw[nex[x]]+w[nex[x]];
}
int vcnt,val[N];
void update(int x){
if(oth[x]){
int y=oth[x];
oth[x]=oth[y]=0;
val[y]=++vcnt;
getd(y);
}
val[x]=++vcnt;
}
priority_queue<tuple<ll,int,int,int>>q;
// (t,x,w1,w2) -t pre[x],x val[pre[x]]=w1,val[x]=w2
void push(int y,int t0){
int x=pre[y];
// cerr<<"push t0 "<<x<<' '<<y<<' '<<t0<<' '<<d[x]<<' '<<d[y]<<endl;
if(geta(y,t0)-geta(x,t0)==0){
q.push({-t0,y,val[x],val[y]});
return;
}
if(d[y]-d[x]>=0)return;
int v=d[x]-d[y];
ll t=t0+(geta(y,t0)-geta(x,t0)+v-1)/v;
// cerr<<"push t "<<x<<' '<<y<<' '<<t<<' '<<d[x]<<' '<<d[y]<<endl;
// cerr<<"push "<<x<<' '<<y<<' '<<t<<' '<<t0<<' '<<d[x]<<' '<<d[y]<<endl;
q.push({-t,y,val[x],val[y]});
}
ll ans[N];
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld%lld",&a[i],&w[i]);
a[0]=-INF,a[n+1]=INF;
iota(cur,cur+1+n+1,0);
sort(cur+1,cur+1+n,[&](int x,int y){
return a[x]<a[y];
});
iota(fa,fa+1+n,0);
for(int l=1,r,las=0;l<=n+1;l=r){
for(r=l+1;r<=n&&a[cur[l]]==a[cur[r]];r++);
for(int i=l+1;i<r;i++)fa[cur[i]]=cur[l],w[cur[l]]+=w[cur[i]];
nex[las]=cur[l],pre[cur[l]]=las;
las=cur[l];
}
for(int i=0;i!=n+1;i=nex[i])pw[nex[i]]=pw[i]+w[i];
for(int i=n+1;i!=0;i=pre[i])sw[pre[i]]=sw[i]+w[i];
for(int i=1;i<=m;i++){
scanf("%d%d",&o[i].t,&o[i].i);
o[i].id=i;
}
sort(o+1,o+1+m,[&](ques x,ques y){
return x.t<y.t;
});
for(int i=0;;i=nex[i]){
getd(i);
if(i==n+1)break;
}
for(int i=0;i!=n+1;i=nex[i])push(nex[i],0);
for(int ot=1;ot<=m;ot++){
int t0=o[ot].t,x=o[ot].i;
for(;!q.empty()&&get<0>(q.top())>-t0;){
auto [t,y,w1,w2]=q.top();
t=-t;
q.pop();
int x=pre[y];
// cerr<<x<<' '<<y<<endl;
if(fa[x]!=x||fa[y]!=y)continue;
if(val[x]!=w1||val[y]!=w2)continue;
x=refind(x,t),y=refind(y,t);
int v=d[x]-d[y];
// cerr<<x<<' '<<y<<endl;
if(a[x]==a[y]){
remove(y);
getd(x);
update(x);
if(pre[x]>0)push(pre[x],t);
push(x,t);
push(nex[x],t);
if(nex[x]<=n)push(nex[nex[x]],t);
}else{
swp(y);
update(x);
update(y);
getd(x),getd(y);
// fprintf(stderr,"%d %d %d %d\n",y,x,d[y],d[x]);
if(d[y]==1&&d[x]==-1){
oth[y]=x,oth[x]=y;
d[x]=d[y]=0;
cerr<<"OK\n";
}
push(y,t);
push(x,t);
push(nex[x],t);
// cerr<<pre[y]<<' '<<y<<' '<<x<<' '<<nex[x]<<endl;
}
}
x=find(x);
refind(x,t0);
ans[o[ot].id]=a[x];
}
for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9932kb
input:
4 12 0 1 1 2 -1 3 2 2 0 1 0 2 0 3 0 4 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4
output:
0 1 -1 2 1 0 0 1 0 1 1 0
result:
ok 12 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 8016kb
input:
5 5 -4 5 -1 3 5 5 0 4 -4 2 0 3 3 1 5 5 1 3 3 3
output:
5 -1 1 4 2
result:
ok 5 lines
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 9916kb
input:
5 5 3 2 1 3 0 3 1 5 0 5 7 1 1 4 1 5 8 5 10 3
output:
0 0 1 1 1
result:
wrong answer 4th lines differ - expected: '0', found: '1'