本文讲述 做法,需要卡常。
本文使用 AI 进行了卡常,保证作者贡献严格大于 AI。
解法简述
话说理论上来说给了 秒, 应该是可过的,不太卡常啊,不管了,我的树剖、倍增常数小!
模拟赛口胡的神秘做法,但是写不动。
首先,这东西是个树上查询链的问题,我们就可以考虑树剖、倍增。
而注意到,倍增向下跳会不知道跳进哪个子树,而这个问题看起来不像数据结构可以维护的,如果你说线段树维护 矩阵那我没话说。
那么我们可不可以将倍增和树剖结合呢?
首先,这个问题在链上是用倍增可做的,具体是我们定义跳一步为交换一次,设 表示从状态 跳 步能跳到什么状态,其中一个状态 定义为在点 ,未应用点 的交换且此时的物品是点 上的第 件。
这里搬到树上我们要钦定倍增只能在同一个重链上面跳。
我们又有一个问题:我们定义状态时钦定物品只能是当前位置已有的物品,而我们进入一个重链时想开始跳就得找到一个其物品中有当前物品的节点。
这个是好维护的,我们参照维护倍增时的方法,扫重链开桶记录每种颜色最近的状态,把找这个的请求离线挂链就能求了。
那么又有一个问题:我们跳重链时跳到一个新重链就又得求一次最近同色节点。我们可以每跳一个重链离线一次。
同时我们需要对每个询问先行维护出在哪个节点下重链去到下一条。
每次离线要扫一遍每条重链,还有每个询问跳倍增 次。
就是这样,我们有一车细节等着你,还要卡常,不过当码力练习倒是不错。
代码
这是卡常前的版本,换换快写、链式前向星、非递归就差不多了。
卡常前代码
#include <bits/stdc++.h>
using namespace std;
namespace io {
char* p1, * p2, buf[100001];
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++)
template<typename T>
inline typename __gnu_cxx::__enable_if<__is_integer<T>::__value, T>::__type read() {
T sum = 0;
char ch;
do ch = getchar(); while (!isdigit(ch));
do sum = (sum << 1) + (sum << 3) + (ch ^ 48), ch = getchar(); while (isdigit(ch));
return sum;
}
template<typename T>
inline typename __gnu_cxx::__enable_if<__is_integer<T>::__value, void>::__type read(T& sum) {
sum = 0;
char ch;
do ch = getchar(); while (!isdigit(ch));
do sum = (sum << 1) + (sum << 3) + (ch ^ 48), ch = getchar(); while (isdigit(ch));
}
#undef getchar
}
const int MAXN = 1000010;
const int LOG = 20;
int a[MAXN][2];
vector<int> mp[MAXN];
int sz[MAXN], son[MAXN], dfn[MAXN], tpfa[MAXN], fa[MAXN][22], dep[MAXN];
vector<int> head, tail;
int dfncnt = 0;
int n, q;
void dfs1(int root, int f) {
fa[root][0] = f;
dep[root] = dep[f] + 1;
dfn[root] = ++dfncnt;
for (int i = 1; i <= 20; i++)
fa[root][i] = fa[fa[root][i - 1]][i - 1];
sz[root] = 1;
for (int v : mp[root]) if (v != f) {
dfs1(v, root);
sz[root] += sz[v];
if (sz[v] > sz[son[root]]) son[root] = v;
}
}
void dfs2(int root, int f) {
if (root == f) head.push_back(root);
tpfa[root] = f;
if (son[root]) dfs2(son[root], f);
else tail.push_back(root);
for (int v : mp[root])
if (v != fa[root][0] && v != son[root]) dfs2(v, v);
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 20; i >= 0; --i)
if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
if (u == v) return u;
for (int i = 20; i >= 0; --i)
if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
struct state {
unsigned u : 20, s : 1;
};
static_assert(sizeof(state) == 4);
vector<state> tp[MAXN];
state nxt_up[MAXN][2][LOG];
struct Query {
int u, v, id, x;
} qry[MAXN];
vector<Query> qid[MAXN];
state near[MAXN];
void dfs_up(int root, int f) {
nxt_up[root][0][0] = tp[a[root][1]].empty() ? state{ 0, 0 } : tp[a[root][1]].back();
nxt_up[root][1][0] = tp[a[root][0]].empty() ? state{ 0, 0 } : tp[a[root][0]].back();
for (int i = 1; i < LOG; i++) {
state t0 = nxt_up[root][0][i - 1];
state t1 = nxt_up[root][1][i - 1];
nxt_up[root][0][i] = nxt_up[t0.u][t0.s][i - 1];
nxt_up[root][1][i] = nxt_up[t1.u][t1.s][i - 1];
}
tp[a[root][0]].push_back({ (unsigned)root, 0 });
tp[a[root][1]].push_back({ (unsigned)root, 1 });
for (const auto& query : qid[root]) {
int x = query.x;
near[query.id] = tp[x].empty() ? state{ 0, 0 } : tp[x].back();
}
for (int v : mp[root]) if (v != f) dfs_up(v, root);
tp[a[root][0]].pop_back();
tp[a[root][1]].pop_back();
}
pair<int, int> Now[MAXN];
vector<int> ask[MAXN], change[MAXN];
int ans[MAXN];
int rounds = 0;
void deal_query1() {
for (int i = 1; i <= q; i++) {
auto [u, v, ID, x] = qry[i];
int lc = lca(u, v);
state now = near[i];
int color = x;
if (now.u && dep[now.u] >= dep[lc]) {
for (int j = LOG - 1; j >= 0; --j) {
state t = nxt_up[now.u][now.s][j];
if (t.u && dep[t.u] >= dep[lc]) now = t;
}
color = a[now.u][now.s ^ 1];
}
Now[i] = { color, lc };
int w = v;
while (tpfa[w] != tpfa[lc]) {
change[i].push_back(w);
w = fa[tpfa[w]][0];
}
if (w != lc) change[i].push_back(w);
if (change[i].empty()) {
ans[i] = color;
continue;
}
rounds = max(rounds, (int)change[i].size());
int end = change[i].back();
if (tpfa[end] == tpfa[lc]) ask[lc].push_back(-i);
else ask[tpfa[end]].push_back(i);
}
}
state nxt_down[MAXN][2][LOG];
state nr[MAXN];
void deal_query2() {
static bool built = false;
vector<int> touched;
touched.reserve(n);
for (int x : tail) {
for (int i = x; i != fa[tpfa[x]][0]; i = fa[i][0]) {
if (!built) {
nxt_down[i][0][0] = nr[a[i][1]];
nxt_down[i][1][0] = nr[a[i][0]];
for (int j = 1; j < LOG; j++) {
state t0 = nxt_down[i][0][j - 1];
state t1 = nxt_down[i][1][j - 1];
nxt_down[i][0][j] = nxt_down[t0.u][t0.s][j - 1];
nxt_down[i][1][j] = nxt_down[t1.u][t1.s][j - 1];
}
}
for (int j : ask[i]) {
int id = abs(j);
int color = Now[id].first;
near[id] = nr[color];
if (j > 0) {
if (a[i][0] == color) near[id] = { (unsigned)i, 0 };
else if (a[i][1] == color) near[id] = { (unsigned)i, 1 };
}
}
ask[i].clear();
if (!nr[a[i][0]].u) touched.push_back(a[i][0]);
if (!nr[a[i][1]].u) touched.push_back(a[i][1]);
nr[a[i][0]] = { (unsigned)i, 0 };
nr[a[i][1]] = { (unsigned)i, 1 };
}
for (int c : touched) nr[c] = { 0, 0 };
touched.clear();
}
built = true;
for (int i = 1; i <= q; i++) {
if (change[i].empty()) continue;
int end = change[i].back();
int color = Now[i].first;
state now = near[i];
if (now.u && dep[now.u] <= dep[end]) {
for (int j = LOG - 1; j >= 0; --j) {
state t = nxt_down[now.u][now.s][j];
if (t.u && dep[t.u] <= dep[end]) now = t;
}
color = a[now.u][now.s ^ 1];
}
change[i].pop_back();
Now[i].first = color;
if (!change[i].empty()) {
int next_end = change[i].back();
if (tpfa[next_end] == tpfa[Now[i].second])
ask[Now[i].second].push_back(-i);
else
ask[tpfa[next_end]].push_back(i);
} else {
ans[i] = color;
}
}
}
using namespace io;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
read(n), read(q);
for (int i = 1; i <= n; i++) read(a[i][0]);
for (int i = 1; i <= n; i++) read(a[i][1]);
for (int i = 1, u, v; i < n; i++) {
read(u), read(v);
mp[u].push_back(v), mp[v].push_back(u);
}
for (int i = 1; i <= q; i++) {
int u = read<int>(), v = read<int>(), x = read<int>();
qry[i] = { u, v, i, x };
qid[u].push_back(qry[i]);
}
dfs1(1, 0);
dfs2(1, 1);
dfs_up(1, 0);
deal_query1();
for (int i = 0; i < rounds; i++) deal_query2();
for (int i = 1; i <= q; i++) cout << ans[i] << endl;
return 0;
}