QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#456356#8547. Whose Land?zqsWA 570ms23084kbC++143.4kb2024-06-27 19:55:522024-06-27 19:55:52

Judging History

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

  • [2024-06-27 19:55:52]
  • 评测
  • 测评结果:WA
  • 用时:570ms
  • 内存:23084kb
  • [2024-06-27 19:55:52]
  • 提交

answer

#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++)

char buf[100000], *p1, *p2;
inline int read() {
    char ch; int x = 0; while ((ch = gc) < 48);
    do x = x * 10 + ch - 48; while ((ch = gc) >= 48); return x;
}
using std::min; using std::max; using std::prev; using std::next;
std::vector<int> G[100005];
std::vector<std::pair<int, int> > vec[100005];
struct node {
    mutable int l, r, v;
    inline bool operator < (const node &rhs) const {return l < rhs.l;}
};
int L[100005][21], R[100005][21], fa[100005], Q[100005], ord[100005], hd, tl, n, k, q, num;
int ans[100005];
bool vis[100005];
std::set<node> s;
void dfs(int u) {
    L[u][0] = R[u][0] = ord[u];
    for (int v : G[u]) if (v != fa[u]) {
        fa[v] = u, dfs(v);
        for (int i = 1; i <= k; ++ i)
            L[u][i] = min(L[u][i], L[v][i - 1]), R[u][i] = max(R[u][i], R[v][i - 1]);
    }
}
struct BIT {
    int c[100005];
    void add(int x, int d) {
        for (int i = n + 1 - x; i <= n; i += (i & ~i + 1)) c[i] += d;
    }
    int qry(int x) {
        int s = 0;
        for (int i = n + 1 - x; i; i &= i - 1) s += c[i];
        return s;
    }
} bit;
void assign(int l, int r, int x) {
    if (l > r) return;
    bit.add(x, r - l + 1);
    auto itl = prev(s.upper_bound(node{l, 0, 0})), itr = prev(s.upper_bound(node{r, 0, 0}));
    node i1 = *itl, i2 = *itr;
    if (itl != itr)
    for (auto i = next(itl); i != itr;) {
        auto nx = next(i);
        bit.add(i -> v, -(i -> r - i -> l + 1)), s.erase(i), i = nx;
    }
    if (itl == itr) {
        bit.add(i1.v, -(r - l + 1));
        s.erase(itl);
        if (i1.l < l) s.insert(node{i1.l, l - 1, i1.v});
        if (r < i1.r) s.insert(node{r + 1, i1.r, i1.v});
        s.insert(node{l, r, x});
        return;
    }
    bit.add(i1.v, -(i1.r - l + 1));
    bit.add(i2.v, -(r - i2.l + 1));
    s.erase(itl), s.erase(itr);
    if (i1.l < l) s.insert(node{i1.l, l - 1, i1.v});
    if (r < i2.r) s.insert(node{r + 1, i2.r, i2.v});
    s.insert(node{l, r, x});
}

void Main() {
    num = 0;
    n = read(), k = read(), q = read();
    for (int i = 1, u, v; i < n; ++ i)
        u = read(), v = read(), G[u].push_back(v), G[v].push_back(u);
    Q[hd = tl = 1] = vis[1] = 1;
    while (hd <= tl) {
        int u = Q[hd ++]; ord[u] = ++ num;
        for (int v : G[u]) if (!vis[v]) vis[v] = true, Q[++ tl] = v;
    }
    dfs(1);
    for (int i = 1, l, r; i <= q; ++ i)
        l = read(), r = read(), vec[r].push_back(std::make_pair(l, i));
    s.clear(), s.insert(node{1, n, 0});
    for (int i = 1; i <= n; ++ i) {
        for (int j = 0; j <= k; ++ j) assign(L[i][j], R[i][j], i);
        int u = i;
        for (int _ = 1; _ <= k && (u = fa[u]); ++ _) {
            assign(L[u][k - _], R[u][k - _], i);
            if (k - _) assign(L[u][k - _ - 1], R[u][k - _ - 1], i);
        }
        for (auto j : vec[i]) ans[j.second] = bit.qry(j.first);
    }
    for (int i = 1; i <= q; ++ i) printf("%d\n", ans[i]);
    for (int i = 1; i <= n; ++ i) {
        memset(L[i], 0x3f, sizeof L[i]), memset(R[i], 0, sizeof R[i]);
        G[i].clear(), vis[i] = bit.c[i] = fa[i] = 0, vec[i].clear();
    }
    num = 0;
}

int main() {
    memset(L, 0x3f, sizeof L);
    int _; scanf("%d", &_); while (_ --) Main();
    return 0;
}

詳細信息

Test #1:

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

input:

2
5 1 2
1 2
1 3
2 4
2 5
2 2
2 3
8 2 3
1 2
1 3
2 4
2 5
4 6
5 7
7 8
2 2
2 5
3 4

output:

4
5
7
8
6

result:

ok 5 number(s): "4 5 7 8 6"

Test #2:

score: 0
Accepted
time: 279ms
memory: 20648kb

input:

1000
500 1 500
291 2
406 9
207 13
71 15
351 17
442 18
496 19
104 20
208 23
448 34
80 42
187 44
352 45
290 46
116 47
318 50
226 52
129 55
83 59
100 60
54 61
73 65
63 66
454 67
43 71
26 74
68 26
320 75
388 76
425 79
170 81
350 83
48 85
153 86
221 90
290 95
130 96
82 98
124 82
36 99
213 100
121 101
132...

output:

255
386
356
124
315
330
437
8
335
423
398
338
180
242
352
500
145
44
342
261
92
326
38
291
259
71
137
456
171
24
162
453
283
325
250
319
478
460
77
354
56
393
372
217
395
265
188
256
134
68
205
429
436
346
300
462
324
170
291
406
207
480
198
182
489
61
476
127
289
204
282
374
114
406
488
366
121
190...

result:

ok 500000 numbers

Test #3:

score: 0
Accepted
time: 435ms
memory: 20012kb

input:

1000
500 2 500
260 2
93 3
399 4
227 5
238 6
382 7
35 12
194 24
141 26
463 27
497 30
102 31
410 32
308 34
357 42
271 43
208 44
446 46
262 50
457 51
467 52
294 53
424 54
267 56
210 58
48 59
339 48
407 65
320 66
33 68
78 33
79 71
315 72
390 74
128 76
83 81
244 87
375 91
79 93
225 94
1 97
433 1
172 100
...

output:

496
471
423
489
270
388
451
329
495
104
453
321
500
357
24
429
409
496
491
454
469
119
495
460
432
450
496
494
459
435
211
298
426
260
371
490
498
259
490
494
492
477
336
412
425
431
235
445
482
422
296
495
361
407
465
492
497
500
394
222
500
500
500
498
470
470
152
414
337
412
325
387
89
492
313
45...

result:

ok 500000 numbers

Test #4:

score: -100
Wrong Answer
time: 570ms
memory: 23084kb

input:

1000
500 3 500
333 1
268 2
183 3
394 5
420 7
322 12
87 14
285 21
178 23
82 36
106 38
28 49
364 28
30 56
110 57
211 58
486 62
456 66
337 67
222 68
175 76
15 81
489 82
79 91
376 79
274 93
417 94
302 96
457 98
142 102
486 103
13 104
186 111
128 114
35 115
184 117
167 118
156 119
429 120
414 122
84 126
...

output:

478
489
465
360
439
27
488
133
75
497
373
470
496
499
487
141
476
500
381
489
495
171
414
372
449
236
489
422
432
373
488
298
480
473
98
500
474
496
449
446
500
487
110
213
499
446
152
283
322
497
304
245
371
218
323
500
416
485
499
439
480
430
489
496
488
405
483
500
339
476
483
497
494
309
257
358...

result:

wrong answer 6th numbers differ - expected: '28', found: '27'