#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define md
#define ll long long
#define inf (int) 1e9
#define eps 1e-8
#define N 300010
using namespace std;
struct yts { int x,t,ne;} e[2*N];
int num=0,cnt=0;
int v[N],sz[N],son[N],fa[N],dep[N],pos[N],left[N],right[N],top[N];
struct tree { int l,r,sum,ad,ans,sz,tag;} tr[4*N];
void put(int x,int y)
{
num++; e[num].x=x; e[num].t=y;
e[num].ne=v[x]; v[x]=num;
}
void dfs1(int x)
{
//printf("x%d ",x);
sz[x]=1; son[x]=0;
for (int i=v[x];i;i=e[i].ne)
{
int y=e[i].t;
if (y!=fa[x])
{
fa[y]=x; dep[y]=dep[x]+1;
dfs1(y);
sz[x]+=sz[y]; if (sz[y]>sz[son[x]]) son[x]=y;
}
}
}
void dfs2(int x,int hd)
{
pos[x]=left[x]=++cnt; top[x]=hd;
if (son[x]!=0) dfs2(son[x],hd);
for (int i=v[x];i;i=e[i].ne)
{
int y=e[i].t;
if (y!=fa[x]&&y!=son[x]) dfs2(y,y);
}
right[x]=cnt;
}
void build(int i,int l,int r)
{
tr[i].l=l; tr[i].r=r; tr[i].sz=r-l+1; tr[i].sum=tr[i].ad=tr[i].tag=tr[i].ans=0;
if (l==r) return;
int mid=(l+r)>>1;
build(2*i,l,mid); build(2*i+1,mid+1,r);
}
void tagit(int i,int d)
{
tr[i].tag=d;
if (d==1) tr[i].ans=tr[i].sum;
if (d==2) tr[i].ans=0;
}
void release(int i)
{
int l=2*i,r=2*i+1;
if (tr[i].ad)
{
int der=tr[i].ad;
tr[i].ad=0;
tr[l].ad+=der; tr[l].sum+=tr[l].sz*der;
tr[r].ad+=der; tr[r].sum+=tr[r].sz*der;
}
if (tr[i].tag)
{
tagit(l,tr[i].tag); tagit(r,tr[i].tag);
tr[i].tag=0;
}
}
void update(int i)
{
int l=2*i,r=2*i+1;
tr[i].sum=tr[l].sum+tr[r].sum;
tr[i].ans=tr[l].ans+tr[r].ans;
}
void add(int i,int l,int r,int d)
{
if (l<=tr[i].l&&tr[i].r<=r) { tr[i].sum+=d*tr[i].sz; tr[i].ad+=d; return;}
release(i);
int mid=(tr[i].l+tr[i].r)>>1;
if (l<=mid) add(2*i,l,r,d);
if (mid+1<=r) add(2*i+1,l,r,d);
update(i);
}
void modify(int i,int l,int r)
{
if (l<=tr[i].l&&tr[i].r<=r) { tagit(i,1); return; }
release(i);
int mid=(tr[i].l+tr[i].r)>>1;
if (l<=mid) modify(2*i,l,r);
if (mid+1<=r) modify(2*i+1,l,r);
update(i);
}
void outit()
{
for (int i=1;i<=10;i++) printf("%d %d %d %d %d %d %d\n",i,tr[i].l,tr[i].r,tr[i].sum,tr[i].ad,tr[i].ans,tr[i].tag); printf("\n");
}
int main()
{
//freopen("data.in","r",stdin); freopen("data.out","w",stdout);
int n,x,y;
scanf("%d",&n);
for (int i=1;i<=n-1;i++) { scanf("%d%d",&x,&y); put(x,y); put(y,x); }
dfs1(1); dfs2(1,1);
//for (int i=1;i<=n;i++) printf("%d ",pos[i]); printf("\n\n");
build(1,1,n);
int tt,opt,d,k;
scanf("%d",&tt);
while (tt--)
{
scanf("%d",&opt);
if (opt==0)
{
scanf("%d%d",&x,&d);
add(1,left[x],right[x],d);
}
else
{
scanf("%d",&k);
for (int i=1;i<=k;i++)
{
scanf("%d%d",&x,&y);
if (dep[x]<dep[y]) swap(x,y);
while (dep[top[x]]>dep[y])
{
modify(1,pos[top[x]],pos[x]);
x=fa[top[x]];
}
modify(1,pos[y],pos[x]);
}
printf("%d\n",tr[1].ans&2147483647);
tagit(1,2);
}
//outit();
}
return 0;
}
bzoj 3589 动态树
最新推荐文章于 2019-09-26 10:07:00 发布
lct水题?
逗你玩!
其实就是线段树的裸题,不明白为什么网上都要用容斥。
tle是线段树结构写错了+dep[y]<dep[top[x]]
wa是因为应该先下放ad标记,再下放tag标记