QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1001#643988#8547. Whose Land?NailongGroupMiniLongFailed.2024-10-16 10:18:062024-10-16 10:18:07

Details

Extra Test:

Invalid Input

input:

100000
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0...

output:


result:

FAIL Integer parameter [name=n] equals to 1, violates the range [2, 100000] (test case 1, stdin, line 2)

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#643988#8547. Whose Land?MiniLongAC ✓1668ms54576kbC++205.7kb2024-10-16 09:21:502024-10-16 09:21:51

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, M = 5e5 + 5;
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;
    mutable int v;
    node(int _l = 0, int _r = 0, int _v = 0){l = _l, r = _r, v = _v;}
    bool operator<(const node b)const{return l < b.l;}
};
set<node> st;
int col[N];
auto split(int pos){
    if(pos > n) return st.end();
    auto it = st.lower_bound(pos);
    if(it != st.end() && (*it).l == pos) return it;
    it = prev(it);
    if((*it).r < pos) return st.end();
    int l = (*it).l, r = (*it).r, v = (*it).v;
    st.erase(it), st.insert(node(l, pos - 1, v));
    return st.insert(node(pos, r, v)).fi;
}
void modify(int l, int r, int val){
    // debug("modify [%d,%d] val:%d\n", l, r, val);
    // for(auto &i : st) debug("[%d,%d,%d] ", i.l, i.r, i.v);
    // debug("\n");
    // _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 rit = split(r + 1), lit = split(l);
    // for(auto &i : st) debug("[%d,%d,%d] ", i.l, i.r, i.v);
    // debug("\n");
    for(auto it = lit; it != rit; ++it){
        // debug("[%d,%d] %d\n", (*it).l, (*it).r, (*it).v);
        int len = (*it).r - (*it).l + 1;
        if((*it).v) bit::add(n - (*it).v + 1, -len);
    }
    st.erase(lit, rit);
    st.insert(node(l, r, val));
    bit::add(n - val + 1, r - l + 1);
    // debug("===========\n");
}
void work(int u){
    int id = u, cur = 0;
    while(u && cur <= k){
        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++;
    }
}
int ans[M];
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();
    id = 0;
}
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); st.insert(node(1, n, 0));
        _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;
}