跳到正文

剪贴板

剪贴板 z1p1k6e4

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#define ls (p<<1)
#define rs (ls|1)
#define mid ((l+r)>>1)
#define int long long
using namespace std;
const int N=1e5+10,M=3000,INF=1e9,INFF=1e18,mod=1e9+7;
int n,a[N],b[N],c[N];vector<int>g[N];
namespace Fast_IO
{
    static char buf[1000000],*paa=buf,*pd=buf,out[10000000];int length=0;
    #define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
    inline int read()
    {
        int x(0),t(1);char fc(getchar());
        while(!isdigit(fc)){if(fc=='-') t=-1;fc=getchar();}
        while(isdigit(fc)) x=(x<<1)+(x<<3)+(fc^48),fc=getchar();
        return x*t;
    }
    inline void flush(){fwrite(out,1,length,stdout);length=0;}
    inline void put(char c){if(length==9999999) flush();out[length++]=c;}
    inline void put(string s){for(char c:s) put(c);}
    inline void print(int x)
    {
        if(x<0) put('-'),x=-x;
        if(x>9) print(x/10);
        put(x%10+'0');
    }
    inline bool chk(char c) { return !(c>='a'&&c<='z'||c>='A'&&c<='Z'||c>='0'&&c<='9'||c=='.'); }
    inline bool ck(char c) { return c!='\n'&&c!='\r'&&c!=-1&&c!=' '; }
    inline void rd(char s[],int&n)
    {
        s[++n]=getchar();
        while(chk(s[n])) s[n]=getchar();
        while(ck(s[n])) s[++n]=getchar();
        n--;
    }
}
using namespace Fast_IO;
namespace tree_decomposition_SGT_Point
{
	struct SGT
	{
		#define ls (p<<1)
		#define rs (p<<1|1)
		#define mid ((l+r)>>1)
		#define pushup(p) (t[p]=t[ls]+t[rs])
		#define pushdown(p) (t[ls]=0,t[rs]=0,lz[ls]=1,lz[rs]=1,lz[p]=0)
		int L,R;
		vector<int> t,lz;//t for tree and lz for lazy tag
		void init(int l,int r)
		{
			t=lz=vector<int>((r-l+10)<<2);
			L=l,R=r;
		}
		void build(int p,int l,int r)
		{
			if(p==1) l=L,r=R;lz[p]=0;
			if(l==r) return t[p]=1,void();
			build(ls,l,mid),build(rs,mid+1,r);
			pushup(p);
		}
		void modify(int p,int l,int r,int s,int e)
		{
			if(p==1) l=L,r=R;
			if(l>=s&&r<=e) return lz[p]=1,t[p]=0,void();
			if(lz[p]) pushdown(p);
			if(mid>=s) modify(ls,l,mid,s,e);
			if(mid<e) modify(rs,mid+1,r,s,e);
			pushup(p);
		}
		int query(int p,int l,int r,int s,int e)
		{
			if(p==1) l=L,r=R;
			if(l>=s&&r<=e) return t[p];
			if(lz[p]) pushdown(p);int res=0;
			if(mid>=s) res=query(ls,l,mid,s,e);
			if(mid<e) res+=query(rs,mid+1,r,s,e);
			return res;
		}
		#undef ls
		#undef rs
		#undef pushup
		#undef pushdown
	} sgt;
	int cnt;
	vector<int> e[N];
	int fa[N],son[N],si[N],dep[N];
	int top[N],id[N],dfn[N];
	inline void init(int l,int r)
	{
		// sgt.init(l,r);
		sgt.build(1,0,0);
	}
	inline void add(int u,int v) { e[u].emplace_back(v),e[v].emplace_back(u); }
	inline void dfs1(int u,int ff)
	{
		fa[u]=ff,si[u]=1,dep[u]=dep[ff]+1;
		for(auto v:e[u])
		{
			if(v==ff) continue;
			dfs1(v,u);
			si[u]+=si[v];
			if(si[son[u]]<si[v]) son[u]=v;
		}
	}
	inline void dfs2(int u,int topf)
	{
		top[u]=topf,dfn[u]=++cnt,id[cnt]=u;
		if(son[u]) dfs2(son[u],topf);
		for(auto v:e[u])
		{
			if(v==fa[u]||v==son[u]) continue;
			dfs2(v,v);
		}
	}
	inline 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]?x:y;
	}
	inline void modify(int x,int y)
	{
		while(top[x]!=top[y])
		{
			if(dep[top[x]]<dep[top[y]]) swap(x,y);
			sgt.modify(1,0,0,dfn[top[x]],dfn[x]);
			x=fa[top[x]];
		}
		if(dep[x]>dep[y]) swap(x,y);
		sgt.modify(1,0,0,dfn[x],dfn[y]);
	}
	inline int query(int x,int y)
	{
		int res=0;
		while(top[x]!=top[y])
		{
			if(dep[top[x]]<dep[top[y]]) swap(x,y);
			res+=sgt.query(1,0,0,dfn[top[x]],dfn[x]);
			x=fa[top[x]];
		}
		if(dep[x]>dep[y]) swap(x,y);
		return res+sgt.query(1,0,0,dfn[x],dfn[y]);
	}
}
using namespace tree_decomposition_SGT_Point;
inline bool check(int x)
{
    for(int i=1;i<=n+5;i++) g[i].clear();
    for(int i=1,xx,y,z,w;i<=n;i++)
    {
        if(c[i]==0)
        {
            if((x-n+1)*b[i]>=a[i]) continue;
            if(x*b[i]<a[i]) return false;
            g[x-(a[i]+b[i]-1)/b[i]+1].emplace_back(i);
        }
        if(c[i]>0)
        {
            y=a[i]-b[i]*x,z=(x+1)*x/2;
        	if(INFF/c[i]<=z) w=INFF;
        	else w=z*c[i];
            if(y>w) return false;
            y=a[i]-b[i]*(x-n+1),z=(x+1)*x/2-(n-1)*n/2;
        	if(INFF/c[i]<=z) w=INFF;
        	else w=z*c[i];
        	if(y<=w) continue;int l=1,r=n,res=-1;
            while(l<=r)
            {
                y=a[i]-b[i]*(x-mid+1),z=(x+1)*x/2-(mid-1)*mid/2;
            	if(INFF/c[i]<=z) w=INFF;
        		else w=z*c[i];
                if(y<=w) res=mid,l=mid+1;
                else r=mid-1;
            }g[res].emplace_back(i);
        }
        if(c[i]<0)
        {
            xx=(b[i]-1)/-c[i];
            if(x<=xx)
            {
                y=a[i]-b[i]*x,z=(x+1)*x/2;
            	if(INFF/-c[i]<=z) w=-INFF;
            	else w=z*c[i];
                if(y>w) return false;
                int l=1,r=n,res=-1;
                while(l<=r)
                {
                    y=a[i]-b[i]*(x-mid+1),z=(x+1)*x/2-(mid-1)*mid/2;
                	if(INFF/-c[i]<=z) w=-INFF;
                	else w=z*c[i];
                    if(y<=w) res=mid,l=mid+1;
                    else r=mid-1;
                }g[res].emplace_back(i);
            }
            else
            {
                y=a[i]-b[i]*xx-(x-xx),z=(xx+1)*xx/2;
            	if(INFF/-c[i]<=z) w=-INFF;
            	else w=z*c[i];
                if(y>w) return false;
                int l=1,r=n,res=-1;
                while(l<=r)
                {
                    if(mid>xx)
                    {
                        if(x-mid+1>=a[i]) res=mid,l=mid+1;
                        else r=mid-1;continue;
                    }y=a[i]-b[i]*(xx-mid+1)-(x-xx),z=(xx+1)*xx/2-(mid-1)*mid/2;
                	if(INFF/-c[i]<=z) w=-INFF;
                	else w=z*c[i];
                    if(y<=w) res=mid,l=mid+1;
                    else r=mid-1;
                }g[res].emplace_back(i);
            }
        }
    }
	init(1,n);g[1].emplace_back(1);int s=0;
    for(int i=1;i<=n;i++)
    {
        for(auto u:g[i]) s+=query(1,u),modify(1,u);
    	if(s>i) return false;
    }
    return true;
}
inline void solve()
{
    n=read();
    for(int i=1;i<=n;i++) a[i]=read(),b[i]=read(),c[i]=read();
    for(int i=1,u,v;i<n;i++) u=read(),v=read(),e[u].emplace_back(v),e[v].emplace_back(u);
	dfs1(1,0),dfs2(1,1);sgt.init(1,n);
    int l=n,r=INF,ans=0;
    while(l<=r)
        if(check(mid)) ans=mid,r=mid-1;
        else l=mid+1;print(ans);
}
signed main()
{
	// freopen("tree3.in","r",stdin);
    int T=1;
    // T=read();
    while(T--) solve();
    genshin:;flush();return 0;
}