1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
#include<bits/stdc++.h> using namespace std;
using i64=long long;
const int N=2e5+5;
struct Seg_Tree{ int val[N<<2]; int clk[N<<2]; array<int,2> lz[N<<2]; void pushup(int x){ val[x]=val[x<<1]+val[x<<1|1]; clk[x]=max(clk[x<<1],clk[x<<1|1]); } void build(int x,int l,int r,int clock){ clk[x]=clock; lz[x]={-1,-1}; if(l==r){ val[x]=1; return; } int mid=l+r>>1; build(x<<1,l,mid,clock); build(x<<1|1,mid+1,r,clock); pushup(x); return; } void pushdown(int x){ if(lz[x][0]!=-1){ auto [k,clock]=lz[x]; if(clock>=clk[x<<1]){ val[x<<1]=k; clk[x<<1]=clock; lz[x<<1]=lz[x]; } if(clock>=clk[x<<1|1]){ val[x<<1|1]=k; clk[x<<1|1]=clock; lz[x<<1|1]=lz[x]; } lz[x]={-1,-1}; } return; } void change(int x,int l,int r,int ql,int qr,array<int,2> arr){ if(ql>qr) return; if(l>=ql&&r<=qr){ auto [k,clock]=arr; if(clock<clk[x]) return; val[x]=k; clk[x]=clock; lz[x]=arr; return; } pushdown(x); int mid=l+r>>1; if(mid>=ql) change(x<<1,l,mid,ql,qr,arr); if(mid+1<=qr) change(x<<1|1,mid+1,r,ql,qr,arr); pushup(x); return; } int search(int x,int l,int r,int ql,int qr){ if(ql>qr) return 0; if(l>=ql&&r<=qr) return val[x]; pushdown(x); int res=0; int mid=l+r>>1; if(mid>=ql) res+=search(x<<1,l,mid,ql,qr); if(mid+1<=qr) res+=search(x<<1|1,mid+1,r,ql,qr); return res; } array<int,2> search(int x,int l,int r,int pos){ if(l==r) return {val[x],clk[x]}; pushdown(x); int mid=l+r>>1; if(pos<=mid) return search(x<<1,l,mid,pos); else return search(x<<1|1,mid+1,r,pos); } }tr[2]; namespace TCD{ const int N=::N; vector<int> e[N]; int rt,dfn0,clk; int sz[N],fa[N],dep[N]; int dfn[N],son[N],top[N]; void init(int n,int _rt){ rt=_rt; dfn0=clk=0; for(int i=0;i<=n;i++){ sz[i]=1; fa[i]=dfn[i]=top[i]=dep[i]=0; son[i]=0; e[i].clear(); } sz[0]=0; } void add(int x,int y){ e[x].push_back(y); e[y].push_back(x); } void getson(int x,int f){ fa[x]=f; for(auto v:e[x]){ if(v==f) continue; dep[v]=dep[x]+1; getson(v,x); sz[x]+=sz[v]; if(sz[v]>sz[son[x]]) son[x]=v; } } void dfs(int x,int f,int tp){ dfn[x]=++dfn0; top[x]=tp; if(son[x]) dfs(son[x],x,tp); for(auto v:e[x]){ if(v==f||v==son[x]) continue; dfs(v,x,v); } } void divide(){ getson(rt,0); dfs(rt,0,rt); clk++; tr[0].build(1,1,dfn0,clk); } int get_lca(int x,int y){ while(top[x]!=top[y]){ if(dep[top[x]]>dep[top[y]]) swap(x,y); y=fa[top[y]]; } if(dep[x]>dep[y]) swap(x,y); return x; } void change(int x,int y){ int lca=get_lca(x,y); clk+=2; if(lca!=x){ while(top[x]!=top[lca]){ tr[0].change(1,1,dfn0,dfn[top[x]],dfn[x],{0,clk}); if(son[x]) tr[0].change(1,1,dfn0,dfn[son[x]],dfn[son[x]],{1,clk-1}); tr[1].change(1,1,dfn0,dfn[top[x]],dfn[x],{1,clk-1}); x=fa[top[x]]; } if(x!=lca) tr[0].change(1,1,dfn0,dfn[son[lca]],dfn[x],{0,clk}); if(son[x]) tr[0].change(1,1,dfn0,dfn[son[x]],dfn[son[x]],{1,clk-1}); tr[1].change(1,1,dfn0,dfn[lca],dfn[x],{1,clk-1}); } if(lca!=y){ while(top[y]!=top[lca]){ tr[0].change(1,1,dfn0,dfn[top[y]],dfn[y],{0,clk}); if(son[y]) tr[0].change(1,1,dfn0,dfn[son[y]],dfn[son[y]],{1,clk-1}); tr[1].change(1,1,dfn0,dfn[top[y]],dfn[y],{1,clk-1}); y=fa[top[y]]; } if(y!=lca) tr[0].change(1,1,dfn0,dfn[son[lca]],dfn[y],{0,clk}); if(son[y]) tr[0].change(1,1,dfn0,dfn[son[y]],dfn[son[y]],{1,clk-1}); tr[1].change(1,1,dfn0,dfn[lca],dfn[y],{1,clk-1}); } if(fa[lca]) tr[0].change(1,1,dfn0,dfn[lca],dfn[lca],{1,clk}); if(x==y){ if(son[x]) tr[0].change(1,1,dfn0,dfn[son[x]],dfn[son[x]],{1,clk-1}); tr[1].change(1,1,dfn0,dfn[x],dfn[x],{1,clk-1}); } } int search(int x,int y){ int res=0,lca=get_lca(x,y); if(lca!=x){ while(top[x]!=top[lca]){ if(top[x]!=x) res+=tr[0].search(1,1,dfn0,dfn[son[top[x]]],dfn[x]); auto arr0=tr[0].search(1,1,dfn0,dfn[top[x]]); auto arr1=tr[1].search(1,1,dfn0,dfn[fa[top[x]]]); res+=(arr0[1]>=arr1[1]?arr0[0]:arr1[0]); x=fa[top[x]]; } if(x!=lca) res+=tr[0].search(1,1,dfn0,dfn[son[lca]],dfn[x]); } if(lca!=y){ while(top[y]!=top[lca]){ if(top[y]!=y) res+=tr[0].search(1,1,dfn0,dfn[son[top[y]]],dfn[y]); auto arr0=tr[0].search(1,1,dfn0,dfn[top[y]]); auto arr1=tr[1].search(1,1,dfn0,dfn[fa[top[y]]]); res+=(arr0[1]>=arr1[1]?arr0[0]:arr1[0]); y=fa[top[y]]; } if(y!=lca) res+=tr[0].search(1,1,dfn0,dfn[son[lca]],dfn[y]); } return res; } }
int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
int n; cin>>n;
TCD::init(n,1); for(int i=1,x,y;i<n;i++){ cin>>x>>y; TCD::add(x,y); } TCD::divide();
int q; cin>>q;
while(q--){ int op; cin>>op;
if(op==0){ int x,y; cin>>x>>y;
cout<<TCD::search(x,y)<<"\n"; } else{ int x,y; cin>>x>>y;
TCD::change(x,y); } }
return true&&false; }
|