QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#415851#8547. Whose Land?fireinceRE 0ms13272kbC++144.2kb2024-05-21 11:32:002024-05-21 11:32:00

Judging History

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

  • [2024-05-21 11:32:00]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:13272kb
  • [2024-05-21 11:32:00]
  • 提交

answer

#include<iostream>
#include<queue>
#include<set>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<array>
#define endl '\n'
using namespace std;
using ll = long long;
using pi2 = array<int,2>;
using pi3 = array<int,3>;

const int N=1e5+500;
int n,q,K;

struct BIT{
    int t[N];
    void add(int x,int v){
        // cout<<"add "<<x<<" "<<v<<endl;
        x++;
        for(;x<=n+1;x+=x&-x)t[x]+=v;
    }
    int ask(int x){
        x++;
        int res=0;
        for(;x;x-=x&-x)res+=t[x];
        return res;
    }
} bit;

set<pi3> st;
set<pi3>::iterator split(int p){
    if(p<=0)return st.begin();
    auto it=--st.upper_bound(pi3{p,n+1,n+1});
    if(it->at(1)==p)return ++it;
    auto rg=*it;
    st.erase(it);
    st.insert({rg[0],p,rg[2]}).first;
    return st.insert({p+1,rg[1],rg[2]}).first;
}
void range_set(int l,int r,int v){
    if(l==r&&l==0)return;
    // cout<<l<<" "<<r<<" "<<v<<endl;
    auto il=split(l-1),ir=split(r);
    for(auto it=il;it!=ir;it=st.erase(it))
        bit.add(it->at(2),-(it->at(1)-it->at(0)+1));
    bit.add(v,r-l+1);
    st.insert({l,r,v});
}
int query(int l){
    return bit.ask(n)-bit.ask(l-1);
}

vector<int> G[N];
int bfn[N],bdl[N][2],dep[N],bcnt=0,dfn[N],dcnt=0,barr[N];
int fa[N],top[N],son[N],siz[N];
pi2 rg[N][40];


int lca(int a,int b){
    while(top[a]!=top[b]){
        if(dep[top[a]]<dep[top[b]])swap(a,b);
        a=fa[top[a]];
    }
    return dep[a]<dep[b]?a:b;
}
int dis(int a,int b){
    return dep[a]+dep[b]-2*dep[lca(a,b)];
}
void dfs(int u,int f){
    dep[u]=dep[f]+1;
    dfn[u]=++dcnt,fa[u]=f;
    siz[u]=1;
    son[u]=0;
    for(int v:G[u])
        if(v!=f)dfs(v,u),siz[u]+=siz[v],son[u]=siz[v]>siz[son[u]]?v:son[u];
}
void dfs2(int u,int tp){
    top[u]=tp;
    if(son[u])dfs2(son[u],tp);
    for(int v:G[u])if(v!=fa[u]&&v!=son[u])dfs2(v,v);
}

void bfs(int s){
    dep[s]=1;
    queue<int> q;
    q.push(s);
    while(q.size()){
        int u=q.front();
        q.pop();
        bfn[u]=++bcnt,barr[bfn[u]]=u,
        bdl[dep[u]][0]=min(bdl[dep[u]][0],bfn[u]),
        bdl[dep[u]][1]=max(bdl[dep[u]][1],bfn[u]);
        for(int v:G[u]){
            if(!bfn[v])q.push(v);
        }
    }
}
void findranges(int u){
    for(int d=max(1,dep[u]-K);d<=dep[u]+K;d++){
        if(bdl[d][1]==0)continue;
        int s=lower_bound(barr+bdl[d][0],barr+bdl[d][1]+1,u,[](int a,int b){return dfn[a]<dfn[b];})-barr;
        if(d<dep[u])s--;
        if(dis(s,u)>K)continue;
        
        //left lim
        {
            int l=bdl[d][0],r=s,mid;
            while(l<r)
                if(dis(barr[mid=(l+r)>>1],u)>K)l=mid+1;
                else r=mid;
            rg[u][d-dep[u]+K][0]=l;
        }
        {
            int l=s,r=bdl[d][1],mid;
            while(l<r)
                if(dis(barr[mid=(l+r+1)>>1],u)>K)r=mid-1;
                else l=mid;
            rg[u][d-dep[u]+K][1]=l;
        }
        // cout<<u<<" "<<d<<" "<<rg[u][d-dep[u]+K][0]<<" "<<rg[u][d-dep[u]+K][1]<<endl;
    }
}
void area_set(int u){
    for(int d=max(1,dep[u]-K);d<=dep[u]+K;d++){
        if(bdl[d][1]==0)continue;
        range_set(rg[u][d-dep[u]+K][0],rg[u][d-dep[u]+K][1],u);
    }
    // for(auto rg:st)cout<<"("<<rg[0]<<","<<rg[1]<<","<<rg[2]<<") ";
    // cout<<endl;
}

int ans[N];
vector<pi2> qs[N];
void solve(){
    cin>>n>>K>>q;
    for(int i=1;i<=n;i++)bdl[i][0]=n+1,bdl[i][1]=0,bfn[i]=0;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        G[u].push_back(v),G[v].push_back(u);
    }
    for(int i=1;i<=q;i++){
        int l,r;
        cin>>l>>r;
        qs[r].push_back({l,i});
    }

    dfs(1,0),dfs2(1,1),bfs(1);
    // for(int i=1;i<=n;i++)cout<<bdl[i][0]<<" "<<bdl[i][1]<<endl;
    for(int i=1;i<=n;i++)findranges(i);

    st.insert({1,n,n+1});
    
    for(int i=1;i<=n;i++){
        area_set(i);
        for(auto p:qs[i])ans[p[1]]=query(p[0]);
    }
    for(int i=1;i<=q;i++)cout<<ans[i]<<endl;

    st.clear(),bcnt=dcnt=0;
    for(int i=1;i<=n;i++)G[i].clear(),bit.t[i]=0;
}

signed main(){
    // freopen("a.in","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}

详细

Test #1:

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

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
Runtime Error

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:

2
3
4
0
3
2
5
0
4
4
2
2
2
2
1
6
1
0
2
1
0
1
0
2
2
0
1
6
2
0
1
5
1
4
1
2
6
3
2
2
0
3
2
1
4
2
1
4
2
0
2
3
3
3
2
3
4
2
2
4
1
6
2
1
5
0
5
0
1
1
2
4
1
3
6
4
2
2
1
2
2
2
1
1
4
2
0
2
5
4
4
5
0
3
5
1
0
0
2
0
2
3
5
3
3
2
1
2
5
5
6
0
1
1
0
5
1
2
2
0
5
5
3
5
3
1
3
6
5
0
1
2
2
3
3
2
0
3
3
6
3
2
3
0
5
3
5
3
2
0
...

result: