题目链接
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307
题意
题解
树链剖分暴力莽。
将每根绳子的承重对应到点的值上。每挂一根绳子,该绳子对应的点到“最上面的点”的值都减w[i]。当这个区间上的点的最小值小于0了,那么绳子就会断。
显然就可以用树链剖分将路径对应到区间,用线段树维护区间修改、区间查询最小值。
然后想知道”最上面的点“就靠并查集了。
我没有将钉子也对应线段树中的一个点,因为我感觉很难给钉子一个无限大的值,使得所有绳子都挂在钉子上依旧不会断裂。
时间复杂度为O(n*logn*logn).
debug:
1.树链剖分确定哪个结点往上爬时比较的是“路径深度”而不是“结点深度”,即dep[top[x]]与dep[top[y]]比较。
2.区间懒标的传递是累加的,而非赋值。即T[lson].lazy+=T[p].lazy.
3.初始ans应置为n,而非0.
还是弱啊,本着照模板敲的代码没有灵魂的原则,凭着记忆加理解,勉勉强强写出来了啊。
AC代码
#include <bits/stdc++.h>
#define lson p<<1
#define rson p<<1|1
using namespace std;
const int maxn=5e4+7;
struct node
{
int l,r,sum,lazy;
node(){}
node(int l,int r,int sum,int lazy):l(l),r(r),sum(sum),lazy(lazy){}
}T[maxn<<2];
int a[maxn];
void up(int p)
{
T[p].sum=min(T[lson].sum,T[rson].sum);
}
void down(int p)
{
T[lson].sum-=T[p].lazy;
T[rson].sum-=T[p].lazy;
T[lson].lazy+=T[p].lazy;
T[rson].lazy+=T[p].lazy;
T[p].lazy=0;
}
void build(int p,int l,int r)
{
T[p].l=l,T[p].r=r,T[p].lazy=0;
if(l==r)
{
T[p].sum=0;
return ;
}
int mid=(l+r)>>1;
build(lson,l,mid);
build(rson,mid+1,r);
up(p);
}
int query(int p,int x,int y)
{
if(T[p].l==x && T[p].r==y)
{
return T[p].sum;
}
down(p);
int mid=(T[p].l+T[p].r)>>1;
if(y<=mid) return query(lson,x,y);
else if(x>mid) return query(rson,x,y);
else
{
int s1=query(lson,x,mid);
int s2=query(rson,mid+1,y);
int s=min(s1,s2);
return s;
}
}
void update(int p,int x,int y,int val)//区间[x,y]的值都减val
{
if(T[p].l==x && T[p].r==y)
{
T[p].sum-=val;
T[p].lazy+=val;
return ;
}
down(p);
int mid=(T[p].l+T[p].r)>>1;
if(y<=mid) update(lson,x,y,val);
else if(x>mid) update(rson,x,y,val);
else
{
update(lson,x,mid,val);
update(rson,mid+1,y,val);
}
up(p);
}
//树链剖分
struct edge
{
int to,next,c,w;
}e[maxn<<1];
int head[maxn],cnt;
void init()
{
memset(head,-1,sizeof(head));
cnt=-1;
}
void add_edge(int u,int v,int c,int w)
{
e[++cnt].to=v;
e[cnt].c=c;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
int son[maxn],dep[maxn],sz[maxn],fa[maxn];
void dfs(int u,int f,int deep)
{
dep[u]=deep;
fa[u]=f;
sz[u]=1;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(v==f) continue;
dfs(v,u,deep+1);
sz[u]+=sz[v];
if(son[u]==-1 || sz[son[u]]<sz[v])
son[u]=v;
}
}
int tot;
int id[maxn],pos[maxn],top[maxn];
void dfs2(int u,int topf)
{
top[u]=topf;
id[u]=++tot;//u结点在第tot个位置
pos[tot]=u;//第tot个位置是u结点
if(son[u]==-1) return ;
dfs2(son[u],topf);
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(v==fa[u] || v==son[u]) continue;
dfs2(v,v);
}
}
bool uPath(int x,int y,int val)
{
int xx=x,yy=y;
int ans=1e9;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])
{
swap(x,y);
}
int l=id[top[x]],r=id[x];
update(1,l,r,val);
ans=min(ans,query(1,l,r));
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
int l=id[x],r=id[y];
update(1,l,r,val);
ans=min(ans,query(1,l,r));
return ans>=0;
}
int par[maxn];
int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
}
int c[maxn],w[maxn],p[maxn];
int main()
{
int n;
scanf("%d",&n);
init();
build(1,1,n);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&c[i],&w[i],&p[i]);
add_edge(p[i]+1,i,c[i],w[i]);
}
memset(son,-1,sizeof(son));
dfs(0,-1,0);
tot=-1;
dfs2(0,0);
for(int i=1;i<=n;i++) par[i]=i;
int ans=n;
for(int i=1;i<=n;i++)
{
if(p[i]==-1)
{
update(1,id[i],id[i],w[i]-c[i]);
continue;
}
update(1,id[i],id[i],-c[i]);
int fx=find(p[i]+1);
par[i]=fx;
if(!uPath(fx,i,w[i]))
{
ans=i-1;
break;
}
}
printf("%d\n",ans);
}