QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#42467 | #4401. Prize | catthomas | 0 | 2494ms | 264920kb | C++ | 3.7kb | 2022-08-02 15:15:21 | 2022-08-02 15:15:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
inline void Flu(){fflush(stdout);}
const int maxm=100025;
const int maxn=1000025;
int n,K,Q,T,m,i,j,t,k,s,Log[maxn];
struct Edge
{
int nxt,aim;
};
bool lit[maxn];
int seq[maxm],tsq,stk[maxm],tp,tof[maxm],quer[maxm][4],ord[maxm];
struct Tree
{
int N,head[maxn],nrt,fat[maxn][21],dfn[maxn],bac[maxn];
int dep[maxn],tod[maxn],dep2[maxn];
Edge edge[maxn];
inline void add_edge(int x,int y)
{
if (x==-1){nrt=y;return;}
fat[y][0]=x;
edge[++N]=(Edge){head[x],y};head[x]=N;
//edge[++N]=(Edge){head[y],x};head[y]=N;
}
void dfs_gt_seq(int x)
{
lit[x]=1;ord[++tsq]=x;if (tsq>=K) return;
for (int i=head[x];i;i=edge[i].nxt)
{
int des=edge[i].aim;
dfs_gt_seq(des);if (tsq>=K) return;
}
}
void dfs1(int x)
{
dfn[x]=++t;bac[t]=x;dep[x]=dep[fat[x][0]]+1;
for (int i=1;i<=Log[n];++i) fat[x][i]=fat[fat[x][i-1]][i-1];
for (int i=head[x];i;i=edge[i].nxt)
{
int des=edge[i].aim;
dfs1(des);
}
}
int LCA(int x,int y)
{
if (dep[x]<dep[y]) swap(x,y);
for (int i=Log[n];~i;--i) if (dep[fat[x][i]]>=dep[y]) x=fat[x][i];
if (x==y) return x;
for (int i=Log[n];~i;--i) if (fat[x][i]^fat[y][i])
x=fat[x][i],y=fat[y][i];
return fat[x][0];
}
void dfs2(int x)
{
dep2[x]=dep2[fat[x][0]]+tod[x];
for (int i=head[x];i;i=edge[i].nxt)
{
int des=edge[i].aim;
dfs2(des);
}
}
void Process1()
{
tsq=0;
for (int i=1;i<=n;++i)
if (lit[bac[i]]) seq[++tsq]=bac[i];
for (int i=1;i<=tsq;++i) printf("%d ",seq[i]);puts("");
for (int i=1;i<tsq;++i) printf("? %d %d\n",seq[i],seq[i+1]);
puts("!");Flu();
for (int i=1;i<tsq;++i)
scanf("%d%d%d%d",&quer[i][0],&quer[i][1],
&quer[i][2],&quer[i][3]);
stk[tp=1]=seq[1];
for (int i=2;i<=tsq;++i)
{
int lc=LCA(stk[tp],seq[i]),lst=-1,len=quer[i-1][0];
while (tp&&dep[stk[tp]]>dep[lc])
{
if (lst!=-1) tod[lst]=tof[tp+1],len-=tof[tp+1];
lst=stk[tp];--tp;
}
if (stk[tp]^lc) stk[++tp]=lc;
if (lst!=-1) tod[lst]=len;
tof[tp]-=len;
stk[++tp]=seq[i];tof[tp]=quer[i-1][1];
}
while (tp>1) tod[stk[tp]]=tof[tp],--tp;
dfs2(1);
}
int pre[maxn],nxt[maxn],qv[maxn][2];
void Process2()
{
for (int i=1;i<tsq;++i)
{
int t0=seq[i],t1=seq[i+1];
nxt[t0]=t1;pre[t1]=t0;
qv[t0][0]=quer[i][2];qv[t0][1]=quer[i][3];
}
int nowtt=tsq;
while (nowtt>1)
{
int now=ord[nowtt];--nowtt;
int t0=pre[now],t1=nxt[now];
if (!t1)
{
int lc0=LCA(t0,now);
nxt[t0]=0;
tod[now]=lc0;dep2[now]=qv[t0][1];
continue;
}
else if (!t0)
{
int lc1=LCA(t1,now);
pre[t1]=0;
tod[now]=lc1;dep2[now]=qv[now][0];
continue;
}
int lc0=LCA(t0,now),lc1=LCA(t1,now);
tod[now]=lc0;dep2[now]=qv[t0][1];
qv[t0][1]=0ll+qv[t0][1]+qv[now][0]+qv[now][1]
-2ll*min(qv[t0][1],qv[now][0]);
pre[t1]=t0;nxt[t0]=t1;
}
for (int i=2;i<=tsq;++i)
dep2[ord[i]]+=dep2[tod[ord[i]]];
}
int gtds(int x,int y){return 0ll+dep2[x]-2ll*dep2[LCA(x,y)]+dep2[y];}
}tr1,tr2;
int ask[maxm][2];
int main()
{
for (i=2;i<maxn;++i)
Log[i]=Log[i>>1]+1;
scanf("%d%d%d%d",&n,&K,&Q,&T);
for (i=1;i<=n;++i)
{
scanf("%d",&t);
tr1.add_edge(t,i);
}
for (i=1;i<=n;++i)
{
scanf("%d",&t);
tr2.add_edge(t,i);
}
tr2.dfs_gt_seq(tr2.nrt);
t=0;tr1.dfs1(tr1.nrt);t=0;tr2.dfs1(tr2.nrt);
tr1.Process1();
tr2.Process2();
Flu();
for (i=1;i<=T;++i)
scanf("%d%d",&ask[i][0],&ask[i][1]),Flu();
for (i=1;i<=T;++i)
{
printf("%d %d\n",tr1.gtds(ask[i][0],ask[i][1]),tr2.gtds(ask[i][0],ask[i][1]));
}
Flu();
return 0;
}
/*
9 3 2 3
2 -1 2 1 1 5 1 4 5
9 4 5 5 7 3 -1 3 7
0 3 5 0
3 4 0 1
1 7
7 9
1 9
*/
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1065ms
memory: 137288kb
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:
422989 414496 290928 388223 160563 301045 470257 259625 222733 231286 345214 169817 435263 277447 386014 210139 455433 225855 264772 199736 355788 288506 233893 146148 454958 267562 498596 183745 352665 151125 266374 43142 9414 204593 212097 311775 25324 300764 6643 94847 396968 428563 311355 255767...
result:
wrong answer wrong answer on the first integer of query #1: read 0 but expected 23499297
Subtask #2:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 1181ms
memory: 138164kb
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:
63742 263216 146169 50728 199716 469441 459156 322328 152164 66876 274063 180006 237497 208598 249207 359435 96669 110070 41714 147909 214779 59127 151892 216797 194356 199621 20899 418742 198323 158340 163745 123748 85656 172672 123919 47108 313725 12227 183377 183933 348552 102798 184923 290145 17...
result:
wrong answer wrong answer on the first integer of query #1: read 51591013 but expected 218890432
Subtask #3:
score: 0
Wrong Answer
Test #25:
score: 0
Wrong Answer
time: 819ms
memory: 124976kb
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:
464387 27779 146694 443858 405500 46371 375328 183696 253669 95388 173896 183797 18073 431275 140576 468877 345574 227090 361228 17134 261985 60381 64649 124883 275006 345205 205047 166559 173438 437370 498046 158980 365732 106698 145138 342120 407307 83109 296453 316074 219468 97176 251586 177490 2...
result:
wrong answer wrong answer on the second integer of query #1: read -58187 but expected 47544
Subtask #4:
score: 0
Wrong Answer
Test #37:
score: 0
Wrong Answer
time: 2125ms
memory: 250348kb
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:
927453 237540 859419 982835 971518 506285 771618 939329 16802 700671 845162 359776 499849 958003 722555 893539 667107 399090 361260 56054 518738 929831 330952 261064 845434 378738 416383 813166 332967 155083 279300 603715 217430 73563 278581 71462 840056 191244 422478 38987 402361 21178 733103 92045...
result:
wrong answer wrong answer on the second integer of query #1: read 7356480 but expected 628652
Subtask #5:
score: 0
Wrong Answer
Test #49:
score: 0
Wrong Answer
time: 2494ms
memory: 264920kb
input:
1000000 91074 91073 100000 844855 360256 604500 520288 3402 603913 199722 732526 574997 429775 182518 190073 386932 693624 254661 333433 557929 350362 247817 201441 960948 519977 461212 493412 852908 455639 732827 432452 320916 223796 413293 969300 617038 438432 2369 51283 908991 374139 410798 19612...
output:
100266 524911 805244 861271 648132 338218 588017 846372 361674 257564 857806 809152 146144 655284 305021 895380 787314 337733 840423 783219 849918 564122 924389 456471 56411 922287 626528 573150 955857 663398 713622 677964 973127 106150 529384 890628 839820 734754 971452 312629 105748 707190 993032 ...
result:
wrong answer wrong answer on the first integer of query #1: read 664905742 but expected 870729592