QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#643955 | #8547. Whose Land? | MiniLong | TL | 1ms | 7732kb | C++20 | 4.9kb | 2024-10-16 08:54:25 | 2024-10-16 08:54:26 |
Judging History
answer
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
#ifdef ONLINE_JUDGE
char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
#define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
#else
#define get() getchar()
#endif
template<typename T> inline void read(T &t){
T x = 0, f = 1;
char c = getchar();
while(!isdigit(c)){
if(c == '-') f = -f;
c = getchar();
}
while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
t = x * f;
}
template<typename T, typename ... Args> inline void read(T &t, Args&... args){
read(t);
read(args...);
}
template<typename T> void write(T t){
if(t < 0) putchar('-'), t = -t;
if(t >= 10) write(t / 10);
putchar(t % 10 + '0');
}
template<typename T, typename ... Args> void write(T t, Args... args){
write(t), putchar(' '), write(args...);
}
template<typename T> void writeln(T t){
write(t);
puts("");
}
template<typename T> void writes(T t){
write(t), putchar(' ');
}
#undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
const ll mod = 998244353;
ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
void add(ll &x, ll y){x = (x + y) % mod;}
void mul(ll &x, ll y){x = x * y % mod;}
ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 1e5 + 5, B = 20;
int n, k, q, dep[N], pre[N], siz[N];
vector<int> G[N];
int id, dfn[N], rev[N], L[N][B + 5], R[N][B + 5];
void dfs(int u, int fa){
dep[u] = dep[fa] + 1, siz[u] = 1, pre[u] = fa;
for(auto &v : G[u]) if(v != fa) dfs(v, u);
}
void bfs(int s){
queue<int> q; q.push(s);
while(q.size()){
int u = q.front(); q.pop();
rev[dfn[u] = ++id] = u;
for(auto &v : G[u]) if(v != pre[u]) q.push(v);
}
_rep(i, 1, n){
int u = i;
_rep(j, 0, 20){
L[u][j] = !L[u][j] ? dfn[i] : min(L[u][j], dfn[i]);
R[u][j] = !R[u][j] ? dfn[i] : max(R[u][j], dfn[i]);
u = pre[u];
}
}
}
namespace bit{
int c[N];
void add(int x, int val){for(; x <= n; x += x & -x) c[x] += val;}
int ask(int x){int res = 0; for(; x; x -= x & -x) res += c[x]; return res;}
}
vector<PII> h[N];
struct node{
int l, r, v;
bool operator<(const node b)const{return r < b.r;}
};
set<node> st;
int col[N];
void modify(int l, int r, int val){
// debug("modify [%d,%d] val:%d\n", l, r, val);
_rep(i, l, r){
if(col[i]) bit::add(n - col[i] + 1, -1);
col[i] = val;
bit::add(n - col[i] + 1, 1);
}
// auto lit = st.lower_bound(make_pair(l + 1, 0)), rit = st.lower_bound(make_pair(r, 0));
}
void work(int u){
// debug("work %d\n", u);
int id = u, cur = 0;
while(u && cur <= k){
// debug("u:%d cur:%d [%d,%d]\n", u, cur, L[u][k - cur], R[u][k - cur]);
if(L[u][k - cur]) modify(L[u][k - cur], R[u][k - cur], id);
if(k > cur && L[u][k - cur - 1]) modify(L[u][k - cur - 1], R[u][k - cur - 1], id);
if(u == 1 && cur < k){
_rep(i, 0, k - cur - 2) if(L[u][i]){
modify(L[u][i], R[u][i], id);
}
}
u = pre[u], cur++;
}
// debug("==============\n");
}
int ans[N];
void clr(){
_rep(i, 1, n){
pre[i] = dep[i] = siz[i] = dfn[i] = rev[i] = col[i] = 0;
h[i].clear(), G[i].clear();
mst(L[i], 0), mst(R[i], 0);
bit::c[i] = 0;
}
_rep(i, 1, q) ans[i] = 0;
st.clear();
}
int main(){
multitest(){
read(n, k, q);
_rep(i, 2, n){
int u, v; read(u, v);
G[u].pb(v), G[v].pb(u);
}
_rep(i, 1, q){
int l, r; read(l, r);
h[r].pb({l, i});
}
dfs(1, 0), bfs(1);
_rep(i, 1, n){
work(i);
for(auto &[l, id] : h[i]){
ans[id] = bit::ask(n - l + 1);
}
}
_rep(i, 1, q) writeln(ans[i]);
clr();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 7732kb
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
Time Limit Exceeded
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...