HDU3966-Aragorn's Story-树链剖分-点权

时间:2021-09-12 21:30:31

很模板的树链剖分题

注意什么时候用线段树上的标号,什么时候用点的标号。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector> using namespace std;
const int maxn = ;
int val_pre[maxn],val[maxn],siz[maxn],son[maxn],id[maxn],fa[maxn],top[maxn],dep[maxn];
int topw;
int M,N,P; vector<int> G[maxn]; void dfs_1(int u,int f,int d)
{
siz[u] = ;
fa[u] = f;
dep[u] = d;
son[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == f) continue;
dfs_1(v,u,d+);
siz[u] += siz[v];
if(siz[son[u]] < siz[v])
son[u] = v;
}
} void dfs_2(int u,int tp)
{
top[u] = tp;
id[u] = ++topw;
if(son[u]) dfs_2(son[u],tp);
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(v == fa[u]|| v==son[u]) continue;
dfs_2(v,v);
}
} void debug()
{
for(int i=;i<=N;i++)
{
printf("%d siz:%d son:%d dep:%d fa:%d ",i,siz[i],son[i],dep[i],fa[i]);
printf("top:%d id:%d\n",top[i],id[i]);
}
} ////////////////////////////////////////
//Segment Tree #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int add[maxn<<];
void push_down(int rt)
{
if(add[rt])
{
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
add[rt] = ;
}
} void build(int l,int r,int rt)
{
add[rt] = ;
if(l == r)
{
add[rt] = val[r];
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l && R>= r)
{
add[rt] += c;
return ;
}
push_down(rt);
int m = (l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
} int query(int x,int l,int r,int rt)
{
if(l == r)
{
return add[rt];
}
push_down(rt);
int m = (l+r)>>;
if(x <= m) query(x,lson);
else query(x,rson);
} void change(int u,int v,int add)
{
int fu = top[u],fv = top[v];
while(fu != fv)
{
//printf("fu:%d u:%d v:%d fv:%d \n",fu,u,v,fv);
if(dep[fu] < dep[fv])
{
swap(u,v);swap(fu,fv);
}
update(id[fu],id[u],add,,topw,);
u = fa[fu];
fu = top[u];
}
if(u == v)
{
update(id[u],id[v],add,,topw,);
return;
}
else{
if(dep[u] > dep[v]) swap(u,v);
update(id[u],id[v],add,,topw,);
return ;
}
} int main()
{
//freopen("input.in","r",stdin);
while(~scanf("%d%d%d",&N,&M,&P))
{
for(int i=;i<=N;i++) scanf("%d",&val_pre[i]);
for(int i=,a,b;i<=M;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
topw = ;
dfs_1(,,);
dfs_2(,);
for(int i=;i<=N;i++)
{
val[id[i]] = val_pre[i];
}
build(,topw,);
//debug(); char op[];
int a,b,c; for(int i=;i<P;i++)
{
scanf("%s",op);
if(op[] == 'I')
{
scanf("%d%d%d",&a,&b,&c);
change(a,b,c);
}
else if(op[] == 'D')
{
scanf("%d%d%d",&a,&b,&c);
change(a,b,-c);
}
else if(op[] == 'Q')
{
scanf("%d",&a);
int ans = query(id[a],,topw,);
printf("%d\n",ans);
}
}
for(int i=;i<maxn;i++) G[i].clear();
}
}