QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#608230#8547. Whose Land?WaterSunWA 1323ms36568kbC++142.2kb2024-10-03 20:01:392024-10-03 20:01:43

Judging History

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

  • [2024-10-03 20:01:43]
  • 评测
  • 测评结果:WA
  • 用时:1323ms
  • 内存:36568kb
  • [2024-10-03 20:01:39]
  • 提交

answer

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=5e5,Maxk=1e5;

int n,m,q,bfn[Maxn+5],h[Maxn+5],ans[Maxn+5],cur;
int L[Maxk+5][22],R[Maxk+5][22],fa[Maxn+5];
vector<int> v[Maxn+5];
vector<pair<int,int>> qr[Maxn+5];
struct Node{int l,r,k;};
inline bool operator<(Node a,Node b) {return a.l<b.l;}
set<Node> st;

inline auto Split(int x)
{
    if(x>n) return st.end(); auto it=--st.upper_bound(Node{x,0,0});
    if(it->l==x) return it; auto [l,r,k]=*it; st.erase(it);
    st.insert(Node{l,x-1,k}); return st.insert(Node{x,r,k}).first;
}
inline void Assign(int l,int r,int k)
{
    auto ir=Split(r+1),il=Split(l);
    st.erase(il,ir); st.insert(Node{l,r,k});
}
inline int Query(int k){
    int ans = 0;
    for (auto it = st.begin();it != st.end();it++){
        if ((it -> k) >= k) ans += ((it -> r) - (it -> l) + 1);
    }
    return ans;
}
inline void bfs()
{
    For(i,1,n) bfn[i]=0; cur=0;
    queue<int> q; q.push(1),bfn[1]=++cur;
    while(!q.empty())
    {
        int x=q.front(); q.pop();
        for(auto y:v[x]) if(!bfn[y]) bfn[y]=++cur,q.push(y);
    }
}
inline void dfs(int x,int fa)
{
    For(i,1,m) L[x][i]=n+1,R[x][i]=0; ::fa[x]=fa,L[x][0]=R[x][0]=bfn[x]; for(auto y:v[x]) if(y!=fa)
        {dfs(y,x); For(i,1,m) L[x][i]=min(L[x][i],L[y][i-1]),R[x][i]=max(R[x][i],R[y][i-1]);}
}
inline void Solve()
{
    cin>>n>>m>>q;
    For(i,1,n) vector<int>().swap(v[i]);
    For(i,1,n) vector<pair<int,int>>().swap(qr[i]);
    For(i,1,n-1)
    {
        int x,y; cin>>x>>y;
        v[x].push_back(y),v[y].push_back(x);
    }
    For(i,1,q) {int l,r; cin>>l>>r; qr[r].emplace_back(l,i);}
    bfs(),dfs(1,0),st.clear(),st.insert(Node{1,n,0});
    For(i,1,n)
    {
        for(int x=i,k=m;k>=0 && x;k--,x=fa[x])
        {
            int l=L[x][k],r=R[x][k]; if(l<=r) Assign(l,r,i);
            if(k) {l=L[x][k-1],r=R[x][k-1]; if(l<=r) Assign(l,r,i);}
        } for(auto [l,id]:qr[i]) ans[id]=Query(l);
    }
    For(i,1,q) cout<<ans[i]<<'\n';
}

int main()
{
    // freopen("1.in","r",stdin);

    ios::sync_with_stdio(false);
    int T; cin>>T;
    while(T--) Solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 35688kb

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: 1323ms
memory: 35340kb

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: 1092ms
memory: 36568kb

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'