QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#226357 | #7103. Red Black Tree | real_sigma_team# | AC ✓ | 1906ms | 52780kb | C++20 | 4.1kb | 2023-10-25 21:14:16 | 2023-10-25 21:14:16 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using ll = long long;
const int LOG = 20;
template<typename T>
struct sparse_table {
vector<array<T, LOG>> spt;
sparse_table() = default;
sparse_table(vector<T> a) {
int n = a.size();
spt.resize(n);
for (int i = 0; i < n; ++i) {
spt[i][0] = a[i];
}
for (int h = 0; 1 << h < n; ++h) {
for (int i = 0; i + (1 << h) < n; ++i) {
spt[i][h + 1] = min(spt[i][h], spt[i + (1 << h)][h]);
}
}
}
T get(int l, int r) {
int k = 31 - __builtin_clz(r - l + 1);
return min(spt[l][k], spt[r - (1 << k) + 1][k]);
}
};
struct LCA {
int n;
vector<vector<pair<int, int>>> g;
sparse_table<pair<int, int>> spt;
vector<int> tin;
LCA() = default;
LCA(int n) : n(n) {
g.resize(n);
tin.assign(n, 0);
}
void dfs(int u, int p, int d, vector<pair<int, int>>& vv) {
tin[u] = vv.size();
vv.emplace_back(d, u);
for (auto [to, w] : g[u]) {
if (to != p) {
dfs(to, u, d + 1, vv);
vv.emplace_back(d, u);
}
}
}
void build() {
vector<pair<int, int>> vv;
dfs(0, -1, 0, vv);
spt = sparse_table<pair<int, int>>(vv);
}
int lca(int u, int v) {
int l = tin[u], r = tin[v];
if (l > r) swap(l, r);
return spt.get(l, r).second;
}
vector<pair<int, int>>& operator[](int i) {
return g[i];
}
};
template<typename T>
struct segtree {
int R;
vector<T> tr;
segtree() = default;
segtree(int n) {
R = 1;
while (R < n) R <<= 1;
tr.assign(2 * R, 0);
}
void update(int k, int x) {
k += R;
tr[k] = x;
while (k > 1) {
k >>= 1;
tr[k] = tr[k << 1] + tr[k << 1 | 1];
}
}
T get(int l, int r) {
T res = 0;
for (l += R, r += R; l <= r; l >>= 1, r >>= 1) {
if (l & 1) res += tr[l++];
if (~r & 1) res += tr[r--];
}
return res;
}
};
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector<bool> red(n);
for (int i = 0; i < m; ++i) {
int x;
cin >> x;
red[x - 1] = 1;
}
LCA tr(n);
for (int i = 0; i < n - 1; ++i) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
tr[u].emplace_back(v, w);
tr[v].emplace_back(u, w);
}
tr.build();
vector<ll> d(n);
vector<int> vert(n, -1);
vector<int> tin(n), tout(n);
int tim = 0;
auto dfs = [&](auto dfs, int u, int p) -> void {
tin[u] = tim++;
if (red[u]) vert[u] = u;
for (auto [to, w] : tr[u]) {
if (to != p) {
d[to] = d[u] + w;
vert[to] = vert[u];
dfs(dfs, to, u);
}
}
tout[u] = tim - 1;
};
dfs(dfs, 0, -1);
while (q--) {
int k;
cin >> k;
vector<int> a(k);
for (auto& i : a) cin >> i, --i;
auto check = [&](ll x) -> bool {
vector<int> b;
for (auto i : a) {
if (d[i] - d[vert[i]] > x) b.push_back(i);
}
if (b.empty()) return 1;
int lc = b[0];
for (auto i : b) lc = tr.lca(lc, i);
bool ok = 1;
for (auto i : b) ok &= d[i] - d[lc] <= x;
return ok;
};
ll L = -1, R = 1e18;
while (R - L > 1) {
ll M = (L + R) / 2;
if (check(M)) R = M;
else L = M;
}
cout << R << '\n';
}
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#else
freopen("input.txt", "r", stdin);
#endif
int t;
cin >> t;
while (t--)
solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3620kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
ok 7 lines
Test #2:
score: 0
Accepted
time: 1906ms
memory: 52780kb
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...
output:
148616264 148616264 0 319801028 319801028 255904892 317070839 1265145897 1265145897 1072765445 667742619 455103436 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 303203698 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...
result:
ok 577632 lines
Extra Test:
score: 0
Extra Test Passed