Description
给一个树,n个点,有点权,初始根是1。
m 个操作,每次操作:
1.将树根换为 x。
2.给出两个点 x,y,从 x 的子树中选每一个点,y 的子树中选每一个点,如果两个点点权相等,ans++,求 ans。
Sample Input
5 5
1 2 3 4 5
1 2
1 3
3 4
3 5
2 4 5
2 1 5
2 3 5
1 5
2 4 5
Sample Output
0
1
1
1
这题好码农啊。。。
这题一看,换根操作,马上就想到遥远的国度,于是你就dfs序一下,然后你就想到一个简单的询问那道题,然后就莫队做。。。
一开始我把它拆成16个操作,结果被一条链那个数据点卡了。。。
后来肉老师一波小提示,结果发现可以拆成9个操作,然后就A了,但时间复杂度怎么还是这么高啊,全OJ垫底???
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int read() {
int x = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int _min(int x, int y) {return x < y ? x : y;}
struct edge {
int x, y, next;
} e[210000]; int len, last[110000];
struct node {
int l, r, kk, opt, ll;
} q[8100000];
int n, id, root, ll[110000], rr[110000], dep[110000], fa[110000][20];
int ans, a[110000], pos[110000], b[110000];
int p1[110000], p2[110000];
int gg[510000];
bool v[510000];
bool cmp(node a, node b) {
if(a.kk == b.kk) return a.r < b.r;
return a.kk < b.kk;
}
void ins(int x, int y) {
e[++len].x = x; e[len].y = y;
e[len].next = last[x]; last[x] = len;
}
int erfen(int x) {
int l = 1, r = n, ans;
while(l <= r) {
int mid = (l + r) / 2;
if(x <= b[mid]) r = mid - 1, ans = mid;
else l = mid + 1;
}
return ans;
}
void dfs(int x) {
ll[x] = ++id;
pos[id] = a[x];
for(int i = 1; i <= 18; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for(int k = last[x]; k; k = e[k].next) {
int y = e[k].y;
if(y != fa[x][0]) {
fa[y][0] = x;
dep[y] = dep[x] + 1;
dfs(y);
}
}
rr[x] = id;
}
int jump(int x, int h) {
for(int i = 18; i >= 0; i--)
if((1 << i) <= h)
x = fa[x][i], h -= (1 << i);
return x;
}
void hl(int k, int o) {p1[pos[k]] += o; ans += p2[pos[k]] * o;}
void hr(int k, int o) {p2[pos[k]] += o; ans += p1[pos[k]] * o;}
int main() {
int m; n = read(); m = read();
for(int i = 1; i <= n; i++) a[i] = read(), b[i] = a[i];
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; i++) a[i] = erfen(a[i]);
for(int i = 1; i < n; i++) {
int x = read(), y = read();
ins(x, y); ins(y, x);
}
dfs(1); root = 1;
int len = 0;
for(int i = 1; i <= m; i++) {
int opt = read();
if(opt == 1) root = read();
else {
v[i] = 1;
int x = read(), y = read();
if(ll[root] > ll[x] && ll[root] <= rr[x] && ll[root] > ll[y] && ll[root] <= rr[y]) {
int o1 = jump(root, dep[root] - dep[x] - 1), o2 = jump(root, dep[root] - dep[y] - 1);
int l1 = ll[o1] - 1, r1 = rr[o1];
int l2 = ll[o2] - 1, r2 = rr[o2];
q[++len].l = n; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = r2; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = l2; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = r1; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = r1; q[len].r = r2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = l2; q[len].r = r1; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = l1; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = l1; q[len].r = r2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = l1; q[len].r = l2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
}
else if(ll[root] > ll[x] && ll[root] <= rr[x]) {
int o1 = jump(root, dep[root] - dep[x] - 1);
int l1 = ll[o1] - 1, r1 = rr[o1];
int l2 = ll[y] - 1, r2 = rr[y];
if(y == root) l2 = 0, r2 = n;
q[++len].l = r2; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = r1; q[len].r = r2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = l1; q[len].r = r2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = l2; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = l2; q[len].r = r1; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = l1; q[len].r = l2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
}
else if(ll[root] > ll[y] && ll[root] <= rr[y]) {
int o2 = jump(root, dep[root] - dep[y] - 1);
int l1 = ll[x] - 1, r1 = rr[x];
if(x == root) l1 = 0, r1 = n;
int l2 = ll[o2] - 1, r2 = rr[o2];
q[++len].l = r1; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = l1; q[len].r = n; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = r1; q[len].r = r2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = l1; q[len].r = r2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = l2; q[len].r = r1; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = l1; q[len].r = l2; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
}
else {
int l1 = ll[x] - 1, r1 = rr[x];
if(x == root) l1 = 0, r1 = n;
int l3 = ll[y] - 1, r3 = rr[y];
if(y == root) l3 = 0, r3 = n;
q[++len].l = r1; q[len].r = r3; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
q[++len].l = r1; q[len].r = l3; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = l1; q[len].r = r3; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = -1;
q[++len].l = l1; q[len].r = l3; if(q[len].l > q[len].r) swap(q[len].l, q[len].r);
q[len].opt = i; q[len].ll = 1;
}
}
}
int pp = sqrt(n);
for(int i = 1; i <= len; i++) q[i].kk = _min(q[i].l / pp + 1, pp + 1);
sort(q + 1, q + len + 1, cmp);
int nowl = 0, nowr = 0;
for(int i = 1; i <= len; i++) {
for(int j = nowr + 1; j <= q[i].r; j++) hr(j, 1);
for(int j = nowr; j > q[i].r; j--) hr(j, -1);
for(int j = nowl + 1; j <= q[i].l; j++) hl(j, 1);
for(int j = nowl; j > q[i].l; j--) hl(j, -1);
nowl = q[i].l, nowr = q[i].r;
gg[q[i].opt] += ans * q[i].ll;
}
for(int i = 1; i <= m; i++) if(v[i]) printf("%d\n", gg[i]);
return 0;
}