QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#538936 | #8547. Whose Land? | IllusionaryDominance | WA | 0ms | 40544kb | C++20 | 6.6kb | 2024-08-31 13:36:55 | 2024-08-31 13:36:56 |
Judging History
answer
#include <bits/stdc++.h>
template <class T>
inline int qlog(T a) {
if (!a) return 0;
double x = a;
return ((*(long long*) & x) >> 52 & 2047) - 1023;
}
#define cin Mizuhashi
#define cout Parsee
#define endl '\n'
class Reader{
private:
static const int BUF_SIZE = 1 << 22;
char BUF_R[BUF_SIZE], *csy1, *csy2;
#ifndef _LOCAL_RUNNING
#define GC (csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2) ? EOF : *csy1 ++)
#else
char cur;
#define GC (cur = getchar())
#endif
#define IL inline
public:
IL bool eof() {
#ifndef _LOCAL_RUNNING
return csy1 == csy2 && (csy2 = (csy1 = BUF_R) + fread(BUF_R, 1, BUF_SIZE, stdin), csy1 == csy2);
#else
return cur == EOF;
#endif
}
template <class Ty>
IL Reader& operator >> (Ty &t) {
int u = 0;
char c = GC;
for (t = 0; c < 48 || c > 57; c = GC)
if (c == EOF) break;
else if (c == '-') u = 1;
for ( ; c > 47 && c < 58; c = GC) t = (t << 1) + (t << 3) + (c ^ 48);
t = u ? -t : t; return *this;
}
IL Reader& operator >> (double &t) {
int tmp, u = 0; char c = GC;
for (tmp = 0; c < 48 || c > 57; c = GC)
if (c == EOF) break;
else if (c == '-') u = 1;
for ( ; c > 47 && c < 58; c = GC) tmp = (tmp << 1) + (tmp << 3) + (c ^ 48);
t = (tmp = u ? -tmp : tmp);
if (c == '.') {
double x = 1;
for (c = GC; c > 47 && c < 58; c = GC) t += (x /= 10) * (c ^ 48);
}
return *this;
}
IL Reader& operator >> (char *s) {
char c = GC;
for (*s = 0; c < 33; c = GC) if (c == EOF) return *this;
for ( ; c > 32; c = GC) *s ++ = c;
*s = 0; return *this;
}
IL Reader& operator >> (char &c) {
for (c = GC; c < 33; c = GC) if (c == EOF) return *this;
return *this;
}
}cin;
class Writer{
private:
static const int BUF_SIZE = 1 << 22;
char BUF_W[BUF_SIZE], *csy;
#define IL inline
inline void WC(const char c) {
if (csy - BUF_W == BUF_SIZE) fwrite(BUF_W, 1, BUF_SIZE, stdout), csy = BUF_W;
*csy ++ = c;
}
public:
Writer() : csy(BUF_W) {}
~ Writer() {fwrite(BUF_W, 1, csy - BUF_W, stdout);}
IL void flush() {fwrite(BUF_W, 1, csy - BUF_W, stdout); csy = BUF_W;}
template <class Ty>
IL Writer& operator << (Ty x) {
static int sta[32], top;
if (x < 0) {
WC('-');
do sta[top ++] = - (x % 10); while (x /= 10);
}else do sta[top ++] = x % 10; while (x /= 10);
while (top) WC(sta[-- top] ^ 48);
return *this;
}
IL Writer& operator << (const char &c) {WC(c); return *this;}
IL Writer& operator << (const char *s) {while (*s) WC(*s ++); return *this;}
IL Writer& operator << (char *s) {while (*s) WC(*s ++); return *this;}
}cout;
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef unsigned int u32;
typedef unsigned long long u64;
typedef vector <int> vi;
typedef vector <ll> vl;
const int MAX_N = 500000 + 5;
int N, M, K, Q;
struct Edge{
int y, prev;
}e[MAX_N << 1];
int elast[MAX_N], ecnt;
int dep[MAX_N], fa[MAX_N], cnt[MAX_N];
pii f[MAX_N][2];
struct Node{
int l, r, v;
Node (int l_ = 0, int r_ = 0, int v_ = 0) : l(l_), r(r_), v(v_) {}
inline bool operator < (const Node &comp) const {return l < comp.l;}
};
set <Node> s[MAX_N];
vector <pii> qry[MAX_N];
int c[MAX_N], ans[MAX_N];
void modify(int x, int v) {
assert(x > 0);
for (int i = x; i <= N; i += i & -i) c[i] += v;
}
int query(int l, int r) {
assert(l > 0 && r > 0);
l --;
int res = 0;
while (l < r) {
res += c[r];
r -= r & -r;
}
while (r < l) {
res -= c[l];
l -= l & -l;
}
return res;
}
void Build(int x, int y) {
ecnt ++;
e[ecnt].y = y;
e[ecnt].prev = elast[x];
elast[x] = ecnt;
}
inline pii operator | (const pii &a, const pii &b) {
return pair(min(a.first, b.first), max(a.second, b.second));
}
inline pii& operator |= (pii &a, const pii &b) {
return (a = a | b);
}
void dfs(int u, int fath) {
fa[u] = fath;
dep[u] = dep[fath] + 1;
M = max(M, dep[u]);
cnt[dep[u]] ++;
f[u][0] = pair(cnt[dep[u]], cnt[dep[u]]);
for (int i = 1; i <= K; i ++) f[u][i] = pair(N, 0);
for (int i = elast[u]; i; i = e[i].prev) {
int v = e[i].y;
if (v != fath) {
dfs(v, u);
for (int j = 1; j <= K; j ++) {
f[u][j] |= f[v][j - 1];
}
}
}
}
void modify(set <Node> &s, int l, int r, int x) {
while (!s.empty()) {
auto it = s.upper_bound(Node(r));
if (it == s.begin()) break;
auto [l_, r_, v] = *(-- it);
if (r_ < r) break;
modify(v, -(min(r_, r) - max(l_, l) + 1));
s.erase(it);
if (r_ > r) {
s.insert(Node(r + 1, r_, v));
}
if (l_ < l) {
s.insert(Node(l_, l - 1, v));
}
}
modify(x, r - l + 1);
s.insert(Node(l, r, x));
}
void modify(int u) {
static pii mdf[64];
for (int i = 0; i <= (K << 1); i ++) mdf[i] = pair(N, 0);
for (int i = K, v = u; i >= 0 && v; i --, v = fa[v]) {
for (int j = 0; j <= i; j ++) {
mdf[i + j] |= f[v][j];
}
}
for (int i = 0; i <= (K << 1); i ++) {
if (mdf[i].first <= mdf[i].second) {
int d = dep[u] - K + i;
assert(d > 0);
modify(s[d], mdf[i].first, mdf[i].second, u);
}
}
}
void solve() {
ecnt = 0;
memset(c, 0, sizeof(int) * (N + 1));
memset(cnt, 0, sizeof(int) * (N + 1));
memset(elast, 0, sizeof(int) * (N + 1));
cin >> N >> K >> Q;
for (int i = 1; i < N; i ++) {
int x, y;
cin >> x >> y;
Build(x, y);
Build(y, x);
qry[i].clear();
}
dfs(1, 0);
for (int i = 1; i <= M; i ++) {
s[i].clear();
}
for (int i = 1; i <= Q; i ++) {
int l, r;
cin >> l >> r;
qry[r].emplace_back(l, i);
}
for (int i = 1; i <= N; i ++) {
modify(i);
for (auto [l, idx] : qry[i]) {
ans[idx] = query(l, i);
}
}
for (int i = 1; i <= Q; i ++) cout << ans[i] << endl;
}
int main() {
int T;
cin >> T;
while (T --) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 40544kb
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 6 9 7
result:
wrong answer 3rd numbers differ - expected: '7', found: '6'