洛谷 - P4556 雨天的尾巴 (树上差分)


传送门:洛谷 - P4556

思路分析

利用树上差分的性质,将路径修改单点查询转化为单点修改子树查询

  1. u++
  2. v++
  3. lca–
  4. fa[lca]–

AC代码


#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))

namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc() {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    template<class T> inline bool read(T &x) {
        bool sign=0;
        char ch=nc();
        x=0;
        for(; blank(ch); ch=nc());
        if(IOerror)return false;
        if(ch=='-')sign=1,ch=nc();
        for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if(sign)x=-x;
        return true;
    }
    template<class T,class... U>bool read(T& h,U&... t) {
        return read(h)&&read(t...);
    }
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N = 2e5+10;

struct node{
    int ls,rs,ma,id;
}tree[N * 40];

vector<int>e[N];
int root[N],cnt,ans[N];
int dep[N],fa[N],top[N],siz[N],son[N];

void dfs1(int u,int f){
    dep[u]=dep[f]+1;
    fa[u]=f;
    siz[u]=1;
    for(auto v:e[u]){
        if(v==f) continue;
        dfs1(v,u);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]]) son[u]=v;
    }
}

void dfs2(int u,int t){
    top[u]=t;
    if(!son[u]) return ;
    dfs2(son[u],t);
    for(auto v:e[u]){
        if(v==son[u] || v==fa[u]) continue;
        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]];
    }
    return dep[x]>dep[y]?y:x;
}

void pp(int x){
    int ls=tree[x].ls;
    int rs=tree[x].rs;
    if(tree[ls].ma>=tree[rs].ma){
        tree[x].id=tree[ls].id;
        tree[x].ma=tree[ls].ma;
        return ;
    }else{
        tree[x].id=tree[rs].id;
        tree[x].ma=tree[rs].ma;
        return ;
    }
} 

void ins(int &x,int l,int r,int pos){
    if(!x) x=++cnt;
    if(l==r){
        tree[x].ma++;
        tree[x].id=l;
        return ;
    }
    int mid=l+r>>1;
    if(pos<=mid) ins(tree[x].ls,l,mid,pos);
    else ins(tree[x].rs,mid+1,r,pos);
    pp(x);
}

void del(int &x,int l,int r,int pos){
    if(!x) x=++cnt;
    if(l==r){
        tree[x].ma--;
        tree[x].id=l;
        return ;
    }
    int mid=l+r>>1;
    if(pos<=mid) del(tree[x].ls,l,mid,pos);
    else del(tree[x].rs,mid+1,r,pos);
    pp(x);
}

int merge(int x,int y,int l,int r){
    if( !x || !y) return x?x:y;
    if(l==r){
        tree[x].ma+=tree[y].ma;
        tree[x].id=l;
        return x;
    }
    int mid=l+r>>1;
    tree[x].ls=merge(tree[x].ls,tree[y].ls,l,mid);
    tree[x].rs=merge(tree[x].rs,tree[y].rs,mid+1,r);
    pp(x);
    return x;
}

void dfs(int u,int f){
    for(auto v:e[u]){
        if(v==f) continue;
        dfs(v,u);
        root[u]=merge(root[u],root[v],1,N);
    }
    if(tree[root[u]].ma>0) ans[u]=tree[root[u]].id;
}

int main() {

#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int n,m;
    read(n,m);
    for(int i=1;i<n;i++){
        int u,v;
        read(u,v);
        e[u].pb(v);
        e[v].pb(u);
    }
    dfs1(1,0);
    dfs2(1,1);
    while(m--){
        int a,b,c;
        read(a,b,c);
        int lc=lca(a,b);
        ins(root[a],1,N,c);
        ins(root[b],1,N,c);
        del(root[lc],1,N,c);
        if(fa[lc]) del(root[fa[lc]],1,N,c);
    }
    dfs(1,0);
    for(int i=1;i<=n;i++) cout<<ans[i]<<endl;



    return 0;
}

文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}