QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#41385#4401. Prizejli50510 1391ms180440kbC++2010.1kb2022-07-30 06:07:382022-07-30 06:07:39

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-07-30 06:07:39]
  • 评测
  • 测评结果:10
  • 用时:1391ms
  • 内存:180440kb
  • [2022-07-30 06:07:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

void fastIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
}

const int MAXN = 500000+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();
    }
}

详细

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 1330ms
memory: 180060kb

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: 1391ms
memory: 180440kb

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: 0
Accepted
time: 936ms
memory: 146672kb

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 106115 32426 8390 3821 314935 201979 171459 413397 469146 119187 74265 167902 479051 182695 260054 235048 135315 280891 13044 240704 209304 211564 188960 481631 289686 273785 175837 385737 204887 288861 330677 315423 120726 278204 129910 396267 322633 472675 325914 329277 67326 391455 ...

result:

ok good job!

Test #4:

score: 0
Accepted
time: 991ms
memory: 146676kb

input:

500000 63976 63975 100000
230132
63748
303785
13497
431672
370351
360004
412191
378555
409703
485802
218204
475692
27602
220794
398856
89157
166559
116145
350738
277404
196706
40307
118602
171802
378360
389092
485168
224465
383516
33147
322617
254917
274019
57283
272241
216098
421952
489927
75641
40...

output:

210552 1449 40773 30337 3831 195031 251118 21848 332855 402436 374839 357357 42119 382885 377328 13863 386544 201759 32946 323963 484564 215037 277370 472684 81309 484227 89315 381707 431727 439267 216824 485515 421976 411697 230680 43213 25204 68073 30255 143879 164080 135142 441489 282767 310382 1...

result:

ok good job!

Test #5:

score: 0
Accepted
time: 999ms
memory: 146924kb

input:

500000 87673 87672 100000
151599
456749
347511
703
348209
260440
488627
416030
419890
408089
83617
120781
133411
374231
460689
211838
137587
252914
392401
321583
55161
335205
334340
4527
14086
142229
197076
17695
262896
258702
273353
51181
10968
366799
324067
299421
281975
7236
420627
92324
299845
1...

output:

51300 4033 3297 46811 80464 284515 347374 116368 204675 12242 236061 42585 91081 312035 285728 234206 326918 231575 193431 385908 123360 219570 237308 488275 146973 278867 303046 17686 461933 83949 100486 65040 253090 278869 342370 141292 167787 205320 41653 29945 83893 40950 348576 412681 220300 26...

result:

ok good job!

Test #6:

score: 0
Accepted
time: 1289ms
memory: 154404kb

input:

500000 77912 77911 100000
270576
129318
366297
25873
179787
473782
221947
331327
209469
412992
410608
286179
37554
355546
297085
420463
496948
223036
122019
151250
478469
468136
19073
318549
398897
364415
23730
407160
26064
436939
30150
336421
375149
131841
58480
259944
117641
414831
64311
336164
31...

output:

210887 26617 47747 209286 31977 243665 65697 394458 66936 330203 111706 400826 188117 490312 451749 377213 451432 482110 450513 372367 243217 17878 326862 79427 343913 203244 140881 256494 329204 164961 461047 338802 166743 393825 25540 420037 374407 50003 96053 427346 365280 191816 338726 463407 32...

result:

ok good job!

Test #7:

score: 0
Accepted
time: 1242ms
memory: 154276kb

input:

500000 77688 77687 100000
433011
472346
395389
187114
436024
138403
189990
398859
136147
195283
331183
46789
19828
335128
387768
442181
65556
72327
318927
462834
421288
227912
37067
387794
145879
258896
185861
356020
202881
490952
443694
95413
137215
137239
112863
481338
167802
304239
309781
391976
...

output:

176419 131882 35390 373863 172713 204978 297105 474574 443479 326538 390969 34874 492305 157831 85371 217598 310810 104348 344506 18218 34919 284048 191391 375157 93215 437374 179027 95313 246201 105486 90705 261692 432138 60063 214041 101698 415529 126781 367122 27413 85730 36224 346513 104818 2238...

result:

ok good job!

Test #8:

score: 0
Accepted
time: 1165ms
memory: 154508kb

input:

500000 70973 70972 100000
449081
8094
7358
89457
426121
454508
470543
485236
63347
441977
422774
88672
243638
499709
170209
157788
229166
106888
228931
289706
435222
496384
381579
323479
499140
1511
385050
44171
413854
248273
352221
305112
24289
277461
391744
395003
85800
396455
355110
186446
285096...

output:

449195 92100 139432 131622 170991 324408 396138 18454 365411 494280 359470 62857 516 49116 212775 228269 406044 238351 73730 344036 164637 142035 62522 287306 191612 27113 107127 151520 273425 3029 266766 489355 250496 60335 369915 212496 230914 324800 64090 294847 116290 472262 346162 136322 249997...

result:

ok good job!

Test #9:

score: 0
Accepted
time: 945ms
memory: 146728kb

input:

500000 66403 66402 100000
297237
432967
138046
88503
315699
372893
55309
335404
127581
165919
247543
254268
285147
289728
275281
44427
94393
302830
489861
429097
425153
11083
439096
414157
386411
152968
394984
46119
149177
369378
413029
198215
134317
366218
281170
465540
39702
367778
247925
64320
86...

output:

294428 15990 60747 1844 173342 476686 180822 429820 298329 356039 58547 290254 180015 476506 20120 265956 172302 27153 59287 30817 110197 441521 428010 2003 112355 265905 198312 129358 442298 120472 138884 373998 58266 256425 7274 137614 43114 65060 393472 137647 293565 81701 495260 317778 230822 47...

result:

ok good job!

Test #10:

score: 0
Accepted
time: 975ms
memory: 146928kb

input:

500000 82328 82327 100000
280281
366446
183709
14447
442815
440473
121531
103568
472324
479656
337467
424742
474404
340302
269686
457628
230012
484228
422877
10759
156759
66102
130428
307888
123685
460634
235321
98667
93133
489886
479420
34961
352500
322001
129001
121871
135775
235639
100221
221760
...

output:

185494 36690 87374 138798 36564 181594 428424 260437 178882 134288 146942 90320 210326 241671 445549 121178 164319 184591 354583 355428 247773 281684 307841 387907 97435 102464 184979 164216 317633 56960 295193 191071 295961 328549 299162 27136 188202 118130 161902 236258 147998 155971 322975 474055...

result:

ok good job!

Test #11:

score: 0
Accepted
time: 911ms
memory: 146756kb

input:

500000 53948 53947 100000
287984
258934
272973
481182
131565
217198
34714
463056
337977
495727
310042
26372
320480
231799
249741
340990
365501
267377
460708
248843
285777
172137
492784
201463
213559
259528
461602
235849
398717
25475
241699
451061
188952
251790
83551
169967
335575
209367
55705
6381
2...

output:

490646 30912 58228 256224 419416 179276 226624 297156 434671 224297 66900 102019 206352 345445 170824 216398 382142 38139 295276 461808 479814 117039 338283 434145 494560 141370 72417 19374 27632 289877 24100 185985 333545 136905 137035 102602 147548 27797 299360 304944 475001 306860 73631 185755 27...

result:

ok good job!

Test #12:

score: 0
Accepted
time: 984ms
memory: 146796kb

input:

500000 77935 77934 100000
38748
422564
39441
105430
38474
225464
237519
121832
72613
477531
321661
29181
307418
314049
120252
261006
88761
17726
492112
460837
55199
354114
417097
133271
231933
436973
110894
478550
291976
50101
38774
316091
306160
121826
315769
361823
82990
188508
124574
13093
235123...

output:

423149 92225 16389 129241 166449 184539 134974 114717 355886 329721 379424 292962 421117 497443 381527 149162 408 10702 212632 50138 317213 372008 378849 113411 195237 172507 239020 420304 489080 360466 438166 227686 419986 209153 382570 15084 218300 418265 483901 215816 378626 452355 214360 491276 ...

result:

ok good job!

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:

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
Wrong Answer

Test #25:

score: 0
Wrong Answer
time: 1138ms
memory: 178632kb

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 second integer of query #1: read 53295 but expected 11339644

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...

output:


result: