QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#448661 | #8547. Whose Land? | ucup-team173 | WA | 389ms | 4072kb | C++20 | 3.4kb | 2024-06-19 20:56:53 | 2024-06-19 20:56:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct bit {
vector<int> tr;
int n;
bit(int n_ = 0) : n(n_) {
tr.assign(n + 1, 0);
}
void add(int x, int v) {
for(; x <= n; x += x & -x) tr[x] += v;
}
int query(int x) {
int res = 0;
for(; x; x -= x & -x) res += tr[x];
return res;
}
void print() {
for(int i = 1; i <= n; i++) {
cerr << query(n - i + 1) << ' ';
}
cerr << '\n';
}
};
void solve() {
int n, K, q;
cin >> n >> K >> q;
vector G(n + 1, vector<int>());
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
vector qry(n + 1, vector<pair<int, int>>());
vector<int> ans(q + 1);
for(int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
qry[r].emplace_back(l, i);
}
vector<int> bfn(n + 1), fa(n + 1);
vector L(n + 1, vector(K + 1, 0)), R(n + 1, vector(K + 1, 0));
int bfc = 0;
queue<int> que;
que.push(1), fa[1] = 1;
while(que.size()) {
int x = que.front(); que.pop();
bfn[x] = ++bfc;
for(auto y : G[x]) if(y != fa[x]) {
fa[y] = x, que.push(y);
}
}
// for(int i = 1; i < n; i++) cerr << bfn[i] << ' '; cerr << bfn[n] << '\n';
function<void(int)> dfs = [&](int x) {
L[x][0] = R[x][0] = bfn[x];
for(auto y : G[x]) if(y != fa[x]) {
dfs(y);
for(int k = 1; k <= K; k++) {
if(L[y][k - 1]) {
if(L[x][k] == 0) L[x][k] = L[y][k - 1];
L[x][k] = min(L[x][k], L[y][k - 1]);
R[x][k] = max(R[x][k], R[y][k - 1]);
}
}
}
};
dfs(1);
// for(int i = 1; i <= n; i++) {
// for(int k = 0; k <= K; k++) {
// cerr << "(" << L[i][k] << ", " << R[i][k] << ") ";
// }
// cerr << '\n';
// }
set<array<int, 3>> st;
bit tr(n);
auto insert = [&](int l, int r, int v) {
// cerr << "insert " << l << ' ' << r << ' ' << v << '\n';
if(!l) return;
while(1) {
auto it = st.lower_bound({r, 0, 0});
if(it == st.end()) break;
auto [R, L, V] = *it;
if(L > r) break;
st.erase(it);
tr.add(n - V + 1, -(R - L + 1));
if(L < l) {
st.insert({l - 1, L, V});
tr.add(n - V + 1, l - L);
}
if(R > r) {
st.insert({R, r + 1, V});
tr.add(n - V + 1, R - r);
}
}
st.insert({r, l, v});
tr.add(n - v + 1, r - l + 1);
// tr.print();
};
for(int i = 1; i <= n; i++) {
for(int k = K, t = i; k >= 0; k--) {
insert(L[t][k], R[t][k], i);
if(k && t != 1) insert(L[t][k - 1], R[t][k - 1], i);
t = fa[t];
}
// cerr << "answer " << i << '\n';
for(auto [l, id] : qry[i]) {
ans[id] = tr.query(n - l + 1);
}
}
for(int i = 1; i <= q; i++) {
cout << ans[i] << '\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int _; cin >> _; for(int cas = 1; cas <= _; cas++) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3624kb
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: 389ms
memory: 4072kb
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:
263 404 371 124 332 346 470 8 349 454 419 354 186 252 366 567 146 44 358 267 93 342 39 306 267 71 139 495 173 24 163 488 291 336 256 338 525 496 78 371 56 422 391 221 420 273 193 264 138 70 211 459 466 363 308 496 335 173 299 428 212 529 201 186 553 61 522 127 298 206 293 392 117 428 544 381 123 194...
result:
wrong answer 1st numbers differ - expected: '255', found: '263'