QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#864962#5020. 举办乘凉州喵,举办乘凉州谢谢喵cooluoCompile Error//C++2321.7kb2025-01-21 12:11:052025-01-21 12:11:15

Judging History

你现在查看的是最新测评结果

  • [2025-01-21 12:11:15]
  • 评测
  • [2025-01-21 12:11:05]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ul unsigned ll
#define LL __int128_t
#define db double
#define DB long db
#define pii pair<int, int>
#define fi first
#define se second
#define mkpr make_pair
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define rsz resize
#define eb emplace_back
#define all(c) (c).begin(), (c).end()
#define bit(k) (1 << (k))
#define Bit(k) (1ll << (k))
#define BIT(k) ((LL)1 << (k))
#define lowbit(x) ((x) & -(x))
#define bin(s, k) ((s) >> (k) & 1)
#define lg2(x) (31 - __builtin_clz(x))
#define LG2(x) (63 - __builtin_clzll(x))
#define popcnt(x) __builtin_popcount(x)
#define mem(a, x) memset(a, x, sizeof(a))
#define req(i, l, r) for (int i(l), i##End(r); i < i##End; i = -~i)
#define rep(i, l, r) for (int i(l), i##End(r); i <= i##End; i = -~i)
#define per(i, r, l) for (int i(r), i##End(l); i >= i##End; i = ~-i)

#define FILERR

#ifdef JYR
#ifdef FILERR
auto filerr = fopen("Test.err", "w");
#else
auto filerr = stderr;
#endif
#define errs(x) fputs(x "\n", filerr)
#define errm(x, ...) fprintf(filerr, x, ##__VA_ARGS__)
#else
#define errs(x) 0
#define errm(x, ...) 0
#endif

template<typename T, typename U> void chkmx(T &_a, U _b) { if (_a < _b) _a = _b; }
template<typename T, typename U> void chkmn(T &_a, U _b) { if (_b < _a) _a = _b; }

bool Mbe;

struct FastIO {
    char buf[1 << 20], *p1, *p2;
    char puf[1 << 20], *pf;

    FastIO() : p1(buf), p2(buf), pf(puf) {}
    ~FastIO() { fwrite(puf, 1, pf - puf, stdout); }

    char gc() {
        if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin);
        return p1 == p2 ? EOF : *p1++;
    }

    bool blank(char c) { return c == ' ' || c == '\r' || c == '\n' || c == '\t'; }

    char rd() {
        char c = gc(); while (blank(c)) c = gc();
        return c;
    }

    template<typename T> T rd() {
        T x = 0; int f = 0; char c = gc();
        while (!isdigit(c)) f = (c == '-'), c = gc();
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c - '0'), c = gc();
        return f ? -x : x;
    }

    int rds(char *s) {
        char c = gc(), *S = s;
        while (blank(c)) c = gc();
        while (!blank(c) && c != EOF) *s++ = c, c = gc();
        return *s = 0, abs(s - S);
    }

    int rdl(char *s) {
        char c = gc(), *S = s;
        while (c == '\r' || c == '\n') c = gc();
        while (c != '\r' && c != '\n' && c != EOF) *s++ = c, c = gc();
        return *s = 0, abs(s - S);
    }

    void rd(char &c) { c = rd(); }

    void rd(char *s) {
        char c = gc();
        while (blank(c)) c = gc();
        if (c == EOF) { *s = 0; return; }
        while (!blank(c) && c != EOF) *s++ = c, c = gc();
        *s = 0;
    }

    template<typename T> void rd(T &x) {
        x = 0; int f = 0; char c = gc();
        while (!isdigit(c)) f = (c == '-'), c = gc();
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c - '0'), c = gc();
        if (f) x = -x;
    }

    template<typename T, typename... Ts>
    void rd(T& x, Ts&... xs) { rd(x), rd(xs...); }

    void pc(const char &c) {
        if (pf - puf == 1 << 20) fwrite(pf = puf, 1, 1 << 20, stdout);
        *pf++ = c;
    }

    void prt(char c) { pc(c); }
    void prt(char* s) { while (*s) pc(*s++); }
    void prt(const char* s) { while (*s) pc(*s++); }

    template<typename T> void prt(T x) {
        static int st[41], tp = 0;
        if (x == 0) { pc('0'); return; }
        if (x < 0) x = -x, pc('-');
        while (x) st[++tp] = x % 10, x /= 10;
        while (tp) pc(st[tp--] + '0');
    }

    template<typename T> void prt(T *x) { while (*x) pc(*x++); }

    template<typename T, typename... Ts>
    void prt(T x, Ts... xs) { prt(x), prt(xs...); }

    void prts(const char *s, char c = '\n') {
        while (*s) pc(*s++);
        if (c) pc(c);
    }
} IO;

#define rd IO.rd
#define rdl IO.rdl
#define ri rd<int>()
#define rl rd<ll>()
#define prt IO.prt
#define prs IO.prts
#define edl IO.pc('\n')

// #define MC

#define N 200005
#define mod 998244353
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f

int n, m, q, R, X, S;
vi G[N];
int fa[N], de[N], sz[N], sn[N];
int tp[N], df[N], nf[N], ed[N], tt;
vii ve[N], vc[N], vt[N], vo[N], vr[N];
int mx[N] = {inf}, su[N];
int ans[N];
bool vs[N];

struct BITree {
    int sz;
    vector<int> vt;

    void rsz(int _sz = n) {
        sz = _sz + 1;
        vt = vector<int>(sz + 1, 0);
    }

    void mdf(int p, int k) {
        assert(++p > 0);
        for (int i = p; i <= sz; i += lowbit(i))
            vt[i] += k;
    }

    int qry(int p) {
        if (++p < 0) return 0;
        chkmn(p, sz);
        int res = 0;
        for (int i = p; i; i -= lowbit(i))
            res += vt[i];
        return res;
    }
} T[N << 1];

struct bitree {
    int s[N];

    void mdf(int p, int k) {
        for (int i = max(p, 0); i <= n; i += lowbit(i))
            s[i] += k;
    }

    int qry(int p) {
        int res = 0;
        for (int i = min(p, n); i; i -= lowbit(i))
            res += s[i];
        return res;
    }
} tk;

void dfs1(int u, int ft) {
    de[u] = de[fa[u] = ft] + (sz[u] = 1);
    su[u] = su[ft] + G[u].size() - !!ft;
    for (auto v : G[u]) if (v != ft) {
        dfs1(v, u), sz[u] += sz[v];
        if (sz[sn[u]] < sz[v]) sn[u] = v;
    }
}

void dfs2(int u, int ft) {
    tp[u] = ft, nf[df[u] = ed[u] = ++tt] = u;
    if (!sn[u]) return; dfs2(sn[u], ft);
    for (auto v : G[u]) if (v != fa[u] && v != sn[u]) dfs2(v, v);
    ed[u] = tt;
}

int lca(int u, int v) {
    while (tp[u] != tp[v]) de[tp[u]] > de[tp[v]] ? u = fa[tp[u]] : v = fa[tp[v]];
    return de[u] < de[v] ? u : v;
}

void bld(int u) {
    int rt = 0, s = sz[u];
    function<void(int, int)> fdrt = [&](int u, int ft) {
        sz[u] = 1, mx[u] = 0;
        for (auto v : G[u]) if (!vs[v] && v != ft)
            fdrt(v, u), sz[u] += sz[v], chkmx(mx[u], sz[v]);
        chkmx(mx[u], s - sz[u]);
        if (mx[rt] > mx[u]) rt = u;
    }; fdrt(u, 0);
    int tu = ++m, du = 0;
    ve[rt].eb(0, tu);
    for (auto v : G[rt]) if (!vs[v]) {
        int tv = ++m, dv = 0;
        function<void(int, int, int)> gtds = [&](int u, int ft, int D) {
            chkmx(du, D), chkmx(dv, D);
            ve[u].eb(D, tu), ve[u].eb(D, -tv);
            for (auto v : G[u]) if (!vs[v] && v != ft)
                gtds(v, u, D + 1);
        }; gtds(v, rt, 1), T[tv].rsz(dv);
    }
    T[tu].rsz(du), vs[rt] = 1;
    for (auto v : G[rt]) if (!vs[v]) bld(v);
}

int qry(int u, int x) {
    int res = 0;
    for (auto [d, _] : ve[u]) {
        int t = _ < 0 ? -1 : 1, i = abs(_);
        res += t * T[i].qry(x - d);
    }
    return res;
}

void dfs3(int u, int ft) {
    if (sn[u]) rep(i, ed[sn[u]] + 1, ed[u]) tk.mdf(de[nf[i]] - de[u], 1);
    for (auto [x, i] : vc[u]) ans[i] += tk.qry(x);
    for (auto [x, i] : vt[u]) ans[i] -= tk.qry(x);
    errm("dfs3(%d, %d)\n", u, ft);
    rep(i, 1, n) errm(" | %d", tk.qry(i) - tk.qry(i - 1)); errs(" |");
    for (auto [x, i] : vc[u]) errm(" + [%d %d]\n", x, i);
    for (auto [x, i] : vt[u]) errm(" - [%d %d]\n", x, i);
    for (auto v : G[u]) if (v != ft) dfs3(v, u);
    if (sn[u]) rep(i, ed[sn[u]] + 1, ed[u]) tk.mdf(de[nf[i]] - de[u], -1);
}

void dfs4(int u, int ft, bool fg) {
    if (sn[u]) {
        for (auto v : G[u]) if (v != ft && v != sn[u]) dfs4(v, u, 0);
        dfs4(sn[u], u, 1);
    }
    tk.mdf(de[u], 1);
    if (sn[u]) rep(i, ed[sn[u]] + 1, ed[u]) tk.mdf(de[nf[i]], 1);
    for (auto [x, i] : vo[u]) ans[i] += tk.qry(de[u] + x) - tk.qry(de[u] - 1);
    for (auto [x, i] : vr[u]) ans[i] -= tk.qry(de[u] + x) - tk.qry(de[u] - 1);
    errm("dfs4(%d, %d, %d)\n", u, ft, fg);
    rep(i, 1, n) errm(" | %d", tk.qry(i) - tk.qry(i - 1)); errs(" |");
    for (auto [x, i] : vo[u]) errm(" + [%d %d]: [%d, %d]\n", x, i, de[u], de[u] + x);
    for (auto [x, i] : vr[u]) errm(" - [%d %d]: [%d, %d]\n", x, i, de[u], de[u] + x);
    if (!fg) rep(i, df[u], ed[u]) tk.mdf(de[nf[i]], -1);
}

void mslv() {
    req(i, 1, n = ri) {
        int u, v; rd(u, v);
        G[u].eb(v), G[v].eb(u);
    }
    dfs1(1, 0), dfs2(1, 1), bld(1);
    rep(u, 1, n) for (auto [d, _] : ve[u])
        T[abs(_)].mdf(d, 1);
        
    rep(i, 1, q = ri) {
        int u, v, x; rd(u, v, x);
        int t = lca(u, v);
        if (x == 0) { ans[i] = de[u] - de[t] + de[v] - de[fa[t]]; continue; }
        if (x == 1) { ans[i] = su[u] - su[t] + su[v] - su[fa[t]] + (t != 1) + 1; continue; }
        ans[i] = qry(t, x) + max(de[u] - de[t] - x, 0) + max(de[v] - de[t] - x, 0);
        errm("%d: %d\n", i, ans[i]);
        vc[u].eb(x, i), vt[u].eb(x - 1, i);
        vt[t].eb(x, i), vc[t].eb(x - 1, i);
        vc[v].eb(x, i), vt[v].eb(x - 1, i);
        vt[t].eb(x, i), vc[t].eb(x - 1, i);
        vo[sn[u]].eb(x - 1, i);
        // vr[sn[u]].eb(x - 2, i);
        if (int k = x - de[sn[u]] + de[t]; k >= 0) vr[sn[u]].eb(k, i);
        // if (int k = x - de[sn[u]] + de[t]; k >= 1) vo[sn[u]].eb(k - 1, i);
        vo[sn[v]].eb(x - 1, i);
        // vr[sn[v]].eb(x - 2, i);
        if (int k = x - de[sn[v]] + de[t]; k >= 0) vr[sn[v]].eb(k, i);
        // if (int k = x - de[sn[v]] + de[t]; k >= 1) vo[sn[v]].eb(k - 1, i);
        while (tp[u] != tp[v]) {
            if (de[tp[u]] < de[tp[v]]) swap(u, v);
            vr[u = tp[u]].eb(x - 1, i);
            // vo[u].eb(x - 2, i);
            if (int k = x - de[u] + de[t]; k >= 0) vo[u].eb(k, i);
            // if (int k = x - de[u] + de[t]; k >= 1) vr[u].eb(k - 1, i);
            vo[sn[u = fa[u]]].eb(x - 1, i);
            // vr[sn[u]].eb(x - 2, i);
            if (int k = x - de[sn[u]] + de[t]; k >= 0) vr[sn[u]].eb(k, i);
            // if (int k = x - de[sn[u]] + de[t]; k >= 1) vo[sn[u]].eb(k - 1, i);
        }
        // ans[i] = qry(t, x) + max(de[u] - de[t] - x, 0) + max(de[v] - de[t] - x, 0);
        // errm("%d: %d\n", i, ans[i]);
        // vc[u].eb(x, i), vt[u].eb(x - 1, i);
        // vt[t].eb(x, i), vc[t].eb(x - 1, i);
        // vc[v].eb(x, i), vt[v].eb(x - 1, i);
        // vt[t].eb(x, i), vc[t].eb(x - 1, i);
        // // if (de[u] - de[t] >= x) {
        // // if (u != t) {
        //     vo[u].eb(x, i), vr[u].eb(x - 1, i);
        //     // vo[sn[u]].eb(x - 1, i);
        //     // if (x > 1) vr[sn[u]].eb(x - 2, i);
        // // }
        // // if (de[v] - de[t] >= x) {
        // // if (v != t) {
        //     vo[v].eb(x, i), vr[v].eb(x - 1, i);
        //     // vo[sn[v]].eb(x - 1, i);
        //     // if (x > 1) vr[sn[v]].eb(x - 2, i);
        // // }
        // errm("? %d %d\n", u, v);
        // while (tp[u] != tp[v]) {
        //     if (de[tp[u]] < de[tp[v]]) swap(u, v);
        //     if (de[tp[u]] - de[t] < x) break;
        //     vr[u = tp[u]].eb(x - 1, i);
        //     if (x > 1) vo[u].eb(x - 2, i);
        //     errm("! %d %d\n", u, v);
        //     if (sn[u = fa[u]]) {
        //         vo[sn[u]].eb(x - 1, i);
        //         if (x > 1) vr[sn[u]].eb(x - 2, i);
        //     }
        // }
    }
    dfs3(1, 0);
    errm("ans:"); rep(i, 1, q) errm(" | %d", ans[i]); errs(" |");
    dfs4(1, 0, 1);
    rep(i, 1, q) prt(ans[i]), edl;
}

/*















*/

void mprw() {}

bool Med;

int main() {
    #ifdef JYR
    errs("Running!");
    freopen("Test.in", "r", stdin);
    freopen("Test.out", "w", stdout);
    #endif
    mprw();
    #ifdef MC
    int _ = ri;
    while (_--) mslv();
    #else
    mslv();
    #endif
    errm("%.3lfMB %.0lfms\n", abs(&Med - &Mbe) / 1048576., clock() * 1000. / CLOCKS_PER_SEC);
    return 0;
}#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ul unsigned ll
#define LL __int128_t
#define db double
#define DB long db
#define pii pair<int, int>
#define fi first
#define se second
#define mkpr make_pair
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define rsz resize
#define eb emplace_back
#define all(c) (c).begin(), (c).end()
#define bit(k) (1 << (k))
#define Bit(k) (1ll << (k))
#define BIT(k) ((LL)1 << (k))
#define lowbit(x) ((x) & -(x))
#define bin(s, k) ((s) >> (k) & 1)
#define lg2(x) (31 - __builtin_clz(x))
#define LG2(x) (63 - __builtin_clzll(x))
#define popcnt(x) __builtin_popcount(x)
#define mem(a, x) memset(a, x, sizeof(a))
#define req(i, l, r) for (int i(l), i##End(r); i < i##End; i = -~i)
#define rep(i, l, r) for (int i(l), i##End(r); i <= i##End; i = -~i)
#define per(i, r, l) for (int i(r), i##End(l); i >= i##End; i = ~-i)

// #define FILERR

#ifdef JYR
#ifdef FILERR
auto filerr = fopen("Test.err", "w");
#else
auto filerr = stderr;
#endif
#define errs(x) fputs(x "\n", filerr)
#define errm(x, ...) fprintf(filerr, x, ##__VA_ARGS__)
#else
#define errs(x) 0
#define errm(x, ...) 0
#endif

template<typename T, typename U> void chkmx(T &_a, U _b) { if (_a < _b) _a = _b; }
template<typename T, typename U> void chkmn(T &_a, U _b) { if (_b < _a) _a = _b; }

bool Mbe;

struct FastIO {
    char buf[1 << 20], *p1, *p2;
    char puf[1 << 20], *pf;

    FastIO() : p1(buf), p2(buf), pf(puf) {}
    ~FastIO() { fwrite(puf, 1, pf - puf, stdout); }

    char gc() {
        if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin);
        return p1 == p2 ? EOF : *p1++;
    }

    bool blank(char c) { return c == ' ' || c == '\r' || c == '\n' || c == '\t'; }

    char rd() {
        char c = gc(); while (blank(c)) c = gc();
        return c;
    }

    template<typename T> T rd() {
        T x = 0; int f = 0; char c = gc();
        while (!isdigit(c)) f = (c == '-'), c = gc();
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c - '0'), c = gc();
        return f ? -x : x;
    }

    int rds(char *s) {
        char c = gc(), *S = s;
        while (blank(c)) c = gc();
        while (!blank(c) && c != EOF) *s++ = c, c = gc();
        return *s = 0, abs(s - S);
    }

    int rdl(char *s) {
        char c = gc(), *S = s;
        while (c == '\r' || c == '\n') c = gc();
        while (c != '\r' && c != '\n' && c != EOF) *s++ = c, c = gc();
        return *s = 0, abs(s - S);
    }

    void rd(char &c) { c = rd(); }

    void rd(char *s) {
        char c = gc();
        while (blank(c)) c = gc();
        if (c == EOF) { *s = 0; return; }
        while (!blank(c) && c != EOF) *s++ = c, c = gc();
        *s = 0;
    }

    template<typename T> void rd(T &x) {
        x = 0; int f = 0; char c = gc();
        while (!isdigit(c)) f = (c == '-'), c = gc();
        while (isdigit(c)) x = (x << 1) + (x << 3) + (c - '0'), c = gc();
        if (f) x = -x;
    }

    template<typename T, typename... Ts>
    void rd(T& x, Ts&... xs) { rd(x), rd(xs...); }

    void pc(const char &c) {
        if (pf - puf == 1 << 20) fwrite(pf = puf, 1, 1 << 20, stdout);
        *pf++ = c;
    }

    void prt(char c) { pc(c); }
    void prt(char* s) { while (*s) pc(*s++); }
    void prt(const char* s) { while (*s) pc(*s++); }

    template<typename T> void prt(T x) {
        static int st[41], tp = 0;
        if (x == 0) { pc('0'); return; }
        if (x < 0) x = -x, pc('-');
        while (x) st[++tp] = x % 10, x /= 10;
        while (tp) pc(st[tp--] + '0');
    }

    template<typename T> void prt(T *x) { while (*x) pc(*x++); }

    template<typename T, typename... Ts>
    void prt(T x, Ts... xs) { prt(x), prt(xs...); }

    void prts(const char *s, char c = '\n') {
        while (*s) pc(*s++);
        if (c) pc(c);
    }
} IO;

#define rd IO.rd
#define rdl IO.rdl
#define ri rd<int>()
#define rl rd<ll>()
#define prt IO.prt
#define prs IO.prts
#define edl IO.pc('\n')

// #define MC

#define N 200005
#define mod 998244353
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f

int n, m, q, R, X, S;
vi G[N];
int fa[N], de[N], sz[N], sn[N];
int tp[N], df[N], nf[N], ed[N], tt;
vii ve[N], vc[N], vt[N], vo[N], vr[N];
int mx[N] = {inf};
int ans[N];
bool vs[N];

struct BITree {
    int sz;
    vector<int> vt;

    void rsz(int _sz = n) {
        sz = _sz + 1;
        vt = vector<int>(sz + 1, 0);
    }

    void mdf(int p, int k) {
        assert(++p > 0);
        for (int i = p; i <= sz; i += lowbit(i))
            vt[i] += k;
    }

    int qry(int p) {
        if (++p < 0) return 0;
        chkmn(p, sz);
        int res = 0;
        for (int i = p; i; i -= lowbit(i))
            res += vt[i];
        return res;
    }
} T[N << 1];

struct bitree {
    int s[N];

    void mdf(int p, int k) {
        for (int i = max(p, 0); i <= n; i += lowbit(i))
            s[i] += k;
    }

    int qry(int p) {
        int res = 0;
        for (int i = min(p, n); i; i -= lowbit(i))
            res += s[i];
        return res;
    }
} tk;

void dfs1(int u, int ft) {
    de[u] = de[fa[u] = ft] + (sz[u] = 1);
    for (auto v : G[u]) if (v != ft) {
        dfs1(v, u), sz[u] += sz[v];
        if (sz[sn[u]] < sz[v]) sn[u] = v;
    }
}

void dfs2(int u, int ft) {
    tp[u] = ft, nf[df[u] = ed[u] = ++tt] = u;
    if (!sn[u]) return; dfs2(sn[u], ft);
    for (auto v : G[u]) if (v != fa[u] && v != sn[u]) dfs2(v, v);
    ed[u] = tt;
}

int lca(int u, int v) {
    while (tp[u] != tp[v]) de[tp[u]] > de[tp[v]] ? u = fa[tp[u]] : v = fa[tp[v]];
    return de[u] < de[v] ? u : v;
}

void bld(int u) {
    int rt = 0, s = sz[u];
    function<void(int, int)> fdrt = [&](int u, int ft) {
        sz[u] = 1, mx[u] = 0;
        for (auto v : G[u]) if (!vs[v] && v != ft)
            fdrt(v, u), sz[u] += sz[v], chkmx(mx[u], sz[v]);
        chkmx(mx[u], s - sz[u]);
        if (mx[rt] > mx[u]) rt = u;
    }; fdrt(u, 0);
    int tu = ++m, du = 0;
    ve[rt].eb(0, tu);
    for (auto v : G[rt]) if (!vs[v]) {
        int tv = ++m, dv = 0;
        function<void(int, int, int)> gtds = [&](int u, int ft, int D) {
            chkmx(du, D), chkmx(dv, D);
            ve[u].eb(D, tu), ve[u].eb(D, -tv);
            for (auto v : G[u]) if (!vs[v] && v != ft)
                gtds(v, u, D + 1);
        }; gtds(v, rt, 1), T[tv].rsz(dv);
    }
    T[tu].rsz(du), vs[rt] = 1;
    for (auto v : G[rt]) if (!vs[v]) bld(v);
}

int qry(int u, int x) {
    int res = 0;
    for (auto [d, _] : ve[u]) {
        int t = _ < 0 ? -1 : 1, i = abs(_);
        res += t * T[i].qry(x - d);
    }
    return res;
}

void dfs3(int u, int ft) {
    if (sn[u]) rep(i, ed[sn[u]] + 1, ed[u]) tk.mdf(de[nf[i]] - de[u], 1);
    for (auto [x, i] : vc[u]) ans[i] += tk.qry(x);
    for (auto [x, i] : vt[u]) ans[i] -= tk.qry(x);
    for (auto v : G[u]) if (v != ft) dfs3(v, u);
    if (sn[u]) rep(i, ed[sn[u]] + 1, ed[u]) tk.mdf(de[nf[i]] - de[u], -1);
}

void dfs4(int u, int ft, bool fg) {
    if (sn[u]) {
        for (auto v : G[u]) if (v != ft && v != sn[u]) dfs4(v, u, 0);
        dfs4(sn[u], u, 1);
    }
    tk.mdf(de[u], 1);
    if (sn[u]) rep(i, ed[sn[u]] + 1, ed[u]) tk.mdf(de[nf[i]], 1);
    for (auto [x, i] : vo[u]) ans[i] += tk.qry(de[u] + x) - tk.qry(de[u] - 1);
    for (auto [x, i] : vr[u]) ans[i] -= tk.qry(de[u] + x) - tk.qry(de[u] - 1);
    if (!fg) rep(i, df[u], ed[u]) tk.mdf(de[nf[i]], -1);
}

void mslv() {
    req(i, 1, n = ri) {
        int u, v; rd(u, v);
        G[u].eb(v), G[v].eb(u);
    }
    dfs1(1, 0), dfs2(1, 1), bld(1);
    rep(u, 1, n) for (auto [d, _] : ve[u])
        T[abs(_)].mdf(d, 1);
        
    rep(i, 1, q = ri) {
        int u, v, x; rd(u, v, x);
        int t = lca(u, v);
        if (x == 0) { ans[i] = de[u] - de[t] + de[v] - de[fa[t]]; continue; }
        ans[i] = qry(t, x) + max(de[u] - de[t] - x, 0) + max(de[v] - de[t] - x, 0);
        vc[u].eb(x, i), vt[u].eb(x - 1, i);
        vt[t].eb(x, i), vc[t].eb(x - 1, i);
        vc[v].eb(x, i), vt[v].eb(x - 1, i);
        vt[t].eb(x, i), vc[t].eb(x - 1, i);
        if (u != t) {
            vo[sn[u]].eb(x - 1, i);
            if (x > 1) vr[sn[u]].eb(x - 2, i);
        }
        if (v != t) {
            vo[sn[v]].eb(x - 1, i);
            if (x > 1) vr[sn[v]].eb(x - 2, i);
        }
        while (tp[u] != tp[v]) {
            if (de[tp[u]] < de[tp[v]]) swap(u, v);
            if (de[tp[u]] - de[t] < x) break;
            vr[u = tp[u]].eb(x - 1, i);
            if (x > 1) vo[u].eb(x - 2, i);
            if (sn[u = fa[u]]) {
                vo[sn[u]].eb(x - 1, i);
                if (x > 1) vr[sn[u]].eb(x - 2, i);
            }
        }
    }
    dfs3(1, 0);
    dfs4(1, 0, 1);
    rep(i, 1, q) prt(ans[i]), edl;
}

/*















*/

void mprw() {}

bool Med;

int main() {
    #ifdef JYR
    errs("Running!");
    freopen("Test.in", "r", stdin);
    freopen("Test.out", "w", stdout);
    #endif
    mprw();
    #ifdef MC
    int _ = ri;
    while (_--) mslv();
    #else
    mslv();
    #endif
    errm("%.3lfMB %.0lfms\n", abs(&Med - &Mbe) / 1048576., clock() * 1000. / CLOCKS_PER_SEC);
    return 0;
}

详细

answer.code:400:2: error: stray ‘#’ in program
  400 | }#include <bits/stdc++.h>
      |  ^
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:12: error: ‘bits’ was not declared in this scope; did you mean ‘bit’?
  400 | }#include <bits/stdc++.h>
      |            ^~~~
      |            bit
answer.code:400:17: error: ‘stdc’ was not declared in this scope; did you mean ‘std’?
  400 | }#include <bits/stdc++.h>
      |                 ^~~~
      |                 std
answer.code:400:3: error: ‘include’ does not name a type
  400 | }#include <bits/stdc++.h>
      |   ^~~~~~~
answer.code:446:39: error: redefinition of ‘template<class T, class U> void chkmx(T&, U)’
  446 | template<typename T, typename U> void chkmx(T &_a, U _b) { if (_a < _b) _a = _b; }
      |                                       ^~~~~
answer.code:47:39: note: ‘template<class T, class U> void chkmx(T&, U)’ previously declared here
   47 | template<typename T, typename U> void chkmx(T &_a, U _b) { if (_a < _b) _a = _b; }
      |                                       ^~~~~
answer.code:447:39: error: redefinition of ‘template<class T, class U> void chkmn(T&, U)’
  447 | template<typename T, typename U> void chkmn(T &_a, U _b) { if (_b < _a) _a = _b; }
      |                                       ^~~~~
answer.code:48:39: note: ‘template<class T, class U> void chkmn(T&, U)’ previously declared here
   48 | template<typename T, typename U> void chkmn(T &_a, U _b) { if (_b < _a) _a = _b; }
      |                                       ^~~~~
answer.code:449:6: error: redefinition of ‘bool Mbe’
  449 | bool Mbe;
      |      ^~~
answer.code:50:6: note: ‘bool Mbe’ previously declared here
   50 | bool Mbe;
      |      ^~~
answer.code:451:8: error: redefinition of ‘struct FastIO’
  451 | struct FastIO {
      |        ^~~~~~
answer.code:52:8: note: previous definition of ‘struct FastIO’
   52 | struct FastIO {
      |        ^~~~~~
answer.code:537:3: error: conflicting declaration ‘int IO’
  537 | } IO;
      |   ^~
answer.code:138:3: note: previous declaration as ‘FastIO IO’
  138 | } IO;
      |   ^~
answer.code:554:5: error: redefinition of ‘int n’
  554 | int n, m, q, R, X, S;
      |     ^
answer.code:155:5: note: ‘int n’ previously declared here
  155 | int n, m, q, R, X, S;
     ...