QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#415941 | #8547. Whose Land? | fireince | WA | 0ms | 8232kb | C++14 | 4.6kb | 2024-05-21 12:55:33 | 2024-05-21 12:55:33 |
Judging History
answer
#include<iostream>
#include<cassert>
#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;
void split(int p){
if(p<=0)return;
auto it=--st.upper_bound(pi3{p,n+1,n+1});
if(it->at(1)==p)return;
auto rg=*it;
st.erase(it);
st.insert({rg[0],p,rg[2]}).first;
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;
split(l-1),split(r);
auto il=st.lower_bound({l,l,0}),ir=st.lower_bound({r+1,r+1,0});
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(s>bdl[d][1])s--;
if(s>bdl[d][0]&&dis(barr[s-1],u)<dis(barr[s],u))s--;
if(dis(barr[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];
/*
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];
*/
void solve(){
cin>>n>>K>>q;
st.clear(),bcnt=dcnt=0;
for(int i=0;i<=n+1;i++)G[i].clear(),bit.t[i]=0;
for(int i=0;i<=n;i++)bdl[i][0]=n+1,bdl[i][1]=0,bfn[i]=0;
for(int i=0;i<=n;i++)qs[i].clear();
for(int i=0;i<=n;i++)
for(int d=0;d<=K*2;d++)rg[i][d][0]=rg[i][d][1]=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;
}
signed main(){
freopen("p.in","r",stdin);
// freopen("a.out","w",stdout);
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
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: 0ms
memory: 8232kb
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:
result:
wrong answer Answer contains longer sequence [length = 5], but output contains 0 elements