QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#409056 | #8547. Whose Land? | definieren | WA | 299ms | 8468kb | C++20 | 6.4kb | 2024-05-11 16:52:30 | 2024-05-11 16:52:30 |
Judging History
answer
#include <bits/stdc++.h>
#define fir first
#define sec second
#ifdef LOCAL
#define dbg(x) cerr << "In Line " << __LINE__ << " the " << #x << " = " << x << '\n';
#define dpi(x, y) cerr << "In Line " << __LINE__ << " the " << #x << " = " << x << " ; " << "the " << #y << " = " << y << '\n';
#define dbgf(fmt, args...) fprintf(stderr, fmt, ##args);
#else
#define dbg(x) void();
#define dpi(x, y) void();
#define dbgf(fmt, args...) void();
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using vi = vector<int>;
using vpii = vector<pii>;
bool Mbe;
constexpr int MOD = 998244353;
template<typename T> T add(T a, T b, T p = MOD) { return (a + b >= p) ? (a + b - p) : (a + b); }
template<typename T> T del(T a, T b, T p = MOD) { return (a - b < 0) ? (a - b + p) : (a - b); }
template<typename T> T mul(T a, T b, T p = MOD) { return 1ll * a * b % p; }
template<typename T> T cadd(T &a, T b, T p = MOD) { return a = add(a, b, p); }
template<typename T> T cdel(T &a, T b, T p = MOD) { return a = del(a, b, p); }
template<typename T> T cmul(T &a, T b, T p = MOD) { return a = mul(a, b, p); }
template<typename T> T cmax(T &a, T b) { return a = max(a, b); }
template<typename T> T cmin(T &a, T b) { return a = min(a, b); }
namespace FastIO {
constexpr int LEN = 1 << 20;
char in[LEN + 1], out[LEN + 1];
char *pin = in, *pout = out, *ein = in, *eout = out + LEN;
char gc() { return pin == ein && (ein = (pin = in) + fread(in, 1, LEN, stdin), ein == in) ? EOF : *pin ++; }
void pc(char c) { pout == eout && (fwrite(out, 1, LEN, stdout), pout = out); (*pout ++) = c; return; }
void Flush() { fwrite(out, 1, pout - out, stdout); pout = out; }
template<typename T> T Read() {
T x = 0; int f = 1; char ch = gc();
while (ch < '0' || ch > '9') f = (ch == '-' ? (~f + 1) : f), ch = gc();
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
return x * f;
}
template<typename T> void Write(T x, char c) {
static char stk[40]; int tp = 0;
if (x < 0) pc('-'), x = ~x + 1;
do stk[tp++] = x % 10 + 48, x /= 10; while (x);
while (tp --) pc(stk[tp]); pc(c); return;
}
void Read(char *s) {
char ch = gc();
while (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9'))) ch = gc();
while ((ch != EOF) && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9'))) *s = ch, s ++, ch = gc();
*s = '\0'; return;
}
void Write(char *s) {
while (*s != '\0') pc(*s), s ++; return;
}
void Puts(char *s) {
Write(s), pc('\n'); return;
}
}
#define getchar FastIO::gc
#define putchar FastIO::pc
#define Flush FastIO::Flush
#define Read FastIO::Read
#define Write FastIO::Write
#define Puts FastIO::Puts
constexpr int N = 1e5 + 5, K = 23;
int n, k, q, bfn[N], dep[N], id[N], fa[N], ans[N];
pair<int, int> rng[N][K];
vector<int> G[N];
vector<pair<int, int> > qry[N];
struct Fenwick {
int n; vector<int> bit;
Fenwick(): n(0) { bit.clear(); return; }
Fenwick(int _n): n(_n), bit(_n + 2, 0) { return; }
void upd(int x, int k) {
for (x ++; x <= n; x += x & -x) bit[x] += k;
return;
}
int qry(int x) {
int ans = 0;
for (x ++; x; x ^= x & -x) ans += bit[x];
return ans;
}
void Update(int pos, int k) { upd(pos, k); return; }
int Query(int l, int r) { return qry(r) - qry(l - 1); }
} tr;
struct Node {
int l, r, w;
Node(int _l = 0, int _r = 0, int _w = 0):
l(_l), r(_r), w(_w) { return; }
friend bool operator < (Node x, Node y) {
return x.l < y.l;
}
}; set<Node> s;
auto Split(int pos) {
auto it = s.lower_bound(Node(pos));
if (it != s.end() && it -> l == pos) return it;
it --; int l = it -> l, r = it -> r, w = it -> w;
s.erase(it), s.insert(Node(l, pos - 1, w));
return s.insert(Node(pos, r, w)).fir;
}
void Assign(int l, int r, int k) {
if (l > r) return;
auto itr = Split(r + 1), itl = Split(l);
for (auto it = itl; it != itr; it ++)
tr.Update(it -> w, - it -> r + it -> l - 1),
tr.Update(k, it -> r - it -> l + 1);
s.erase(itl, itr), s.insert(Node(l, r, k));
return;
}
void Bfs() {
int bfc = 0;
queue<int> q; q.emplace(1);
while (q.size()) {
int u = q.front(); q.pop();
id[bfn[u] = ++ bfc] = u;
dep[u] = dep[fa[u]] + 1;
for (auto v : G[u]) {
if (v == fa[u]) continue;
q.emplace(v), fa[v] = u;
}
}
for (int i = n; i >= 1; i --) {
int u = id[i];
for (int j = 0; j <= k; j ++)
rng[u][j] = {n + 1, 0};
rng[u][1] = {i, i};
for (auto v : G[u]) if (v ^ fa[u])
for (int j = 2; j <= k; j ++)
cmin(rng[u][j].fir, rng[v][j - 1].fir),
cmax(rng[u][j].sec, rng[v][j - 1].sec);
}
// for (int u = 1; u <= n; u ++)
// printf("bfs[%d]=%d\n", u, bfn[u]);
// puts("");
// for (int u = 1; u <= n; u ++) {
// printf("node %d:\n", u);
// for (int i = 0; i <= k; i ++)
// printf("dist=%d, range=[%d,%d]\n", i, rng[u][i].fir, rng[u][i].sec);
// puts("");
// }
return;
}
void Cover(int u) {
int w = u;
// printf("Cover node %d:\n", u);
for (int i = k; i > 0 && u; i --) {
// printf("Covered range [%d,%d]\n", rng[u][i].fir, rng[u][i].sec);
Assign(rng[u][i].fir, rng[u][i].sec, w);
// printf("Covered range [%d,%d]\n", rng[u][i - 1].fir, rng[u][i - 1].sec);
Assign(rng[u][i - 1].fir, rng[u][i - 1].sec, w);
u = fa[u];
}
// puts("");
return;
}
void slv() {
n = Read<int>(), k = Read<int>() + 1, q = Read<int>();
for (int i = 1; i < n; i ++) {
int u = Read<int>(), v = Read<int>();
G[u].emplace_back(v), G[v].emplace_back(u);
}
for (int i = 1; i <= q; i ++) {
int l = Read<int>(), r = Read<int>();
qry[r].emplace_back(l, i);
}
Bfs(), s.insert(Node(1, n, 0));
tr = Fenwick(n), tr.Update(0, n);
for (int r = 1; r <= n; r ++) {
Cover(r);
for (auto [l, id] : qry[r])
ans[id] = tr.Query(l, r);
}
for (int i = 1; i <= q; i ++)
Write(ans[i], '\n');
return;
}
void clr() {
s.clear();
for (int u = 1; u <= n; u ++)
G[u].clear(), qry[u].clear();
return;
}
bool Med;
int main() {
#ifdef LOCAL
freopen("!in.in", "r", stdin);
freopen("!out.out", "w", stdout);
fprintf(stderr, "%.3lf Mb\n", fabs((&Mbe - &Med) / 1048576.0));
#endif
// int T = 1;
int T = Read<int>();
while (T --) slv(), clr();
Flush();
#ifdef LOCAL
fprintf(stderr, "%d ms\n", (int)clock());
#endif
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7624kb
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: 299ms
memory: 8468kb
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:
255 386 356 124 315 330 437 8 335 423 398 338 180 242 352 500 145 44 342 261 92 326 38 291 259 71 137 456 171 24 162 453 283 325 250 319 478 460 77 354 56 393 372 217 395 265 188 256 134 68 205 429 436 346 300 462 324 170 291 406 207 480 198 182 489 61 476 127 289 204 282 374 114 406 488 366 121 190...
result:
wrong answer 80th numbers differ - expected: '267', found: '264'