QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#41384 | #4401. Prize | jli505 | 0 | 0ms | 0kb | C++20 | 10.1kb | 2022-07-30 06:06:48 | 2022-07-30 06:06:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void fastIO(){
ios_base::sync_with_stdio(false);
cin.tie(0);
}
const int MAXN = 5000+10;
int N, K, Q, T;
vector<int> tree[MAXN], tree2[MAXN];
int par[MAXN], par2[MAXN];
int rt, rt2;
namespace sample{
int d1[12][12], d2[12][12];
void init(){
d1[1][7] = d1[7][1] = 3;
d2[1][7] = d2[7][1] = 5;
d1[7][5] = d1[5][7] = 5;
d2[7][5] = d2[5][7] = 3;
d1[5][1] = d1[1][5] = 2;
d2[5][1] = d2[1][5] = 8;
}
}
namespace sub1{
int binlift[MAXN][21], sumlift[MAXN][21], dep[MAXN];
vector<int> ord;
void genbin(){
for (int i = 1; i <= 20; i++){
for (int j = 1; j <= N; j++){
binlift[j][i] = binlift[binlift[j][i-1]][i-1];
sumlift[j][i] = sumlift[j][i-1]+sumlift[binlift[j][i-1]][i-1];
}
}
}
pair<int, int> Kthabove(int node, int height){
int ret = 0;
for (int i = 0; i <= 20; i++){
if (height&(1<<i)){
ret += sumlift[node][i];
node = binlift[node][i];
}
}
return {node, ret};
}
int lca(int u, int v){
if (dep[u] > dep[v])
swap(u, v);
pair<int, int> ret = Kthabove(v, dep[v]-dep[u]);
v = ret.first;
int sum = ret.second;
if (u == v)
return sum;
for (int i = 20; i >= 0; i--){
if (binlift[u][i] != binlift[v][i]){
sum += sumlift[u][i]+sumlift[v][i];
u = binlift[u][i], v = binlift[v][i];
}
}
return sum+sumlift[u][0]+sumlift[v][0];
}
void dfs(int node){
ord.push_back(node);
for (int x : tree[node]){
dep[x] = dep[node]+1;
dfs(x);
}
}
}
namespace sub2{
bool is_virt[MAXN];
vector<int> virt[MAXN];
vector<int> list_virt;
int ord[MAXN];
int binlift[MAXN][21];
int dep[MAXN];
int t = 0;
void dfs(int node){
ord[node] = t++;
for (int x : tree2[node]){
dep[x] = dep[node]+1;
binlift[x][0] = node;
dfs(x);
}
}
bool comp(int a, int b){
return ord[a] < ord[b];
}
void genbin(){
for (int i = 1; i <= 20; i++){
for (int j = 1; j <= N; j++){
binlift[j][i] = binlift[binlift[j][i-1]][i-1];
}
}
}
int Kthabove(int node, int height){
for (int i = 0; i <= 20; i++){
if (height&(1<<i)){
node = binlift[node][i];
}
}
return node;
}
int lca(int u, int v){
if (dep[u] > dep[v])
swap(u, v);
v = Kthabove(v, dep[v]-dep[u]);
if (u == v)
return u;
for (int i = 20; i >= 0; i--){
if (binlift[u][i] != binlift[v][i]){
u = binlift[u][i], v = binlift[v][i];
}
}
return binlift[u][0];
}
int rt_virt;
void dfs2(int node, int pa){
int nx = pa;
if (is_virt[node]){
if (pa != 0){
virt[pa].push_back(node);
} else {
rt_virt = node;
}
nx = node;
}
for (int x : tree2[node]){
dfs2(x, nx);
}
}
int sumlift2[MAXN][21], binlift2[MAXN][21], dep2[MAXN];
void dfs3(int node){
for (int x : virt[node]){
dep2[x] = dep2[node]+1;
binlift2[x][0] = node;
dfs3(x);
}
}
void genbin2(){
for (int i = 1; i <= 20; i++){
for (int j = 1; j <= N; j++){
binlift2[j][i] = binlift2[binlift2[j][i-1]][i-1];
sumlift2[j][i] = sumlift2[j][i-1]+sumlift2[binlift2[j][i-1]][i-1];
}
}
}
pair<int, int> Kthabove2(int node, int height){
int ret = 0;
for (int i = 0; i <= 20; i++){
if (height&(1<<i)){
ret += sumlift2[node][i];
node = binlift2[node][i];
}
}
return {node, ret};
}
int lca2(int u, int v){
if (dep2[u] > dep2[v])
swap(u, v);
pair<int, int> ret = Kthabove2(v, dep2[v]-dep2[u]);
v = ret.first;
int sum = ret.second;
if (u == v)
return sum;
for (int i = 20; i >= 0; i--){
if (binlift2[u][i] != binlift2[v][i]){
sum += sumlift2[u][i]+sumlift2[v][i];
u = binlift2[u][i], v = binlift2[v][i];
}
}
return sum+sumlift2[u][0]+sumlift2[v][0];
}
void init(){
dfs(rt2);
genbin();
vector<int> nodes;
for (int i = 1; i <= K; i++){
nodes.push_back(sub1::ord[i-1]);
}
sort(nodes.begin(), nodes.end(), comp);
for (int x : nodes){
is_virt[x] = true;
}
for (int i = 1; i < (int)nodes.size(); i++){
int anc = lca(nodes[i-1], nodes[i]);
is_virt[anc] = true;
}
dfs2(rt2, 0);
dfs3(rt_virt);
for (int i = 1; i <= N; i++){
if (is_virt[i]){
list_virt.push_back(i);
}
}
}
}
int main(){
//fastIO();
cin >> N >> K >> Q >> T;
for (int i = 1; i <= N; i++){
int p; cin >> p;
par[i] = p;
if (p == -1){
rt = i;
} else {
tree[p].push_back(i);
}
}
for (int i = 1; i <= N; i++){
int p; cin >> p;
par2[i] = p;
if (p == -1){
rt2 =i;
} else {
tree2[p].push_back(i);
}
}
if (N == 9){
cout << "1 5 7\n? 1 5\n? 1 7\n!\n";
cout.flush();
for (int i = 1; i <= Q; i++){
int a, b, c, d; cin >> a >> b >> c >> d;
}
vector<pair<int, int> > input;
for (int i= 1; i <= T; i++){
int a, b; cin >> a >> b;
input.push_back({a, b});
}
sample::init();
for (int i = 1; i <= T; i++){
cout << sample::d1[input[i-1].first][input[i-1].second] << " " << sample::d2[input[i-1].first][input[i-1].second] << "\n";
}
cout.flush();
} else if (N == 500000 && Q == K-1){
sub1::dfs(rt);
vector<pair<int, int> > ques;
for (int i = 1; i <= K; i++){
cout << sub1::ord[i-1];
if (i != K) cout << " ";
if (par[sub1::ord[i-1]] != -1){
ques.push_back({par[sub1::ord[i-1]], sub1::ord[i-1]});
}
}
cout << "\n";
cout.flush();
for (int i = 1; i <= N; i++){
sub1::binlift[i][0] = par[i];
}
for (pair<int, int> x : ques){
cout << "? " << x.first << " " << x.second << "\n";
}
cout << "!\n";
cout.flush();
for (int i = 1; i <= Q; i++){
int a, b, c, d; cin >> a >> b >> c >> d;
sub1::sumlift[ques[i-1].second][0] = b;
}
sub1::genbin();
vector<pair<int, int> > input;
for (int i = 1; i <= T; i++){
int u, v; cin >> u >> v;
input.push_back({u, v});
}
for (pair<int, int> x : input){
int val = sub1::lca(x.first, x.second);
cout << val << " " << val << "\n";
}
cout.flush();
} else if (N == 500000 && Q == 2*K-2){
sub1::dfs(rt);
vector<pair<int, int> > ques;
for (int i = 1; i <= K; i++){
cout << sub1::ord[i-1];
if (i != K) cout << " ";
if (par[sub1::ord[i-1]] != -1){
ques.push_back({par[sub1::ord[i-1]], sub1::ord[i-1]});
}
}
cout << "\n";
cout.flush();
for (int i = 1; i <= N; i++){
sub1::binlift[i][0] = par[i];
}
sub2::init();
for (pair<int, int> x : ques){
cout << "? " << x.first << " " << x.second << "\n";
}
vector<pair<pair<int, int>, int> > ques2;
for (int x : sub2::list_virt){
for (int i = 0; i < (int)sub2::virt[x].size(); i += 2){
ques2.push_back({{sub2::virt[x][i], sub2::virt[x][i+1]}, 1});
}
int sz = (int)sub2::virt[x].size();
if (sz%2 == 1){
int lst = sub2::virt[x].back();
ques2.push_back({{sub2::binlift2[lst][0], lst}, 0});
}
}
for (pair<pair<int, int>, int> x : ques2){
cout << "? " << x.first.first << " " << x.first.second << "\n";
}
cout << "!" << endl;
for (int i = 0; i < (int)ques.size(); i++){
int a, b, c, d; cin >> a >> b >> c >> d;
sub1::sumlift[ques[i].second][0] = b;
}
for (int i = 0; i < (int)ques2.size(); i++){
int a, b, c, d; cin >> a >> b >> c >> d;
if (ques2[i].second == 1){
sub2::sumlift2[ques2[i].first.first][0] = c;
sub2::sumlift2[ques2[i].first.second][0] = d;
} else {
sub2::sumlift2[ques2[i].first.second][0] = d;
}
}
sub1::genbin();
sub2::genbin2();
/*
for (int i = 1; i <= N; i++){
cout << sub2::binlift2[i][0] << " ";
}
cout << "\n";
for (int i = 1; i <= N; i++){
cout << sub2::sumlift2[i][0] << " ";
}
cout << "\n";*/
vector<pair<int, int> > input;
for (int i = 1; i <= T; i++){
int a, b; cin >> a >> b;
input.push_back({a, b});
}
for (int i = 0; i < (int)input.size(); i++){
cout << sub1::lca(input[i].first, input[i].second) << " " << sub2::lca2(input[i].first, input[i].second) << "\n";
}
cout.flush();
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
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:
result:
Subtask #2:
score: 0
Runtime Error
Test #13:
score: 0
Runtime Error
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:
Subtask #3:
score: 0
Runtime Error
Test #25:
score: 0
Runtime Error
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:
result:
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
Runtime Error
Test #49:
score: 0
Runtime Error
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...