QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#516606#4401. PrizeJWRuixi0 862ms176876kbC++203.3kb2024-08-12 19:39:482024-08-12 19:39:49

Judging History

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

  • [2024-08-12 19:39:49]
  • 评测
  • 测评结果:0
  • 用时:862ms
  • 内存:176876kb
  • [2024-08-12 19:39:48]
  • 提交

answer

#ifdef LOCAL
#include "stdafx.h"
#else
#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
using namespace std;

using vi = vector<int>;
#endif

constexpr int N = 1e6 + 9;
constexpr int M = 1e5 + 9;
int n, K, Q, T;
int S[M], cnt;

struct tree {
  int rt, hd[N], tot, p[N];

  struct {
    int v, nx;
  } e[N];

  void add_edge (int u, int v) {
    e[++tot] = {v, hd[u]};
    hd[u] = tot;
  }

  int dfn[N], dfc, sz[N], son[N], dep[N], tp[N];

  void dfs1 (int u, int f) {
    sz[u] = 1;
    dfn[u] = ++dfc;
    dep[u] = dep[f] + 1;
    for (int i = hd[u], v; i; i = e[i].nx) {
      dfs1(v = e[i].v, u);
      sz[u] += sz[v];
      if(sz[v] > sz[son[u]]) {
        son[u] = v;
      }
    }
  }

  void dfs2 (int u, int t) {
    tp[u] = t;
    if (son[u]) {
      dfs2(son[u], t);
    }
    for (int i = hd[u], v; i; i = e[i].nx) {
      if ((v = e[i].v) ^ son[u]) {
        dfs2(v, v);
      }
    }
  }

  int lca (int x, int y) {
    while (tp[x] ^ tp[y]) {
      if (dep[x] < dep[y]) {
        swap(x, y);
      }
      x = p[tp[x]];
    }
    return dep[x] < dep[y] ? x : y;
  }

  void in () {
    L (i, 1, n) {
      scanf("%d", p + i);
      if (p[i] == -1) {
        rt = i;
      } else {
        add_edge(p[i], i);
      }
    }
    dfs1(rt, 0);
    dfs2(rt, rt);
  }

  int H[N], tt;

  struct {
    int v, nx, w;
  } E[M * 2];

  bool vis[N];
  int d[N];

  void add (int u, int v, int w) {
    E[++tt] = {v, H[u], w};
    H[u] = tt;
    E[++tt] = {u, H[v], -w};
    H[v] = tt;
  }

  void sol () {
    queue<int> q;
    q.push(S[1]);
    d[S[1]] = 0, vis[S[1]] = true;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = H[u], v; i; i = E[i].nx) {
        v = E[i].v;
        if (!vis[v]) {
          d[v] = d[u] + E[i].w, vis[v] = true;
          q.push(v);
        } else {
          // assert(d[v] == d[u] + E[i].w);
        }
      }
    }
  }

  int dis (int x, int y) {
    int l = lca(x, y);
    assert(vis[l]);
    return d[x] + d[y] - 2 * d[l];
  }
} t[2];

int qx[M], qy[M];

int main () {
  scanf("%d%d%d%d", &n, &K, &Q, &T);
  L (i, 0, 1) {
    t[i].in();
  }
  L (i, 1, n) {
    if (t[0].dfn[i] <= K) {
      S[++cnt] = i;
    }
  }
  L (i, 1, K) {
    printf("%d ", S[i]);
  }
  printf("\n");
  fflush(stdout);
  sort(S + 1, S + K + 1, [] (int x, int y) {
    return t[1].dfn[x] < t[1].dfn[y];
  });
  L (i, 2, K) {
    printf("? %d %d\n", S[i - 1], S[i]);
  }
  printf("!\n");
  fflush(stdout);
  L (i, 2, K) {
    int l = t[0].lca(S[i - 1], S[i]), d1, d2;
    scanf("%d%d", &d1, &d2);
    t[0].add(l, S[i - 1], d1);
    t[0].add(l, S[i], d2);
    l = t[1].lca(S[i - 1], S[i]);
    scanf("%d%d", &d1, &d2);
    t[1].add(l, S[i - 1], d1);
    t[1].add(l, S[i], d2);
  }
  t[0].sol();
  t[1].sol();
  L (i, 1, T) {
    scanf("%d%d", qx + i, qy + i);
  }
  L (i, 1, T) {
    printf("%d %d\n", t[0].dis(qx[i], qy[i]), t[1].dis(qx[i], qy[i]));
  }
  fflush(stdout);
}
// I love WHQ!

详细

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:

10 12 22 35 36 38 46 51 57 72 87 92 96 101 112 129 130 132 180 186 206 208 209 217 219 224 228 229 244 245 247 251 255 256 285 294 310 314 319 344 346 351 355 365 368 376 378 383 388 391 406 412 413 414 425 426 427 440 443 448 449 459 461 463 466 474 480 485 498 506 507 515 520 543 544 554 566 578 5...

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:

16 22 25 34 38 54 55 56 61 65 67 78 81 101 117 119 120 121 123 124 130 134 149 155 163 169 170 177 189 196 204 205 214 232 234 239 265 269 273 281 282 289 311 318 335 338 339 341 354 358 377 380 383 389 403 414 427 435 437 438 448 451 455 459 468 469 475 479 485 495 496 506 508 511 521 543 544 551 5...

result:


Subtask #3:

score: 0
Runtime Error

Test #25:

score: 19
Accepted
time: 344ms
memory: 114796kb

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:

1214 2143 3735 4030 7129 8253 11748 12225 17034 19060 20242 30813 32330 34833 45188 48206 50829 52835 55343 59162 60737 65783 68301 70140 70498 70917 71690 81733 81917 85913 88306 88328 88823 90620 92449 95551 96560 109196 112780 113741 116227 116954 120898 121346 124294 124622 125508 125537 128363 ...

result:

ok good job!

Test #26:

score: 19
Accepted
time: 323ms
memory: 114096kb

input:

500000 200 199 40000
83785
150667
304961
267635
97760
385201
77226
6522
352645
72592
427133
30755
100574
359648
403948
394809
425453
115868
11287
351385
494434
245106
58157
395180
326236
277135
359592
13569
76251
45366
172378
122783
216597
466130
284420
342613
471698
380682
92490
79264
241049
54038
...

output:

174 222 8696 9763 16321 18992 19463 20773 22079 24946 26574 27600 28928 29151 39717 49980 57895 59196 60167 60540 63346 63493 63656 64655 66300 66858 67025 72009 72619 74050 81605 86191 88704 92915 97493 98656 99891 101597 101629 102338 103609 103875 105525 106791 107095 107714 110572 111898 112101 ...

result:

ok good job!

Test #27:

score: 0
Runtime Error

input:

500000 200 199 40000
94863
498513
460682
411416
360517
309831
253717
325019
496632
255803
130770
289206
181204
74729
481723
293737
94126
307214
342974
448321
17084
433126
387809
279606
251781
65795
125269
129465
433572
219622
11806
179248
367117
84640
114067
122590
4140
116015
77759
392439
408930
10...

output:

4158 4806 5114 7486 20753 21008 23627 26655 28237 28866 38287 39372 48111 49287 49512 56814 57121 60570 60999 63045 63515 65483 68411 68716 69339 71778 72448 77209 78705 79739 80064 81602 94835 95296 96963 96990 97609 98374 103118 105384 106250 111382 113388 114276 118968 121418 123558 124666 127603...

result:


Subtask #4:

score: 0
Runtime Error

Test #37:

score: 22
Accepted
time: 862ms
memory: 176876kb

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:

2107 2333 3694 4616 5440 6866 6983 7525 9720 10062 10226 11086 11280 11649 12530 12977 15895 18501 18543 18720 18751 19029 19970 23318 23665 25440 27083 28832 29922 34062 36058 36066 36918 37375 37551 37681 37693 38230 42470 42868 42991 45539 46443 46606 47245 49131 49762 51122 51277 52728 53512 535...

result:

ok good job!

Test #38:

score: 22
Accepted
time: 842ms
memory: 176568kb

input:

1000000 1000 999 100000
530144
36744
762893
712555
181981
816257
634992
419372
362279
817260
80801
697008
163211
900947
207310
862766
871091
388529
304808
574011
609949
509094
682125
781230
431445
517909
578411
288003
874415
410542
327673
607230
278208
956997
60166
842448
708661
562761
996349
382922...

output:

547 3682 4035 5064 7800 10059 10172 10282 11190 13550 13950 19507 19951 20573 21971 22202 23695 24229 24998 25047 27609 27877 29018 29794 30062 30304 32338 33234 34560 34645 34777 35293 35986 36817 38843 39569 40971 41584 42008 42414 42639 43257 45363 46124 48229 48615 49040 49432 50750 52274 52940 ...

result:

ok good job!

Test #39:

score: 0
Runtime Error

input:

1000000 1000 999 100000
184414
849676
938006
927343
390133
327580
229110
507237
712311
8816
414520
114671
637641
82050
586607
523821
775429
139792
129360
175687
202474
801377
53523
281419
268534
488983
371227
294280
754555
448802
474939
391153
68307
762784
972243
245396
471656
982894
891252
945526
5...

output:

396 1630 2334 3557 3956 4889 8037 9146 9273 9759 11176 12015 16344 18728 21253 21318 23622 24817 25572 27338 27794 28460 28753 29398 30803 33401 34851 35790 36907 37617 38031 41761 41779 42027 43379 46947 47886 50551 50880 51091 53194 54718 54800 54870 54898 55888 55948 56423 56604 58323 60766 60840...

result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%