[Ynoi2011] 成都七中
Solution
不是分块的Ynoi。/jk
我们注意到树上一个连通块一定存在一个节点使得连通块里面所有节点都在它子树内。点分树同理。那么对于一次查询 \((l,r,x)\),我们可以找到点分树上深度最低的节点 \(u\) 使得在保留 \([l,r]\) 的情况下 \(x,u\) 连通,那么在 \(u\) 处考虑一定最优。所以我们可以把这个查询插到 \(u\) 这个节点。
然后对于每一个节点我们都直接做一遍就好了,直接用树状数组之类的维护一下就好了。复杂度 \(\Theta(n\log^2 n)\)。
这个问题解决的关键之处就在于,树上连通块的包含关系可以用点分治优化,其原本的祖先关系并不重要了。
Code
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define MAXN 100005
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}
int n,m,col[MAXN];
vector <int> g[MAXN];
struct node{
int x,y,z,type;
bool operator < (const node &p)const{return x != p.x ? x > p.x : (type < p.type);}
};
vector <node> s[MAXN],p[MAXN];
bool vis[MAXN];
int rt,siz[MAXN],mxs[MAXN];
void findroot (int u,int fa,int all){
siz[u] = 1,mxs[u] = 0;
for (Int v : g[u]) if (v != fa && !vis[v]) findroot (v,u,all),siz[u] += siz[v],chkmax (mxs[u],siz[v]);
chkmax (mxs[u],all - siz[u]),rt = !rt ? u : (mxs[rt] > mxs[u] ? u : rt);
}
void dfs (int u,int fa,int miv,int mxv){
chkmin (miv,u),chkmax (mxv,u),s[rt].push_back (node{miv,mxv,col[u],0}),p[u].push_back (node{miv,mxv,rt,0});
for (Int v : g[u]) if (v != fa && !vis[v]) dfs (v,u,miv,mxv);
}
void solve (int u,int Siz){
vis[u] = 1,rt = u,dfs (u,0,u,u);
for (Int v : g[u]) if (!vis[v]){
int all = siz[u] > siz[v] ? siz[v] : (Siz - siz[u]);
rt = 0,findroot (v,u,all),solve (rt,all);
}
}
int ans[MAXN],app[MAXN];
struct BIT{
int sum[MAXN];
int lowbit (int x){return x & (-x);}
void modify (int x,int v){for (Int i = x;i <= n;i += lowbit (i)) sum[i] += v;}
int query (int x){int res = 0;for (Int i = x;i;i -= lowbit (i)) res += sum[i];return res;}
void backit (int x){for (Int i = x;i <= n;i += lowbit (i)) sum[i] = 0;}
}tree;
signed main(){
read (n,m);
for (Int u = 1;u <= n;++ u) read (col[u]);
for (Int i = 2,u,v;i <= n;++ i) read (u,v),g[u].push_back (v),g[v].push_back (u);
findroot (1,0,n),solve (rt,n);
for (Int i = 1,l,r,x;i <= m;++ i){
read (l,r,x);
for (node it : p[x]) if (l <= it.x && it.y <= r){s[it.z].push_back (node{l,r,x,i});break;}
}
memset (app,0x3f,sizeof (app));
for (Int u = 1;u <= n;++ u){
sort (s[u].begin(),s[u].end());
for (node it : s[u]){
if (it.type) ans[it.type] = tree.query (it.y);
else if (it.y < app[it.z]) tree.modify (app[it.z],-1),tree.modify (it.y,1),app[it.z] = it.y;
}
for (node it : s[u]) if (!it.type) app[it.z] = n + 1,tree.backit (it.y);
}
for (Int i = 1;i <= m;++ i) write (ans[i]),putchar ('\n');
return 0;
}