本蒟蒻又来求助树链剖分了QAQ
查看原帖
本蒟蒻又来求助树链剖分了QAQ
182792
Jie_Rans楼主2021/11/14 13:18

这里是评测记录

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,Q;
int fa[N],depth[N],son[N],size[N],top[N],id[N],label;
int tot,h[N];
ll ans;
struct Node{
	int ver,next;
}l[N*2];
struct node{
	int l,r;
	ll sum,add;
}t[N*4];

void add(int x,int y)
{
	l[++tot].ver=y;
	l[tot].next=h[x];
	h[x]=tot;
}

void dfs1(int x,int f,int dep)
{
	depth[x]=dep;
	fa[x]=f;
	size[x]=1;
	for(int i=h[x];i;i=l[i].next){
		int y=l[i].ver;
		if(y==f) continue;
		dfs1(y,x,dep+1);
		size[x]+=size[y];
		if(size[y]>size[son[x]]) son[x]=y;
	}
}
void dfs2(int x,int topf)
{
	id[x]=++label;
	top[x]=topf;
	if(!son[x]) return ;
	dfs2(son[x],topf);
	for(int i=h[x];i;i=l[i].next){
		int y=l[i].ver;
		if(y==fa[x]||y==son[x]) continue;
		dfs2(y,y);
	}
}

void build(int p,int l,int r)
{
	t[p].l=l; t[p].r=r;
	if(l==r) return ;
	int mid=(l+r)>>1;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
void spread(int p)
{
	if(t[p].add){
		int left=p<<1,right=p<<1|1;
		t[left].sum+=(ll)t[p].add*(t[left].r-t[left].l+1);
		t[right].sum+=(ll)t[p].add*(t[right].r-t[right].l+1);
		t[left].add+=t[p].add;
		t[right].add+=t[p].add;
		t[p].add=0;
	}
}
void update(int p,int l,int r,int d)
{
	if(t[p].l>=l&&t[p].r<=r){
		t[p].sum+=(ll)d*(t[p].r-t[p].l+1);
		t[p].add+=d;
		return ;
	}
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	if(l<=mid) update(p*2,l,r,d);
	if(r>mid) update(p*2+1,l,r,d);
	t[p].sum=t[p*2].sum+t[p*2+1].sum;
}
void addpath(int x,int y,int d)
{
	while(top[x]!=top[y]){
	    if(depth[top[x]]<depth[top[y]]) swap(x,y);
		update(1,id[top[x]],id[x],d);
		x=fa[top[x]];
	}
	if(depth[x]>depth[y]) swap(x,y);
	update(1,id[x],id[y],d);
}
void query(int p,int l,int r)
{ 
	if(t[p].l>=l&&t[p].r<=r){
		ans+=t[p].sum;
		return ;
	}
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	if(l<=mid) query(p*2,l,r);
	if(r>mid) query(p*2+1,l,r);
}
int querytree(int x,int y)
{
	ans=0;
	while(top[x]!=top[y]) {
		if(depth[top[x]]<depth[top[y]]) swap(x,y);
		cout<<id[top[x]]<<" "<<id[x]<<endl;
		query(1,id[top[x]],id[x]);
		x=fa[top[x]];
	}
	if(depth[x]>depth[y]) swap(x,y);
	query(1,id[x],id[y]);
	return ans;
}
int ask(int x)
{
	ans=0;
	query(1,id[x],id[x]+size[x]-1);
	return ans;
}
void init()
{
	cin>>n;
	for(int i=1;i<n;i++){
		int x,y;
		cin>>x>>y;
		add(x+1,y+1);
		add(y+1,x+1);
	}
	cin>>Q;
	dfs1(1,0,1);
	dfs2(1,1);
	build(1,1,n);
}
int main()
{
    freopen("test.in","r",stdin);
	freopen("write.out","w",stdout);
	init();
	while(Q--){
		char c;
		cin>>c;
		if(c=='A') {
			int u,v,d;
			cin>>u>>v>>d;
			addpath(u+1,v+1,d);
		}
		else {
			int u;
			cin>>u;
			cout<<ask(u+1)<<endl;
		}
	}
	return 0;
}

2021/11/14 13:18
加载中...