60pts TLE,线段树合并板子题求调
查看原帖
60pts TLE,线段树合并板子题求调
308729
sheeplittlecloud楼主2023/9/9 11:04
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,q;
const int N=4e5+7;
inline int read() 
{
	register int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') 
	{
		if(ch=='-') w=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') 
	{
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*w;
}

struct node
{
    int to,nxt;
}e[N*4];
int head[N*4],cnt1;
void add(int u,int v)
{
    e[++cnt1].to=v;
    e[cnt1].nxt=head[u];
    head[u]=cnt1;
}
int dfn[N],id[N],top[N],dep[N],fa[N],siz[N],son[N],cnt;
void dfs1(int x)
{
    dep[x]=dep[fa[x]]+1;
    siz[x]=1;
    for(int i=head[x];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa[x]) continue;
        fa[v]=x;
        dfs1(v);
        siz[x]+=siz[v];
        if(!son[x]||siz[son[x]]<siz[v])
            son[x]=v;
    }
}
void dfs2(int x,int d)
{
    top[x]=d;
    dfn[x]=++cnt;
    id[dfn[x]]=x;
    if(son[x]) dfs2(son[x],d);
    for(int i=head[x];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v!=fa[x]&&v!=son[x])
            dfs2(v,v);
    }
}
int LCA(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    return x;
}
struct treee
{
    int l,r,w,col;
}t[N*4];
int tot;
void pushup(int x)
{
    if(t[t[x].l].w>=t[t[x].r].w) {t[x].col=t[t[x].l].col;t[x].w=t[t[x].l].w;}
    else {t[x].col=t[t[x].r].col;t[x].w=t[t[x].r].w;}
}
int change(int x,int l,int r,int k,int val)
{
    if(!x) x=++tot;
    if(l==r)
    {
        t[x].w+=val;
        t[x].col=l;
        return x;
    }
    int mid=(l+r)/2;
    if(k<=mid) t[x].l=change(t[x].l,l,mid,k,val);
    if(k>mid) t[x].r=change(t[x].r,mid+1,r,k,val);
    pushup(x);
    return x;
}
int merge(int x,int y,int l,int r)
{
    if(!x) return y;
    if(!y) return x;
    if(l==r)
    {
        t[x].w+=t[y].w;
        t[x].col=l;//color
        return x;
    }
    int mid=(l+r)/2;
    t[x].l=merge(t[x].l,t[y].l,l,mid);
    t[x].r=merge(t[x].r,t[y].r,mid+1,r);
    pushup(x);
    return x;
}
int m;
struct qust
{
    int x,y,z;
}qus[N];
int rot[N];
int ans[N];
void work(int x)
{
    for(int i=head[x];i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(v==fa[x]) continue;
        work(v);
        rot[x]=merge(rot[x],rot[v],1,m);
    }
    if(t[rot[x]].w>0) 
        ans[x]=t[rot[x]].col;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    n=read();q=read();
    for(int i=1;i<n;i++)
    {
        int a=read(),b=read();
        add(a,b);
        add(b,a);
    }
    dfs1(1);
    dfs2(1,1);
    for(int i=1;i<=q;i++)
    {
        qus[i].x=read();qus[i].y=read();qus[i].z=read();
        m=max(m,qus[i].z);
    }
    for(int i=1;i<=q;i++)
    {
        int lca=LCA(qus[i].x,qus[i].y);
        int x=qus[i].x,y=qus[i].y,z=qus[i].z;
        rot[x]=change(rot[x],1,m,z,1);
        rot[y]=change(rot[y],1,m,z,1);
        rot[lca]=change(rot[lca],1,m,z,-1);
        if(fa[lca]) rot[fa[lca]]=change(rot[fa[lca]],1,m,z,-1);
    }
    work(1);
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<"\n";
    return 0;
}
2023/9/9 11:04
加载中...