QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#401127#8547. Whose Land?ocharinWA 298ms3756kbC++233.5kb2024-04-28 00:18:382024-04-28 00:18:39

Judging History

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

  • [2024-04-28 00:18:39]
  • 评测
  • 测评结果:WA
  • 用时:298ms
  • 内存:3756kb
  • [2024-04-28 00:18:38]
  • 提交

answer

/*

                                _/                                      _/
       _/_/        _/_/_/      _/_/_/        _/_/_/      _/  _/_/               _/_/_/
    _/    _/    _/            _/    _/    _/    _/      _/_/          _/       _/    _/
   _/    _/    _/            _/    _/    _/    _/      _/            _/       _/    _/
    _/_/        _/_/_/      _/    _/      _/_/_/      _/            _/       _/    _/

*/
#include<bits/stdc++.h>
#define int long long

using namespace std;
const int inf=1e18;

struct FenwickTree{
    int sz;
    vector<int>c;
    FenwickTree(int n=0){sz=n,c.assign(sz+1,0ll);}
    void init(int n=0){sz=n,c.assign(sz+1,0ll);}
    int lowbit(int x){return -x&x;}
    void add(int u,int k){
        for(;u;u-=lowbit(u)) c[u]+=k;
    }
    int query(int u){
        int ans=0;
        for(;u<=sz;u+=lowbit(u)) ans+=c[u];
        return ans;
    }
}ft;

struct SegmentTree{
    vector<int>s;
    vector<int>tag;
    SegmentTree(int _n){s.assign(_n+1<<2,0ll),tag.assign(_n+1<<2,0ll);};
    void pushtag(int u,int cl,int cr,int k){
        if(k) s[u]=tag[u]=k;
    }
    void pushdown(int u,int cl,int cr){
        int mid=cl+cr>>1;
        pushtag(u<<1,cl,mid,tag[u]);
        pushtag(u<<1|1,mid+1,cr,tag[u]);
        tag[u]=0;
    }
    void modify(int u,int cl,int cr,int l,int r,int k){
        if(r<cl || cr<l) return;
        if(l<=cl && cr<=r){
            int x=cr-cl+1;
            ft.add(s[u],-x);
            pushtag(u,cl,cr,k);
            ft.add(s[u],x);
            return;
        }
        pushdown(u,cl,cr);
        int mid=cl+cr>>1;
        modify(u<<1,cl,mid,l,r,k);
        modify(u<<1|1,mid+1,cr,l,r,k);
    }
};

void solve(){
    int n,k,m;
    cin>>n>>k>>m;
    vector<vector<int>>e(n+1);
    for(int i=1;i<n;++i){
        int u,v;
        cin>>u>>v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    vector<int>bfn(n+1),fa(n+1),p(n+1);
    vector l(n+1,vector(k+1,n+1)),r(n+1,vector(k+1,0ll));
    queue<int>q;
    q.push(1);
    int tot=0;
    bfn[++tot]=1;
    while(!q.empty()){
        auto u=q.front();q.pop();
        for(auto v:e[u]){
            if(v==fa[u]) continue;
            fa[v]=u;
            bfn[++tot]=v;
            p[v]=tot;
            q.push(v);
        }
    }
    for(int i=n;i;--i){
        int u=i;
        int pos=p[u];
        for(int j=0;j<=k;++j){
            l[u][j]=min(l[u][j],pos);
            r[u][j]=max(r[u][j],pos);
            u=fa[u];
            if(!u) break;
        }
    }
    vector<vector<pair<int,int>>>Q(n+1);
    for(int i=0;i<m;++i){
        int l,r;
        cin>>l>>r;
        Q[r].push_back({l,i});
    }
    vector<int>res(m);
    ft.init(n);
    SegmentTree seg(n);
    for(int i=1;i<=n;++i){
        vector<int>cl(2*k+1,n+1),cr(2*k+1,0ll);
        int u=i;
        for(int x=0;x<=k;++x){
            for(int y=0;x+y<=k;++y){
                cl[k+x-y]=min(cl[k+x-y],l[u][y]);
                cr[k+x-y]=max(cr[k+x-y],r[u][y]);
            }
            u=fa[u];
            if(!u) break;
        }
        for(int j=0;j<=2*k;++j){
            if(cl[j]>cr[j]) continue;
            // cerr<<i<<" "<<j<<" "<<cl[j]<<" "<<cr[j]<<endl;
            seg.modify(1,1,n,cl[j],cr[j],i);
        }
        for(auto [l,id]:Q[i]) res[id]=ft.query(l);
    }
    for(auto i:res) cout<<i+1<<"\n";
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;cin>>T;
    while(T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3532kb

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: 298ms
memory: 3756kb

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:

264
408
371
125
332
344
475
9
349
459
418
353
187
250
371
584
147
45
356
271
95
338
39
306
268
72
140
502
174
25
164
494
293
341
261
340
539
499
78
369
57
422
388
225
422
273
194
267
137
71
209
462
469
365
312
501
341
175
303
436
216
542
202
188
564
62
529
129
303
208
293
397
116
431
554
385
124
197...

result:

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