QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#199306#6404. Shuttle TourUrgantTeamTL 2642ms15964kbC++236.6kb2023-10-04 05:05:102023-10-04 05:05:10

Judging History

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

  • [2023-10-04 05:05:10]
  • 评测
  • 测评结果:TL
  • 用时:2642ms
  • 内存:15964kb
  • [2023-10-04 05:05:10]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

struct pt {
    int to, w, node;
};

typedef long long ll;
int const maxn = 2e5 + 5, K = 55;
string s;
int numb[maxn], tmp = 1;
ll h[maxn], ans;
vector < pair < int, int > > g[maxn];
pair < int, int > dp[(1 << 20)][K];
pair < int, int > f[K];
vector < pt > G[K];
int ok[maxn], good[maxn];
int lst[maxn];

void dfs(int v, int p, int cur) {
    //assert(cur < K);
    int flag = 1;
    lst[v] = p;
    numb[v] = cur;
    for (auto [u, w] : g[v]) {
        if (u == p) continue;
        h[u] = h[v] + w;
        if (flag) dfs(u, v, cur), flag = 0;
        else {
            G[cur].push_back({tmp, w, v});
            tmp++;
            dfs(u, v, tmp - 1);
        }
    }
}

inline pair < int, int > merge(pair < int, int > a, pair < int, int > b) {
    if (a.first == -1 || (b.first != -1 && b.first != -1 && h[a.first] > h[b.first])) a.first = b.first;
    if (a.second == -1 || (a.second != -1 && b.second != -1 && h[a.second] < h[b.second])) a.second = b.second;
    return a;
}

void build(int i, int l, int r) {
    if (r - l == 1) {
        for (int j = 0; j < tmp; j++) {
            dp[i][j] = {-1, -1};
        }
        if (s[l] == '1') dp[i][numb[l]] = {l, l};
    } else {
        int m = (r + l) / 2;
        build(2 * i + 1, l, m);
        build(2 * i + 2, m, r);
        for (int j = 0; j < tmp; j++) {
            dp[i][j] = merge(dp[2 * i + 1][j], dp[2 * i + 2][j]);
        }
    }
}

void update(int i, int l, int r, int lq) {
    if (r - l == 1) {
        //assert(l == lq);
        if (s[lq] == '1') s[lq] = '0';
        else s[lq] = '1';
        for (int j = 0; j < tmp; j++) {
            dp[i][j] = {-1, -1};
        }
        if (s[l] == '1') dp[i][numb[l]] = {l, l};
    } else {
        int m = (r + l) / 2;
        if (lq < m) update(2 * i + 1, l, m, lq);
        else update(2 * i + 2, m, r, lq);
        for (int j = 0; j < tmp; j++) {
            dp[i][j] = merge(dp[2 * i + 1][j], dp[2 * i + 2][j]);
        }
    }
}

void get(int i, int l, int r, int lq, int rq) {
    if (lq >= r || l >= rq) return;
    if (lq <= l && r <= rq) {
        for (int j = 0; j < tmp; j++) f[j] = merge(f[j], dp[i][j]);
        return;
    }
    int m = (r + l) / 2;
    get(2 * i + 1, l, m, lq, rq);
    get(2 * i + 2, m, r, lq, rq);
}

void calc(int v) {
    ok[v] = 0;
    good[v] = 0;
    if (f[v].first != -1) ok[v] = 1, good[v] = 1;
    for (auto u : G[v]) {
        calc(u.to);
        ok[v] |= ok[u.to];
    }
}

void get(int v, int s) {
    for (auto u : G[v]) {
        s += ok[u.to];
    }
    s += good[v];
    for (auto u : G[v]) {
        if (s - ok[u.to] > 0) {
            f[u.to].first = u.node;
            if (f[u.to].second == -1) f[u.to].second = u.node;
            get(u.to, 1);
        } else {
            get(u.to, 0);
        }
        if (ok[u.to]) {
            if (f[v].second == -1 || h[f[v].second] < h[u.node]) f[v].second = u.node;
            if (f[v].first == -1 || h[f[v].first] > h[u.node]) f[v].first = u.node;
            if (f[v].first != -1 && f[v].second == -1) f[v].second = f[v].first;
            if (f[v].second != -1 && f[v].first == -1) f[v].first = f[v].second;
        }
    }
}

int check(int v, int p, int l, int r) {
    int ans = (l <= v && v <= r && s[v] == '1');
    for (auto [u, w] : g[v]) {
        if (u != p) ans |= check(u, v, l, r);
    }
    return ans;
}

main() {
#ifdef HOME
    freopen("input.txt", "r", stdin);
#endif // HOME
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, q, u, v, w, l, r, type;
    cin >> n >> q;
    cin >> s;
    s = "#" + s;
    vector < vector < int > > edge;
    for (int i = 1; i < n; i++) {
        cin >> u >> v >> w;
        //assert(w >= 0);
        edge.push_back({u, v, w});
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    dfs(1, 0, 0);
    //assert(tmp < K);
    build(0, 1, n + 1);
    set < int > goal;
    for (int i = 1; i <= q; i++) {
        cin >> type;
        if (type == 2) {
            cin >> l >> r;
            for (int j = 0; j < tmp; j++) f[j] = {-1, -1};
            get(0, 1, n + 1, l, r + 1);
            /*for (int j = 0; j < tmp; j++) {
                cout << f[j].first << " " << f[j].second << endl;
            }*/
            for (int j = 0; j < tmp; j++) {
                for (int z = l; z <= r; z++) {
                    if (s[z] == '0' || numb[z] != j) continue;
                    /*if (f[j].first == -1 || h[z] < h[f[j].first]) {
                        assert(1 == 0);
                    }*/
                    if (f[j].second == -1 || h[z] > h[f[j].second]) {
                        assert(1 == 0);
                    }
                }
            }
            set < pair < int, int > > used;
            ans = 0;
            calc(0);
            get(0, 0);
            int flag = 0;
            for (int j = 0; j < tmp; j++) {
                if (f[j].first == -1) f[j].first = f[j].second;
                else if (f[j].second == -1) f[j].second = f[j].first;
                //cout << f[j].first << " " << f[j].second << endl;
                if (f[j].first != -1) {
                    ans += h[f[j].second] - h[f[j].first], flag = 1;
                    u = f[j].second, v = f[j].first;
                    while (u != v) {
                        //assert(used.find({u, lst[u]}) == used.end());
                        used.insert({u, lst[u]});
                        u = lst[u];
                    }
                }
            }
            ll out;
            if (flag) out = 2 * ans;
            else out = -1;
            cout << out << '\n';
            ll res = -1;
            for (int j = l; j <= r; j++) {
                if (s[j] == '1') res = 0;
            }
            if (res == 0) {
                for (auto key : edge) {
                    //assert(key[2] >= 0);
                    if (check(key[0], key[1], l, r) && check(key[1], key[0], l, r)) {
                        res += key[2];
                        /*if (used.find({key[0], key[1]}) == used.end() && used.find({key[1], key[0]}) == used.end()) {
                            assert(1 == 0);
                        }*/
                        used.erase({key[0], key[1]});
                        used.erase({key[1], key[0]});
                    }
                }
                res *= 2;
            }
            //assert(used.size() == 0);
            //assert(out == res);
        } else {
            cin >> u;
            update(0, 1, n + 1, u);
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 2100ms
memory: 13904kb

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:

17149847378
-1
26540740138
29613692754
21307948558
27003443184
11893407374
18332625722
20412196350
20281573062
29727468956
9593307160
9380710234
23682775470
16688997988
2856655228
14982588156
0
-1
9140136614
26193602866
22558599316
24807163344
19126284190
8533875940
7695830712
18079494744
0
27004673...

result:

ok 100000 numbers

Test #3:

score: 0
Accepted
time: 2146ms
memory: 15964kb

input:

50 200000
10010001110011101101101011010011100101101010111001
1 25 560163135
2 7 696374536
33 39 14485266
39 22 456690113
4 5 267811886
18 6 657680382
3 43 865617406
25 14 885515220
41 34 919525052
42 50 410634144
8 3 487403277
3 30 701549676
2 43 54223104
7 34 691078766
14 23 323352590
26 48 4936558...

output:

17177676326
31373486130
15290175638
8192974494
22734716092
27802380120
6315957348
0
15780401446
15929384564
26423899248
9639532596
21199366790
26413065782
-1
29063665908
29925313598
28651879288
17144176262
16526083688
28206620550
26873163342
14840246174
32414950818
29985336496
0
11937422088
11131990...

result:

ok 100000 numbers

Test #4:

score: 0
Accepted
time: 2642ms
memory: 11904kb

input:

50 200000
01011001001010000100001101100111011001101011101000
34 29 654100339
33 5 947063765
45 25 962067151
11 13 509130797
26 47 988757358
49 22 75267557
7 11 431530851
46 1 531969122
22 43 449891945
34 6 526679193
50 17 499004364
22 23 226725950
26 48 203414490
11 44 900725507
10 29 451714017
35 2...

output:

24080362406
0
0
21418182584
28358635244
28257750030
24520294678
21418182584
0
15335211126
28621468542
18664505530
19335499776
32374868794
23618866752
26801803500
24116134918
27676993638
30222353942
25612316674
20504702130
28400398734
29472795250
24400110084
20586186786
25612316674
0
30067400886
1297...

result:

ok 100000 numbers

Test #5:

score: 0
Accepted
time: 2ms
memory: 11888kb

input:

2 10
01
1 2 340134039
2 1 2
1 1
2 1 2
1 2
2 1 1
1 2
2 2 2
1 2
2 2 2
1 1

output:

0
680268078
0
0
-1

result:

ok 5 number(s): "0 680268078 0 0 -1"

Test #6:

score: -100
Time Limit Exceeded

input:

200000 100
1100001001001101010000111111011011101010001011011011011010101111010001111010101100101001010000110100100011111010011100101010010001011010111100101110010000101010000100111100000011001100111101110011000011010011011011100101100111101010111101110111001111111010001010010000110110100000111000111...

output:


result: