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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
| #include<bits/stdc++.h> namespace Pozhu{ using namespace std; #define N 100010 int n,q; vector<int>g[N]; struct Tree{ int ls,rs; int sum; #define ls(x) t[x].ls #define rs(x) t[x].rs }t[N * 40]; int pcnt; void modify(int &p,int l,int r,int x) { t[++pcnt] = t[p]; p = pcnt; t[p].sum++; if(l == r) return; int mid = (l + r) >> 1; if(mid >= x) modify(ls(p),l,mid,x); else modify(rs(p),mid+1,r,x); } int query(int p,int l,int r,int x,int y) { if(x <= l && r <= y) return t[p].sum; int mid = (l + r) >> 1,res = 0; if(mid >= x) res += query(ls(p),l,mid,x,y); if(mid < y) res += query(rs(p),mid+1,r,x,y); return res; } int rt[N]; namespace edt{ vector<int> t1[N << 2],t2[N << 2]; int t3[N << 2][2]; void build(int u,int fa) { for(int v : g[u]) { if(v == fa) continue; t1[u].push_back(v); build(v,u); } } struct edge{ int u,v,w; edge(){} edge(int _u,int _v,int _w) : u(_u),v(_v),w(_w){} }; vector<edge> e; inline void addE(int u,int v,int w) { t2[u].push_back(e.size()); e.emplace_back(u,v,w); t2[v].push_back(e.size()); e.emplace_back(v,u,w); } int pcnt; int ori[N << 2]; void rebuild(int u) { if(t1[u].size() <= 2) { for(auto v : t1[u]) { addE(u,v,1); rebuild(v); } } else { int s1 = ++ pcnt,s2 = ++pcnt; ori[s1] = ori[s2] = ori[u]; for(int i = 0;i < t1[u].size();i++) { if(i & 1) t1[s1].push_back(t1[u][i]); else t1[s2].push_back(t1[u][i]); } addE(u,s1,0),addE(u,s2,0); rebuild(s1),rebuild(s2); } } int dep[N << 2]; int st[N << 2],ed[N << 2]; int stamp; void dfs(int u,int fa) { st[u] = ++stamp; for(int eid : t2[u]) { int v = e[eid].v; if(v == fa) continue; dep[v] = dep[u] + 1; dfs(v,u); } ed[u] = stamp; } bool vis[N << 2]; int siz[N << 2]; int rt,mns; void findrt(int u,int fa,int tots) { siz[u] = 1; for(int eid : t2[u]) { if(vis[eid >> 1]) continue; int v = e[eid].v; if(v == fa) continue; findrt(v,u,tots); siz[u] += siz[v]; int ns = max(siz[v],tots - siz[v]); if(ns < mns) mns = ns,rt = eid; } } vector<pair<int,long long>> lst[N << 2][2]; int dis[N << 2][25]; void calc(int u,int d,vector<pair<int ,long long>> &l,int fa) { if(u <= n) l.emplace_back(u,dis[u][d]); for(int eid : t2[u]) { if(vis[eid >> 1]) continue; int v = e[eid].v,w = e[eid].w; if(v == fa) continue; dis[v][d] = dis[u][d] + w; calc(v,d,l,u); } } int divide(int p,int tots,int d) { mns = INT_MAX,findrt(p,0,tots); if(mns == INT_MAX) return -1; int nrt = rt,rrt = nrt ^ (nrt & 1),id = rrt >> 1; int u = e[rrt].u,v = e[rrt].v; vis[id] = true; calc(u,d,lst[id][0],0); calc(v,d,lst[id][1],0); lst[id][0].emplace_back(0,0); lst[id][1].emplace_back(0,0); sort(lst[id][0].begin(),lst[id][0].end()); sort(lst[id][1].begin(),lst[id][1].end()); for(int i = 1;i < lst[id][0].size();i++) lst[id][0][i].second += lst[id][0][i - 1].second; for(int i = 1;i < lst[id][1].size();i++) lst[id][1][i].second += lst[id][1][i - 1].second; if(nrt & 1) { t3[id][0] = divide(u,siz[u],d + 1); t3[id][1] = divide(v,tots - siz[u],d + 1); } else { t3[id][0] = divide(e[rrt].u,tots - siz[v],d + 1); t3[id][1] = divide(e[rrt].v,siz[v],d + 1); } return id; } int res; struct route{ int eid,d,dir; route(){} route(int _e,int _d,int _dir) : eid(_e),d(_d),dir(_dir){} }; vector<route> rte; void find(int eid,int rt1,int rt2,int siz,int d) { if(eid == -1) return; int u = e[eid << 1].u,v = e[eid << 1].v; int c = query(rt2,1,pcnt,st[v],ed[v]) - query(rt1,1,pcnt,st[v],ed[v]); if(c > (siz >> 1)) { rte.emplace_back(eid,d,1); res = v,find(t3[eid][1],rt1,rt2,siz,d + 1); } else if(siz - c >= (siz >> 1)) { rte.emplace_back(eid,d,0); res = u,find(t3[eid][0],rt1,rt2,siz,d + 1); } } long long recall(int l,int r) { long long sum = 0; for(const auto & p : rte) { int eid = p.eid,dir = p.dir ^ 1,d = p.d; auto itr = --lower_bound(lst[eid][dir].begin(),lst[eid][dir].end(),pair<int,long long>(r + 1,0ll)); auto itl = --lower_bound(lst[eid][dir].begin(),lst[eid][dir].end(),pair<int,long long>(l,0ll)); sum += itr -> second - itl -> second + 1ll * (itr - itl) * (dis[res][d] + e[eid << 1].w); } rte.clear(); return sum; } } void main() { scanf("%d%d",&n,&q); for(int i = 1;i < n;i++) { int u,v; scanf("%d%d",&u,&v); g[u].emplace_back(v),g[v].emplace_back(u); } edt :: build(1,0); for(int i = 1;i <= n;i ++) edt :: ori[i] = i; edt :: pcnt = n,edt :: rebuild(1); edt :: dfs(1,0); int drt = edt :: divide(1,edt :: pcnt,0); for(int i = 1;i <= n;i++) rt[i] = rt[i-1],modify(rt[i],1,edt :: pcnt,edt :: st[i]); for(int i = 1;i <= q;i++) { int l,r; scanf("%d%d",&l,&r); edt :: find(drt,rt[l - 1],rt[r],r - l + 1,0); printf("%lld\n",edt :: recall(l,r)); } return; } } signed main() { Pozhu :: main(); return 0; }
|