QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#387949#3171. Rooted Subtreesohiostatescarlet#WA 98ms36000kbC++172.3kb2024-04-13 07:26:522024-04-13 07:26:53

Judging History

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

  • [2024-04-13 07:26:53]
  • 评测
  • 测评结果:WA
  • 用时:98ms
  • 内存:36000kb
  • [2024-04-13 07:26:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define L long long
#ifdef LOCAL
#define dbg(x) cerr << '[' << #x << " = " << (x) << "]\n";
#else
#define dbg(x)
#endif
// g++ -std=c++17 -O2 -DLOCAL -o a a.cpp

template <class T = int>
struct RMQ {
    vector<vector<int>> jmp;
    RMQ(const vector<T>& V) : jmp(1, V) {
        for (int pw = 1, k = 1; pw * 2 <= V.size(); pw *= 2, ++k) {
            jmp.emplace_back(int(V.size()) - pw * 2 + 1);
            for (int j = 0; j < jmp[k].size(); j++)
                jmp[k][j] = min(jmp[k - 1][j], jmp[k - 1][j + pw]);
        }
    }
    T query(int a, int b) {
        assert(a < b);
        int dep = 31 - __builtin_clz(b - a);
        return min(jmp[dep][a], jmp[dep][b - (1 << dep)]);
    }
};
struct LCA {
    int tim = 0;
    vector<int> time, path, ret;
    RMQ<int> rmq;
    LCA(vector<vector<int>>& g, int rt) : time(g.size()), rmq((dfs(g, rt, -1), ret)) {}
    void dfs(vector<vector<int>> &g, int u, int fa) {
        time[u] = tim++;
        for (int v : g[u]) {
            if (v == fa) continue;
            path.push_back(u), ret.push_back(time[u]);
            dfs(g, v, u);
        }
    }
    int lca(int a, int b) {
        if (a == b) return a;
        tie(a, b) = minmax(time[a], time[b]);
        return path[rmq.query(a, b)];
    }
};

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, q;
    cin >> n >> q;
    vector<vector<int>> edges(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v; u--; v--;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    vector<int> depth(n), vis(n);
    auto dfs = [&](auto&& self, int cur, int par, int dep) {
        if (vis[cur]) return;
        vis[cur] = 1;
        depth[cur] = dep;
        for (int nbr : edges[cur]) {
            if (nbr == par) continue;
            self(self, nbr, cur, dep + 1);
        }
    };
    dfs(dfs, 0, -1, 0);
    LCA lca(edges, 0);
    auto dist = [&](int u, int v) {
        return depth[u] + depth[v] - 2 * depth[lca.lca(u, v)];
    };

    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r; l--; r--;
        int d = dist(l,r)+1;
        int sol = d * d - d * (d - 1) / 2 + n - d;
        cout << sol << '\n';
    }
}

/*

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

*/

詳細信息

Test #1:

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

input:

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

output:

8
6

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 94ms
memory: 31300kb

input:

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

output:

200153
200253
200171
200465
200231
200406
200231
200325
200325
200435
200253
200136
200435
200136
200105
200300
200253
200276
200276
200351
200231
200105
200253
200325
200435
200351
200105
200325
200351
200300
200120
200325
200120
200561
200496
200378
200351
200210
200378
200190
200351
200120
200378...

result:

ok 200000 lines

Test #3:

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

input:

1 1
1 1

output:

1

result:

ok single line: '1'

Test #4:

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

input:

2 3
1 2
1 1
2 2
1 2

output:

2
2
3

result:

ok 3 lines

Test #5:

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

input:

20 18
8 14
8 19
8 20
8 16
8 15
8 6
8 9
8 1
8 3
8 7
8 18
8 11
8 10
8 4
8 5
8 17
8 12
8 13
8 2
8 1
8 16
8 12
8 11
8 20
8 3
8 17
8 7
8 19
15 18
18 2
2 14
14 6
6 4
4 13
13 5
5 10
10 9

output:

21
21
21
21
21
21
21
21
21
23
23
23
23
23
23
23
23
23

result:

ok 18 lines

Test #6:

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

input:

20 14
10 19
19 2
4 11
18 6
16 3
12 18
8 16
3 15
2 9
17 14
9 17
6 4
7 8
15 20
5 1
11 10
14 7
1 13
20 5
12 13
12 1
18 13
18 1
15 3
11 14
8 14
12 13
18 10
17 13
13 2
9 14
12 5
7 12

output:

210
191
191
173
21
41
23
210
30
75
98
23
173
86

result:

ok 14 lines

Test #7:

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

input:

250 250
135 53
204 234
5 21
81 96
35 134
154 239
173 46
34 30
98 31
128 127
25 213
160 69
89 139
136 83
31 44
135 93
221 37
150 86
178 29
189 2
214 4
71 107
28 94
86 104
179 181
108 147
192 240
54 24
153 194
8 20
215 137
214 77
218 177
82 118
14 195
20 61
115 142
90 10
121 169
50 157
212 3
154 222
2...

output:

305
328
341
295
316
305
328
305
355
286
286
341
341
305
278
355
316
316
305
341
341
316
305
278
355
295
316
328
316
341
253
265
341
265
316
316
328
328
328
355
341
341
341
355
355
341
256
328
316
305
316
355
278
328
341
250
328
271
286
316
328
355
260
286
341
316
286
251
265
305
355
328
316
295
341
...

result:

ok 250 lines

Test #8:

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

input:

250 250
243 188
243 20
243 165
243 194
243 161
243 9
243 46
243 24
243 196
243 109
243 232
243 23
243 162
243 207
243 237
243 2
243 167
243 229
243 146
243 136
243 76
243 148
243 18
243 56
243 51
243 5
243 204
243 91
243 168
243 82
243 78
243 235
243 124
243 171
243 84
243 224
243 36
243 79
243 160
...

output:

253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
253
...

result:

ok 250 lines

Test #9:

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

input:

2000 1998
500 341
500 1100
500 922
500 15
500 154
500 509
500 1527
500 19
500 1623
500 969
500 812
500 766
500 990
500 448
500 587
500 319
500 856
500 376
500 818
500 1568
500 186
500 1733
500 1459
500 777
500 64
500 1342
500 1920
500 767
500 793
500 1955
500 93
500 22
500 1875
500 1465
500 96
500 2...

output:

2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
2001
...

result:

ok 1998 lines

Test #10:

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

input:

2000 1484
1323 1302
394 1722
758 274
64 1429
1966 1209
598 254
1034 210
1950 177
1990 589
1479 1993
346 831
924 241
424 133
743 714
1032 57
1296 1164
975 1398
951 939
1075 378
971 1804
833 1343
1758 1053
1477 298
1336 790
1810 344
1466 639
309 1449
1732 1169
1618 787
1139 1409
1059 1855
495 85
1627 ...

output:

2001000
1999001
1997003
1995006
1993010
1991015
1989021
1987028
1985036
1983045
1981055
1979066
1977078
1975091
1973105
1971120
1969136
1967153
1965171
1963190
1961210
1959231
1999001
1997003
1995006
1993010
1991015
1989021
1987028
1985036
1983045
1981055
1979066
1977078
1975091
1973105
1971120
1969...

result:

ok 1484 lines

Test #11:

score: 0
Accepted
time: 76ms
memory: 32032kb

input:

200000 199998
122727 159749
122727 51751
122727 190479
122727 24518
122727 2343
122727 44912
122727 156204
122727 164965
122727 100393
122727 2873
122727 115927
122727 110614
122727 98988
122727 145408
122727 151559
122727 2158
122727 79937
122727 69667
122727 173913
122727 10739
122727 55150
122727...

output:

200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001
200001...

result:

ok 199998 lines

Test #12:

score: 0
Accepted
time: 8ms
memory: 5768kb

input:

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

output:

20055
20378
20231
20351
20105
20231
20171
20325
20231
20078
20078
20276
20105
20153
20190
20105
20153
20210
20253
20120
20253
20190
20253
20055
20276
20171
20190
20120
20120
20136
20066
20171
20153
20078
20153
20190
20171
20253
20136
20276
20091
20136
20153
20045
20153
20120
20190
20120
20001
20253
...

result:

ok 20000 lines

Test #13:

score: 0
Accepted
time: 8ms
memory: 5752kb

input:

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

output:

20001
20078
20231
20153
20055
20171
20171
20091
20171
20171
20190
20091
20171
20120
20036
20091
20153
20105
20190
20120
20253
20190
20091
20136
20190
20136
20190
20091
20210
20120
20276
20253
20105
20210
20066
20045
20091
20253
20091
20105
20045
20171
20136
20120
20120
20136
20231
20105
20300
20006
...

result:

ok 20000 lines

Test #14:

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

input:

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

output:

2091
2045
2021
2120
2171
2021
2171
2105
2036
2231
2078
2045
2136
2036
2153
2136
2190
2055
2231
2136
2136
2078
2021
2190
2105
2045
2078
2171
2055
2171
2105
2153
2253
2105
2045
2136
2028
2036
2055
2091
2078
2120
2153
2045
2105
2055
2136
2045
2153
2036
2045
2015
2190
2120
2078
2105
2091
2036
2136
2190
...

result:

ok 2000 lines

Test #15:

score: 0
Accepted
time: 93ms
memory: 31308kb

input:

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

output:

200105
200171
200231
200231
200406
200066
200406
200253
200406
200300
200190
200325
200171
200253
200210
200253
200066
200153
200231
200091
200253
200153
200190
200325
200325
200561
200153
200435
200231
200276
200276
200435
200190
200300
200231
200325
200190
200325
200210
200406
200105
200325
200406...

result:

ok 200000 lines

Test #16:

score: -100
Wrong Answer
time: 98ms
memory: 36000kb

input:

200000 149729
118758 41941
151264 48439
55427 89341
884 3017
98046 178879
37859 41209
8520 9002
176846 128808
1902 173002
155465 19807
103967 66987
17993 146252
118641 115148
109971 140772
58928 165270
167188 70418
47798 58904
192945 37112
82164 51901
128275 28560
35914 103689
99159 3637
40027 14212...

output:

672747168
672547169
672347171
672147174
671947178
671747183
671547189
671347196
671147204
670947213
670747223
670547234
670347246
670147259
669947273
669747288
669547304
669347321
669147339
668947358
668747378
668547399
668347421
668147444
667947468
667747493
667547519
667347546
667147574
666947603
...

result:

wrong answer 1st lines differ - expected: '20000100000', found: '672747168'