标签:
input | output |
---|---|
4 1 2 2 3 2 4 6 I 1 1 G 1 1 G 3 4 I 2 3 G 1 1 G 3 4 |
1 0 1 3
|
分析:树上点修改+区间极值查询,树链剖分;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, rt<<1 #define Rson mid+1, R, rt<<1|1 const int maxn=1e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;} int n,m,k,t,q,tot,fa[maxn],dep[maxn],son[maxn],h[maxn],bl[maxn],pos[maxn]; struct node { int to,nxt; }a[maxn<<1]; struct node1 { char a[2]; int x,y; }op[maxn]; void add(int x,int y) { tot++; a[tot].to=y; a[tot].nxt=h[x]; h[x]=tot; } void dfs(int now,int pre) { son[now]=1; for(int i=h[now];i;i=a[i].nxt) { if(a[i].to!=pre) { fa[a[i].to]=now; dep[a[i].to]=dep[now]+1; dfs(a[i].to,now); son[now]+=son[a[i].to]; } } } void dfs1(int now,int chain) { int k=0; pos[now]=++t; bl[now]=chain; for(int i=h[now];i;i=a[i].nxt) { int x=a[i].to; if(dep[x]>dep[now]&&son[x]>son[k]) k=x; } if(k==0)return; dfs1(k,chain); for(int i=h[now];i;i=a[i].nxt) { int x=a[i].to; if(dep[x]>dep[now]&&k!=x) dfs1(x,x); } } struct Node { ll Max, lazy; } T[maxn<<2]; void PushUp(int rt) { T[rt].Max = max(T[rt<<1].Max, T[rt<<1|1].Max); } void PushDown(int L, int R, int rt) { int mid = (L + R) >> 1; ll t = T[rt].lazy; T[rt<<1].Max += t; T[rt<<1|1].Max += t; T[rt<<1].lazy += t; T[rt<<1|1].lazy += t; T[rt].lazy = 0; } void Update(int l, int r, int v, int L, int R, int rt) { if(l==L && r==R) { T[rt].lazy += v; T[rt].Max += v; return ; } int mid = (L + R) >> 1; if(T[rt].lazy) PushDown(L, R, rt); if(r <= mid) Update(l, r, v, Lson); else if(l > mid) Update(l, r, v, Rson); else { Update(l, mid, v, Lson); Update(mid+1, r, v, Rson); } PushUp(rt); } ll Query(int l, int r, int L, int R, int rt) { if(l==L && r== R) { return T[rt].Max; } int mid = (L + R) >> 1; if(T[rt].lazy) PushDown(L, R, rt); if(r <= mid) return Query(l, r, Lson); else if(l > mid) return Query(l, r, Rson); return max(Query(l, mid, Lson) , Query(mid + 1, r, Rson)); } ll gao(int x,int y) { ll ma=-1e19; while(bl[x]!=bl[y]) { if(dep[bl[x]]<dep[bl[y]])swap(x,y); ma=max(ma,Query(pos[bl[x]],pos[x],1,n,1)); x=fa[bl[x]]; } if(pos[x]>pos[y])swap(x,y); ma=max(ma,Query(pos[x],pos[y],1,n,1)); return ma; } int main() { int i,j; scanf("%d",&n); rep(i,1,n-1) { int x,y; scanf("%d%d",&x,&y); add(x,y); add(y,x); } dfs(1,-1); dfs1(1,1); scanf("%d",&q); rep(i,1,q) { scanf("%s%d%d",op[i].a,&op[i].x,&op[i].y); if(op[i].a[0]==‘I‘)Update(pos[op[i].x],pos[op[i].x],op[i].y,1,n,1); else printf("%lld\n",gao(op[i].x,op[i].y)); } //system("Pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/dyzll/p/5838062.html