QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#140064 | #4401. Prize | APROHACK | 0 | 584ms | 222668kb | C++14 | 3.0kb | 2023-08-15 01:58:15 | 2023-08-15 01:58:18 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define ff first
#define ss second
using namespace std;
int n, q, k, t;
int roots[2];
vector<int>child[500005][2];
vector<int>chosen;
bool isChosen[500005];
int lca[500005][20][2];
int distancia[500005][20][2];
int deep[5000005][2];
void bfs(int s, int tri = 0){
queue<pair<int, int > >cola; // node, deep
cola.push({s, 0});
while(!cola.empty() and chosen.size() < k){
auto cur = cola.front();
cola.pop();
chosen.pb(cur.ff);
deep[cur.ff][tri] = cur.ss;
isChosen[cur.ff] = true;
for(int i = 0 ; i < child[cur.ff][tri].size() ; i ++){
lca[child[cur.ff][tri][i]][0][tri] = cur.ff;
cola.push({child[cur.ff][tri][i], cur.ss + 1});
}
}
}
int getDist(int a, int b, int tri){
if(deep[a][tri] < deep[b][tri])swap(a, b);
int dd = deep[a][tri] - deep[b][tri];
int ans = 0;
for(int i = 19 ; i >= 0 ; i --){
if(dd & (1<<i)){
ans += distancia[a][i][tri];
a = lca[a][i][tri];
}
}
if(a == b)return ans;
for(int i = 19 ; i >= 0 ; i --){
if(lca[a][i][tri] != lca[b][i][tri]){
ans += distancia[a][i][tri];
ans += distancia[b][i][tri];
a = lca[a][i][tri];
b = lca[a][i][tri];
}
}
ans += distancia[a][0][tri] + distancia[b][0][tri];
return ans;
}
int main(){
ios::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
/*
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
* */
cin >> n >> k >> q >> t;
memset(isChosen, false, sizeof isChosen);
int temp;
for(int i = 1 ; i <= n ; i ++){
cin >> temp;
if(temp == -1)roots[0] = i;
else child[temp][0].pb(i);
}
for(int i = 1 ; i <= n ; i ++){
cin >> temp;
if(temp == -1)roots[1] = i;
else child[temp][1].pb(i);
}
bfs(roots[0]);
for(auto i : chosen)cout << i << " ";
cout << endl;
vector<int>questions;
for(int i = 0 ; i < k ; i ++){
for(int j = 0 ; j < child[chosen[i]][0].size() ; j ++){
if(isChosen[child[chosen[i]][0][j]]){
questions.pb(child[chosen[i]][0][j]);
}
}
}
for(int i = 0 ; i < questions.size() ; i ++){
cout << "? " << questions[i] << " " << lca[questions[i]][0][0] << endl;
}
cout << "!" << endl;
for(int i = 0 ; i < questions.size() ; i ++){
int a, b;
cin >> a >> b >> a >> b;
distancia[questions[i]][0][0] = a;
}
lca[roots[0]][0][0] = roots[0];
distancia[roots[0]][0][0] = 0;
for(int x = 1 ; x < 20 ; x ++){
for(int i = 1 ; i <= n ; i++){
lca[i][x][0] = lca[lca[i][x-1][0]][x-1][0];
distancia[i][x][0] = distancia[lca[i][x-1][0]][x-1][0] + distancia[i][x-1][0];
//lca[i][x][1] = lca[lca[i][x-1][1]][x-1][1];
}
}
vector<pair<int, int> > queries;
for(int i = 0 ; i < t ; i ++){
int a, b;
cin >> a >> b;
queries.pb({a, b});
}
for(int i = 0 ; i < t ; i ++){
cout << getDist(queries[i].ff, queries[i].ss, 0) << " " << getDist(queries[i].ff, queries[i].ss, 0) << "\n";
}
cout << endl;
int ready;
//~ cin >> ready;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 10
Accepted
time: 536ms
memory: 222464kb
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:
ok good job!
Test #2:
score: 0
Accepted
time: 584ms
memory: 222668kb
input:
500000 90967 90966 100000 122547 312039 290084 118442 352297 175176 294396 496975 127062 90539 132654 408480 493670 419897 53432 141795 264165 60368 473480 5634 253119 64236 85346 422987 28583 262389 111931 271291 13577 415079 132797 256502 76402 265607 11274 289667 398726 32021 302401 410650 369760...
output:
3090 193269 3028 186608 498475 64618 82114 231445 7541 329983 134623 235591 70401 18906 403427 280451 146897 355174 160090 144279 193430 332022 488244 228900 80781 84465 218682 27818 6035 368489 155673 440755 443926 241570 193717 143661 374105 56616 323329 95909 337798 20531 236329 28564 437244 4969...
result:
ok good job!
Test #3:
score: -10
Wrong Answer
time: 496ms
memory: 207916kb
input:
500000 68287 68286 100000 273928 229768 65518 144983 311611 494773 489379 439644 467893 456131 430188 247387 485565 272285 474827 476962 338340 365804 344570 390867 390170 456217 43185 447057 385874 305750 107742 230530 259907 252254 280920 16831 45761 185191 117450 55891 175190 255615 35904 14855 2...
output:
242387 146602 170589 196742 270393 272971 277513 290951 335954 339454 343790 360585 408863 410420 416115 435584 462604 475865 106115 141534 208403 209977 237211 239423 262337 296002 322708 452430 483541 74367 118980 156994 257460 419650 336953 360548 369080 222228 264307 433002 210925 330758 346781 ...
result:
wrong answer wrong answer on the first integer of query #1: read 12233 but expected 15669
Subtask #2:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 537ms
memory: 222628kb
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:
wrong answer wrong answer on the first integer of query #1: read 117051623 but expected 39120665
Subtask #3:
score: 0
Wrong Answer
Test #25:
score: 0
Wrong Answer
time: 341ms
memory: 220700kb
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:
wrong answer wrong answer on the first integer of query #1: read 512146033 but expected 53295
Subtask #4:
score: 0
Runtime Error
Test #37:
score: 0
Runtime Error
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:
Subtask #5:
score: 0
Skipped
Dependency #4:
0%