QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#608246#8547. Whose Land?WaterSunWA 1370ms36032kbC++143.7kb2024-10-03 20:08:162024-10-03 20:08:17

Judging History

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

  • [2024-10-03 20:08:17]
  • 评测
  • 测评结果:WA
  • 用时:1370ms
  • 内存:36032kb
  • [2024-10-03 20:08:16]
  • 提交

answer

#include <bits/stdc++.h>
#define re register
#define fst first
#define snd second
#define chmin(a,b) (a = min(a,b))
#define chmax(a,b) (a = max(a,b))

using namespace std;

const int N = 5e5 + 10,M = 24;
const int inf = 1e9 + 10;
int n,k,q,ans[N];
int L[N][M],R[N][M];
int tim,f[N],dfn[N];
vector<int> g[N];

struct Query{
    int l,id;
};
vector<Query> Q[N];

inline int read(){
    int r = 0,w = 1;
    char c = getchar();
    while (c < '0' || c > '9'){
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9'){
        r = (r << 3) + (r << 1) + (c ^ 48);
        c = getchar();
    }
    return r * w;
}

struct{
    struct node{
        int l,r;
        mutable int val;

        bool friend operator <(const node &a,const node &b){
            return a.l < b.l;
        }
    };
    set<node> st;

    inline void init(){
        st.clear(); st.insert({1,n,0});
    }

    inline auto split(int x){
        if (x > n) return st.end();
        auto it = prev(st.upper_bound({x,-1,-1}));
        int l = (it -> l),r = (it -> r),val = (it -> val);
        if (l == x) return it;
        st.erase(it); st.insert({l,x - 1,val});
        return st.insert({x,r,val}).fst;
    }

    inline void assign(int l,int r,int k){
        // cerr << l << " " << r << " assign\n";
        if (l <= 0 || r > n) return;
        auto itr = split(r + 1),itl = split(l);
        st.erase(itl,itr);
        st.insert({l,r,k});
    }

    inline int query(int k){
        int ans = 0;
        for (auto it = st.begin();it != st.end();it++){
            // cerr << (it -> l) << " " << (it -> r) << " " << pid[it -> l] << " " << pid[it -> r] << " " << (it -> val) << " ????\n";
            if ((it -> val) >= k) ans += ((it -> r) - (it -> l) + 1);
        }
        return ans;
    }
}T;

inline void bfs(int s){
    queue<int> q; q.push(s);
    while (!q.empty()){
        int u = q.front(); q.pop();
        dfn[u] = ++tim;
        for (int v:g[u]){
            if (!dfn[v]) q.push(v);
        }
    }
}

inline void dfs(int u,int fa){
    f[u] = fa;
    L[u][0] = R[u][0] = dfn[u];
    for (re int i = 1;i <= k;i++){
        L[u][i] = inf; R[u][i] = -inf;
    }
    for (int v:g[u]){
        if (v == fa) continue;
        dfs(v,u);
        for (re int i = 1;i <= k;i++){
            chmin(L[u][i],L[v][i - 1]);
            chmax(R[u][i],R[v][i - 1]);
        }
    }
}

inline void solve(){
    n = read(),k = read(),q = read();
    tim = 0; T.init();
    fill(dfn + 1,dfn + n + 1,0);
    for (re int i = 1;i <= n;i++){
        g[i].clear(); Q[i].clear();
    }
    for (re int i = 1,a,b;i < n;i++){
        a = read(),b = read();
        g[a].push_back(b); g[b].push_back(a);
    }
    for (re int i = 1,l,r;i <= q;i++){
        l = read(),r = read();
        Q[r].push_back({l,i});
    }
    bfs(1); dfs(1,0);
    // for (re int u = 1;u <= n;u++){
    //     cerr << u << " " << dfn[u] << " begin:\n";
    //     for (re int i = 0;i <= k;i++) cerr << i << " " << L[u][i] << " " << R[u][i] << " !!!\n";
    // }
    for (re int i = 1;i <= n;i++){
        for (re int u = i,tot = k;~tot && u;tot--,u = f[u]){
            int l = L[u][tot],r = R[u][tot];
            // cerr << l << " " << r << " " << tot << " sbsb\n";
            if (l <= r) T.assign(l,r,i);
            if (tot){
                l = L[u][tot - 1],r = R[u][tot - 1];
                if (l <= r) T.assign(l,r,i);
            }
        }
        for (Query p:Q[i]) ans[p.id] = T.query(p.l);
        // cerr << "===========\n";
    }
    for (re int i = 1;i <= q;i++) printf("%d\n",ans[i]);
}

int main(){
    int T; T = read();
    while (T--) solve();
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 6ms
memory: 36032kb

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: 1370ms
memory: 35672kb

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

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:

wrong answer 10076th numbers differ - expected: '240', found: '239'