蒟蒻初学 淀粉质,有点自闭

P3806 【模板】点分治 1

Provicy @ 2020-01-15 10:06:05

#include <bits/stdc++.h>
#define ri register
#define inf 0x3f3f3f3f
using namespace std; const int N=20010;
inline int read()
{
    int s=0, w=1; ri char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') w=-1; ch=getchar(); }
    while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar(); return s*w;
}
int n,m,size[N],dis[N],book[N],ans,root,maxx,K,cnt=1;
int head[N],maxE; struct Edge{int nxt,to,rdis;}e[N];
inline void Add(int u,int v,int w) {e[++maxE].nxt=head[u]; head[u]=maxE; e[maxE].to=v; e[maxE].rdis=w; }
void FindRoot(int x,int fa,int S)
{
    int cs=-inf; size[x]=1;
    for(int i=head[x];i;i=e[i].nxt)
    {
        if(e[i].to==fa||book[e[i].to]) continue;
        FindRoot(e[i].to,x,S);
        size[x]+=size[e[i].to];
        cs=max(cs,size[e[i].to]);
    }
    cs=max(cs,S-size[x]);
    if(cs<maxx) maxx=cs, root=x;
}
void Dis(int x,int fa,int val)
{
    for(int i=head[x];i;i=e[i].nxt)
    {
        if(e[i].to==fa||book[e[i].to]) continue;
        dis[++cnt]=val+e[i].rdis;
        Dis(e[i].to,x,val+e[i].rdis);
    }
}
inline int Get_L(int l,int val)
{
    int r=cnt, rss=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(dis[mid]<=val) rss=mid, l=mid+1;
        else r=mid-1;
    }
    return rss;
}
inline int Get_R(int l,int val)
{
    int r=cnt, rss=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(dis[mid]<val) l=mid+1;
        else rss=mid, r=mid-1;
    }
    return rss;
 } 
int Ans(int x,int val)
{
    cnt=1; dis[cnt]=val;
    Dis(root,0,val);
    sort(dis+1,dis+1+cnt);
    int p=1, res=0;
    while(p<cnt&&dis[cnt]+dis[p]<K) p++;
    while(p<cnt&&(dis[p]<<1)<=K)
    {
        int L=Get_R(p+1,K-dis[p]), R=Get_L(p+1,K-dis[p]);
        if(R>=L) res+=R-L+1; p++;
    }
    return res;
}
void Solve(int x)
{
    book[x]=1; ans+=Ans(x,0); printf("%d\n",ans);
    for(int i=head[x];i;i=e[i].nxt)
    {
        if(book[e[i].to]) continue;
        ans-=Ans(e[i].to,e[i].rdis);
        printf("%d\n",ans);
        root=0; maxx=inf;
        FindRoot(e[i].to,x,size[e[i].to]);
        Solve(root);
    }
}
int main()
{
    n=read(), m=read();
    for(ri int i=1;i<n;i++)
    {
        int u,v,w; u=read(), v=read(), w=read();
        Add(u,v,w), Add(v,u,w);
    }
    for(ri int i=1;i<=m;i++)
    {
        K=read(); ans=root=0; maxx=inf;
        FindRoot(1,0,n); Solve(root);
        memset(book,0,sizeof(book));
        ans?puts("AYE"):puts("NAY");
    }
    return 0;
}

40 分 WA 了,第一个点都没过,自闭了qaq


by Provicy @ 2020-01-15 12:29:59

@土田共戈 看的x义x板子/kk


上一页 |