QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#291863#2918. Tree Number Generatorvijay72AC ✓308ms64512kbC++203.9kb2023-12-27 10:17:482023-12-27 10:17:48

Judging History

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

  • [2023-12-27 10:17:48]
  • 评测
  • 测评结果:AC
  • 用时:308ms
  • 内存:64512kb
  • [2023-12-27 10:17:48]
  • 提交

answer

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

// clang-format off

using ll = long long;
#define sz(x) (int)x.size()

#ifdef DEBUG
    #include "debug.hpp"
#else
    #define dbg(x...) 0
#endif

// clang-format on

int mod;
vector<int> pw10;
int add(ll a, ll b) { return (a + b) % mod; }
int mul(ll a, ll b) { return (a * b) % mod; }

int combine(int x, int y, int len) {
  x = mul(x, pw10[len]);
  x = add(x, y);
  return x;
}

using Graph = vector<vector<int>>;
class Lift {
  int max_pow;
  vector<int> depth;
  vector<vector<int>> up;
  vector<vector<int>> num_up;
  vector<vector<int>> num_down;

  void dfs(int cur_node, int par, vector<int> &val, Graph &adj) {
    up[0][cur_node] = par;
    num_up[0][cur_node] = val[cur_node];
    num_down[0][cur_node] = val[cur_node];

    for (auto child : adj[cur_node]) {
      if (child == par) continue;

      depth[child] = depth[cur_node] + 1;
      dfs(child, cur_node, val, adj);
    }
  }

 public:
  Lift(Graph &G, vector<int> &val)
      : max_pow(31 - __builtin_clz(G.size()) + 1),
        depth(G.size()),
        up(max_pow, vector<int>(G.size(), -1)),
        num_up(max_pow, vector<int>(G.size())),
        num_down(max_pow, vector<int>(G.size())) {
    dfs(0, -1, val, G);

    for (size_t x = 1; x < up.size(); x++) {
      for (size_t i = 0; i < G.size(); i++) {
        int mid = up[x - 1][i];

        if (mid != -1 && up[x - 1][mid] != -1) {
          up[x][i] = up[x - 1][mid];

          num_up[x][i] =
              combine(num_up[x - 1][i], num_up[x - 1][mid], (1 << (x - 1)));
          num_down[x][i] =
              combine(num_down[x - 1][mid], num_down[x - 1][i], (1 << (x - 1)));
        }
      }
    }
    num_up[0][0] = val[0];
    num_down[0][0] = val[0];
  }

  int walk(int node, int dist) const {
    for (int i = 0; i < max_pow && node != -1; i++) {
      if ((dist >> i) & 1) node = up[i][node];
    }
    return node;
  }

  int lca(int node1, int node2) const {
    if (depth[node1] < depth[node2]) swap(node1, node2);

    int diff = depth[node1] - depth[node2];
    node1 = walk(node1, diff);
    if (node1 == node2) return node1;

    for (int i = max_pow - 1; i >= 0; i--) {
      if (up[i][node1] != up[i][node2]) {
        node1 = up[i][node1];
        node2 = up[i][node2];
      }
    }
    return up[0][node1];
  }

  int query(int node1, int node2) {
    int l = lca(node1, node2);

    int ans = 0, len = 0;

    // 1. go up from a till the lca
    int dist = depth[node1] - depth[l];
    for (int i = 0; i < max_pow; i++) {
      if ((dist >> i) & 1) {
        ans = combine(ans, num_up[i][node1], (1 << i));
        node1 = up[i][node1];
      }
    }
    assert(node1 == l);

    // but we also need to include the value at the lca itself
    // so we need to go one above the lca also
    ans = add(mul(ans, 10), num_up[0][l]);

    // 2. down from lca to b
    int ans2 = 0;
    len = 0;
    dist = depth[node2] - depth[l];
    for (int i = 0; i < max_pow; i++) {
      if ((dist >> i) & 1) {
        ans2 = combine(num_down[i][node2], ans2, len);
        len += (1 << i);
        node2 = up[i][node2];
      }
    }
    assert(node2 == l);

    ans = combine(ans, ans2, len);
    return ans;
  }
};

void solve() {
  int n, q;
  cin >> n >> mod >> q;

  pw10.assign(n + 1, 0);
  pw10[0] = 1 % mod;
  for (int i = 1; i <= n; i++) {
    pw10[i] = mul(pw10[i - 1], 10);
  }

  Graph adj(n);
  for (int i = 1; i <= n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  vector<int> val(n);
  for (auto &x : val) {
    cin >> x;
    x %= mod;
  }

  Lift l(adj, val);
  while (q--) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    cout << l.query(u, v) << "\n";
  }
}

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int T = 1;
  while (T--) solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3808kb

input:

2 1 4
1 2
1
3
1 2
2 1
1 1
2 2

output:

0
0
0
0

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3528kb

input:

3 10 5
1 2
2 3
0
0
0
1 3
3 1
2 2
2 3
2 1

output:

0
0
0
0
0

result:

ok 5 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 3896kb

input:

2000 2 2000
937 1471
1672 937
356 1672
976 356
1257 356
1503 1257
783 1503
1783 937
1284 976
1955 1503
802 1257
583 1471
526 356
701 1783
393 1955
307 1955
386 1955
1070 937
1724 802
1397 1724
1140 937
422 526
1941 1955
1638 937
1469 526
1800 526
1035 1800
1009 1140
1195 1800
142 1471
1225 1469
1524...

output:

0
1
1
1
0
0
1
0
0
1
0
1
0
0
1
1
0
0
1
1
1
1
1
0
0
0
1
0
1
1
1
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
1
0
0
0
1
1
0
1
0
0
0
0
0
1
0
1
1
0
0
0
0
1
1
1
1
1
0
0
1
1
1
0
1
1
1
0
0
1
1
1
1
0
0
0
1
0
1
0
0
1
1
1
0
1
1
0
1
0
1
0
0
1
0
1
0
0
1
1
0
1
1
1
1
0
0
0
1
0
0
0
0
1
1
0
1
1
0
1
1
0
1
1
0
0
0
1
1
0
0
1
...

result:

ok 2000 lines

Test #4:

score: 0
Accepted
time: 1ms
memory: 3944kb

input:

2000 1000 2000
1664 1028
763 1664
1541 1028
1544 1664
69 1544
1473 1541
475 1541
1551 1541
579 1473
262 475
1777 475
1916 475
391 763
807 1028
1357 1028
1682 69
345 1544
234 1541
63 391
480 807
1762 1544
306 1916
1436 1777
891 391
1852 306
523 1852
264 475
313 306
1139 391
1792 69
1604 69
398 313
10...

output:

263
429
976
56
31
940
168
28
487
658
273
218
944
664
498
705
709
490
61
931
421
664
632
538
876
282
145
61
430
984
589
436
780
641
69
126
625
208
629
603
566
57
355
843
705
781
514
898
804
290
366
642
429
899
716
466
371
620
252
606
690
500
412
226
495
380
61
580
805
132
423
845
618
862
924
729
637
...

result:

ok 2000 lines

Test #5:

score: 0
Accepted
time: 0ms
memory: 3980kb

input:

2000 1000000 2000
676 154
686 154
357 154
187 676
299 686
1508 357
1515 1508
972 686
105 357
748 1515
1711 686
1692 154
1869 299
1017 187
829 1017
809 1515
1505 676
383 1515
1002 972
1448 829
1657 1515
1824 1508
1271 1711
545 1515
1099 748
1255 748
556 545
388 1017
1290 357
992 1824
66 1017
1812 972...

output:

911946
658749
941314
251735
719871
241911
734055
143108
569168
899637
173402
264918
271418
632775
991506
402910
517268
914587
379978
462220
622382
658742
329239
43729
56506
192359
410979
57536
866374
142798
124989
947854
413121
183893
602943
488815
292373
183960
602947
199025
301900
603305
260397
64...

result:

ok 2000 lines

Test #6:

score: 0
Accepted
time: 1ms
memory: 4000kb

input:

2000 1000000000 2000
352 747
534 747
294 534
1548 534
1051 534
1850 747
574 1850
43 1548
1395 1850
1615 1051
1236 294
1869 43
1039 1051
288 294
299 747
217 1395
1220 1051
622 1850
673 574
1296 288
1331 1296
721 1039
1062 1615
518 721
1276 1296
1806 1276
1731 1869
477 574
461 1051
1703 622
358 1703
1...

output:

79101007
887079978
189124001
400049688
284
43253539
818072051
53354038
9042495
272009043
500270860
700462128
335009411
350090556
872000107
46209971
707883989
53354276
271083550
900288054
2682651
818832941
27070294
70720415
513017327
230170729
271033998
200904807
27104651
347046254
900272592
70720094...

result:

ok 2000 lines

Test #7:

score: 0
Accepted
time: 171ms
memory: 59608kb

input:

200000 2417 200000
146436 54121
82979 146436
129255 146436
99397 146436
148744 129255
125174 99397
55826 129255
13345 55826
131204 146436
59873 148744
148074 55826
23402 125174
24052 146436
93765 99397
177707 131204
33986 59873
51616 23402
144922 55826
92135 82979
63586 148744
45845 33986
199631 148...

output:

2040
1290
1149
1665
1466
926
1073
943
364
1804
1113
1831
354
780
998
1573
2076
2214
1019
1824
2049
1446
2081
1851
299
2303
448
2335
73
719
1799
732
722
2250
1138
1336
1945
1959
1012
804
459
2094
880
592
716
434
1256
1467
564
1585
962
615
1260
994
2294
1750
1134
341
168
573
722
1702
1933
836
772
1678...

result:

ok 200000 lines

Test #8:

score: 0
Accepted
time: 164ms
memory: 59680kb

input:

200000 689377511 200000
52219 109460
83892 109460
47762 83892
174199 52219
153148 52219
390 52219
48721 390
118607 47762
108230 118607
196969 390
19048 390
136354 47762
30259 390
104080 19048
197168 136354
27383 108230
68792 196969
184569 196969
151197 47762
145028 197168
113317 184569
158439 19048
...

output:

8921815
655545122
413125645
651771343
629854532
363619108
581379549
394047890
45313165
369057355
230323637
280900531
433560470
443738858
643201707
67861883
4293430
80204622
558065854
685316296
279856013
210825924
492017142
257611694
413449325
610859708
460920074
23610481
401158796
26475359
127087919...

result:

ok 200000 lines

Test #9:

score: 0
Accepted
time: 167ms
memory: 59732kb

input:

200000 798411196 200000
119892 140661
122570 119892
14051 140661
140377 140661
175884 119892
172490 175884
36439 140661
199156 122570
75015 175884
181651 36439
153384 181651
20560 36439
18520 140377
75840 140377
17289 75015
169639 175884
65984 175884
95205 36439
127142 14051
174900 127142
153555 205...

output:

18206054
13921879
554449006
772395766
557255853
520448412
507247136
47917633
376090796
700418943
676359175
214166568
648313632
82067541
125461294
605485604
138967754
244721878
364968617
512562081
576599652
505602437
175819487
616033716
583697253
454913885
471362032
271603982
587870303
24338233
39835...

result:

ok 200000 lines

Test #10:

score: 0
Accepted
time: 174ms
memory: 59596kb

input:

200000 280159818 200000
164279 83194
111000 83194
77502 83194
48962 164279
131483 48962
197541 164279
179851 197541
93984 111000
98027 93984
93720 77502
116832 98027
111404 93720
5005 197541
36711 197541
106738 98027
190136 48962
23525 164279
12120 23525
132712 5005
102957 98027
197475 98027
55603 9...

output:

39726229
239438494
80597096
247427433
65365333
137798171
72892099
261795854
498099
245220473
50727250
217900240
262134273
277364703
53544683
65461475
91643428
3581860
272885338
217376254
176047124
189818173
84059593
272796564
3704171
235036917
141102972
2640784
276410090
259686566
52790736
80821595
...

result:

ok 200000 lines

Test #11:

score: 0
Accepted
time: 159ms
memory: 59664kb

input:

200000 464082821 200000
27766 124265
132971 27766
194939 124265
156102 194939
106065 156102
47089 132971
131387 124265
49131 131387
175665 132971
184239 106065
23475 49131
70204 49131
86203 124265
138938 23475
27882 124265
147656 138938
149622 124265
10070 131387
154727 10070
156440 86203
181725 862...

output:

395874707
386956602
220345465
457555611
385827294
42812267
243503528
16329160
307138978
213715808
45053878
381009388
332821716
438672142
61062189
254427850
8489478
270003010
176186699
438163414
390902618
11906359
242145360
286126688
8148311
84288930
148216530
363107387
461882430
23045210
125649908
1...

result:

ok 200000 lines

Test #12:

score: 0
Accepted
time: 277ms
memory: 63944kb

input:

200000 689377511 200000
140227 172738
172738 119928
119928 83393
83393 178840
178840 6764
6764 142437
142437 14811
14811 146490
146490 136145
136145 101044
101044 147015
147015 41173
41173 46516
46516 170791
170791 61520
61520 2052
2052 135224
135224 194338
194338 44814
44814 77730
77730 21828
21828...

output:

537042913
113039324
358611217
600746018
265476833
258584119
207895068
86577032
341799971
612633720
470435321
248047296
226547378
620728661
607247110
492279889
173675868
282584874
222830374
294744374
477025674
465075939
200385257
218777733
459369367
52198834
184439105
516904574
431364752
17899032
554...

result:

ok 200000 lines

Test #13:

score: 0
Accepted
time: 308ms
memory: 64188kb

input:

200000 1000000000 200000
77758 62196
62196 16421
16421 62760
62760 152867
152867 34987
34987 177822
177822 160850
160850 8214
8214 199310
199310 43728
43728 98016
98016 132998
132998 28156
28156 94244
94244 139053
139053 61734
61734 64167
64167 111653
111653 16856
16856 87363
87363 194624
194624 581...

output:

559049320
70102397
327830245
335668295
779119821
194119880
253560006
651259316
234783434
646531145
632200901
233836977
106470762
88907325
225679166
251105025
957029591
329966092
438867254
944147950
876772976
804329577
138228210
699550883
423095301
647683763
114570966
126613270
836102469
775571132
31...

result:

ok 200000 lines

Test #14:

score: 0
Accepted
time: 115ms
memory: 60256kb

input:

199999 689377511 200000
145229 135625
135205 135625
7641 135625
54637 135625
44492 135625
55047 135625
10088 135625
86026 135625
143016 135625
101555 135625
89225 135625
127821 135625
81009 135625
62058 135625
49303 135625
86806 135625
179915 135625
85123 135625
7780 135625
191525 135625
67526 13562...

output:

879
777
374
473
478
779
176
377
379
372
376
672
277
271
679
978
879
278
678
471
971
779
276
277
874
875
679
471
475
78
77
776
372
875
772
879
677
475
872
671
579
174
878
476
573
778
876
973
971
374
372
877
878
172
879
171
377
975
574
76
877
78
77
672
971
570
179
170
272
478
473
379
570
674
770
375
1...

result:

ok 200000 lines

Test #15:

score: 0
Accepted
time: 151ms
memory: 59592kb

input:

199999 1000000000 200000
3127 106904
106904 153170
153170 67972
67972 85531
85531 118702
3127 147575
56594 75403
75403 191063
191063 53300
53300 5978
5978 115613
56594 147575
126416 93080
93080 5945
5945 168547
168547 29859
29859 79313
126416 147575
78580 54710
54710 165869
165869 54092
54092 130088...

output:

475137
685134742
99879533
11454694
664576811
51251939
7251665
912548
52966858
65583469
965217664
352556957
4835565
58065575
28055
65737658
305150741
222758911
284405034
274785449
385997380
45265454
9974354
694059
76506522
5727223
487538
711833523
3611529
24555654
67453
865058757
867595159
5762371
81...

result:

ok 200000 lines

Test #16:

score: 0
Accepted
time: 224ms
memory: 59456kb

input:

199999 1000000000 200000
90088 5172
5172 100583
100583 103425
103425 106493
106493 35965
35965 21031
21031 12984
12984 96499
96499 32676
32676 4350
4350 74436
74436 114352
114352 83766
83766 22440
22440 126392
126392 60155
60155 83624
83624 24141
24141 60748
60748 156936
156936 159961
159961 196783
...

output:

495017188
562439707
38652970
156934545
942459531
462095787
208980934
679653955
111094228
918671944
200980606
775136952
731972708
799429576
627799997
947843230
682354535
882348658
801820239
237288494
902792648
852434459
542523272
73465901
461282102
167031029
8984512
51783634
484523299
45961814
770251...

result:

ok 200000 lines

Test #17:

score: 0
Accepted
time: 260ms
memory: 60116kb

input:

199999 1000000000 200000
199850 141116
141116 118336
118336 44120
44120 78908
78908 25031
25031 95366
95366 39501
39501 147716
147716 94629
94629 72445
72445 13639
13639 81991
81991 158028
158028 184623
184623 123550
123550 184909
184909 154715
154715 140200
140200 113654
113654 174369
174369 152717...

output:

606035883
580439053
326660965
643424983
721140923
543464
852622306
202672174
572436700
656976381
749135466
614832048
450463782
296153285
187364124
571340354
772568993
487284312
812279857
146040926
857341945
823512758
715604542
24731931
542047387
448715909
618758781
911649694
987137285
453924560
8613...

result:

ok 200000 lines

Test #18:

score: 0
Accepted
time: 284ms
memory: 64512kb

input:

199999 1000000000 200000
5229 58817
58817 135175
135175 156312
156312 194105
194105 153160
153160 131604
131604 123598
123598 97353
97353 118244
118244 108242
108242 156368
156368 79820
79820 177070
177070 106354
106354 63229
63229 145863
145863 7115
7115 146250
146250 46631
46631 187569
187569 1150...

output:

551290858
920528404
425788554
128150095
726745185
707294059
639420308
506165526
161927983
712692752
912993910
622548764
185655560
599129939
156764357
441140681
898405011
944111193
264596315
226285081
597319864
41086868
368339584
372796375
362426955
661028970
788044817
67570536
786445714
347038590
67...

result:

ok 200000 lines

Test #19:

score: 0
Accepted
time: 0ms
memory: 3756kb

input:

5 100 4
1 2
1 3
1 4
5 3
1
2
3
0
4
1 5
5 1
4 2
3 3

output:

34
31
12
3

result:

ok 4 lines