标签:
题意:给一个树,三个操作,一个询问点的权值,一个将u到v路径上的点的权值增加,一个是减少
思路:还是比较裸的模版题,线段树改为区间更新就好了
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f3fll; const int maxn=50010; int fa[maxn],siz[maxn],son[maxn],w[maxn],p[maxn],dep[maxn],fp[maxn]; //fa为父节点,siz为子节点中siz最大的,dep为深度,son为重儿子,w表示在线段树中的位置 int num[maxn<<2],lazy[maxn<<2]; int tree_id,n; vector<int>G[maxn]; void dfs1(int u,int ff,int deep){ son[u]=0;fa[u]=ff;siz[u]=1;dep[u]=deep; for(unsigned int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v==ff) continue; dfs1(v,u,deep+1); siz[u]+=siz[v]; if(siz[v]>siz[son[u]]) son[u]=v; } } void dfs2(int u,int ff){ w[u]=++tree_id;p[u]=ff; if(son[u]) dfs2(son[u],ff); else return ; for(unsigned int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v!=fa[u]&&v!=son[u]) dfs2(v,v); } } void pushdown(int node){ if(lazy[node]){ lazy[node<<1]+=lazy[node]; lazy[node<<1|1]+=lazy[node]; num[node<<1]+=lazy[node]; num[node<<1|1]+=lazy[node]; lazy[node]=0; } } void update(int l,int r,int val,int le,int ri,int node){ if(l<=le&&ri<=r){ num[node]+=val; lazy[node]+=val; return ; } pushdown(node); int t=(le+ri)>>1; if(l<=t) update(l,r,val,le,t,node<<1); if(r>t) update(l,r,val,t+1,ri,node<<1|1); } int query(int pos,int le,int ri,int node){ if(le==ri) return num[node]; pushdown(node); int t=(le+ri)>>1; if(pos<=t) query(pos,le,t,node<<1); else query(pos,t+1,ri,node<<1|1); } void getans(int u,int v,int val){ int f1=p[u],f2=p[v]; while(f1!=f2){ if(dep[f1]<dep[f2]){ swap(f1,f2); swap(u,v); } update(w[f1],w[u],val,1,n,1); u=fa[f1];f1=p[u]; } if(dep[u]>dep[v]) swap(u,v); update(w[u],w[v],val,1,n,1); } int A[maxn]; int main(){ char str[10]; int m,q,u,v; while(scanf("%d%d%d",&n,&m,&q)!=-1){ for(int i=1;i<=n;i++) scanf("%d",&A[i]); for(int i=0;i<maxn;i++) G[i].clear(); memset(son,0,sizeof(son));tree_id=0; memset(lazy,0,sizeof(lazy)); for(int i=0;i<m;i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } dfs1(1,1,0); dfs2(1,1); memset(num,0,sizeof(num)); while(q--){ scanf("%s",str); if(str[0]=='Q'){ scanf("%d",&u); printf("%d\n",A[u]+query(w[u],1,n,1)); }else if(str[0]=='I'){ scanf("%d%d%d",&u,&v,&m); getans(u,v,m); }else{ scanf("%d%d%d",&u,&v,&m); getans(u,v,-m); } } } return 0; }
标签:
原文地址:http://blog.csdn.net/dan__ge/article/details/51871997