QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#709205 | #9524. 1D Galaxy | qzez# | WA | 1ms | 10112kb | C++14 | 3.0kb | 2024-11-04 12:52:56 | 2024-11-04 12:52:57 |
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];
swap(pre[x],pre[y]);
swap(nex[x],nex[y]);
nex[pre[y]]=y,pre[nex[x]]=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;
}
}
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[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<<endl;
if(d[y]-d[x]>=0)return;
x=refind(x,t0),y=refind(y,t0);
int v=d[x]-d[y];
ll t=t0+(a[y]-a[x]+v-1)/v;
// cerr<<"push "<<x<<' '<<y<<' '<<t<<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();
q.pop();
int x=pre[y];
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);
update(y);
push(pre[x],t);
push(x,t);
}else{
swp(y);
getd(x),getd(y);
update(x);
update(y);
if(d[y]==1&&d[x]==-1){
oth[y]=x,oth[x]=y;
d[x]=d[y]=0;
}
push(y,t);
push(x,t);
push(nex[x],t);
}
}
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 8076kb
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: 0ms
memory: 10112kb
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: 7948kb
input:
5 5 3 2 1 3 0 3 1 5 0 5 7 1 1 4 1 5 8 5 10 3
output:
-4 0 1 1 1
result:
wrong answer 1st lines differ - expected: '0', found: '-4'