洛谷 P5478 [BJOI2015]骑士的旅行
题意
有一棵 n n n 个城市组成的树,有 m m m 个骑士在各个城市中,骑士有一个武力值 f [ i ] f[i] f[i] 。需要进行 m m m 次操作:
- 输出从 x x x 到 y y y 路径上的所有城市中最大的 k k k 个骑士的武力值。
- 编号为 x x x 的骑士搬到城市 y y y 。
- 编号为 x x x 的骑士武力值变为 y y y 。
数据范围: 1 ≤ n , m ≤ 4 ⋅ 1 0 4 1\le n,m \le 4\cdot 10^4 1≤n,m≤4⋅104 , 1 ≤ m ≤ 8 ⋅ 1 0 4 1 \le m \le 8 \cdot 10^4 1≤m≤8⋅104 , 1 ≤ k ≤ 20 1\le k \le 20 1≤k≤20 。
解法
树上的路径问题,可以考虑使用树链剖分。
- 由于 k k k 很小,考虑暴力在线段树上存储最大的 k k k 个武力值,在线段树上做单点修改即可。
- 线段树的合并操作就是归并左右子树的有序数组。
- 对于每个城市的所有武力值用 m u l t i s e t multiset multiset 存储。
- 询问的时候维护一个长度最大为 k k k 的答案数组。
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define tr t[root]
#define lson t[root << 1]
#define rson t[root << 1 | 1]
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
if (ch == '-')
sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
x = x * 10 + ch - '0';
if (sign)
x = -x;
return true;
}
inline bool R(double &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
if (ch == '-')
sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
x = x * 10 + ch - '0';
if (ch == '.') {
double tmp = 1;
ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
tmp /= 10.0, x += tmp * (ch - '0');
}
if (sign)
x = -x;
return true;
}
inline bool R(char *s) {
char ch = nc();
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
for (; !blank(ch) && !IOerror; ch = nc())
*s++ = ch;
*s = 0;
return true;
}
inline bool R(char &c) {
c = nc();
if (IOerror) {
c = -1;
return false;
}
return true;
}
template <class T, class... U>
bool R(T &h, U &... tmp) { return R(h) && R(tmp...); }
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
for (auto i = x.begin(); i != x.end(); _W(*i++))
if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 4e4 + 5;
int n, m, k, q;
int p[maxn], f[maxn];
vector<int> g[maxn];
multiset<int, greater<int>> pf[maxn];
int son[maxn], fa[maxn], dep[maxn], sz[maxn];
int top[maxn], cnt, id[maxn], idd[maxn];
void dfs1(int u, int f, int deep) {
sz[u] = 1, fa[u] = f, dep[u] = deep;
for (auto v : g[u]) {
if (v == f) continue;
dfs1(v, u, deep + 1);
sz[u] += sz[v];
if (sz[son[u]] < sz[v]) son[u] = v;
}
}
void dfs2(int u, int topf) {
top[u] = topf, id[u] = ++cnt, idd[cnt] = u;
if (!son[u]) return;
dfs2(son[u], topf);
for (auto v : g[u]) {
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
struct segtree {
int l, r;
vector<int> v;
} t[maxn << 2];
vector<int> merge(vector<int> v1, vector<int> v2) {
vector<int> ans;
int p1 = 0, p2 = 0;
while (ans.size() < k) {
if (p1 == v1.size() && p2 == v2.size())
break;
else if (p1 == v1.size())
ans.push_back(v2[p2++]);
else if (p2 == v2.size())
ans.push_back(v1[p1++]);
else
ans.push_back(v1[p1] > v2[p2] ? v1[p1++] : v2[p2++]);
}
return ans;
}
void build(int root, int l, int r) {
tr.l = l, tr.r = r;
if (l == r) {
int u = idd[l];
for (auto it : pf[u]) {
tr.v.push_back(it);
if (tr.v.size() >= k) break;
}
return;
}
int mid = (l + r) >> 1;
build(root << 1, l, mid);
build(root << 1 | 1, mid + 1, r);
tr.v = merge(lson.v, rson.v);
}
void update(int root, int pos) {
if (tr.l == tr.r) {
tr.v.clear();
int u = idd[pos];
for (auto it : pf[u]) {
tr.v.push_back(it);
if (tr.v.size() >= k) break;
}
return;
}
int mid = (tr.l + tr.r) >> 1;
if (pos <= mid) update(root << 1, pos);
if (pos > mid) update(root << 1 | 1, pos);
tr.v = merge(lson.v, rson.v);
}
vector<int> ans;
void query(int root, int l, int r) {
if (l <= tr.l && tr.r <= r) {
ans = merge(ans, tr.v);
return;
}
int mid = (tr.l + tr.r) >> 1;
if (l <= mid) query(root << 1, l, r);
if (r > mid) query(root << 1 | 1, l, r);
}
void debug(int root, int l, int r) {
if (l <= tr.l && tr.r <= r) {
W(tr.v.size(), tr.v);
return;
}
int mid = (tr.l + tr.r) >> 1;
if (l <= mid) query(root << 1, l, r);
if (r > mid) query(root << 1 | 1, l, r);
}
void solve(int u, int v) {
ans.clear();
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) swap(u, v);
query(1, id[top[u]], id[u]);
u = fa[top[u]];
}
if (dep[u] > dep[v]) swap(u, v);
query(1, id[u], id[v]);
if (ans.size() == 0) {
puts("-1");
return;
}
W(ans);
}
void remove(int x, int y) {
int u = p[x];
pf[u].erase(pf[u].find(f[x]));
p[x] = y;
pf[y].insert(f[x]);
update(1, id[y]);
update(1, id[u]);
}
void upd(int x, int y) {
int u = p[x];
pf[u].erase(pf[u].find(f[x]));
f[x] = y;
pf[u].insert(f[x]);
update(1, id[u]);
}
int main() {
R(n);
rep(i, 1, n - 1) {
int u, v;
R(u, v);
g[u].push_back(v);
g[v].push_back(u);
}
R(m);
rep(i, 1, m) {
R(f[i], p[i]);
pf[p[i]].insert(f[i]);
}
dfs1(1, 0, 1);
dfs2(1, 1);
R(q, k);
build(1, 1, n);
while (q--) {
int op, x, y;
R(op, x, y);
if (op == 1) solve(x, y);
if (op == 2) remove(x, y);
if (op == 3) upd(x, y);
}
}