【题解】P8353
题解
不难想到树分块。在树上随机选择
构建虚树不能采用 dfs 序 + lca 的传统方法,注意到只需要求一次虚树,因此可以暴力。从每个点暴力往上跳并打标记,碰到标记过的点就加入虚树并停止,这样每个点只会被标记一次,可以用 bitset 处理,效率
考虑如何处理修改和询问,为了方便可以拆成四条,这一步需要求 lca。拆成四条后每条最开始非标记点的部分可以暴力,在虚树上遇到非关键点之前的一段需要找到这一段底部的关键点然后暴力重构,剩下的段可以整体标记/查询。因此需要支持以下函数:
int find (int u);
int dep (int u);
int lca (int u, int v);
其中 find(u) 表示求出 dep(u) 表示求出 lca(u,v) 表示求出
对于 find(u),首先可以暴力跳到
对于 dep(u),可以暴力跳到
对于 lca(u, v),设 find 函数可以找到
综上,时间复杂度
代码
#include <bits/stdc++.h>
#define Getchar() p1 == p2 and (p2 = (p1 = Inf) + fread(Inf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++
#define Putchar(c) p3 == p4 and (fwrite(Ouf, 1, 1 << 21, stdout), p3 = Ouf), *p3++ = c
char Inf[1 << 21], Ouf[1 << 21], *p1, *p2, *p3 = Ouf, *p4 = Ouf + (1 << 21);
inline unsigned int read (void)
{
unsigned int x = 0; char c = Getchar();
while (c < '0' or c > '9') c = Getchar();
while (c >= '0' and c <= '9') x = x * 10 + c - 48, c = Getchar();
return x;
}
inline void write (unsigned int x)
{
if (x >= 10) write(x / 10), x %= 10;
Putchar(x ^ 48);
}
const int maxn = 7000000 + 10;
int n, m, q, fa[maxn], p[6000], anc[6000], dep[6000];
unsigned int A, B, C, a[maxn], sum[6000], add[6000];
std::bitset<maxn> vis, tag; std::unordered_map<int, int> nxt[6000];
inline int Dep (int u)
{
int d = 0;
for (; !vis[u]; u = fa[u]) d++;
return d + dep[std::lower_bound(p + 1, p + m + 1, u) - p];
}
inline int find (int u)
{
while (!vis[fa[u]]) u = fa[u];
return nxt[std::lower_bound(p + 1, p + m + 1, fa[u]) - p][u];
}
inline void upd (int u, unsigned int w)
{
if (!u) return;
for (; !tag[u]; u = fa[u]) a[u] += w;
if (!vis[u])
{
int x = find(u), y = anc[x];
for (int v = p[x]; v != p[y]; v = fa[v]) a[v] += add[x];
for (int v = u; v != p[y]; v = fa[v]) a[v] += w, sum[x] += w;
add[x] = 0; u = p[y];
}
for (u = std::lower_bound(p + 1, p + m + 1, u) - p; u; u = anc[u]) sum[u] += w * (dep[u] - dep[anc[u]]), add[u] += w;
}
inline unsigned int ask (int u)
{
if (!u) return 0;
unsigned int res = 0;
for (; !tag[u]; u = fa[u]) res += a[u];
if (!vis[u])
{
int x = find(u), y = anc[x];
for (int v = p[x]; v != p[y]; v = fa[v]) a[v] += add[x];
for (int v = u; v != p[y]; v = fa[v]) res += a[v];
add[x] = 0; u = p[y];
}
for (u = std::lower_bound(p + 1, p + m + 1, u) - p; u; u = anc[u]) res += sum[u];
return res;
}
inline int LCA (int u, int v)
{
while (u != v) dep[u] < dep[v] ? v = anc[v] : u = anc[u];
return u;
}
inline int lca (int u, int v)
{
int x = u, y = v, depu = Dep(u), depv = Dep(v);
while (!tag[x]) x = fa[x];
while (!tag[y]) y = fa[y];
if (x == y)
{
while (u != v) depu < depv ? (v = fa[v], depv--) : (u = fa[u], depu--);
return u;
}
u = x; x = vis[x] ? std::lower_bound(p + 1, p + m + 1, x) - p : find(x);
v = y; y = vis[y] ? std::lower_bound(p + 1, p + m + 1, y) - p : find(y);
if (x == y) return Dep(u) < Dep(v) ? u : v;
int z = LCA(x, y);
if (z == x) return u;
if (z == y) return v;
return p[z];
}
inline void upd (int u, int v, unsigned int w)
{
if (u == v) {upd(u, w); upd(fa[u], -w); return;}
int x = lca(u, v);
if (x == u) {upd(v, w); upd(fa[u], -w); return;}
if (x == v) {upd(u, w); upd(fa[v], -w); return;}
upd(u, w); upd(v, w); upd(x, -w); upd(fa[x], -w);
}
inline unsigned int ask (int u, int v)
{
if (u == v) return ask(u) - ask(fa[u]);
int x = lca(u, v);
if (x == u) return ask(v) - ask(fa[u]);
if (x == v) return ask(u) - ask(fa[v]);
return ask(u) + ask(v) - ask(x) - ask(fa[x]);
}
signed main ()
{
read(); n = read(); q = read();
A = read(); B = read(); C = read(); a[0] = read();
for (int i = 1; i <= n; i++) a[i] = A * a[i - 1] * a[i - 1] + B * a[i - 1] + C;
for (int i = 2; i <= n; i++) fa[i] = read();
srand(time(NULL)); p[1] = 1; m = sqrt(n);
for (int i = 2; i <= m; i++) p[i] = rand() % n + 1;
std::sort(p + 1, p + m + 1); m = std::unique(p + 1, p + m + 1) - p - 1;
for (int i = 1; i <= m; i++) vis[p[i]] = 1;
tag[1] = 1;
for (int i = 1, u; i <= m; i++)
{
for (u = p[i]; !tag[u]; u = fa[u]) tag[u] = 1;
if (!vis[u]) p[++m] = u, vis[u] = 1;
}
std::sort(p + 1, p + m + 1); sum[1] = a[1]; dep[1] = 1;
for (int i = 2, lst; i <= m; i++)
{
sum[i] = a[p[i]]; dep[i] = 1;
for (anc[i] = fa[p[i]], lst = p[i]; !vis[anc[i]]; lst = anc[i], anc[i] = fa[anc[i]]) sum[i] += a[anc[i]], dep[i]++;
anc[i] = std::lower_bound(p + 1, p + m + 1, anc[i]) - p; dep[i] += dep[anc[i]]; nxt[anc[i]][lst] = i;
}
for (int op, u, v, w, ans = 0; q--; )
{
op = read(); u = read() ^ ans; v = read() ^ ans;
if (!op) w = read() ^ ans, upd(u, v, w);
else write(ans = ask(u, v)), Putchar('\n'), ans &= (1 << 20) - 1;
}
fwrite(Ouf, 1, p3 - Ouf, stdout); fflush(stdout);
return 0;
}