左偏树求助
查看原帖
左偏树求助
102709
zjy1412楼主2022/9/10 18:37

0pts全wa,查了好久没查出来bug,也没有数据。。 另外中秋快乐呀各位!

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define ull unsigned long long
ll read(){
	int a=0,b=1;char c=getchar();
	while(!isdigit(c)){if(c=='-')b=-1;c=getchar();}
	while(isdigit(c)){a=a*10+c-'0';c=getchar();}
	return a*b;
}
const ll N=3e5+50;
ll n,m,tot,ed[N],h[N],ver[N],nx[N],ls[N],rs[N],dis[N],
val[N],mu[N],ad[N],ta[N],tmu[N],tad[N],top[N],ans1[N],ans2[N],d[N],c[N];
void add(ll u,ll v){
	ver[++tot]=v;
	nx[tot]=h[u];h[u]=tot;
}
void ead(ll x,ll v){
	val[x]+=v;
	ad[x]+=v;
}
void emu(ll x,ll v){
	val[x]*=v;
	ad[x]*=v;
	mu[x]*=v;
}
void pushdown(ll x){
	emu(ls[x],mu[x]);
	emu(rs[x],mu[x]);
	ead(ls[x],ad[x]);
	ead(rs[x],ad[x]);
	mu[x]=1;ad[x]=0;
}
ll mer(ll x,ll y){
	if(!x||!y)return x|y;
	if(val[x]>val[y])swap(x,y);
	pushdown(x);
	rs[x]=mer(rs[x],y);
	if(dis[ls[x]]<dis[rs[x]])swap(ls[x],rs[x]);
	dis[x]=dis[rs[x]]+1;
	return x;
}
ll del(ll x){
	pushdown(x);
	return mer(ls[x],rs[x]);
}
void dfs(ll x){
	for(ll i=h[x],v;i;i=nx[i]){
		v=ver[i];
		d[v]=d[x]+1;
		dfs(v);
		top[x]=mer(top[x],top[v]);
	}
	while(top[x]&&val[top[x]]<ed[x]){
		ans1[x]++;
		ans2[top[x]]=d[c[top[x]]]-d[x];
		top[x]=del(top[x]);
	}
	if(!ta[x])ead(top[x],tad[x]);
	else emu(top[x],tmu[x]);
	if(x==1){
		while(top[x]){
			ans2[top[x]]=d[c[top[x]]]+1;
			top[x]=del(top[x]);
		}
	}
}
int main(){
	n=read();m=read();
	for(ll i=1;i<=n;i++){
		ed[i]=read();
		mu[i]=1;
	}
	for(ll i=2,u,a,v;i<=n;i++){
		u=read();ta[i]=a=read();v=read();
		add(u,i);
		if(!a)tad[i]=v;
		else tmu[i]=v;
	}
	for(ll i=1;i<=m;i++){
		dis[i]=1;
		val[i]=read();
		c[i]=read();
		top[c[i]]=mer(top[c[i]],i);
	}
	dfs(1);
	for(ll i=1;i<=n;i++){
		printf("%lld\n",ans1[i]);
	}
	for(ll i=1;i<=m;i++){
		printf("%lld\n",ans2[i]);
	}
	return 0;
}
2022/9/10 18:37
加载中...