浅谈一种通用型树分块

· · 个人记录

好难啊。

整编自 《浅谈一类树分块的构建算法及其应用》 周欣。

首先要感谢 @gxy001 的帮助,没有他的帮助也就没有这篇博客了。(因为我根本学都学不会)

其次还是要感谢 @gxy001 的帮助,如果没有他的帮助就没有模板代码了!!!

接着要感谢 @GuidingStar 和 @b1ngxu 的 Hack ,没有你们的 Hack 那我也不会发现我的树分块代码存在巨大的问题。

这篇树分块不同于比较经典的树剖后链上树分块以及树上随机撒点,是一种万能型的树分块,并且常数也比较小,不难码,更能完成一些比较复杂的问题,唯一的缺点是细节比较多。

top cluster

这章的定义部分了解清楚即可。

定义:一个树簇表示树上的一个联通子图,存在至多两个点与树上的其他位置相连接,我们称这两个点为界点。

我们定义如下几个概念:

在学完后我来补充一下这一章,为什么要用分割树簇作为树分块的一个载体,正是因为一个树簇上只有两个点与树上其他位置有关联,这就可以类比分块,很方便我们进行块内操作和整体操作。

同时,树分块有个比较麻烦的地方就是它是可以交点的,所以我们有时候得重复操作一些点带来一些额外的常数。

树分块基本结构

树分块的本质思想是想要将树划分为 \frac{n}{B} 数量级个数的不交并的树簇,其中 B 为块长。

并且我们把树簇收好之后得到了一个小树,这个树叫做收缩树。

我们可以重新回过头来理解一下:树簇的定义是不是一个树上的联通子图,且该联通子图中只有至多两个界点与树上其他位置相连,这就很像我们的分块,块内其实都是执行标记或者重构来做,只要知道了两端点就很好办了。

那么我们现在需要一种易于实现的方法,这里就直接用论文的方法了:

考虑选择任意一个点为根进行 dfs 算法,我们来一个栈维护当前未被归类进入一个簇的边,在该点结束 dfs 后,一些特定情况下需要把当前栈中的边拿出来处理:

  1. 当当前 u 为根时。这么做是为了我们方便接下来的操作而强制根节点在最后的收缩树中;

  2. 当当前点的不同子树中出现了两个不同的簇的界点,当前点就必须要成为界点来断开这两个簇;

  3. 当栈中剩余边数量大于 B 。

首先情况 1是特殊情况直接不管,我们只来看情况 2 和情况 3 中弹栈处理的次数。

从情况 3 开始确定,我们弹栈处理的次数肯定不超过 \frac{n}{B} 。

接着我们考虑情况 2 的本质:其实就是对于多个界点合并,在向上走的时候相当于就只有 1 个界点了,每弹栈一次就至少减去一个在情况 3 中产生的初始界点,那么我们情况 2 中弹栈处理的次数是不是就只有 2 \times \frac{n}{B} 了。

综上我们的弹栈处理次数就在 O(\frac{n}{B}) 这个级别了。

接着我们来证明一下簇个数为 \frac{n}{B} 级别。

首先我们要抽象化一下我们的弹栈处理过程:

我们考虑一下我们结束合并的条件:

其中,第一个条件肯定至多划分出来了 2 \times \frac{n}{B} 个簇,第二三个条件至多划分出来的簇的个数即我们的弹栈次数 2 \times 2 \times \frac{n}{B} = 4 \times \frac{n}{B} 。

这样,我们划分出来的簇的个数至多只为 6 \times \frac{n}{B} 个,则簇个数为 \frac{n}{B} 级别。

一般都是这么个流程来做 top cluster 树分块:

一般都是 \sqrt n 的时间复杂度完成所有查询,但是对于特殊的题目例如 rpdq ,就需要用一些技巧完成 O(1) 查询,这里也是 top cluster 树分块的一个优越性。

top cluster 树分块经典例题

本来top cluster的树分块可以做动态树,但是我不想学。

P4211[LNOI2014]LCA

终于来到了代码讲解块。

再定义两个概念:

首先是几个函数:

为了实现方便,我们用一个树簇的下界点标识一个树簇。

```cpp #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int Len = 1e5 + 5 , Mod = 201314; int n,m,pos[Len],tem[Len],treebl; struct node { int to,val; node(){to = val = 0;} node(int TO,int VAL){to = TO , val = VAL;} }sim; vector<node> v[Len]; int dep[Len],fa[Len],Fa[Len]; int top[Len],siz[Len],stk[Len],Top,Nearp[Len],cnt,btm[Len],ind[Len],Toper[Len],Btmer[Len]; int ST[Len],Topp; int block[Len],lenbk; vector<int> nde[Len]; bool vis[Len]; int val[Len],sum[Len],ADD[Len]; void Add(int from,int to,int val){v[from].push_back(node(to , val));} void add(int x,int y) { if(y) { block[++ lenbk] = y; Fa[y] = x; for(int now = y ; now != x ; now = fa[now]) vis[now] = 1; } for(int i = 1 ; i <= Top ; i ++) Nearp[stk[i]] = 0; for(int i = 1 ; i <= Top ; i ++) { btm[stk[i]] = y; if(stk[i] != y) top[stk[i]] = x; if(!y) { Nearp[stk[i]] = x; continue; } int p = x; for(int now = stk[i] ; now != x ; now = fa[now]) { if(vis[now]) { p = now; break; } if(Nearp[now]) { p = Nearp[now]; break; } } Nearp[stk[i]] = p; } if(y) for(int now = y ; now != x ; now = fa[now]) vis[now] = 0; } vector<node>::iterator itor; void dfs(int x,int f) { fa[x] = f; for (itor = v[x].begin(); itor != v[x].end(); itor ++) { sim = *itor; if (sim.to == f) { itor = v[x].erase(itor); break; } } siz[x] = 1; top[x] = 0; int Cnt = 0; for(int i = 0 ; i < v[x].size() ; i ++) { int to = v[x][i].to , val = v[x][i].val; if(to == f) continue; dep[to] = dep[x] + val; ST[Topp ++] = to; dfs(to , x); if(top[to]) Cnt ++ , top[x] = top[to]; siz[x] += siz[to]; } int Btm = 0; if(siz[x] >= treebl || Cnt > 1 || x == 1) { int sz = 0 , ct = 0; for(int i = v[x].size() - 1 ; i >= 0 ; i --) { int to = v[x][i].to , val = v[x][i].val; if(to == f) continue; sz += siz[to]; if(top[to]) ct ++; if(sz > treebl || ct > 1) { sz = siz[to]; if(top[to]) ct = 1; Top = 0; cnt ++; nde[cnt].clear(); Btm = 0; do { stk[++ Top] = ST[-- Topp]; Btm = max(Btm , top[ST[Topp]]); ind[ST[Topp]] = cnt; nde[cnt].push_back(ST[Topp]); }while(ST[Topp] != v[x][i + 1].to); add(x , Btm); Toper[cnt] = x , Btmer[cnt] = Btm; } } cnt ++;nde[cnt].clear();Top = 0;Btm = 0;//printf("###%d:\n",cnt); do { stk[++ Top] = ST[-- Topp]; Btm = max(Btm , top[ST[Topp]]); ind[ST[Topp]] = cnt; nde[cnt].push_back(ST[Topp]); }while(ST[Topp] != v[x][0].to); add(x , Btm); Toper[cnt] = x , Btmer[cnt] = Btm; top[x] = x; siz[x] = 1; } } void Addval(int x) { if(x == 1) return; int tmp = (!Fa[x]) ? btm[x] : 0; for(int i = 1 ; i < lenbk ; i ++) sum[block[i]] -= sum[Fa[block[i]]];//把收缩树给杀回来,要注意这里1 ~ i其实是深度递减的树簇 if(tmp) sum[tmp] += dep[Nearp[x]] - dep[top[x]]; if(!Fa[x]) { int j = ind[x] , u; for(int i = nde[j].size() - 1 ; i >= 0 ; i --) { u = nde[j][i]; if(!Fa[fa[u]]) val[u] -= val[fa[u]];//如果父亲不是top } for(; !Fa[x] && x > 1 ; x = fa[x]) val[x] += dep[x] - dep[fa[x]]; for(int i = 0 ; i < nde[j].size() ; i ++)//这里顺序也不能反 { u = nde[j][i]; if(!Fa[fa[u]]) val[u] += val[fa[u]]; } //for(int i = 0 ; i < nde[j].size() ; i ++) printf("%d\n",val[nde[j][i]]); } for(; x >= 1 ; x = Fa[x]) { ADD[x] ++; sum[x] += dep[x] - dep[Fa[x]]; } for(int i = lenbk - 1 ; i >= 1 ; i --) sum[block[i]] += sum[Fa[block[i]]]; } inline int GetAns(int x) { int bel = ind[x]; int res = val[x] * (Btmer[bel] != x) + sum[top[x]]; if(!Fa[x] && btm[x]) res += (dep[Nearp[x]] - dep[top[x]]) * ADD[btm[x]]; return res; } struct Node { int idx,z,wh,opt; Node(){} Node(int a,int b,int c,int d){idx = a , z = b , wh = c , opt = d;} }Ques[Len << 1]; int Print[Len]; bool cmp(Node x , Node y){return x.z < y.z;} bool cmd(int x,int y){return dep[x] < dep[y];} signed main() { scanf("%d %d",&n,&m); treebl = sqrt(n); for(int i = 2 ; i <= n ; i ++) { int x,y; scanf("%d",&x);x ++; Add(x , i , 1); } int cnts = 0; for(int i = 1 ; i <= m ; i ++) { int x,y,z; scanf("%d %d %d",&x,&y,&z); y ++ , z ++; Ques[++ cnts] = Node(i , x , z , -1); Ques[++ cnts] = Node(i , y , z , 1); Print[i] = y - x; } sort(Ques + 1 , Ques + 1 + cnts , cmp); dfs(1 , 0); for(int i = 1 ; i <= cnt ; i ++) sort(nde[i].begin() , nde[i].end() , cmd); int nowAdd = 0; for(int i = 1 ; i <= cnts ; i ++) { while(nowAdd < Ques[i].z) Addval(++ nowAdd); Print[Ques[i].idx] = (Print[Ques[i].idx] + Ques[i].opt * GetAns(Ques[i].wh) + Mod) % Mod; } for(int i = 1 ; i <= m ; i ++) printf("%d\n",Print[i]); return 0; } ``` # [uoj435](https://uoj.ac/problem/435) ~~爷先咕为敬。~~ # [P6778 [Ynoi2009] rpdq](https://www.luogu.com.cn/problem/P6778) 在[此题](https://www.luogu.com.cn/problem/P4211)的铺垫下,我们只需二次离线莫队然后来一个 $O(\sqrt n)$ 修改 $O(1)$ 查询的数据结构即可,这里考虑 top cluster 树分块。 ```cpp #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> using namespace std; const int Len = 2e5 + 5; int n,m,pos[Len],tem[Len],treebl; struct node { int to; unsigned int val; node(){to = val = 0;} node(int TO,unsigned int VAL){to = TO , val = VAL;} }sim; vector<node> v[Len]; int dep[Len],fa[Len],Fa[Len]; int top[Len],siz[Len],stk[Len],Top,Nearp[Len],cnt,btm[Len],ind[Len],Toper[Len],Btmer[Len]; int ST[Len],Topp; int block[Len],lenbk; vector<int> nde[Len]; bool vis[Len]; int val[Len],sum[Len],ADD[Len]; void Add(int from,int to,int val){v[from].push_back(node(to , val));} void add(int x,int y) { if(y) { block[++ lenbk] = y; Fa[y] = x; for(int now = y ; now != x ; now = fa[now]) vis[now] = 1; } for(int i = 1 ; i <= Top ; i ++) Nearp[stk[i]] = 0; for(int i = 1 ; i <= Top ; i ++) { btm[stk[i]] = y; if(stk[i] != y) top[stk[i]] = x; if(!y) { Nearp[stk[i]] = x; continue; } int p = x; for(int now = stk[i] ; now != x ; now = fa[now]) { if(vis[now]) { p = now; break; } if(Nearp[now]) { p = Nearp[now]; break; } } Nearp[stk[i]] = p; } if(y) for(int now = y ; now != x ; now = fa[now]) vis[now] = 0; } vector<node>::iterator itor; void dfs(int x,int f) { fa[x] = f; for (itor = v[x].begin(); itor != v[x].end(); itor ++) { sim = *itor; if (sim.to == f) { itor = v[x].erase(itor); break; } } siz[x] = 1; top[x] = 0; int Cnt = 0; for(int i = 0 ; i < v[x].size() ; i ++) { int to = v[x][i].to , val = v[x][i].val; if(to == f) continue; dep[to] = dep[x] + val; ST[Topp ++] = to; dfs(to , x); if(top[to]) Cnt ++ , top[x] = top[to]; siz[x] += siz[to]; } int Btm = 0; if(siz[x] >= treebl || Cnt > 1 || x == 1) { int sz = 0 , ct = 0; for(int i = v[x].size() - 1 ; i >= 0 ; i --) { int to = v[x][i].to , val = v[x][i].val; if(to == f) continue; sz += siz[to]; if(top[to]) ct ++; if(sz > treebl || ct > 1) { sz = siz[to]; if(top[to]) ct = 1; Top = 0; cnt ++; nde[cnt].clear(); Btm = 0; do { stk[++ Top] = ST[-- Topp]; Btm = max(Btm , top[ST[Topp]]); ind[ST[Topp]] = cnt; nde[cnt].push_back(ST[Topp]); }while(ST[Topp] != v[x][i + 1].to); add(x , Btm); Toper[cnt] = x , Btmer[cnt] = Btm; } } cnt ++;nde[cnt].clear();Top = 0;Btm = 0;//printf("###%d:\n",cnt); do { stk[++ Top] = ST[-- Topp]; Btm = max(Btm , top[ST[Topp]]); ind[ST[Topp]] = cnt; nde[cnt].push_back(ST[Topp]); }while(ST[Topp] != v[x][0].to); add(x , Btm); Toper[cnt] = x , Btmer[cnt] = Btm; top[x] = x; siz[x] = 1; } } void Addval(int x) { if(x == 1) return; int tmp = (!Fa[x]) ? btm[x] : 0; for(int i = 1 ; i < lenbk ; i ++) sum[block[i]] -= sum[Fa[block[i]]];//把收缩树给杀回来,要注意这里1 ~ i其实是深度递减的树簇 if(tmp) sum[tmp] += dep[Nearp[x]] - dep[top[x]]; if(!Fa[x]) { int j = ind[x] , u; for(int i = nde[j].size() - 1 ; i >= 0 ; i --) { u = nde[j][i]; if(!Fa[fa[u]]) val[u] -= val[fa[u]];//如果父亲不是top } for(; !Fa[x] && x > 1 ; x = fa[x]) val[x] += dep[x] - dep[fa[x]]; for(int i = 0 ; i < nde[j].size() ; i ++)//这里顺序也不能反 { u = nde[j][i]; if(!Fa[fa[u]]) val[u] += val[fa[u]]; } //for(int i = 0 ; i < nde[j].size() ; i ++) printf("%d\n",val[nde[j][i]]); } if(!Fa[x]) x = top[x]; for(; x >= 1 ; x = Fa[x]) { ADD[x] ++; sum[x] += dep[x] - dep[Fa[x]]; } for(int i = lenbk - 1 ; i >= 1 ; i --) sum[block[i]] += sum[Fa[block[i]]]; } inline unsigned int GetAns(int x) { int bel = ind[x]; unsigned int res = val[x] * (Btmer[bel] != x) + sum[top[x]]; if(!Fa[x] && btm[x]) res += (dep[Nearp[x]] - dep[top[x]]) * ADD[btm[x]]; return res; } struct nnode { int l,r; unsigned int ans; int idx; }Sec[Len]; struct Node { int l,r,idx,op; Node(){l = 0 , r = 0 , idx = 0 , op = 0;} Node(const int L , const int R , const int IDX , const int OP){l = L , r = R , idx = IDX , op = OP;} }; vector<Node> q[Len]; unsigned int Print[Len]; int p1[Len],p2[Len],Block[Len],t,Sum[Len]; bool cmp(nnode x , nnode y) { if(Block[x.l] ^ Block[y.l]) return Block[x.l] < Block[y.l]; return x.r < y.r; } bool cmd(int x,int y){return dep[x] < dep[y];} signed main() { scanf("%d %d",&n,&m); treebl = sqrt(n); t = sqrt(n); for(int i = 1 ; i <= n ; i ++) Block[i] = (i - 1) / t + 1; for(int i = 2 ; i <= n ; i ++) { int x,y;unsigned int z; scanf("%d %d %u",&x,&y,&z); Add(x , y , z) , Add(y , x , z); } int cnts = 0; for(int i = 1 ; i <= m ; i ++) { scanf("%d %d",&Sec[i].l,&Sec[i].r); Sec[i].idx = i; } sort(Sec + 1 , Sec + 1 + m , cmp); dfs(1 , 0); for(int i = 1 ; i <= cnt ; i ++) sort(nde[i].begin() , nde[i].end() , cmd); int l = 1 , r = 0; for(int i = 1 ; i <= m ; i ++) { if(l > Sec[i].l) q[r].push_back(Node(Sec[i].l , l - 1 , i , 1)) , l = Sec[i].l; if(r < Sec[i].r) q[l - 1].push_back(Node(r + 1 , Sec[i].r , i , -1)) , r = Sec[i].r; if(l < Sec[i].l) q[r].push_back((Node){l , Sec[i].l - 1 , i , -1}) , l = Sec[i].l;//因为对于原来的答案而言,现在是要减去这些值,所以把原式取了个-1 if(r > Sec[i].r) q[l - 1].push_back((Node){Sec[i].r + 1 , r , i , 1}) , r = Sec[i].r;//与上同 } Node Calc; for(int i = 1 ; i <= n ; i ++) { p1[i] = GetAns(i); Addval(i); for(int j = 0 ; j < q[i].size() ; j ++) { Calc = q[i][j]; for(int k = Calc.l ; k <= Calc.r ; k ++) Sec[Calc.idx].ans += Calc.op * GetAns(k); } p2[i] = GetAns(i); } l = 1 , r = 0; for(int i = 1 ; i <= m ; i ++) { while(l > Sec[i].l) Sec[i].ans -= p2[-- l]; while(r < Sec[i].r) Sec[i].ans += p1[++ r]; while(l < Sec[i].l) Sec[i].ans += p2[l ++]; while(r > Sec[i].r) Sec[i].ans -= p1[r --]; } for(int i = 1 ; i <= n ; i ++) Sum[i] = Sum[i - 1] + dep[i]; for(int i = 1 ; i <= m ; i ++) Sec[i].ans += Sec[i - 1].ans; for(int i = 1 ; i <= m ; i ++) Print[Sec[i].idx] = (Sec[i].r - Sec[i].l) * (Sum[Sec[i].r] - Sum[Sec[i].l - 1]) - 2 * Sec[i].ans; for(int i = 1 ; i <= m ; i ++) printf("%u\n",Print[i]); return 0; } ``` Upd:2022/3/24 改了一个也会被菊花图卡的但写的更简洁的同学的代码。 把这个当模板效果会好很多。 ```cpp /* 改一下一个写的很精简但是是错误树分块同学的代码 ~~其实是自己想嫖来当模板~~ 写出来应该只能保证块的大小是 2B 级别 */ #include <bits/stdc++.h> #define ud unsigned #define F(i, l, r) for (int i = (l); i <= (r); i++) #define R(i, r, l) for (int i = r; i >= l; i--) #define bi(x) ((x - 1) / S + 1) using namespace std; const int N = 4e5 + 5, Qn = 4e5 + 5; int n, q, S, B, fa[N], top[N], id[N], btm[N], np[N], cnt; ud dep[N], sdep[N], sum[N], add[N], val[N], pre[N], ret[N], ans[N] , sizv[N]; vector<pair<int, int>> g[N]; vector<int> nd[N], p2; struct Q { int l, r, id; friend bool operator < (Q a, Q b) { if (bi(a.l) != bi(b.l)) return bi(a.l) < bi(b.l); return bi(a.l) & 1 ? a.r < b.r : a.r > b.r; } } qr[Qn]; vector<Q> qry[N]; inline unsigned sgn(int x) { return x < 0 ? -1 : 1; } stack<int> st; void get1(int u, int tp, int bt, int p) { nd[id[u] = cnt].push_back(u), top[u] = tp, np[u] = p; if (btm[u] == u) return; btm[u] = bt; for (auto [v, w] : g[u]) if (v != fa[u]) get1(v, tp, bt, btm[v] ? v : p); } void get2(int u, int tp, int bt, int p) { nd[id[u] = cnt].push_back(u), top[u] = tp, np[u] = p; if (btm[u] == u) return; btm[u] = bt; for (auto [v, w] : g[u]) if (v != fa[u]) get2(v, tp , bt, p); } void dfs(int u) { int c = 0, cur = st.size();sizv[u] = 1; for (auto [v, w] : g[u]) if (v != fa[u]) { fa[v] = u, dep[v] = dep[u] + w, dfs(v); if (btm[v]) ++c, btm[u] = btm[v]; } if (c >= 2 || (int)st.size() - cur >= B || u == 1) { btm[u] = u, sizv[u] = 0 , p2.push_back(u); while ((int)st.size() > cur) st.pop(); int nowc = 0 , Siz = 0 , lst = 0 , v , uu = 0; for (int i = 0 ; i < g[u].size() ; i ++) { v = g[u][i].first; if (v == fa[u]) continue; Siz += sizv[v]; if(Siz >= B || (btm[v] && uu)) { ++ cnt; for(int j = lst ; j < i ; j ++) { int to = g[u][j].first; if(to == fa[u]) continue; if(to == uu) get1(to , u, btm[uu], btm[uu] ? uu : 0); else get2(to , u , btm[uu] , btm[uu] ? u : 0); } Siz = 0 , uu = 0; lst = i; i --; continue; } if(btm[v]) uu = v; } if(lst != g[u].size()) { int i = g[u].size() - 1; if(uu) v = uu; else v = g[u][i].first; ++ cnt; Siz = 0; for(int j = lst ; j < g[u].size() ; j ++) { int to = g[u][j].first; if(to == fa[u]) continue; if(to == v) get1(to, u, btm[v], btm[v] ? v : 0); else get2(to , u , btm[v] , btm[v] ? u : 0); } } } st.push(u); } void ins(int u) { if (u == 1) return ; int p = id[u]; F(i, 0, (int)p2.size() - 1) sum[p2[i]] -= sum[top[p2[i]]]; if (u != btm[u]) { R(i, (int)nd[p].size() - 1, 0) { int x = nd[p][i]; if (id[fa[x]] == p) val[x] -= val[fa[x]]; } if (btm[u]) sum[btm[u]] += dep[np[u]] - dep[top[u]]; while (u != btm[u]) { val[u] += dep[u] - dep[fa[u]]; u = fa[u]; } F(i, 0, (int)nd[p].size() - 1) { int x = nd[p][i]; if (id[fa[x]] == p) val[x] += val[fa[x]]; } } while (u) ++add[u], sum[u] += dep[u] - dep[top[u]], u = top[u]; R(i, (int)p2.size() - 1, 0) sum[p2[i]] += sum[top[p2[i]]]; } ud query(int u) { return val[u] + sum[top[u]] + (btm[u] ? add[btm[u]] * (dep[np[u]] - dep[top[u]]) : 0); } int main() { scanf("%d%d", &n, &q); F(i, 1, n - 1) { int u, v, w; scanf("%d%d%d", &u, &v, &w); g[u].push_back({v, w}); g[v].push_back({u, w}); } S = n / sqrt(q), B = sqrt(n), dfs(1); //printf("###%d\n",cnt); F(i, 1, n) sdep[i] = sdep[i - 1] + dep[i]; F(i, 1, q) { scanf("%d%d", &qr[i].l, &qr[i].r), qr[i].id = i; ans[i] = (sdep[qr[i].r] - sdep[qr[i].l - 1]) * (qr[i].r - qr[i].l); } sort(qr + 1, qr + q + 1); F(i, 1, n) pre[i] = query(i), ins(i); memset(sum, 0, sizeof(sum)); memset(val, 0, sizeof(val)); memset(add, 0, sizeof(add)); int L = 1, R = 0; F(i, 1, q) { int l = qr[i].l, r = qr[i].r; if (L > l) qry[R].push_back({l, L - 1, i}); while (L > l) ret[i] -= pre[L - 1], L--; if (R < r) qry[L - 1].push_back({R + 1, r, -i}); while (R < r) ret[i] += pre[R + 1], R++; if (L < l) qry[R].push_back({L, l - 1, -i}); while (L < l) ret[i] += pre[L], L++; if (R > r) qry[L - 1].push_back({r + 1, R, i}); while (R > r) ret[i] -= pre[R], R--; } F(i, 1, n) { ins(i); for (auto p : qry[i]) F(k, p.l, p.r) ret[abs(p.id)] += sgn(p.id) * (query(k) - (k <= i ? dep[k] : 0)); } F(i, 1, q) ret[i] += ret[i - 1], ans[qr[i].id] -= (ud)2 * ret[i]; F(i, 1, q) printf("%u\n", ans[i]); return 0; } ```