1. 程式人生 > >832D Misha, Grisha and Underground ( 樹鏈剖分/LCA

832D Misha, Grisha and Underground ( 樹鏈剖分/LCA

題意:

給三個點,一棵樹, 將其中的兩個點直接的路徑+1 問第三個點和這其中的兩個點之一的一個點直接的路徑上面1最多有幾個 非常暴力的樹鏈剖分可以過,複雜度也夠, 正解是LCA的方法 很巧妙,,,,

樹鏈剖分

//    Created by Yishui
//    Time on 2018/10/
//    E-mail: [email protected]

/*---------------------------------*/

#include <bits/stdc++.h>
using namespace std;

#define cpp_io() {ios::sync_with_stdio(false); cin.tie(NULL);}
#define rep(i,a,n) for (int i=a;i<n;i++) #define repp(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define CLR(a,b) memset(a,(b),sizeof(a)) #define all(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair #define fi first
#define se second #define ls o<<1 #define rs o<<1|1 typedef long long ll; typedef vector<int> VI; const int MAXN = (int)2e5+10; const int INF = 0x3f3f3f3f; const int mod = (int)1e9+7; struct node{ int l,r; int lazy,x; int len; }t[MAXN<<2]; int n,m,p; int cnt; int
f[MAXN], d[MAXN], sz[MAXN], son[MAXN], rk[MAXN], top[MAXN], id[MAXN]; vector<int> E[MAXN]; int a[MAXN]; void push_down(int o){ if(t[o].lazy){ t[ls].x=(t[ls].x+t[ls].len*t[o].lazy); t[rs].x=(t[rs].x+t[rs].len*t[o].lazy); t[ls].lazy=(t[o].lazy+t[ls].lazy); t[rs].lazy=(t[o].lazy+t[rs].lazy); t[o].lazy=0; } } void build(int l,int r,int o) { t[o].l=l,t[o].r=r;t[o].len=r-l+1;t[o].lazy=0; if(l==r){ t[o].x=rk[l]; return ; } int mid=(l+r)>>1; build(l,mid,ls); build(mid+1,r,rs); t[o].x=(t[ls].x+t[rs].x); } inline void update(int l,int r, int o, int x) { if(t[o].l>=l&&t[o].r<=r) { t[o].x=(t[o].x+t[o].len*x); t[o].lazy=(t[o].lazy+x); return; } push_down(o); int mid=(t[o].l+t[o].r)>>1; if(r<=mid) update(l,r,ls,x); else if(l>mid) update(l,r,rs,x); else { update(l,mid,ls,x); update(mid+1,r,rs,x); } t[o].x = (t[ls].x+t[rs].x); } inline int query(int l,int r,int o){ if(t[o].l>=l&&t[o].r<=r) return t[o].x; push_down(o); int mid=(t[o].l+t[o].r)>>1; if(r<=mid) return query(l,r,ls); else if(l>mid) return query(l,r,rs); else return (query(l,mid,ls)+query(mid+1,r,rs)); } // 樹上操作----------------------------- void dfs1(int u, int fa, int dep){ f[u]=fa, d[u]=dep, sz[u]=1; rep(i,0,SZ(E[u])){ int v=E[u][i]; if(v==fa) continue; dfs1(v,u,dep+1); sz[u]+=sz[v]; if(sz[v]>sz[son[u]]) son[u] = v; } } void dfs2(int u,int t){ top[u]=t; id[u]=++cnt; rk[cnt]=a[u]; if(!son[u]) return; dfs2(son[u],t); rep(i,0,SZ(E[u])){ int v=E[u][i]; if(v!=son[u]&&v!=f[u]) dfs2(v,v); } } void add_lca(int x,int y,int z) { while(top[x]!=top[y]) { if(d[top[x]]<d[top[y]]) swap(x,y); update(id[top[x]],id[x],1,z); x=f[top[x]]; } if(d[x]>d[y])swap(x,y); update(id[x],id[y],1,z); } int query_lca(int x,int y){ int ans=0; while(top[x]!=top[y]){ if(d[top[x]]<d[top[y]]) swap(x,y); ans=(ans+query(id[top[x]],id[x],1)); x=f[top[x]] ; } if(d[x]>d[y]) swap(x,y); ans=(ans+query(id[x],id[y],1)); return ans; } int qwq(int x,int y,int z) { int ans=0; add_lca(x,y,1); ans = max(query_lca(z,y),ans); add_lca(x,y,-1); add_lca(x,z,1); ans = max(query_lca(x,y),ans); add_lca(x,z,-1); add_lca(z,y,1); ans = max(query_lca(x,z),ans); add_lca(z,y,-1); // add_lca(x,y,1); ans = max(query_lca(z,y),ans); add_lca(x,y,-1); // add_lca(x,y,1); ans = max(query_lca(z,y),ans); add_lca(x,y,-1); // add_lca(x,y,1); ans = max(query_lca(z,y),ans); add_lca(x,y,-1); return ans; } int main() { // cpp_io(); scanf("%d%d",&n,&m); // repp(i,1,n) a[i]=0; repp(i,2,n) { int u; scanf("%d",&u); E[u].pb(i); E[i].pb(u); } cnt=0; dfs1(1,0,1); dfs2(1,1); build(1,n,1); while(m--){ int x,y,z; scanf("%d%d%d",&x,&y,&z); printf("%d\n",qwq(x,y,z)); } return 0; }

LCA

未完待續