QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#608239#8547. Whose Land?WaterSunWA 3ms35568kbC++142.2kb2024-10-03 20:05:142024-10-03 20:05:15

Judging History

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

  • [2024-10-03 20:05:15]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:35568kb
  • [2024-10-03 20:05:14]
  • 提交

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);
    while(!q.empty())
    {
        int x=q.front(); q.pop();
        bfn[x]=++cur;
        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) v[i].clear();
    For(i,1,n) qr[i].clear();
    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,1),st.clear(),st.insert(Node{1,n,0});
    For(i,1,n)
    {
        for(int x=i,k=m;k>=0;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: 0
Wrong Answer
time: 3ms
memory: 35568kb

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:

6
7
8
8
8

result:

wrong answer 1st numbers differ - expected: '4', found: '6'