版本比较
题解:P4618 [SDOI2018] 原题识别
正文
增加 202 行删除 0 行
142 行未改动全部展开143144145146147148149150151152167 行未改动全部展开320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523142 行未改动全部展开
码量有点大,但是难写的地方不多,主要难度在查询部分的推式子。
速度还可以,最大点不到两秒。
:::info[在线主席树]```cpp#include<bits/stdc++.h>using namespace std;#define ui unsigned int167 行未改动全部展开 while (T--) gen(), solve(); return 0;}```:::
使用树剖 $\operatorname{lca}$,把询问离线到 $v$ 和 $x$ 上,再维护 $pre,dep,nod$ 等可以做到时间 $O((n+m)\log n)$,空间 $O(n+m)$。 代码里使用了 BIT 维护 $\sum1 ,\sum pre,\sum dep,\sum pre \times dep$,使用数组维护 $nod$。
:::info[离线 BIT]```cpp#include<bits/stdc++.h>using namespace std;#define ui unsigned int#define ll long long#define rep(a,b,c) for(int a=(b);a<=(c);++a)#define per(a,b,c) for(int a=(b);a>=(c);--a)struct io { #define SZ 1<<21 char buf[SZ], pbuf[SZ]; char * p1,*p2,*pp; io() : p1(buf), p2(buf), pp(pbuf){} ~io(){fwrite(pbuf, 1, pp - pbuf, stdout);} inline char gc(){ if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, SZ, stdin); return p1 == p2 ? EOF :*p1++; } #define isdigit(ch) (ch>47&&ch<58) template<typename T>inline io & operator >>(T & x){ x = 0; bool neg = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = 1; if (neg) for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) - (ch ^ 48); else for (; isdigit(ch); ch = gc()) x = (x << 1) + (x << 3) + (ch ^ 48); return * this; } inline void push(const char & c){ if (pp - pbuf == SZ) fwrite(pbuf, 1, SZ, stdout), pp = pbuf; *pp++ = c; } int stk[45], top; template<typename T>inline io & operator << (T x){ if (!x){push('0'); push('\n'); return * this;} if (x < 0){push('-'); x =-x;} while (x) stk[++top] = x % 10, x /= 10; while (top) push(stk[top--] ^ 48); push('\n'); return * this; }} IO;const int N = 1e5 + 5, M = 2e5 + 5;int T, n, q, lk;int a[N];ui SA, SB, SC;inline ui rng(){ SA ^= SA << 16; SA ^= SA >> 5; SA ^= SA << 1; ui tmp = SA; SA = SB; SB = SC; SC ^= tmp ^ SA; return SC;}vector<int> g[N];int fa[N], dep[N], sz[N], son[N], top[N];void addedge(int f, int u){ g[f].push_back(u); fa[u] = f;}int lca(int u, int v){ while (top[u] != top[v]) dep[top[u]] > dep[top[v]] ? u = fa[top[u]] : v = fa[top[v]]; return dep[u] < dep[v] ? u : v;}struct qy { int op, u, id;};struct del { int u, v, id;};vector<qy> qry[N];vector<del> dlt[N];ll ans[M];struct node { int c; ll d, p, pd; inline void add(int x, int y){++c; d += y; p += x; pd += 1LL * x * y;} inline void del(int x, int y){--c; d -= y; p -= x; pd -= 1LL * x * y;}} t[N];inline node operator + (const node & l, const node & r){return {l.c + r.c, l.d + r.d, l.p + r.p, l.pd + r.pd};}inline node operator - (const node & l, const node & r){return {l.c - r.c, l.d - r.d, l.p - r.p, l.pd - r.pd};}void add(int x, int d){ for (int i = x + 1; i <= n + 1; i += i &-i) t[i].add(x, d);}void del(int x, int d){ for (int i = x + 1; i <= n + 1; i += i &-i) t[i].del(x, d);}node query(int p){ node res = {0, 0, 0, 0}; for (int i = p + 1; i; i &= i - 1) res = res + t[i]; return res;}node query(int l, int r){ return query(r) - query(l - 1);}set<int> s[N];int pre[N], anc[N];ll s1[N], s2[N];int stk[N], tp;int nod[N];void dfs(int u){ stk[++tp] = u; int c = a[u]; if (s[c].empty()) pre[u] = 0; else pre[u] =*--s[c].end(); s[c].insert(dep[u]); anc[u] = stk[pre[u]]; for (int v : g[u]) dfs(v); --tp; s[c].erase(dep[u]);}void calc(int u){ int c = a[u]; nod[c] = u; s1[u] = s1[fa[u]] + dep[u] - pre[u]; s2[u] = s2[fa[u]] + 1LL * (dep[u] - pre[u]) * (dep[u]); add(pre[u], dep[u]); for (auto [x, y, id] : dlt[u]){ node tmp = query(dep[u] - 1); ans[id] -= 1LL * dep[x] * (dep[y] + 1) * tmp.c - tmp.d * dep[x] - tmp.p * (dep[y] + 1) + tmp.pd; } for (auto [op, v, id] : qry[u]){ ll & res = ans[id]; int x = lca(u, v); if (op == 1){ res = dep[u] - dep[x] + 1 - query(dep[x], n).c; while (v != x){ if (pre[v] < dep[x] && dep[nod[a[v]]] < dep[x])++res; v = fa[v]; } } else { res += s1[x] * (dep[u] + dep[v] + 2) - 2LL * s2[x] - dep[x]; res += 1LL * (dep[v] - dep[x]) * (dep[u] - dep[x]); node tmp = query(dep[x] - 1); res += 1LL * dep[v] * (dep[u] + 1) * tmp.c - tmp.d * dep[v] - tmp.p * (dep[u] + 1) + tmp.pd; int y = v; while (y != x){ if (pre[y] < dep[x]){ res += 1LL * (dep[x] - pre[y]) * (dep[v] - dep[y] + 1); int last = u, z = nod[a[y]]; while (dep[z] >= dep[x]) last = z, z = anc[z]; z = last; if (a[z] == a[y]) z = fa[z]; res += 1LL * (dep[v] - dep[y] + 1) * max(0, dep[z] - dep[x]); } y = fa[y]; } } } for (int v : g[u]) calc(v); nod[c] = anc[u]; del(pre[u], dep[u]);}int main(){ IO >> T; while (T--){ IO >> n >> lk >> SA >> SB >> SC; rep(u, 1, n) g[u].clear(), s[u].clear(), qry[u].clear(), dlt[u].clear(); rep(u, 2, lk) addedge(u - 1, u); rep(u, lk + 1, n) addedge(rng() % (u - 1) + 1, u); rep(u, 1, n) a[u] = rng() % n + 1; per(u, n, 1){ sz[u] = 1; son[u] = 0; for (int v : g[u]){ sz[u] += sz[v]; if (sz[son[u]] < sz[v]) son[u] = v; } } top[1] = 1; rep(u, 1, n){ dep[u] = dep[fa[u]] + 1; for (int v : g[u]) top[v] = (v == son[u] ? top[u] : v); } IO>>q; rep(i, 1, q){ int op, u, v; IO >> op >> u >> v; if (dep[u] > dep[v]) swap(u, v); qry[v].push_back({op, u, i}); if (op == 2) dlt[lca(u, v)].push_back({u, v, i}); ans[i] = 0; } dfs(1); calc(1); rep(i, 1, q) IO << ans[i]; } return 0;}```:::
最大点 408ms,空间 60MB,应该是目前最快解。