QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#752249 | #8547. Whose Land? | ucup-team3215 | WA | 776ms | 4004kb | C++23 | 3.6kb | 2024-11-15 23:20:57 | 2024-11-15 23:20:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1e9 + 7;
constexpr int K = 21;
struct Fenwick {
int n;
vector<int> f;
Fenwick(int n) : n(n), f(n) {}
void upd(int i, int x) {
for (; i < n; i |= i + 1)f[i] += x;
}
int get(int i) {
int res = 0;
for (; ~i; i &= i + 1, --i)res += f[i];
return res;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
};
void solve() {
int n, k, q;
cin >> n >> k >> q;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> pos(n);
{
vector<char> used(n);
queue<int> q;
q.push(0);
used[0] = 1;
int cur = 0;
while (q.size()) {
int v = q.front();
pos[v] = cur++;
q.pop();
for (auto to: g[v]) {
if (!used[to]) {
used[to] = 1;
q.push(to);
}
}
}
}
vector<int> par(n);
vector<array<array<int, 2>, K>> dp(n);
auto dfs = [&](auto &&dfs, int v, int p) -> void {
for (int i = 0; i < K; ++i)dp[v][i] = {INF, -INF};
dp[v][0] = {pos[v], pos[v]};
for (auto to: g[v]) {
if (to == p)continue;
par[to] = v;
dfs(dfs, to, v);
for (int i = 1; i < K; ++i) {
dp[v][i][0] = min(dp[v][i][0], dp[to][i - 1][0]);
dp[v][i][1] = max(dp[v][i][1], dp[to][i - 1][1]);
}
}
};
dfs(dfs, 0, -1);
vector<vector<array<int, 2>>> Q(n);
vector<int> res(q);
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l, --r;
Q[r].push_back({l, i});
}
Fenwick f(n);
set<array<int, 3>> s;
auto add = [&](array<int, 3> r) {
if (~r[2])f.upd(r[2], r[1] - r[0]);
s.insert(r);
};
auto del = [&](array<int, 3> r) {
if (~r[2])f.upd(r[2], -(r[1] - r[0]));
s.erase(r);
};
auto erase = [&](array<int, 2> r) {
while (1) {
auto it = s.lower_bound({r[0], -1});
if (it == s.end())break;
auto v = *it;
if (v[1] <= r[1]) {
del(v);
} else {
del(v);
v[0] = r[1];
add(v);
break;
}
}
auto it = s.lower_bound({r[0], -1});
if (it != s.begin()) {
--it;
auto v = *it;
del(v);
if (v[1] <= r[1]) {
v[1] = r[0];
add(v);
}
else{
add({v[0],r[0],v[2]});
add({r[1],v[1],v[2]});
}
}
};
auto insert = [&](array<int, 3> r) {
if (r[0] > r[1])return;
erase({r[0], r[1]});
add(r);
};
insert({0, n, -1});
for (int r = 0; r < n; ++r) {
int cur = r;
for (int i = 0; i <= k; ++i) {
int need = k - i;
insert({dp[cur][need][0], dp[cur][need][1] + 1, r});
if (need)insert({dp[cur][need - 1][0], dp[cur][need - 1][1] + 1, r});
cur = par[cur];
}
for (auto [l, id]: Q[r]) {
res[id] = f.get(l, r);
}
}
for (auto x: res)cout << x << "\n";
}
int main() {
cin.tie(0)->sync_with_stdio(false);
int t;
cin >> t;
while (t--)solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
input:
2 5 1 2 1 2 1 3 2 4 2 5 2 2 2 3 8 2 3 1 2 1 3 2 4 2 5 4 6 5 7 7 8 2 2 2 5 3 4
output:
4 5 7 8 6
result:
ok 5 number(s): "4 5 7 8 6"
Test #2:
score: -100
Wrong Answer
time: 776ms
memory: 4004kb
input:
1000 500 1 500 291 2 406 9 207 13 71 15 351 17 442 18 496 19 104 20 208 23 448 34 80 42 187 44 352 45 290 46 116 47 318 50 226 52 129 55 83 59 100 60 54 61 73 65 63 66 454 67 43 71 26 74 68 26 320 75 388 76 425 79 170 81 350 83 48 85 153 86 221 90 290 95 130 96 82 98 124 82 36 99 213 100 121 101 132...
output:
309 399 510 124 351 333 476 8 482 467 407 341 214 244 379 626 147 44 346 274 92 330 62 297 313 85 171 573 195 65 163 500 289 842 275 326 942 482 84 358 350 407 375 230 429 268 189 492 136 68 208 455 461 397 307 487 1078 317 298 439 225 796 222 183 526 61 523 129 314 205 285 403 115 470 671 1090 137 ...
result:
wrong answer 1st numbers differ - expected: '255', found: '309'