QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#74114 | #4401. Prize | SenseAnone | 0 | 0ms | 0kb | C++14 | 5.2kb | 2023-01-30 19:48:57 | 2023-01-30 19:49:01 |
Judging History
answer
#include <bits/stdc++.h>
#define PII pair<int,int>
#define mp(a,b) make_pair(a,b)
using namespace std;
template<typename T>void read(T &x)
{
T f=1;x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
x*=f;
}
template<typename T>void print(T x) {
if(x<0) putchar('-'),x=-x;
if(x>9) print(x/10);
putchar(x%10+'0');
}
vector <int> S;
int lg[1000005];
bool cho[1000005];//Is a node chosen
int rt1,rt2,n,k,q,t;
int dfn[1000005],Times;
int dep1[1000005],dep2[1000005];
map<int,int>M1[1000005],M2[1000005];
int f1[1000005][21],f2[1000005][21];
vector <int> G1[1000005],G2[1000005];
vector <PII> V1[1000005],V2[1000005];
vector <int> Inc1[1000005],Inc2[1000005];
int Ans1[1000005],Ans2[1000005],Ans3[1000005],Ans4[1000005];
int cnt,Asku[1000005],Askv[1000005],AskA1[1000005],AskA2[1000005];//u,v and ancestor on each tree
void dfs1(int u,int fa)
{
f1[u][0]=fa; dep1[u]=dep1[fa]+1;
for(int i=1;i<=lg[dep1[u]];i++) f1[u][i]=f1[f1[u][i-1]][i-1];
if((int)S.size()<k) S.push_back(u),cho[u]=true;
for(int i=0;i<(int)G1[u].size();i++)
{
int v=G1[u][i];
if(v==fa) continue;
dfs1(v,u);
}
}
int LCA1(int x,int y)
{
if(dep1[x]<dep1[y]) swap(x,y);
for(int i=lg[dep1[x]-dep1[y]];~i;i--) if(dep1[f1[x][i]]>=dep1[y]) x=f1[x][i];
if(x==y) return x;
for(int i=lg[dep1[x]];~i;i--) if(f1[x][i]!=f1[y][i]) x=f1[x][i],y=f1[y][i];
return f1[x][0];
}
int lst=-1;
void dfs2(int u,int fa)
{
dfn[u]=++Times;
f2[u][0]=fa; dep2[u]=dep2[fa]+1;
if(cho[u])
{
if(~lst)
{
putchar('?');putchar(' ');
print(u);putchar(' ');
print(lst);putchar('\n');
Asku[++cnt]=u; Askv[cnt]=lst; AskA1[cnt]=LCA1(u,lst);
Inc1[u].push_back(cnt);
if(lst!=u) Inc1[lst].push_back(cnt);
if(AskA1[cnt]!=u&&AskA1[cnt]!=lst) Inc1[AskA1[cnt]].push_back(cnt);
}
lst=u;
}
for(int i=1;i<=lg[dep2[u]];i++) f2[u][i]=f2[f2[u][i-1]][i-1];
for(int i=0;i<(int)G2[u].size();i++)
{
int v=G2[u][i];
if(v==fa) continue;
dfs2(v,u);
}
}
int LCA2(int x,int y)
{
if(dep2[x]<dep2[y]) swap(x,y);
for(int i=lg[dep2[x]-dep2[y]];~i;i--) if(dep2[f2[x][i]]>=dep2[y]) x=f2[x][i];
if(x==y) return x;
for(int i=lg[dep2[x]];~i;i--) if(f2[x][i]!=f2[y][i]) x=f2[x][i],y=f2[y][i];
return f2[x][0];
}
int dis3[1000005],dis4[1000005];
bool vis3[1000005],vis4[10000005];
int cmp(int x,int y){return dfn[x]<dfn[y];}
int main() {
// freopen("InterOut","r",stdin);
// freopen("MyOut","w",stdout);
read(n);read(k);read(q);read(t);
for(int i=1;i<=n;i++) lg[i]=lg[i>>1]+1;
for(int i=1,fa;i<=n;i++)
{
read(fa);
if(fa==-1) rt1=i;
else G1[fa].push_back(i);
}
for(int i=1,fa;i<=n;i++)
{
read(fa);
if(fa==-1) rt2=i;
else G2[fa].push_back(i);
}
// fclose(stdin);
dfs1(rt1,0);
for(int i=0;i<(int)S.size();i++) print(S[i]),putchar(' ');puts("");
dfs2(rt2,0);//建倍增表,顺便选点和得到询问
assert(cnt<=q);
for(int i=1;i<=cnt;i++)
{
AskA2[i]=LCA2(Asku[i],Askv[i]);
Inc2[AskA2[i]].push_back(i);
if(Asku[i]!=AskA2[i]) Inc2[Asku[i]].push_back(i);
if(Askv[i]!=AskA2[i]) Inc2[Askv[i]].push_back(i);
}
puts("!");
fflush(stdout);
// fclose(stdout);
int rt4=LCA2(S[0],S[1]);
for(int i=2;i<(int)S.size();i++) rt4=LCA2(rt4,S[i]);
for(int i=1;i<=cnt;i++) read(Ans1[i]),read(Ans2[i]),read(Ans3[i]),read(Ans4[i]);
queue<int>Q;
Q.push(rt4);
while(!Q.empty())
{
int u=Q.front();Q.pop();
if(vis4[u]) continue; vis4[u]=true;
for(int i=0;i<(int)Inc2[u].size();i++)
{
int id=Inc2[u][i];
if(AskA2[id]==u)
{
dis4[Asku[id]]=dis4[u]+Ans3[id];
dis4[Askv[id]]=dis4[u]+Ans4[id];
if(!vis4[Asku[id]]) Q.push(Asku[id]);
if(!vis4[Askv[id]]) Q.push(Askv[id]);
}
else
{
if(Asku[id]==u) dis4[AskA2[id]]=dis4[u]-Ans3[id];
if(Askv[id]==u) dis4[AskA2[id]]=dis4[u]-Ans4[id];
if(!vis4[AskA2[id]]) Q.push(AskA2[id]);
}
}
}
Q.push(rt1);
while(!Q.empty())
{
int u=Q.front();Q.pop();
if(vis3[u]) continue; vis3[u]=true;
for(int i=0;i<(int)Inc1[u].size();i++)
{
int id=Inc1[u][i];
if(AskA1[id]==u)
{
dis3[Asku[id]]=dis3[u]+Ans1[id];
dis3[Askv[id]]=dis3[u]+Ans2[id];
if(!vis3[Asku[id]]) Q.push(Asku[id]);
if(!vis3[Askv[id]]) Q.push(Askv[id]);
}
else
{
if(Asku[id]==u) dis3[AskA1[id]]=dis3[u]-Ans1[id];
if(Askv[id]==u) dis3[AskA1[id]]=dis3[u]-Ans2[id];
if(!vis3[AskA1[id]]) Q.push(AskA1[id]);
}
}
}
// freopen("MyOut","a",stdout);
for(int i=1,a,b;i<=t;i++)
{
read(a);read(b);
int A1=LCA1(a,b),A2=LCA2(a,b);
print(dis3[a]+dis3[b]-dis3[A1]*2);putchar(' ');
print(dis4[a]+dis4[b]-dis4[A2]*2);putchar('\n');
}
fflush(stdout);
return 0;
}
/*
先把思路写在这里,感觉是大工程
在第一棵树上dfs取最先得到的k的点
然后在第二课树上建出这k个点的虚树直接挨着询问每条边
这k-1个询问中必定有询问在第一棵树上有LCA为根,因为至少我选了根
那么从根开始可以得到一些点的深度
然后继续把涉及这些点的询问全部拎出来就可以得到每个点的深度(大概)
肯定写复杂了!
9 3 2 3
2 -1 2 1 1 5 1 4 5
9 4 5 5 7 3 -1 3 7
0 10 1 0
3 0 5 13
*/
詳細信息
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:
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:
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:
63742 11431 300071 157785 268420 71772 84553 267656 174540 21500 451751 82419 58833 165916 94199 78203 263216 146169 306934 50728 338250 199716 469441 135516 133967 123248 375309 17045 459156 413018 49645 73720 188292 322328 493921 152164 219927 140202 236207 266137 180568 32077 371348 66876 354136 ...
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:
20242 414878 185020 125537 353357 496468 308518 188057 254952 120898 414314 11748 435424 326112 345902 271794 473882 337923 135188 438050 45188 88306 260313 116954 457474 435919 366460 431766 397351 392326 178950 199724 227083 282259 70917 121346 109196 193669 242154 12225 466790 155481 287973 15749...
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:
545967 706162 53597 107558 776536 230611 572458 293457 390487 241653 638541 42868 433774 438059 293014 739962 25440 503383 628342 573629 887812 909797 805385 862282 382785 706534 190319 439139 648412 626240 131005 848982 269684 840650 376086 933701 18720 749474 336321 160119 795534 671698 201133 610...
result:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%