QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402724 | #4401. Prize | sichengzhou# | 0 | 1ms | 3708kb | C++14 | 3.1kb | 2024-05-01 11:36:24 | 2024-05-01 11:36:25 |
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+6;
int n,K,Q,T,dfn[N];
bool cmp(const int x,const int y)
{
return dfn[x]<dfn[y];
}
struct Tree{
int number;
int p[N],rt;
int fa[N][20];
struct Edge{
int v,nxt;
}e[N];
int h[N],tot1;
void addEdge(int u,int v)
{
tot1++;
e[tot1].v=v;e[tot1].nxt=h[u];
h[u]=tot1;
}
void input()
{
for(int i=1;i<=n;i++)
{
scanf("%d",&p[i]);
if(p[i]>=1)addEdge(p[i],i),fa[i][0]=p[i];
else rt=i,fa[i][0]=0;
}
}
int dfn[N],idx,dep[N],rnk[N];
void dfs(int u)
{
dfn[u]=++idx;rnk[idx]=u;
for(int i=h[u];i;i=e[i].nxt)
{
int v=e[i].v;dep[v]=dep[u]+1;
dfs(v);
}
}
int a[N];
LL d[N];
int lca(int u,int v)
{
if(dep[u]<dep[v])
{
swap(u,v);
}
for(int j=19;j>=0;j--)
{
if(dep[u]-dep[v]>>j&1)
{
u=fa[u][j];
}
}
if(u==v)
{
return u;
}
for(int j=19;j>=0;j--)
{
if(fa[u][j]!=fa[v][j])
{
u=fa[u][j];
v=fa[v][j];
}
}
return fa[u][0];
}
void init()
{
dfs(rt);
for(int j=1;j<=19;j++)
{
for(int i=1;i<=n;i++)
{
fa[i][j]=fa[fa[i][j-1]][j-1];
}
}
for(int i=1;i<=K;i++)
{
a[i]=i;
}
int tp=0;
for(int i=1;i<=n;i++)
{
if(rnk[i]<=K)
{
a[++tp]=rnk[i];
}
}
for(int i=1;i<K;i++)
{
cout<<"? "<<a[i]<<' '<<a[i+1]<<endl;
}
}
void work()
{
d[a[1]]=0;
for(int i=1;i<K;i++)
{
LL X,Y,Z,W;
scanf("%lld%lld%lld%lld",&X,&Y,&Z,&W);
int x=lca(a[i],a[i+1]);
if(number==1)
{
d[x]=d[a[i]]-X;
d[a[i+1]]=d[x]+Y;
}else{
d[x]=d[a[i]]-Z;
d[a[i+1]]=d[x]+W;
}
// cout<<a[i]<<' '<<x<<' '<<a[i+1]<<endl;
// cout<<d[a[i]]<<' '<<d[x]<<' '<<d[a[i+1]]<<endl;
}
/* for(int i=1;i<=n;i++)
{
cout<<d[i]<<' ';
}
cout<<endl;*/
}
LL query(int u,int v)
{
return d[u]+d[v]-2*d[lca(u,v)];
}
}t1,t2;
int main()
{
int u,v;
scanf("%d%d%d%d",&n,&K,&Q,&T);
if(Q!=K-1||n>5e5)return 0;
t1.number=1;t2.number=2;
t1.input();t2.input();
for(int i=1;i<=K;i++)
{
cout<<i<<' ';
}
cout<<endl;
t1.init();
cout<<"!\n";
fflush(stdout);
t1.work();
while(T--)
{
scanf("%d%d",&u,&v);
printf("%lld %lld\n",t1.query(u,v),t1.query(u,v));
}
fflush(stdout);
return 0;
}
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ...
result:
Subtask #2:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 1ms
memory: 3708kb
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:
result:
wrong answer format Unexpected end of file - int32 expected
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ...
result:
Subtask #4:
score: 0
Wrong Answer
Test #37:
score: 0
Wrong Answer
time: 1ms
memory: 3560kb
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:
result:
wrong answer format Unexpected end of file - int32 expected
Subtask #5:
score: 0
Skipped
Dependency #4:
0%