接着考虑优化,我们可以考虑莫队,即把询问离线下来。接着发现,你可以把所有点按照重构树上的 dfs 序排序,莫队删除某个点 u 的时候,只有排序后与 u 相邻的点 v 与 u 的 lca 会造成影响,而左右相邻的点与 u 形成的 lca 中,深度最大的会最终被取消标记。加入时同理。我们可以用 set 维护所有当前所有点,其内部按 dfnu 从小到大排序即可。时间复杂度 O(qnlogn) ,大概可获得 54 分。
正解部分。我们可以使用链表存储所有的前驱后继关系,但删除会变得更劣。这是你发现,只能快速删除的东东可以用回滚莫队(此题中是只删除莫队)处理!于是就用链表代替 set ,每次 l 的所在块变动时,把链表拆了重建,其他的就是只删除莫队板子了。时间复杂度 O(qn) 。
实现上的细节。回滚莫队换块重建时,需要将编号 ∈[lt,n] 的点按 dfnu 从小到大排序后建链表( lt 指 l 端点所在块的左端),这时你可以提前把每个块内的点单独桶排序,按 l 所在块从大往前做,每次将新加的块(已排序)和当前维护的有序数组(代码中的 nw 数组)进行归并,这样才能保证总复杂度。
Code
C++
// Author: JX154#include <algorithm>#include <bitset>#include <cassert>#include <cmath>#include <cstdio>#include <cstring>#include <iostream>#include <set>#include <unordered_map>#include <vector> // #include <map> // #include <deque> // #include <queue> // #include <stack>#define endl '\n'#define Fast \ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
// #define int long long#define pii pair<int, int> // #pragma GCC optimize("O3","Ofast")using namespace std;
namespace fast_IO {
#define FASTIO#define IOSIZE 100000char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() \
((p1 == p2) and (p2 = (p1 = ibuf) + fread(ibuf, 1, IOSIZE, stdin), p1 == p2) ? (EOF) : (*p1++))
#define putchar(x) ((p3 == obuf + IOSIZE) && (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf), *p3++ = x)
#define isdigit(ch) (ch > 47 && ch < 58)
#define isspace(ch) (ch < 33)
template <typename T> inline T read() {
T s = 0;
int w = 1;
char ch;
while(ch = getchar(), !isdigit(ch) and (ch != EOF))
if(ch == '-')
w = -1;
if(ch == EOF)
return false;
while(isdigit(ch))
s = s * 10 + ch - 48, ch = getchar();
return s * w;
}
template <typename T> inline bool read(T &s) {
s = 0;
int w = 1;
char ch;
while(ch = getchar(), !isdigit(ch) and (ch != EOF))
if(ch == '-')
w = -1;
if(ch == EOF)
return false;
while(isdigit(ch))
s = s * 10 + ch - 48, ch = getchar();
return s *= w, true;
}
inline bool read(char &s) {
while(s = getchar(), isspace(s))
;
return true;
}
inline bool read(char *s) {
char ch;
while(ch = getchar(), isspace(ch))
;
if(ch == EOF)
return false;
while(!isspace(ch))
*s++ = ch, ch = getchar();
*s = '\000';
return true;
}
template <typename T> inline void print(T x) {
if(x < 0)
putchar('-'), x = -x;
if(x > 9)
print(x / 10);
putchar(x % 10 + 48);
}
inline void print(char x) { putchar(x); }
inline void print(char *x) {
while(*x)
putchar(*x++);
}
inline void print(const char *x) {
for(int i = 0; x[i]; i++)
putchar(x[i]);
}
#ifdef _GLIBCXX_STRINGinline bool read(std::string &s) {
s = "";
char ch;
while(ch = getchar(), isspace(ch))
;
if(ch == EOF)
return false;
while(!isspace(ch))
s += ch, ch = getchar();
return true;
}
inline void print(std::string x) {
for(int i = 0, n = x.size(); i < n; i++)
putchar(x[i]);
}
#endif // stringtemplate <typename T, typename... T1> inline int read(T &a, T1 &...other) { return read(a) + read(other...); }
template <typename T, typename... T1> inline void print(T a, T1... other) {
print(a);
print(other...);
}
struct Fast_IO {
~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); }
} io;
template <typename T> Fast_IO &operator>>(Fast_IO &io, T &b) { return read(b), io; }
template <typename T> Fast_IO &operator<<(Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
}
using namespace fast_IO;
// #define int long long#define N 400005int n, m, q;
long long ans[N];
struct E {
int u, v, w;
bool operator<(const E &oth) { return w < oth.w; }
} e[N];
struct Q {
int l, r, blk, id;
bool operator<(const Q &oth) { return ((blk ^ oth.blk) ? blk > oth.blk : r > oth.r); }
} qry[N];
struct UFS {
int fa[N];
void init(int n_) {
for(int i = 1; i <= n_; i++)
fa[i] = i;
}
int find(int x) { return ((x == fa[x]) ? x : (fa[x] = find(fa[x]))); }
void merge(int x, int y) { fa[find(x)] = find(y); }
} st;
int head[N], to[N], nxt[N << 2], tote;
void add(int u, int v) {
tote++;
to[tote] = v;
nxt[tote] = head[u];
head[u] = tote;
}
int val[N], cnt;
long long MST, nowans;
void kruskal() {
st.init(n << 1);
cnt = n;
sort(e + 1, e + m + 1);
for(int i = 1; i <= m; i++) {
int u = st.find(e[i].u), v = st.find(e[i].v);
if(u != v) {
MST += e[i].w;
cnt++;
add(u, cnt);
add(cnt, u);
add(v, cnt);
add(cnt, v);
st.merge(u, cnt);
st.merge(v, cnt);
val[cnt] = e[i].w;
}
}
}
int dep[N][20], id[N][20], depth[N], tim[N], dfn[N], ddep[N], tot = 0, ddfn = 0;
int lg[N];
int pos[N];
void dfs(int u, int d) {
tim[++tot] = u;
depth[tot] = d;
pos[u] = tot;
for(int ptr = head[u]; ptr; ptr = nxt[ptr]) {
int v = to[ptr];
if(!pos[v]) {
dfs(v, d + 1);
tim[++tot] = u, depth[tot] = d;
}
}
}
void dfs2(int u, int fa) {
if(u >= 1 && u <= n)
dfn[u] = ++ddfn;
ddep[u] = ddep[fa] + 1;
for(int ptr = head[u]; ptr; ptr = nxt[ptr]) {
int v = to[ptr];
if(v ^ fa)
dfs2(v, u);
}
}
void init_lca() {
lg[0] = -1;
for(int i = 1; i <= (n << 2); i++)
lg[i] = lg[i >> 1] + 1;
for(int i = 1; i <= tot; i++)
dep[i][0] = depth[i], id[i][0] = tim[i];
for(int j = 1; j <= lg[tot]; j++)
for(int i = 1; i <= tot - (1 << j) + 1; i++)
if(dep[i][j - 1] < dep[i + (1 << j - 1)][j - 1])
dep[i][j] = dep[i][j - 1], id[i][j] = id[i][j - 1];
else
dep[i][j] = dep[i + (1 << j - 1)][j - 1], id[i][j] = id[i + (1 << j - 1)][j - 1];
}
int lca(int l, int r) {
l = pos[l], r = pos[r];
if(l > r)
swap(l, r);
int len = lg[r - l + 1];
return dep[l][len] < dep[r - (1 << len) + 1][len] ? id[l][len] : id[r - (1 << len) + 1][len];
}
pii Max(const pii &a, const pii &b) { return (a.first > b.first ? a : b); }
int lt[N], rt[N];
int sorted[N], tong[N];
void Sort(int l, int r) {
for(int i = 1; i <= n; i++)
tong[i] = 0;
for(int i = l; i <= r; i++)
tong[dfn[i]] = i;
for(int i = 1; i <= n; i++)
if(tong[i])
sorted[l++] = tong[i];
}
int tmp[N];
void Merge(int a[], int l1, int r1, int b[], int l2, int r2, int c[]) {
int i = l1, j = l2, k = 0;
while(i <= r1 && j <= r2) {
if(dfn[b[j]] < dfn[a[i]])
tmp[++k] = b[j++];
else
tmp[++k] = a[i++];
}
while(i <= r1)
tmp[++k] = a[i++];
while(j <= r2)
tmp[++k] = b[j++];
c[0] = k;
for(int i = 1; i <= c[0]; i++)
c[i] = tmp[i];
}
int lft[N], rgh[N], nw[N];
pair<int *, int> stk[N << 2];
int top = 0;
long long preans;
void del(int x, bool flg = 0) {
pii res = { -1, 0 };
int lc;
if(lft[x])
res = Max(res, { ddep[lc = lca(x, lft[x])], lc });
if(rgh[x])
res = Max(res, { ddep[lc = lca(x, rgh[x])], lc });
if(flg) {
stk[++top] = { &rgh[lft[x]], rgh[lft[x]] };
stk[++top] = { &lft[rgh[x]], lft[rgh[x]] };
}
nowans += val[res.second];
rgh[lft[x]] = rgh[x];
lft[rgh[x]] = lft[x];
}
signed main() {
cin >> n >> m >> q;
int b = 410;
for(int i = 1; i <= m; i++)
cin >> e[i].u >> e[i].v >> e[i].w, e[i].u++, e[i].v++;
for(int i = 1; i <= q; i++)
cin >> qry[i].l >> qry[i].r, qry[i].id = i, qry[i].l++, qry[i].r++, qry[i].blk = qry[i].l / b + 1;
kruskal();
dfs(cnt, 0);
dfs2(cnt, 0);
init_lca();
sort(qry + 1, qry + q + 1);
int pre = 1;
for(int i = 2; i <= n + 1; i++)
if(i / b != (i - 1) / b || i == n + 1) {
Sort(pre, i - 1);
for(int j = pre; j < i; j++)
lt[j] = pre, rt[j] = i - 1;
pre = i;
}
lt[n + 1] = n + 1;
int nowr = n;
for(int i = 1; i <= q; i++) {
if(qry[i].blk ^ qry[i - 1].blk) {
for(int j = 1; j <= nw[0]; j++)
lft[nw[j]] = rgh[nw[j]] = 0;
int ps = qry[i - 1].l ? qry[i - 1].l : n + 1;
while(1) {
ps = lt[ps] - 1;
Merge(sorted, lt[ps], rt[ps], nw, 1, nw[0], nw);
if(lt[ps] == lt[qry[i].l])
break;
}
nowans = MST;
for(int j = 1; j <= nw[0]; j++) {
lft[nw[j]] = (j == 1 ? 0 : nw[j - 1]), rgh[nw[j]] = (j == nw[0] ? 0 : nw[j + 1]);
if(j > 1)
nowans -= val[lca(nw[j - 1], nw[j])];
}
nowr = n;
}
while(nowr > qry[i].r)
del(nowr--);
preans = nowans;
for(int j = lt[qry[i].l]; j < qry[i].l; j++)
del(j, 1);
ans[qry[i].id] = nowans;
while(top)
*stk[top].first = stk[top].second, top--;
nowans = preans;
}
for(int i = 1; i <= q; i++)
cout << ans[i] << '\n';
return 0;
}