部落冲突
树剖板子题……开战的话我们把深度比较深的那个点点权设为1,然后每次在一条路径上query即可。停战的话改回来。
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<set> #include<queue> #define rep(i,a,n) for(int i = a;i <= n;i++) #define per(i,n,a) for(int i = n;i >= a;i--) #define enter putchar('\n') using namespace std; typedef long long ll; const int M = 300005; const int INF = 1000000009; int read() { int ans = 0,op = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') op = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { ans *= 10; ans += ch - '0'; ch = getchar(); } return ans * op; } struct edge { int next,to; }e[M<<1]; struct seg { int v; }t[M<<2]; int n,m,p,q,dep[M],size[M],top[M],hson[M],head[M],dfn[M],war[M],ecnt,x,idx,cnt,fa[M]; char s[5]; void add(int x,int y) { e[++ecnt].to = y; e[ecnt].next = head[x]; head[x] = ecnt; } int Max(int x,int y) { return (dep[x] > dep[y]) ? x : y; } void dfs1(int x,int f,int depth) { size[x] = 1,fa[x] = f,dep[x] = depth; int maxson = -1; for(int i = head[x];i;i = e[i].next) { if(e[i].to == f) continue; dfs1(e[i].to,x,depth+1); size[x] += size[e[i].to]; if(size[e[i].to] > maxson) maxson = size[e[i].to],hson[x] = e[i].to; } } void dfs2(int x,int t) { top[x] = t,dfn[x] = ++idx; if(!hson[x]) return; dfs2(hson[x],t); for(int i = head[x];i;i = e[i].next) { if(e[i].to == fa[x] || e[i].to == hson[x]) continue; dfs2(e[i].to,e[i].to); } } void modify(int p,int l,int r,int pos,int val) { if(l == r) { t[p].v = val; return; } int mid = (l+r) >> 1; if(pos <= mid) modify(p<<1,l,mid,pos,val); else modify(p<<1|1,mid+1,r,pos,val); t[p].v = t[p<<1].v + t[p<<1|1].v; } int query(int p,int l,int r,int kl,int kr) { if(l == kl && r == kr) return t[p].v; int mid = (l+r) >> 1; if(kr <= mid) return query(p<<1,l,mid,kl,kr); else if(kl > mid) return query(p<<1|1,mid+1,r,kl,kr); else return query(p<<1,l,mid,kl,mid) + query(p<<1|1,mid+1,r,mid+1,kr); } void mrange(int x,int y) { while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x,y); int g = query(1,1,n,dfn[top[x]],dfn[x]); if(g) { printf("No\n"); return; } x = fa[top[x]]; } if(dep[x] > dep[y]) swap(x,y); if(dfn[x] + 1 > dfn[y]) { printf("Yes\n"); return; } int g = query(1,1,n,dfn[x]+1,dfn[y]); g ? printf("No\n") : printf("Yes\n"); } int main() { n = read(),m = read(); rep(i,1,n-1) p = read(),q = read(),add(p,q),add(q,p); dfs1(1,1,0),dfs2(1,1); rep(i,1,m) { scanf("%s",s); if(s[0] == 'Q') p = read(),q = read(),mrange(p,q); else if(s[0] == 'C') p = read(),q = read(),modify(1,1,n,dfn[Max(p,q)],1),war[++cnt] = Max(p,q); else if(s[0] == 'U') x = read(),modify(1,1,n,dfn[war[x]],0); } return 0; }
当你意识到,每个上一秒都成为永恒。