QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#618379#8547. Whose Land?ucup-team3651WA 0ms20064kbC++203.4kb2024-10-06 21:36:152024-10-06 21:36:16

Judging History

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

  • [2024-10-06 21:36:16]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:20064kb
  • [2024-10-06 21:36:15]
  • 提交

answer


#include<bits/stdc++.h>

#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array
#define IL inline

#define mod 998244353
#define N 1000005

using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x*f;
}
void write(int x){
    if(x<0)x=-x,putchar('-');
    if(x/10)write(x/10);
    putchar(x%10+'0');
}

int d[N],dfn[N],tot,L[N][21],R[N][21],fat[N];
vi e[N];
vector<pi> Q[N];
int n,k,q,res[N],id[N];
void dfs(int x,int fa){
    dfn[x]=++tot;d[x]=d[fa]+1;fat[x]=fa;
    for(auto v:e[x])if(v!=fa)dfs(v,x);
}
void dfs2(int x,int fa){
    L[x][0]=R[x][0]=dfn[x];
    for(int i=1;i<=20;i++)L[x][i]=n+1,R[x][i]=0;
    for(auto v:e[x]){
        if(v==fa)continue;
        dfs2(v,x);
        for(int i=1;i<=20;i++)L[x][i]=min(L[x][i],L[v][i-1]),R[x][i]=max(R[x][i],R[v][i-1]);
    }
}

struct BIT{
    int K[N];
    IL int lowbit(int x){return x&(-x);}
    IL void add(int x,int val){while(x<N){K[x]+=val;x+=lowbit(x);}}
    IL int ask(int x){int ans=0;while(x){ans+=K[x];x-=lowbit(x);}return ans;}
}T;

struct Node{
    int l,r,v;
    Node(int l=0,int r=0,int v=0):l(l),r(r),v(v){}
    bool operator <(const Node &x)const{
        return (l==x.l?r<x.r:l<x.l);
    }
};
set<Node> t;
int cnt0;
int chg(int l,int r,int val){
    if(l>r)return 0;
    
    auto itl=t.lower_bound(Node(l+1,0,0)),itr=t.lower_bound(Node(r+1,0,0));
    itl--;itr--;Node infol=*itl,infor=*itr;itr++;
    for(auto it=itl;it!=itr;it++){
        auto [L,R,z]=*it;
        int lth=min(r,R)-max(L,l)+1;
        if(z==0)cnt0-=lth;
        else T.add(z,-lth);
    }
    t.erase(itl,itr);t.insert(Node(l,r,val));
    if(infol.l<l)t.insert(Node(infol.l,l-1,infol.v));
    if(infor.r>r)t.insert(Node(r+1,infor.r,infor.v));
    return r-l+1;
}

void solve(){
    n=read(),k=read(),q=read();
    for(int i=1;i<=n;i++)e[i].clear(),Q[i].clear(),tot=0;
    for(int i=1;i<n;i++){
        int u=read(),v=read();
        e[u].pb(v);e[v].pb(u);
    }
    dfs(1,0);
    for(int i=1;i<=n;i++)id[i]=i;
    sort(id+1,id+n+1,[](int x,int y){return d[x]==d[y]?dfn[x]<dfn[y]:d[x]<d[y];});
    dfs2(1,0);
    t.insert(Node(1,n,0));cnt0=n;
    for(int i=1;i<=q;i++){
        int l=read(),r=read();Q[r].pb(mp(l,i));
    }
    for(int i=1;i<=n;i++){
        auto add=[&](int x){
            int now=k,siz=0;
            while(x && now!=-1){
                int D=max((x==1?0:now-1),0);
                for(int j=D;j<=now;j++)siz+=chg(L[x][j],R[x][j],i);
                now--;x=fat[x];
            }
            T.add(i,siz);
        };
        add(i);
        for(auto [l,id]:Q[i])res[id]=n-T.ask(l-1)-cnt0;
    }
    
    for(auto [l,r,val]:t)if(val)T.add(val,-(r-l+1));
    for(int i=1;i<=q;i++)write(res[i]),putchar('\n');
    t.clear();
}

int main(){
    #ifdef EAST_CLOUD
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    #endif
    
    int T=read();while(T--)solve();

}
//cerr<<fabs(&Med-&Mbe)/1048576.0<<"MB\n";
//cerr<<"\n"<<clock()*1.0/CLOCKS_PER_SEC*1000<<"ms\n";

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 20064kb

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
8
8
8

result:

wrong answer 3rd numbers differ - expected: '7', found: '8'