S
o
u
r
c
e
:
Source:
Source:2016 Multi-University Training Contest 6
P
r
o
b
l
e
m
:
Problem:
Problem:一棵树,每个结点都有颜色。两种查询,第一种,u的子树上有颜色x出现了A次,t1+=x,有颜色y出现了B次,t2+=y,求gcd(t1, t2),第二种,询问u到v链上的情况。
I
d
e
a
:
Idea:
Idea:第一种情况,dfs序分块,然后普通莫队。第二种情况,树分块,然后树上莫队。
按照BZOJ 1086的分块方式,保证了每个块的大小和直径,但不连通。
树上莫队的移动方式只看懂了lca的做法,标程里的还没看懂,先当板子用了= =
C
o
d
e
:
Code:
Code:
#include<bits/stdc++.h>
using namespace std;
#define lc o<<1
#define rc o<<1|1
#define fi first
#define se second
#define pb push_back
#define ALL(X) (X).begin(), (X).end()
#define bcnt(X) __builtin_popcountll(X)
#define CLR(A, X) memset(A, X, sizeof(A))
#define DEBUG printf("Passing [%s] in Line %d\n",__FUNCTION__,__LINE__)
using DB = double;
using LL = long long;
using PII = pair<int, int>;
const int N = 1e5+10, M = 1e5+10;
const LL MOD = 1e9+7;
//const int INF = 0x3f3f3f3f;
//const DB eps = 1e-8;
int fa[N], L[N], R[N], pos[N], dep[N], b[N], lab[N];
int dfs_cnt, b_cnt, B;
stack<int> S;
vector<int> G[N];
int dfs(int u, int fa) {
::fa[u] = fa;
L[u] = ++dfs_cnt; lab[dfs_cnt] = u;
int sz = 0;
for(int v:G[u]) if(v != fa) {
dep[v] = dep[u]+1;
sz += dfs(v, u);
if(sz >= B) {
++b_cnt;
while(sz--) { pos[S.top()] = b_cnt; S.pop(); }
}
}
S.push(u);
R[u] = dfs_cnt;
return sz+1;
}
void gao() {
int sz = dfs(1, 0);
while(sz--) { pos[S.top()] = b_cnt; S.pop(); }
}
int a[N], ms[N], num[N];
LL sum[N], ans[M];
inline void update(int x, int d) {
sum[num[x]] -= ms[x];
num[x] += d;
sum[num[x]] += ms[x];
}
bool in[N];
int cross;
struct Query {
int l, r, id, A, B;
Query(int _l=0, int _r=0, int _id=0, int _A=0, int _B=0):l(_l),r(_r),id(_id),A(_A),B(_B) {
if(l > r) swap(l, r);
}
bool operator<(const Query &A) const {
return pos[l]<pos[A.l]||(pos[l]==pos[A.l]&&L[r]>L[A.r]);
}
}q1[M], q2[M];
bool cmp(const Query &A, const Query &B) {
return b[A.l]<b[B.l]||(b[A.l]==b[B.l]&&A.r>B.r);
}
inline void inv(int x) {
if(in[x]) update(a[x], -1);
else update(a[x], 1);
in[x] ^= 1;
}
inline void move_up(int &x) {
if(!cross) {
if(in[x] && !in[fa[x]]) cross = x;
else if(in[fa[x]] && !in[x]) cross = fa[x];
}
inv(x), x = fa[x];
}
void Move(int u, int v) {
if(u == v) return;
cross = 0;
if(in[v]) cross = v;
while(dep[u] > dep[v]) move_up(u);
while(dep[v] > dep[u]) move_up(v);
while(u != v) move_up(u), move_up(v);
inv(u), inv(cross);
}
void solve1(int m) {
sort(q1, q1+m, cmp);
int l = 1, r = 0;
for(int i = 0; i < m; i++) {
while(l < q1[i].l) update(a[lab[l++]], -1);
while(l > q1[i].l) update(a[lab[--l]], 1);
while(r < q1[i].r) update(a[lab[++r]], 1);
while(r > q1[i].r) update(a[lab[r--]], -1);
ans[q1[i].id] = __gcd(sum[q1[i].A], sum[q1[i].B]);
}
}
void solve2(int m) {
sort(q2, q2+m);
int l = 1, r = 1;
in[1] = 1, update(a[1], 1);
for(int i = 0; i < m; i++) {
Move(l, q2[i].l), l = q2[i].l;
Move(r, q2[i].r), r = q2[i].r;
ans[q2[i].id] = __gcd(sum[q2[i].A], sum[q2[i].B]);
}
}
void init(int n) {
B = sqrt(n)+1;
dfs_cnt = b_cnt = 0;
for(int i = 0; i <= n; i++) {
G[i].clear();
b[i] = i/B+1;
}
}
void input(int n) {
int k = 0;
for(int i = 1; i <= n; i++) {
scanf("%d", a+i);
ms[k++] = a[i];
}
sort(ms, ms+k);
k = unique(ms, ms+k)-ms;
for(int i = 1; i <= n; i++) {
a[i] = lower_bound(ms, ms+k, a[i])-ms;
}
for(int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].pb(v), G[v].pb(u);
}
}
int main() {
int X;
scanf("%d", &X);
while(X--) {
int n, m, m1 = 0, m2 = 0;
scanf("%d%d", &n, &m);
init(n); input(n); gao();
for(int i = 0; i < m; i++) {
int op, u, v, a, b;
scanf("%d%d%d%d%d", &op, &u, &v, &a, &b);
if(op == 1) q1[m1++] = Query(L[u], R[v], i, a, b);
else q2[m2++] = Query(u, v, i, a, b);
}
CLR(num, 0), CLR(sum, 0);
solve1(m1);
CLR(num, 0), CLR(sum, 0); CLR(in, 0);
solve2(m2);
static int cas = 0;
printf("Case #%d:\n", ++cas);
for(int i = 0; i < m; i++) {
printf("%lld\n", ans[i]);
}
}
return 0;
}