QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#464935#6340. Tourismzzy092224 783ms34584kbC++143.8kb2024-07-06 16:15:092024-07-06 16:15:11

Judging History

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

  • [2024-07-06 16:15:11]
  • 评测
  • 测评结果:24
  • 用时:783ms
  • 内存:34584kb
  • [2024-07-06 16:15:09]
  • 提交

answer

#include <bits/stdc++.h>

constexpr int N = 100005;

int n, m, q;
std::vector<int> t[N];

int dep[N], siz[N], son[N], fa[N];

void dfs1(int u) {
    siz[u] = 1;
    for (const int &v : t[u]) {
        if (v == fa[u])
            continue;
        dep[v] = dep[u] + 1;
        fa[v] = u;
        dfs1(v);
        siz[u] += siz[v];
        if (siz[v] > siz[son[u]]) son[u] = v;
    }
}

int dfn[N], rnk[N], top[N], tot;

void dfs2(int u, int tp) {
    dfn[u] = ++tot;
    rnk[tot] = u;
    top[u] = tp;
    if (son[u])
        dfs2(son[u], tp);
    for (const int &v : t[u]) {
        if (v == fa[u] || v == son[u])
            continue;
        dfs2(v, v);
    }
}

inline int lca(int x, int y) {
    int fx = top[x], fy = top[y];
    while (fx != fy) {
        if (dep[fx] > dep[fy])
            x = fa[fx], fx = top[x];
        else 
            y = fa[fy], fy = top[y];
    }
    return dep[x] < dep[y] ? x : y;
}

int c[N];
int st[25][N];

inline void st_init() {
    for (int i = 1; i <= m; i++)
        st[0][i] = c[i];
    for (int k = 1; k <= 20; k++)
        for (int i = 1; i + (1 << k) - 1 <= m; i++)
            st[k][i] = lca(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
}

inline int lca_lr(int l, int r) {
    int k = std::__lg(r - l + 1);
    return lca(st[k][l], st[k][r - (1 << k) + 1]);
}

struct node {
    int l, r, v;
}tree[N << 2];

#define ls(p) (p << 1)
#define rs(p) (ls(p) | 1)
#define l(p)  tree[p].l
#define r(p)  tree[p].r
#define v(p)  tree[p].v

void build(int p, int l, int r) {
    l(p) = l, r(p) = r;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(ls(p), l, mid);
    build(rs(p), mid + 1, r);
}

void add(int p, int x, int vl) {
    if (x > r(p) || x < l(p))
        return;
    v(p) += vl;
    if (l(p) == r(p)) {
        assert(v(p) >= 0);
        return;
    }
    add(ls(p), x, vl);
    add(rs(p), x, vl); 
}

int sum(int p, int l, int r) {
    if (l > r(p) || r < l(p))
        return 0;
    if (l <= l(p) && r(p) <= r)
        return v(p);
    return sum(ls(p), l, r) + sum(rs(p), l, r);
}

struct odt_node {
    int l, r, v;
    odt_node(int _l = 0, int _r = 0, int _v = 0) {
        l = _l, r = _r, v = _v;
    }
    bool operator<(odt_node x) const {
        return l < x.l;
    }
};

std::set<odt_node> odt;

inline auto split(int x) {
    if (x > m) return odt.end();
    auto it = --odt.upper_bound(odt_node(x, 0, 0));
    if (it->l == x) 
        return it;
    int l = it->l, r = it->r, v = it->v;
    odt.erase(it);
    odt.insert(odt_node(l, x - 1, v));
    return odt.insert(odt_node(x, r, v)).first;
}

inline void assign(int l, int r, int v) {
    auto itr = split(r + 1), itl = split(l);
    for (auto it = itl; it != itr; it++)
        add(1, it->v, -(it->r - it->l + 1));
    odt.erase(itl, itr);
    odt.insert(odt_node(l, r, v));
    add(1, v, r - l + 1);
}

inline void ins(int id) {
    int x = c[id];
    int fx = top[x];
    while (x) {
        assign(dfn[fx], dfn[x], id);
        x = fa[fx], fx = top[x];
    }
}

std::vector<std::pair<int, int> > qs[N];
int ans[N];
int main() {
    std::cin >> n >> m >> q;
    for (int i = 1, u, v; i <= n - 1; i++) {
        std::cin >> u >> v;
        t[u].push_back(v);
        t[v].push_back(u);
    }
    dfs1(1);
    dfs2(1, 1);
    for (int i = 1; i <= m; i++)
        std::cin >> c[i];
    st_init();
    build(1, 1, m);
    odt.insert({1, m, 0});
    add(1, 0, m);
    for (int i = 1, l, r; i <= q; i++) {
        std::cin >> l >> r;
        qs[r].push_back({l, i});
    }
    for (int r = 1; r <= m; r++) {
        ins(r);
        for (const auto &[l, id] : qs[r]) 
            ans[id] = sum(1, l, r) - dep[lca_lr(l, r)];// std::cout << dep[lca_lr(l, r)] << '\n';
    }
    for (int i = 1; i <= q; i++)
        std::cout << ans[i] << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 5
Accepted
time: 0ms
memory: 17704kb

input:

166 249 224
158 52
52 82
158 36
117 158
119 117
5 82
158 18
22 36
82 143
105 36
22 152
36 92
117 2
123 158
5 134
119 89
31 119
92 48
105 149
149 17
108 31
134 50
3 52
63 158
3 51
42 22
17 10
103 158
50 122
92 85
50 78
117 159
36 20
143 115
158 83
20 4
142 22
23 3
96 10
19 134
8 10
151 92
65 108
89 5...

output:

67
97
110
93
110
139
84
24
126
121
70
97
83
96
25
113
86
60
79
49
107
131
66
33
4
41
86
110
59
123
46
130
73
63
93
75
113
85
25
95
127
69
85
121
77
127
123
83
34
62
99
121
63
56
99
106
28
69
127
48
74
134
68
94
22
120
105
116
36
83
124
112
130
19
34
112
138
37
98
64
122
109
1
105
137
73
105
109
89
9...

result:

ok 224 numbers

Test #2:

score: -5
Wrong Answer
time: 0ms
memory: 15604kb

input:

225 173 232
88 46
74 88
88 60
86 46
202 86
175 86
165 74
60 61
13 88
78 46
61 163
61 123
13 15
211 60
78 75
123 188
165 146
88 93
93 214
16 202
137 146
75 133
202 1
213 202
30 214
203 165
146 222
91 146
203 106
23 75
104 202
30 47
165 174
144 133
137 58
20 1
75 183
133 26
21 30
80 93
106 36
207 188
...

output:

104
96
80
77
150
166
111
146
22
97
138
25
84
20
19
153
99
77
117
110
48
51
38
109
157
131
58
29
30
135
20
77
104
57
37
50
14
95
148
79
69
43
73
51
42
69
74
89
112
29
17
138
35
55
39
177
83
104
125
173
181
84
18
186
133
122
55
143
54
156
133
113
66
80
57
120
51
54
62
55
42
130
55
52
185
114
60
139
11...

result:

wrong answer 1st numbers differ - expected: '96', found: '104'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #56:

score: 7
Accepted
time: 136ms
memory: 30244kb

input:

55321 88650 75523
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51...

output:

55319
55319
55313
55306
55319
53676
55320
55301
55319
55319
55268
55319
55318
55320
55311
55311
55319
55275
55301
55319
55319
55317
55320
55319
55319
55318
55295
55318
55319
55319
55319
55248
55319
55320
55319
55319
55319
55319
55319
55319
55320
55301
55319
55186
55204
55320
55319
55319
55297
55318
...

result:

ok 75523 numbers

Test #57:

score: -7
Wrong Answer
time: 108ms
memory: 33236kb

input:

80807 56552 65576
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51...

output:

66263
64671
68991
76944
66403
62199
73257
66846
64910
80073
64341
72949
59418
65325
76809
66136
67573
71062
78746
62704
60028
69417
76478
68016
69824
71380
73666
75390
66744
58249
76601
79707
66440
74779
68383
71445
73046
58307
65467
70562
80651
57519
74252
66388
63001
59298
72104
60252
73441
78670
...

result:

wrong answer 1st numbers differ - expected: '80782', found: '66263'

Subtask #4:

score: 0
Wrong Answer

Test #69:

score: 0
Wrong Answer
time: 220ms
memory: 24828kb

input:

54738 54525 1797
45211 4527
4527 43609
4527 19876
16325 43609
32183 4527
16325 32579
43609 25554
32183 38972
45211 53953
16325 19810
10881 19810
45211 12698
27967 19810
25554 46338
51894 45211
25388 16325
512 25554
43609 7820
10206 512
30021 32183
48647 43609
46338 44138
16766 7820
10023 53953
19810...

output:

276
238
198
214
294
240
233
266
184
241
207
241
256
225
215
222
190
269
221
242
287
225
215
252
273
203
281
186
259
195
152
183
206
241
218
205
241
233
194
239
258
244
267
339
224
205
242
202
260
275
173
243
178
228
251
242
239
231
203
249
277
215
202
169
243
258
208
306
232
231
211
224
249
256
203
...

result:

wrong answer 406th numbers differ - expected: '255', found: '259'

Subtask #5:

score: 24
Accepted

Test #102:

score: 24
Accepted
time: 575ms
memory: 29096kb

input:

55965 89652 95687
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26...

output:

42788
7820
39914
18090
47411
43990
2171
29300
18235
16451
44208
47526
32163
44460
32385
30155
45741
45708
47396
43518
30989
19208
13902
35077
49210
21200
43577
32110
19690
35461
45079
11601
42233
16862
23259
44558
41924
39465
34626
41081
32139
34482
41166
24623
11638
18786
29659
38064
42423
42321
30...

result:

ok 95687 numbers

Test #103:

score: 0
Accepted
time: 606ms
memory: 29744kb

input:

58162 92590 89370
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26...

output:

26124
11696
34672
19906
37517
39745
12092
11803
38936
29481
39777
42062
22087
43080
42468
22618
42584
42512
36052
48395
44563
29063
20611
42203
40753
37002
24991
38717
37467
43935
36308
37416
43842
39169
44996
35657
38159
30030
41535
34488
37655
50046
46898
42657
45573
4308
15509
41852
28225
32898
3...

result:

ok 89370 numbers

Test #104:

score: 0
Accepted
time: 711ms
memory: 31872kb

input:

84839 99146 96158
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52
26...

output:

54950
43189
36772
1311
65352
65239
39742
60625
5657
51176
12141
22845
42904
46862
12604
38455
45384
26962
63267
28904
9995
50936
39499
48890
47780
7253
6239
49271
59029
46282
27940
57496
64917
52909
58560
27947
46253
21818
59417
63544
21645
37364
26764
23249
66794
50332
51190
44172
68809
62291
46510...

result:

ok 96158 numbers

Test #105:

score: 0
Accepted
time: 783ms
memory: 32828kb

input:

100000 100000 100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52...

output:

74296
48517
45111
49478
42663
30316
66980
14003
52373
69355
26205
22241
70123
62132
25494
49459
17717
51008
30285
38307
61890
49679
39923
21693
19328
73265
26327
77090
54503
74185
49855
27162
51570
35838
65915
75805
44534
68679
45700
9883
73608
21808
60186
5320
43240
42021
55300
38369
51151
63595
73...

result:

ok 100000 numbers

Test #106:

score: 0
Accepted
time: 718ms
memory: 34584kb

input:

100000 100000 100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52...

output:

55661
72881
55021
58078
53349
21715
54142
73477
55470
56603
40428
59731
54626
8400
56046
17766
66014
48806
76195
61741
49472
66302
55213
40434
58123
46091
46564
60096
73852
67311
77958
38297
54985
40428
23638
29758
15211
31168
52671
58116
56280
67887
68408
37271
74504
52906
24333
39868
15906
62095
1...

result:

ok 100000 numbers

Test #107:

score: 0
Accepted
time: 737ms
memory: 33616kb

input:

100000 100000 100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52...

output:

49384
72701
65618
43104
29539
20835
59080
59167
56906
20578
46819
13802
50034
47888
68226
69683
53386
58689
3005
68357
70057
42532
29448
63486
59387
75767
1484
50118
7531
59768
64142
33124
58482
70372
21619
5524
37882
49407
57595
67194
8814
23826
74496
45761
8423
63268
52487
63160
58909
53162
65003
...

result:

ok 100000 numbers

Test #108:

score: 0
Accepted
time: 772ms
memory: 34060kb

input:

100000 100000 100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52...

output:

38465
43388
71697
56061
6291
40224
57457
67627
68790
34583
73389
43063
39974
73144
52951
58669
70864
15981
45044
23527
30202
26791
60615
60846
50771
69093
32598
52346
53692
33355
42582
70057
12128
52963
31023
36955
48359
39535
6049
31335
74831
64311
32046
45695
67030
59949
69438
71994
5359
26311
621...

result:

ok 100000 numbers

Test #109:

score: 0
Accepted
time: 731ms
memory: 32784kb

input:

100000 100000 100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52...

output:

26184
13746
53879
74756
18323
28970
47470
57972
34286
25245
61319
34479
13814
60595
62463
63360
27912
61408
23127
50981
54756
35666
63071
72127
20254
65490
57530
51176
67262
78884
27346
72671
5421
33642
76750
43390
53948
52980
39795
70102
15985
66308
51198
49738
33588
18825
62592
47119
57733
65814
5...

result:

ok 100000 numbers

Test #110:

score: 0
Accepted
time: 733ms
memory: 32976kb

input:

100000 100000 100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52...

output:

26479
23046
36774
24865
44391
78375
49346
7754
74043
72476
37596
8906
60787
36825
21658
8554
931
49288
50894
46211
36664
3786
41495
47245
37349
28584
60715
26725
77691
56564
70960
62827
50819
57064
61332
43428
62586
63789
53495
59866
32477
44190
33551
48908
8890
73163
59870
77730
42497
75153
46755
4...

result:

ok 100000 numbers

Test #111:

score: 0
Accepted
time: 740ms
memory: 32912kb

input:

100000 100000 100000
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
6 12
6 13
7 14
7 15
8 16
8 17
9 18
9 19
10 20
10 21
11 22
11 23
12 24
12 25
13 26
13 27
14 28
14 29
15 30
15 31
16 32
16 33
17 34
17 35
18 36
18 37
19 38
19 39
20 40
20 41
21 42
21 43
22 44
22 45
23 46
23 47
24 48
24 49
25 50
25 51
26 52...

output:

18616
52534
54018
54103
39251
67225
40344
75167
69598
74411
62698
47426
70535
30737
33020
70772
51000
70396
75726
12926
38592
43706
33934
55736
68545
65599
17447
68380
34831
60273
77781
52341
42480
69806
71029
69797
38672
68520
67630
47153
67495
58497
61878
54799
43576
68697
31787
22247
26277
37491
...

result:

ok 100000 numbers

Subtask #6:

score: 0
Skipped

Dependency #1:

0%