QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#405672#8547. Whose Land?LiuxizaiWA 357ms42524kbC++174.2kb2024-05-06 12:02:492024-05-06 12:02:50

Judging History

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

  • [2024-05-06 12:02:50]
  • 评测
  • 测评结果:WA
  • 用时:357ms
  • 内存:42524kb
  • [2024-05-06 12:02:49]
  • 提交

answer

#include <bits/stdc++.h>
#define File(name) freopen(#name".in", "r", stdin); freopen(#name".out", "w", stdout);
using namespace std;
using ll = long long;
using ull = unsigned long long;
template<typename T>
inline T read(){
    T n = 0; int f = 1; char ch = getchar();
    while(!isdigit(ch)){
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(isdigit(ch)){
        n = n * 10 + ch - '0';
        ch = getchar();
    }
    return f * n;
}
template<typename T>
void write(T n){
    if(n < 0) return putchar('-'), write(-n);
    if(n / 10) write(n / 10);
    putchar(n % 10 + '0');
}
void input() {}
template<typename Type, typename... Types>
void input(Type &arg, Types &...args){
    arg = read<Type>();
    input(args...);
}
namespace Main{
    const int N = 500005, K = 25;
    int t, n, k, q, bfn[N], bfnCnt, fa[N], L[K][N], R[K][N], ans[N];
    vector<int> g[N];
    vector<pair<int, int>> ask[N];
    struct bit{
        int t[N];
        void clear(int n) { fill(t + 1, t + n + 1, 0); }
        int lowbit(int x) { return x & -x; }
        void add(int x, int k) { for(; x < N; x += lowbit(x)) t[x] += k; }
        int query(int x) { int res = 0; for(; x; x -= lowbit(x)) res += t[x]; return res; }
    }tree;
    struct odt_node{
        int l, r, v;
        odt_node() = default;
        odt_node(int l, int r, int v): l(l), r(r), v(v) {}
        bool operator<(const odt_node &b) const& {
            return l < b.l;
        }
    };
    set<odt_node> odt;
    set<odt_node>::iterator split(int x){
        auto it = odt.lower_bound({x, 0, 0});
        if(it == odt.end() || it->l == x) return it;
        it--;
        auto [l, r, v] = *it;
        odt.erase(it);
        odt.emplace(l, x - 1, v);
        return odt.emplace(x, r, v).first;
    }
    void cover(int l, int r, int v){
        auto itr = split(r + 1), itl = split(l);
        for(auto it = itl; it != itr; it++){
            tree.add(it->v, -(it->r - it->l + 1));
        }
        odt.erase(itl, itr);
        odt.emplace(l, r, v);
        tree.add(v, r - l + 1);
    }
    void bfs(){
        queue<int> q;
        q.push(1);
        bfn[1] = ++bfnCnt;
        while(!q.empty()){
            int u = q.front(); q.pop();
            for(int v: g[u]){
                if(!bfn[v]){
                    bfn[v] = ++bfnCnt;
                    q.push(v);
                }
            }
        }
    }
    void dfs(int u, int f){
        fa[u] = f;
        L[0][u] = R[0][u] = bfn[u];
        for(int i = 1; i <= k; i++) L[i][u] = R[i][u] = 0;
        for(int v: g[u]){
            if(v == f) continue;
            dfs(v, u);
            for(int i = 1; i <= k; i++) if(!L[i][u]) L[i][u] = L[i - 1][v];
            for(int i = 1; i <= k; i++) if(R[i - 1][v]) R[i][u] = R[i - 1][v];
        }
    }
    void cover_dep(int u, int d, int v){
        if(d < 0 || !L[d][u]) return;
        cover(L[d][u], R[d][u], v);
    }
    void Main(){
        input(t);
        while(t--){
            input(n, k, q);
            for(int i = 1; i <= n; i++) g[i].clear(), ask[i].clear();
            for(int i = 1, u, v; i < n; i++){
                input(u, v);
                g[u].push_back(v), g[v].push_back(u);
            }
            for(int i = 0, l, r; i < q; i++){
                input(l, r);
                ask[l].emplace_back(r, i);
            }
            bfnCnt = 0, fill(bfn + 1, bfn + n + 1, 0);
            bfs();
            dfs(1, 0);
            odt = {{1, n, n + 1}};
            tree.clear(n);
            for(int i = n; i >= 1; i--){
                int u = i, d = k;
                while(u && d >= 0){
                    cover_dep(u, d, i), cover_dep(u, d - 1, i);
                    u = fa[u], d--;
                }
                while(d >= 0) cover_dep(1, d--, i);
                for(auto [r, id]: ask[i]) ans[id] = tree.query(r);
            }
            for(int i = 0; i < q; i++) write(ans[i]), puts("");
        }
        return;
    }
} // namespace Main
int main(){
#ifdef Liuxizai
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif // Liuxizai
    Main::Main();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 357ms
memory: 40540kb

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
399
338
180
242
352
500
145
44
342
261
92
326
38
292
259
71
137
456
171
24
162
453
283
325
250
320
478
460
77
354
56
394
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:

wrong answer 11th numbers differ - expected: '398', found: '399'