#include using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair; using tll=tuple; using ld=long double; const ll INF=(1ll<<60); #define rep(i,n) for (ll i=0;i<(ll)(n);i++) #define replr(i,l,r) for (ll i=(ll)(l);i<(ll)(r);i++) #define all(v) v.begin(),v.end() #define len(v) ((ll)v.size()) template inline bool chmin(T &a,T b){ if(a>b){ a=b; return true; } return false; } template inline bool chmax(T &a,T b){ if(a struct fenwick_tree{ vector v; int n; fenwick_tree(int x){ n=x+1; v.assign(n+1,0); } fenwick_tree(vector &a){ n=(int)a.size()+1; v.assign(n+1,0); if((int)a.size()==0) return; v[1]=a[0]; for(int i=0;i>=1){ if(x+k<=n&&v[x+k]> g; vector> parent; vector depth; tree(int g_size){ n=g_size; g.resize(n); } void add_edge(int u,int v){ g[u].push_back(v); g[v].push_back(u); } void LCA(int root=0){ while((1<(n,-1)); depth.assign(n,-1); dfs(root,-1,0); for(int k=0;k+1> g; vector par,sz,in,out,head; int in_ord=0,out_ord=0; HLD(int x){ n=x; g.resize(n); par.resize(n); sz.assign(n,0); in.resize(n); out.resize(n); head.resize(n); } void add_edge(int u,int v){ g[u].push_back(v); g[v].push_back(u); } void dfs_sz(int x,int p){ sz[x]++; if(0> query(int u,int v,bool edge){ vector> ret; while(true){ if(in[v]> n; ll sz=n+n-1; vector p(n,-1); tree g(sz); HLD h(sz); replr(i,1,n){ cin >> p[i]; p[i]--; g.add_edge(i,n+i-1); g.add_edge(p[i],n+i-1); h.add_edge(i,n+i-1); h.add_edge(p[i],n+i-1); } string s(n,'x'); replr(i,1,n) cin >> s[i]; ll q; cin >> q; vector u(q),v(q); rep(i,q){ cin >> u[i] >> v[i]; u[i]--; v[i]--; } g.LCA(); rep(i,q){ ll x=g.get_lca(u[i],v[i]); ll nu=u[i],nv=v[i]; if(u[i]==x){ nu=g.doubling(v[i],g.depth[v[i]]-g.depth[u[i]]-1); }else{ nu=g.parent[0][u[i]]; } if(v[i]==x){ nv=g.doubling(u[i],g.depth[u[i]]-g.depth[v[i]]-1); }else{ nv=g.parent[0][v[i]]; } u[i]=nu; v[i]=nv; } h.solve(0); vector out(sz),ord(sz); rep(i,sz) out[i]=h.out[i]; rep(i,sz) ord[out[i]]=i; vector> lr(sz); rep(i,q){ if(out[v[i]] BIT(sz); auto add=[&](ll a,ll b){ auto now=h.query(a,b,false); for(auto [l,r]:now){ BIT.add(l,r,1); } }; auto get=[&](ll x){ auto now=h.query(x,x,false); return BIT.get(now[0].first); }; replr(i,1,n){ if(s[i]=='#') add(n+i-1,n+i-1); } for(auto i:ord){ if(!(get(i)&1)) continue; if(lr[i].empty()) continue; ll j=*begin(lr[i]); add(i,j); lr[i].erase(j); if(len(lr[i])