QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#310975#6404. Shuttle TourPlentyOfPenaltyWA 139ms155256kbC++174.6kb2024-01-21 20:17:322024-01-21 20:17:33

Judging History

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

  • [2024-01-21 20:17:33]
  • 评测
  • 测评结果:WA
  • 用时:139ms
  • 内存:155256kb
  • [2024-01-21 20:17:32]
  • 提交

answer

#include "bits/stdc++.h"
typedef long long ll;
typedef unsigned un;
using namespace std;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, ll> pil;
const int MAXN = 200011, INF = 1e9 + 233;

pll merge(pll a, pll b) { return pll(max(a.first, b.first), min(a.second, b.second)); }
int n, q;
struct Segment_Tree {
    struct node {
        ll maxv, minv;
    } t[MAXN << 2 | 1];
#define rt t[num]
    void pushup(un num) {
        rt.maxv = max(t[num << 1].maxv, t[num << 1 | 1].maxv);
        rt.minv = min(t[num << 1].minv, t[num << 1 | 1].minv);
    }
    void build(un l = 1, un r = n, un num = 1) {
        if (l == r) {
            // if(arr[l]==-1)rt.maxv=0,rt.minv=INF;
            // else rt.maxv=rt.minv=arr[l];
            rt.maxv = 0, rt.minv = INF;
            return;
        }
        un mid = (l + r) >> 1;
        build(l, mid, num << 1), build(mid + 1, r, num << 1 | 1);
        pushup(num);
    }
    void modify(un pos, ll val, un l = 1, un r = n, un num = 1) {
        if (l == r) {
            if (val == -1)
                rt.maxv = 0, rt.minv = INF;
            else
                rt.maxv = rt.minv = val;
            return;
        }
        un mid = (l + r) >> 1;
        if (pos <= mid)
            modify(pos, val, l, mid, num << 1);
        else
            modify(pos, val, mid + 1, r, num << 1 | 1);
        pushup(num);
    }
    pll Query(un ql, un qr, un l = 1, un r = n, un num = 1) {
        if (ql <= l && r <= qr) return pll(rt.maxv, rt.minv);
        pll res(0, INF);
        un mid = (l + r) >> 1;
        if (ql <= mid) res = merge(res, Query(ql, qr, l, mid, num << 1));
        if (qr > mid) res = merge(res, Query(ql, qr, mid + 1, r, num << 1 | 1));
        return res;
    }
} sgt[51];
std::vector<pll> g[MAXN];
int ctrb[MAXN][51];
// std::vector<pil>ctrb[MAXN];
int fa[MAXN], top[MAXN], loc[MAXN];
ll dis[MAXN];
int cur = 0;
string s;
void push(int u, int sp, int c, int d) {
    if (s[u] == '1') sgt[c].modify(u, dis[d]); //, printf("sgt[%d] modify %d %lld\n", c, u, dis[d]);
    ctrb[u][c] = d;
    // ctrb[u].push_back(pil(c,d));

    for (auto P : g[u]) {
        int v = P.first;
        if (v != sp && v != fa[u]) push(v, sp, c, d);
    }
}
void dfs(int u, int t, bool type) {
    // printf("u=%d,t=%d\n",u,t);
    if (type) ++cur, t = u;
    top[u] = t, loc[u] = cur;
    // printf("loc[%d]=%d\n", u, cur);
    int p = u;
    while (p) {
        ctrb[u][loc[p]] = p;
        // ctrb[u].emplace_back(pil(loc[p],dis[p]));

        if (s[u] == '1') sgt[loc[p]].modify(u, dis[p]); //, printf("sgt[%d] modify %d %lld\n", loc[p], u, dis[p]);
        p = fa[top[p]];
    }
    // printf("now,u=%d\n", u);
    bool flag = 0;
    for (auto P : g[u]) {
        int v = P.first;
        ll w = P.second;
        if (v == fa[u]) continue;
        fa[v] = u, dis[v] = dis[u] + w;
        dfs(v, t, flag);
        flag = 1;
    }
}

int main() {
    memset(ctrb, -1, sizeof ctrb);
    // freopen("K.in","r",stdin);
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> q >> s;
    s.insert(s.begin(), 0);
    for (int i = 1; i < n; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        g[u].emplace_back(pll(v, w));
        g[v].emplace_back(pll(u, w));
    }
    // for(int i=1;i<=n;++i)val[0][i]=-1;
    for (int k = 1; k <= 50; ++k)
        sgt[k].build();
    dfs(1, 1, 1);
    for (int u = 1; u <= n; ++u) {
        int flag = 0;
        for (auto P : g[u]) {
            int v = P.first;
            if (v == fa[u]) continue;
            if (flag) push(1, v, loc[v], u);
            flag = 1;
        }
    }
    // puts("Pass dfs!");
    while (q--) {
        int op;
        cin >> op;
        if (op == 1) {
            int x;
            cin >> x;
            if (s[x] == '1') {
                for (int c = 1; c <= cur; ++c)
                    if (ctrb[x][c] != -1) sgt[c].modify(x, -1);
                s[x] = '0';
            } else {
                for (int c = 1; c <= cur; ++c)
                    if (ctrb[x][c] != -1) sgt[c].modify(x, dis[ctrb[x][c]]);
                s[x] = '1';
            }
        } else {
            int l, r, cnt = 0;
            cin >> l >> r;
            ll ans = 0;
            for (int k = 1; k <= cur; ++k) {
                pll f = sgt[k].Query(l, r);
                if (f.first >= f.second)
                    ans += f.first - f.second;
                else
                    ++cnt;
            }
            if (cnt == cur)
                cout << "-1\n";
            else
                cout << (ans << 1) << '\n';
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 155256kb

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: 139ms
memory: 155160kb

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:

64686047784
-1
74076940544
77149893160
68844148964
74539643590
59429607780
65868826128
67948396756
67817773468
77263669362
58413007034
56916910640
71218975876
64225198394
50392855634
62518788562
50677700840
-1
56676337020
73729803272
70094799722
72343363750
66662484596
56070076346
55232031118
656156...

result:

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