QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#752236#8547. Whose Land?ucup-team3215WA 718ms4080kbC++233.5kb2024-11-15 23:14:222024-11-15 23:14:23

Judging History

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

  • [2024-11-15 23:14:23]
  • 评测
  • 测评结果:WA
  • 用时:718ms
  • 内存:4080kb
  • [2024-11-15 23:14:22]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
constexpr int INF = 1e9 + 7;
constexpr int K = 21;

struct Fenwick {
    int n;
    vector<int> f;

    Fenwick(int n) :n(n), f(n) {}

    void upd(int i, int x) {
        for (; i < n; i |= i + 1)f[i] += x;
    }

    int get(int i) {
        int res = 0;
        for (; ~i; i &= i + 1, --i)res += f[i];
        return res;
    }

    int get(int l, int r) {
        return get(r) - get(l - 1);
    }
};

void solve() {
    int n, k, q;
    cin >> n >> k >> q;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        --u, --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int> pos(n);
    {
        vector<char> used(n);
        queue<int> q;
        q.push(0);
        used[0] = 1;
        int cur = 0;
        while (q.size()) {
            int v = q.front();
            pos[v] = cur++;
            q.pop();
            for (auto to: g[v]) {
                if (!used[to]) {
                    used[to] = 1;
                    q.push(to);
                }
            }
        }
    }
    vector<int> par(n);
    vector<array<array<int, 2>, K>> dp(n);
    auto dfs = [&](auto &&dfs, int v, int p) -> void {
        for (int i = 0; i < K; ++i)dp[v][i] = {INF, -INF};
        dp[v][0] = {pos[v], pos[v]};
        for (auto to: g[v]) {
            if (to == p)continue;
            par[to] = v;
            dfs(dfs, to, v);
            for (int i = 1; i < K; ++i) {
                dp[v][i][0] = min(dp[v][i][0], dp[to][i - 1][0]);
                dp[v][i][1] = max(dp[v][i][1], dp[to][i - 1][1]);
            }
        }
    };
    dfs(dfs, 0, -1);
    vector<vector<array<int, 2>>> Q(n);
    vector<int> res(q);
    for (int i = 0; i < q; ++i) {
        int l, r;
        cin >> l >> r;
        --l, --r;
        Q[r].push_back({l, i});
    }
    Fenwick f(n);
    set<array<int, 3>> s;
    auto add = [&](array<int, 3> r) {
        if (~r[2])f.upd(r[2], r[1] - r[0]);
        s.insert(r);
    };
    auto del = [&](array<int, 3> r) {
        if (~r[2])f.upd(r[2], -(r[1] - r[0]));
        s.erase(r);
    };
    auto erase = [&](array<int, 2> r) {
        while (1) {
            auto it = s.lower_bound({r[0], -1});
            if (it == s.end())break;
            auto v = *it;
            if (v[1] <= r[1]) {
                del(v);
            } else {
                del(v);
                v[0] = r[1];
                add(v);
                break;
            }
        }
        auto it = s.lower_bound({r[0], -1});
        if (it != s.begin()) {
            --it;
            auto v = *it;
            del(v);
            v[1] = r[0];
            add(v);
        }
    };
    auto insert = [&](array<int, 3> r) {
        if(r[0] > r[1])return;
        erase({r[0], r[1]});
        add(r);
    };
    insert({0, n, -1});
    for (int r = 0; r < n; ++r) {
        int cur = r;
        for (int i = 0; i <= k; ++i) {
            int need = k - i;
            insert({dp[cur][need][0], dp[cur][need][1] + 1, r});
            if (need)insert({dp[cur][need - 1][0], dp[cur][need - 1][1] + 1, r});
            cur = par[cur];
        }
        for(auto [l,id] : Q[r]){
            res[id] = f.get(l,r);
        }
    }
    for (auto x: res)cout << x << "\n";
}

int main() {
    cin.tie(0)->sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)solve();
}

详细

Test #1:

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

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: -100
Wrong Answer
time: 718ms
memory: 4080kb

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:

307
387
416
126
342
328
446
8
397
437
396
336
223
238
363
500
147
45
342
270
92
324
63
291
311
84
173
475
191
66
162
466
288
480
264
319
500
460
99
352
375
391
370
226
408
263
188
398
132
68
201
430
437
374
304
462
493
270
296
414
220
497
213
182
488
61
486
129
302
206
278
384
114
433
496
498
153
34...

result:

wrong answer 1st numbers differ - expected: '255', found: '307'