QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#311021#6404. Shuttle Tourmemset0WA 128ms52864kbC++204.4kb2024-01-21 20:58:502024-01-21 20:58:50

Judging History

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

  • [2024-01-21 20:58:50]
  • 评测
  • 测评结果:WA
  • 用时:128ms
  • 内存:52864kb
  • [2024-01-21 20:58:50]
  • 提交

answer

#include <bits/stdc++.h>
#ifndef popteam
#define endl '\n'
#endif
#define all(x) begin(x), end(x)
using namespace std;
const int N = 2e5 + 9, M = 59;
int n, m, tot, nod, fa[N], bln[N], siz[N], out[M];
bool ok[N];
long long dep[N];
string s;
vector<int> leaves;
vector<pair<int, int>> G[N], child[M];
void dfs(int u) {
    siz[u] = 1;
    for (auto [v, w] : G[u])
        if (v != fa[u]) {
            fa[v] = u;
            dep[v] = dep[u] + w;
            dfs(v);
            siz[u] += siz[v];
        }
    if (siz[u] == 1) {
        leaves.push_back(u);
    }
}
struct atom {
    long long min, max;
    void reset() { min = LLONG_MAX, max = LLONG_MIN; }
    bool empty() { return min == LLONG_MAX && max == LLONG_MIN; }
    atom operator+(const atom &rhs) const {
        return {
            std::min(min, rhs.min),
            std::max(max, rhs.max),
        };
    }
} dp[M];
struct segtree {
    atom p[524288];
    void maintain(int u) {
        p[u].min = min(p[u << 1].min, p[u << 1 | 1].min);
        p[u].max = max(p[u << 1].max, p[u << 1 | 1].max);
    }
    void build(int u, int l, int r) {
        p[u].reset();
        if (l == r) return;
        int mid = (l + r) >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
    }
    void modify(int u, int k, bool op, int l, int r) {
        // fprintf(stderr, "modify %d %d %d %d %d\n", u, k, op, l, r);
        if (l == r) {
            if (op) {
                p[u].min = p[u].max = dep[k];
            } else {
                p[u].reset();
            }
            return;
        }
        int mid = (l + r) >> 1;
        if (k <= mid) {
            modify(u << 1, k, op, l, mid);
        } else {
            modify(u << 1 | 1, k, op, mid + 1, r);
        }
        maintain(u);
    }
    atom query(int u, int ql, int qr, int l, int r) {
        // fprintf(stderr, "query %d %d %d %d %d\n", u, ql, qr, l, r);
        if (ql == l && qr == r) {
            return p[u];
        }
        int mid = (l + r) >> 1;
        if (qr <= mid) return query(u << 1, ql, qr, l, mid);
        if (ql > mid) return query(u << 1 | 1, ql, qr, mid + 1, r);
        return query(u << 1, ql, mid, l, mid) + query(u << 1 | 1, mid + 1, qr, mid + 1, r);
    }
} seg[M];
int main() {
#ifdef popteam
    freopen("K.in", "r", stdin);
#endif
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> m >> s;
    for (int i = 1; i <= n; i++) {
        ok[i] = s[i - 1] == '1';
    }
    for (int u, v, w, i = 1; i < n; i++) {
        cin >> u >> v >> w;
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
    }
    dfs(1);
    memset(bln, -1, sizeof(bln));
    for (int i = 0; i < leaves.size(); i++) {
        seg[i].build(1, 1, n);
        int x = leaves[i];
        for (int u = x, lst = -1; u; lst = u, u = fa[u])
            if (bln[u] == -1) {
                bln[u] = i;
                if (ok[u]) {
                    seg[i].modify(1, u, true, 1, n);
                }
            } else {
                out[i] = u;
                child[bln[u]].emplace_back(i, lst);
                break;
            }
    }
    // for (int i = 1; i <= n; i++)
    //     cerr << bln[i] << " \n"[i == n];
    for (int op, x, l, r, i = 1; i <= m; i++) {
        cin >> op;
        if (op == 1) {
            cin >> x;
            ok[x] ^= 1;
            seg[bln[x]].modify(1, x, ok[x], 1, n);
        } else {
            cin >> l >> r;
            bool ok = false;
            long long ans = 0;
            for (int i = leaves.size() - 1; i >= 0; i--) {
                dp[i] = seg[i].query(1, l, r, 1, n);
                // fprintf(stderr, "%d >> %lld %lld\n", i, dp[i].min, dp[i].max);
                for (auto [j, x] : child[i]) {
                    if (!dp[j].empty()) {
                        if (dp[i].min < dep[x]) {
                            ans += dep[x] - dep[fa[x]];
                        }
                    }
                }
                for (auto [j, x] : child[i]) {
                    if (!dp[j].empty()) {
                        dp[i] = dp[i] + atom{dep[fa[x]], dep[fa[x]]};
                    }
                }
                if (!dp[i].empty()) {
                    ok = true;
                    ans += dp[i].max - dp[i].min;
                }
            }
            cout << (ok ? (ans << 1) : -1) << endl;
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8468kb

input:

5 6
10110
1 2 1
1 3 10
2 4 100
3 5 1000
2 1 5
1 3
2 1 5
2 2 4
2 5 5
2 1 1

output:

222
202
0
-1
0

result:

ok 5 number(s): "222 202 0 -1 0"

Test #2:

score: -100
Wrong Answer
time: 128ms
memory: 52864kb

input:

50 200000
00100100100001101010110100000111100011101110011010
14 47 940241413
11 43 331483591
37 38 496247070
47 46 832459694
7 15 16479291
1 30 402388666
30 8 513064064
46 50 739311480
5 4 761894947
32 41 631669659
17 24 715786564
35 20 763151642
32 33 492466005
39 11 186023146
9 7 805081251
3 42 25...

output:

11509486460
-1
13260223880
19688192486
14667500572
14757940400
4773174760
10568195126
13738789782
9459933218
18370395560
1495700394
5200572410
13041144716
3823009266
1983486788
7158975850
0
-1
3538258692
14501427758
12236015134
10794985720
8312182988
4405876042
5148421194
9617378832
0
19631728566
0
...

result:

wrong answer 1st numbers differ - expected: '17149847378', found: '11509486460'