淀粉质 & 人机模拟赛T4

· · 个人记录

想的时间有点长了,写的时候还人机了。

非常好的点分治复健题,我好久没写过淀粉质了。

大概题意

一个树,每个点有 a_i d_i k_i。可以在树上游走,u 能走到 v 当且仅当 \sum_{i\in path}d_i\le k_v,如果当前点的 a 大于等于出发点的 a 则可以在这个点停止游走。现在需要对于每个点,求出从这个点开始游走能停止的所有点的 \sum a。

做法

特殊性质有 a 全一样的部分,明示我们先不考虑 a 的问题,即在哪里停都行,能到就能停。

对于一个起点,可以轻松地 O(n) 遍历一遍求出它能停在哪些点,但是不太好 O(n) 遍历一遍求出哪些点能停在它这里。

为了这一点,只能思考一下从 x 开始游走,能停在 y 的充要条件。为了方便对 x 到 y 的一系列点重标号为 1 到 N。

根据题意有:

\prod_{i=2}^N\left[\sum_{j=1}^{i-1}d_j\le k_i\right]

这个形式有点太丑了,必须对于每个起点递推地检查,从终点开始遍历没法玩,试着变形一下:

\prod_{i=2}^N\left[\sum_{j=1}^{i-1}d_j\le k_i\right]\\ =\prod_{i=2}^N\left[\sum_{j=1}^{N-1}d_j\le k_i+\sum_{j=i}^{N-1}d_j\right]\\ =\prod_{i=2}^N\left[\sum_{j=1}^{N-1}d_j\le k_i+\sum_{j=i}^{N-1}d_j\right]\\ =\sum_{i=1}^{N-1}d_j\le \min_{i=2}^N\left(k_i+\sum_{j=i}^{N-1}d_j\right)

这个形式清爽太多了。现在求和都是到终点的 \sum d 而不是到起点的。于是现在我们可以 O(n) 遍历一遍求出哪些点能停在它这里了。

既能求出从 u 停在哪,又能求出有哪些点能停在 u,指向性应该非常明显,u 可以作为类似于中继的角色,由此路径被裂开了,我们来试着思考这个问题:

已经确定从 x 出发能到 y,也确定从 y 开始能到 z(路径不重叠,只是把简单路径劈开),这两个路径能合并,即从 x 出发能到 z 的充要条件是什么?

对前半后半段都考虑一下,发现后半段比较自然。变形起点递推的式子:

\prod_{i=2}^N\left[\sum_{j=1}^{i-1}d_j\le k_i\right]\\ =\prod_{i=2}^N\left[k_i-\sum_{j=1}^{i-1}d_j\ge 0\right]\\ =0\le \min_{i=2}^N\left(k_i-\sum_{j=1}^{i-1}d_j\right)\\

非常帅的式子,看着就爽

不难发现,如果把前半段(x 到 y)插到这后半段前面,那么前半段的 \sum d 对于 \min 内部的每一项都有贡献,所以我们可以把它提出来。中间合并有点小细节,后半段已经考虑进去 d_y 了,前半段要扣掉。于是我们有了合并两个合法路径的充要条件:

\sum_{i\in path_{x,y}}d_i-d_y\le \min_{i=2}^N\left(k_i-\sum_{j=1}^{i-1}d_j\right)

对于点 u,可以 O(n) 求出所有从 u 出发的合法路径终点,可以 O(n) 求出所有以 u 为终点的合法路径起点,能合并在 u 结束和从 u 开始的合法路径,这疑似有点太明显了,上点分治:

别忘了还有 a 的限制没考虑,发现本质上只是一维偏序变成了二位偏序。

于是考试时写了唐氏树套树,还没写完

完全没必要树套树。应该考虑点分治的经典技巧容斥:

这样就做完了。

代码

//%^~
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// #include <iostream>
// #include <cstdio>
// #include <cstring>
// #include <string>
// #include <vector>
// #include <queue>
// #include <algorithm>
// #include <stack>
// #include <unordered_map>
// #include "ext/pb_ds/assoc_container.hpp"
// #include "ext/pb_ds/tree_policy.hpp"
// #include "ext/pb_ds/priority_queue.hpp"
// #include <ext/rope>
// #define PBDS __gnu_pbds
// #include <bits/extc++.h>
#define MAXN 200005
#define eps 1e-10
#define foru(a, b, c) for (int a = (b); (a) <= (c); (a)++)
#define ford(a, b, c) for (int a = (b); (a) >= (c); (a)++)
#define uLL unsigned long long
#define LL long long
#define LXF LL
#define RIN Cap1taLDebug::read()
#define RSIN Cap1taLDebug::rdstr()
#define RCIN Cap1taLDebug::rdchar();
#define HH printf("\n")
#define All(x) (x).begin(), (x).end()
#define fi first
#define se second
using namespace std;
int ID;
#define ceinl(x) cein<<(x)<<endl;
#define ceink(x) cein<<(x)<<' ';
#define ceinp(x,y) cein<<(y)<<(x);
#define ceinpk(x,y) cein<<(y)<<(x)<<' ';
#define ceinpl(x,y) cein<<(y)<<(x)<<endl;
class Cap1taLDebug{
    #ifdef LXF
    #define READTYPE LXF
    #else
    #define READTYPE int
    #endif
    #define DEBUGING
private:
    ostream& buf;
    #ifndef DEBUGING
    static char fbuf[1<<21],*p1,*p2;
    #define getchar() (Cap1taLDebug::p1==p2&&(p1=(p2=fbuf)+fread(fbuf,1,1<<21,stdin),p1==p2)?EOF:*p2++)
    #endif
public:
    Cap1taLDebug(ostream& out=cout):buf(out){}
    ~Cap1taLDebug(){
        #ifdef DEBUGING
        buf.flush();
        #endif
    }
    static READTYPE read(){
        READTYPE x=0,w=1;char ch=0;
        while(!isdigit(ch)){
        if(ch=='-') w=-1;
        ch=getchar();
        }
        while(isdigit(ch))  x=x*10+(ch^48),ch=getchar();
        return x*w;
    }
    static string rdstr(){
        string s;char c=getchar();
        while(c==' ' || c=='\r' || c=='\n') c=getchar();    
        while(c!=' ' && c!='\r' && c!='\n' && c!=EOF)   s+=c,c=getchar();
        return s;
    }
    static char rdchar(){
        char c=getchar();
        while(c==' ' || c=='\r' || c=='\n') c=getchar();
        return c;
    }
    static string int128ToString(__int128 x){
        if(x==0)    return "0";
        string s="",w="";
        if(x<0) w="-",x*=-1;
        while(x) s+=(char)('0'+(int)(x%10)),x/=10;
        reverse(All(s));
        return w+s;
    }
    Cap1taLDebug& operator<<(const string val){
        #ifdef DEBUGING
        buf<<val;
        #endif
        return *this;
    }
    template<typename T1,typename T2>
    Cap1taLDebug& operator<<(const pair<T1,T2>& val){
        #ifdef DEBUGING
        (*this)<<"("<<val.first<<","<<val.second<<")";
        #endif
        return *this;
    }
    template<typename T,template<typename,typename...>class Container,typename...Args>
    Cap1taLDebug& operator<<(const Container<T, Args...>& container){
        #ifdef DEBUGING
        buf<<"{";
        bool fst=0;
        for(const auto& val:container){
            if(!fst)    fst=true;
            else    buf<<",";
            (*this)<<val;   
        }
        buf<<"}";
        #endif
        return *this;
    }
    Cap1taLDebug& operator<<(const __int128& val){
        #ifdef DEBUGING
        buf<<int128ToString(val);
        #endif
        return *this;
    }
    template<typename T>
    Cap1taLDebug& operator<<(const T& val){
        #ifdef DEBUGING
        buf<<val;
        #endif
        return *this;
    }
    Cap1taLDebug& operator<<(ostream& (*manip)(ostream&)){
        #ifdef DEBUGING
        buf<<manip;
        #endif
        return *this;
    }
};
#ifndef DEBUGING
char Cap1taLDebug::fbuf[1<<21],*Cap1taLDebug::p1=nullptr,*Cap1taLDebug::p2=nullptr;
#endif
Cap1taLDebug cein(cout);
// Cap1taLDebug cein(cerr);
std::ostream& operator<<(ostream& os,__int128 val){
    os<<Cap1taLDebug::int128ToString(val);
    return os;
}
/*

*/
int n;
LL a[MAXN];
LL d[MAXN];
LL k[MAXN];
vector<int> e[MAXN];

int sz[MAXN],tot,rt;
bool vis[MAXN];
void get_root(int u,int fath){
    sz[u]=1;
    int Max=0;
    for(auto v:e[u]){
        if(v==fath || vis[v])   continue;
        get_root(v,u);
        sz[u]+=sz[v];
        Max=max(Max,sz[v]);
    }
    Max=max(Max,tot-sz[u]);
    if(Max<=tot/2)  rt=u;
}
LL ans[MAXN];
void updsz(int u,int fath){
    sz[u]=1;
    for(auto v:e[u]){
        if(v==fath || vis[v])   continue;
        updsz(v,u);
        sz[u]+=sz[v];
    }
}
void upd(int u,int fath,LL dg){
    if(a[u]>=a[rt]){
        ans[rt]+=a[u];
    }
    for(auto v:e[u]){
        if(v==fath || vis[v])   continue;
        if(dg+d[u]<=k[v])   upd(v,u,dg+d[u]);
    }
}
vector<pair<int,LL>> dn;
void getdn(int u,int fath,LL dg,LL Min){
    dn.push_back({u,Min});
    for(auto v:e[u]){
        if(v==fath || vis[v])   continue;
        if(k[v]-(dg+d[u])>=0)   getdn(v,u,dg+d[u],min(Min,k[v]-(dg+d[u])));
    }
}
vector<pair<int,LL>> up;
void getup(int u,int fath,LL dg,LL Min){
    up.push_back({u,dg+d[u]});
    for(auto v:e[u]){
        if(v==fath || vis[v])   continue;
        if(dg+d[u]+d[v]<=min(Min,k[u]+dg+d[u])){
            getup(v,u,dg+d[u],min(Min,k[u]+dg+d[u]));
        }
    }
}
// class SegTree{
// 
// }tr[MAXN<<2];
// inline int lc(int x){return x<<1;}
// inline int rc(int x){return x<<1|1;}
// void Build(int p,int l,int r){
    // tr[p].l=l,tr[p].r=r;
// }

// vector<int> dnls[MAXN];
LL tr[MAXN];
stack<int> rmv;
inline int lb(int x){return 1<<(__builtin_ffs(x)-1);}

// vector<LL> X;
vector<LL> Y;
// unordered_map<LL,int> rd1;
unordered_map<LL,int> rd2;
int rdx[MAXN];


void dfz(int u){
    vis[u]=1;

    for(int i=0;i<e[u].size();i++){
        int v=e[u][i];
        if(vis[v])  continue;
        if(d[u]<=k[v])  upd(v,u,d[u]);
    }

    // cout<<"rt: "<<rt<<" ans[2]="<<ans[2]<<endl;
    // X.clear();
    // rd1.clear();
    for(int i=0;i<e[u].size();i++){
        int v=e[u][i];
        if(vis[v])  continue;
        if(d[v]<=k[u])  getup(v,0,0,k[u]);
        if(k[v]-d[u]>=0)    getdn(v,0,d[u],k[v]-d[u]);
    }
    // for(auto &[x,dg]:up){
        // X.push_back(dg);
    // }
    // for(auto &[y,lm]:dn){
        // X.push_back(lm);
    // }
    // sort(All(X));
    // X.erase(unique(All(X)),X.end());
    // for(int i=1;i<=X.size();i++){
        // rd1[X[i-1]]=i;
    // }
    sort(All(up),[&](pair<int,LL> x,pair<int,LL> y)->bool{
        return x.se>y.se;
    });
    sort(All(dn),[&](pair<int,LL> x,pair<int,LL> y)->bool{
        return x.se>y.se;
    });
    int ptr=-1;
    for(auto &[x,dg]:up){
        while(ptr+1<dn.size() && dn[ptr+1].se>=dg){
            ptr++;
            // cout<<rdx[dn[ptr].fi]<<endl;
            for(int i=n-rdx[dn[ptr].fi]+1;i<=n;i+=lb(i)){
                tr[i]+=a[dn[ptr].fi];           
                rmv.push(i);
            }
        }
        for(int i=n-rdx[x]+1;i;i-=lb(i)){
            ans[x]+=tr[i];
        }
    }
    while(!rmv.empty()){
        tr[rmv.top()]=0;
        rmv.pop();
    }
    up.clear();
    dn.clear();


    for(int i=0;i<e[u].size();i++){
        int v=e[u][i];
        if(vis[v])  continue;
        if(d[v]<=k[u])  getup(v,0,0,k[u]);
        if(k[v]-d[u]>=0)    getdn(v,0,d[u],k[v]-d[u]);
        sort(All(up),[&](pair<int,LL> x,pair<int,LL> y)->bool{
            return x.se>y.se;
        });
        sort(All(dn),[&](pair<int,LL> x,pair<int,LL> y)->bool{
            return x.se>y.se;
        });
        int ptr=-1;
        for(auto &[x,dg]:up){
            while(ptr+1<dn.size() && dn[ptr+1].se>=dg){
                ptr++;
                for(int i=n-rdx[dn[ptr].fi]+1;i<=n;i+=lb(i)){
                    tr[i]+=a[dn[ptr].fi];           
                    rmv.push(i);
                }
            }
            for(int i=n-rdx[x]+1;i;i-=lb(i)){
                ans[x]-=tr[i];
            }
            if(a[rt]>=a[x]){
                ans[x]+=a[rt];
            }
        }
        while(!rmv.empty()){
            tr[rmv.top()]=0;
            rmv.pop();
        }
        up.clear();
        dn.clear();
    }
    // cout<<"rt: "<<rt<<" ans[2]="<<ans[2]<<endl;
    // for(int i=e[u].size()-1;i>=0;i--){
        // int v=e[u][i];
        // if(vis[v])   continue;
        // up.clear();
        // if(d[v]<=k[u])   getup(v,0,0,k[u]);
        // for(auto &[x,dg]:up){
            // for(int idx=n-rd1[dg]+1;idx;idx-=lb(idx)){
                // for(auto &y:dnls[idx]){
                    // if(a[y]>=a[x]){
                        // ans[x]+=a[y];
                    // }
                // }
            // }
        // }
        // dn.clear();
        // if(k[v]-d[u]>=0) getdn(v,0,d[u],k[v]-d[u]);
        // for(auto [y,lm]:dn){
            // for(int x=n-rd1[lm]+1;x<=n;x+=lb(x)){
                // dnls[x].push_back(y);
                // rmv.push(x);
            // }
        // }
    // }
    // while(!rmv.empty()){
        // dnls[rmv.top()].clear();
        // rmv.pop();
    // }
    // cout<<"rt: "<<rt<<" ans[2]="<<ans[2]<<endl;

    for(auto v:e[u]){
        if(vis[v])  continue;
        updsz(v,0);
        rt=0;
        tot=sz[v];
        get_root(v,0);
        dfz(rt);
    }
}
void solve(bool SPE){ 
    n=RIN;

    Y.clear();
    rd2.clear();
    foru(i,1,n){
        a[i]=RIN;
        Y.push_back(a[i]);
        d[i]=RIN;
        k[i]=RIN;
    }
    sort(All(Y));
    Y.erase(unique(All(Y)),Y.end());
    for(int i=1;i<=Y.size();i++){
        rd2[Y[i-1]]=i;
    }
    foru(i,1,n){
        rdx[i]=rd2[a[i]];
    }

    foru(i,1,n-1){
        int u=RIN,v=RIN;
        e[u].push_back(v);
        e[v].push_back(u);
    }

    tot=n;
    get_root(1,0);
    dfz(rt);

    foru(i,1,n){
        cout<<ans[i]+a[i]<<' ';
    }

    #ifdef DEBUGING
    if(SPE){

    }
    #endif
    return ;
}
/*
检查文件读写
检查多测清空
检查数组大小
*/
signed main()
{
    // #define RFILE
    // #define MULTITEST
    // #define TESTCASEID

    #ifdef RFILE
    // freopen("7.in","r",stdin);
    freopen("run.in","r",stdin);
    freopen("run.out","w",stdout);
    #endif

    #ifdef MULTITEST
    int T=RIN;
    #else
    int T=1;
    #endif

    #ifdef TESTCASEID
    ID=RIN;
    #endif

    for(int i=1;i<=T;i++) solve(i==0);
    return 0;
}