QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#526795 | #4401. Prize | bachbeo2007 | 0 | 0ms | 0kb | C++23 | 2.3kb | 2024-08-21 20:27:48 | 2024-08-21 20:27:48 |
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int maxl = 25;
int n,m,q;
struct Tree{
int L[maxn],T,rt=-1;
vector<int> edge[maxn];
int dep[maxn],par[maxn][maxl];
void dfs(int u,int p){
par[u][0]=p,dep[u]=dep[p]+1;
for(int i=1;i<20;i++) par[u][i]=par[par[u][i-1]][i-1];
for(int v:edge[u]) dfs(v,u);
}
int lca(int u,int v){
if(dep[u]>dep[v]) swap(u,v);
for(int i=0;i<20;i++) if((dep[v]-dep[u])>>i&1) v=par[v][i];
if(u==v) return u;
for(int i=19;i>=0;i--) if(par[u][i]!=par[v][i]) u=par[u][i],v=par[v][i];
return par[u][0];
}
int f[maxn],d[maxn];
int findpar(int u){
if(u==f[u]) return u;
int x=f[u];f[u]=findpar(f[u]);
d[u]+=d[x];
return f[u];
}
void unions(int u,int v,int w){
int pu=findpar(u),pv=findpar(v);
if(pu==pv) return;
f[pv]=pu;d[pv]+=d[u]-d[v]+w;
}
void build(){
iota(f+1,f+n+1,1);
for(int i=1;i<=n;i++){
int p;cin >> p;
if(p==-1) rt=i;
else edge[p].push_back(i);
}
dfs(rt,0);
}
}T1,T2;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> n >> m >> q >> q;
T1.build();T2.build();
vector<int> ord(n);
iota(ord.begin(),ord.end(),1);
sort(ord.begin(),ord.end(),[&](int x,int y){
return T1.L[x]<T1.L[y];
});
while((int)ord.size()>m) ord.pop_back();
sort(ord.begin(),ord.end(),[&](int x,int y){
return T2.L[x]<T2.L[y];
});
for(int i=0;i<m;i++) cout << ord[i] << ' ';
cout << endl;
for(int i=1;i<m;i++){
int u=ord[i-1],v=ord[i];
cout << "? " << u << ' ' << v << endl;
int x,y,a=T1.lca(u,v);cin >> x >> y;
T1.unions(a,u,x);T1.unions(a,v,y);
a=T2.lca(u,v);cin >> x >> y;
T2.unions(a,u,x);T2.unions(a,v,y);
}
cout << "!" << endl;
for(int i=1;i<=n;i++) T1.findpar(i),T2.findpar(i);
for(int i=1;i<=q;i++){
int u,v;cin >> u >> v;
int a=T1.lca(u,v);
cout << T1.d[u]+T1.d[v]-2*T1.d[a] << ' ';
a=T2.lca(u,v);
cout << T2.d[u]+T2.d[v]-2*T2.d[a] << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
500000 64682 64681 100000 46115 470589 209303 2979 473162 343535 79503 299539 404621 102085 237721 279170 392890 165201 441593 456314 218991 358478 86614 410800 159785 169761 95368 285837 297549 370283 378974 26449 444381 39320 149913 404523 144109 174828 263837 49847 468694 478535 152644 216598 301...
output:
368651 368605 368604 368603 368602 368601 368600 368599 368598 368597 368596 368595 368594 368639 368653 368652 368606 368650 368649 368648 368647 368646 368645 368644 368643 368642 368641 368640 368593 368638 368637 368636 368622 368453 368452 368451 368450 368449 368448 368447 368446 368445 368444...
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #13:
score: 0
Time Limit Exceeded
input:
500000 88721 177440 100000 30974 23891 211201 125199 180489 387190 218020 498838 230147 307989 484136 257785 353027 304420 311738 169842 334090 486070 126212 328609 174959 368840 238722 418092 488389 226349 427271 457322 332454 12958 197530 264474 355717 482774 221286 282148 216441 266659 213750 628...
output:
353366 353295 353296 353297 353298 353299 353300 353301 353302 353303 353288 353294 353367 353368 353369 353370 353371 353372 353373 353374 353375 353376 353283 353319 353274 353275 353276 353277 353278 353279 353280 353281 353282 353377 353284 353285 353286 353287 353304 353289 353290 353291 353292...
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #25:
score: 0
Time Limit Exceeded
input:
500000 200 199 40000 76296 130139 291501 292412 139543 433345 372726 451574 18315 465578 324564 477223 237354 81532 65170 465332 342130 9670 193303 193680 129668 149532 268907 89969 398275 356210 324593 433492 482232 466692 135343 433758 102545 287283 432859 351864 305769 489532 101532 450535 295762...
output:
333453 333465 333464 333463 333462 333461 333460 333459 333458 333457 333456 333455 333454 333466 333468 333451 333450 333449 333448 333447 333446 333445 333444 333443 333442 333294 333306 333305 333304 333303 333302 333301 333300 333376 333298 333297 333296 333295 333441 333293 333292 333291 333290...
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #37:
score: 0
Time Limit Exceeded
input:
1000000 1000 999 100000 678746 439069 32542 85937 936926 284219 461661 203235 533462 940676 230275 621140 780674 254931 562355 229273 201341 493976 358955 963527 880412 91220 474599 160086 698841 591551 718276 844558 39859 765917 34722 401724 219774 443004 682244 545401 968419 968020 354030 411187 1...
output:
666123 666077 666032 666033 666034 666035 666036 666037 666038 666039 666040 666041 666042 666043 666044 666045 666030 666108 666109 666110 666111 666112 666113 666114 666115 666116 666117 666118 666119 666120 666121 666122 666076 666061 666046 666063 666064 666065 666066 666067 666068 666069 666070...
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%