Submission #715939

# Submission time Handle Problem Language Result Execution time Memory
715939 2023-03-28T13:58:29 Z walterw Wild Boar (JOI18_wild_boar) C++17
100 / 100
9772 ms 493048 KB
#include "bits/stdc++.h"

using namespace std;

void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cout << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.first >> a.second;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.first << ", " << a.second << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}

#ifdef local
#define test(args...) abc("[" + string(#args) + "]", args)
#else
#define test(args...) void(0)
#endif

using ll = long long;

vector<array<ll, 3>> adj[2005];
pair<ll, ll> info[2005][2];

priority_queue<array<ll, 4>, vector<array<ll, 4>>, greater<array<ll, 4>>> pq;
vector<array<ll, 3>> v[2005];

void add(int node, ll val, int edgeid) {
    if (info[node][0] == pair<ll, ll> {val, edgeid} || info[node][1] == pair<ll, ll> {val, edgeid}) {
        return;
    }

    if (val < info[node][0].first) {
        swap(info[node][0], info[node][1]);
        info[node][0] = {val, edgeid};

        pq.push({info[node][1].first, info[node][1].second, node, 1});
        pq.push({info[node][0].first, info[node][0].second, node, 0});
    } else if (val < info[node][1].first && edgeid != info[node][0].second) {
        info[node][1] = {val, edgeid};
        pq.push({info[node][1].first, info[node][1].second, node, 1});
    }
}

struct dataa {
    vector<array<ll, 3>> res;

    dataa(){}
    dataa (vector<array<ll, 3>> temp) {
        map<int, int> a, b;
        set<pair<int, int>> s;

        sort(temp.begin(), temp.end());

        for (auto [w, id1, id2] : temp) {
            if (a[id1] == 2 || b[id2] == 2 || s.count({id1, id2}) > 0) continue;

            a[id1]++; b[id2]++;
            s.insert({id1, id2});
            res.push_back({w, id1, id2});
            if (res.size() == 5) break;
        }
    }
};

dataa route[2005][2005];

int arr[100005];

dataa tree[4 * 100005];

dataa comb(dataa lhs, dataa rhs) {
    vector<array<ll, 3>> temp;

    for (auto [w, id, i2] : lhs.res) {
        for (auto [w2, idd, idd2] : rhs.res) {
            if (i2 == idd) continue;
            temp.push_back({w + w2, id, idd2});
        }
    }

    return dataa(temp);
}

void build(int x, int l, int r) {
    if (l == r) {
        tree[x] = route[arr[l]][arr[l + 1]];
        return;
    }

    int mid = (l + r) / 2;

    build(2 * x, l, mid);
    build(2 * x + 1, mid + 1, r);

    tree[x] = comb(tree[2 * x], tree[2 * x + 1]);
}

void update(int x, int l, int r, int upnode) {
    if (l == r) {
        tree[x] = route[arr[l]][arr[l + 1]];
        return;
    }

    int mid = (l + r) / 2;

    if (upnode <= mid) update(2 * x, l, mid, upnode);
    else update(2 * x + 1, mid + 1, r, upnode);

    tree[x] = comb(tree[2 * x], tree[2 * x + 1]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
//    freopen("", "r", stdin);
//    freopen("", "w", stdout);
    ll n, m, t, l; cin >> n >> m >> t >> l;

    for (int i = 1; i <= m; i++) {
        ll a, b, c; cin >> a >> b >> c;
        adj[a].push_back({b, c, i});
        adj[b].push_back({a, c, i});
    }

    for (int x = 1; x <= n; x++) {
        // start from i
        for (int i = 1; i <= n; i++) v[i].clear();

        for (auto [temp, c, id] : adj[x]) {
            fill(&info[0][0], &info[0][0] + sizeof(info) / sizeof(info[0][0]), pair<ll, ll> ({1e17, -1}));

            add(temp, c, id);

            while (!pq.empty()) {
                auto cur = pq.top(); pq.pop();

                if (info[cur[2]][cur[3]] != pair<ll, ll> {cur[0], cur[1]})continue;

                for (auto [nxt, c2, id2] : adj[cur[2]]) {
                    if (cur[1] == id2) continue;
                    add(nxt, cur[0] + c2, id2);
                }
            }

            for (int i = 1; i <= n; i++) {
                if (info[i][0].first < 1e17) {
                    v[i].push_back({info[i][0].first, id, info[i][0].second});
                }

                if (info[i][1].first < 1e17) {
                    v[i].push_back({info[i][1].first, id, info[i][1].second});
                }
            }
        }

        for (int y = 1; y <= n; y++) {

            route[x][y] = dataa(v[y]);
        }
    }

    for (int i = 1; i <= l; i++) {
        cin >> arr[i];
    }

    build(1, 1, l-1);
//        cout << tree[1].res[0][0] << "\n";

    while (t--) {
        int a, b; cin >> a >> b;
        arr[a] = b;

        if (a != 1) {
            update(1, 1, l-1, a - 1);
        }

        if (a != (l)) {
            update(1, 1, l-1, a);
        }

        if (tree[1].res.empty()) cout << -1 << "\n";
        else cout << tree[1].res[0][0] << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 53 ms 104268 KB Output is correct
2 Correct 50 ms 104268 KB Output is correct
3 Correct 48 ms 104228 KB Output is correct
4 Correct 57 ms 104272 KB Output is correct
5 Correct 49 ms 104240 KB Output is correct
6 Correct 53 ms 104272 KB Output is correct
7 Correct 68 ms 104256 KB Output is correct
8 Correct 71 ms 104396 KB Output is correct
9 Correct 64 ms 104288 KB Output is correct
10 Correct 72 ms 104232 KB Output is correct
11 Correct 74 ms 104264 KB Output is correct
12 Correct 65 ms 104268 KB Output is correct
13 Correct 70 ms 104196 KB Output is correct
14 Correct 59 ms 104256 KB Output is correct
15 Correct 67 ms 104208 KB Output is correct
16 Correct 62 ms 104284 KB Output is correct
17 Correct 72 ms 104168 KB Output is correct
18 Correct 69 ms 104244 KB Output is correct
19 Correct 71 ms 104184 KB Output is correct
20 Correct 66 ms 104160 KB Output is correct
21 Correct 58 ms 104272 KB Output is correct
22 Correct 71 ms 104160 KB Output is correct
23 Correct 57 ms 104168 KB Output is correct
24 Correct 51 ms 104208 KB Output is correct
25 Correct 56 ms 104252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 104268 KB Output is correct
2 Correct 50 ms 104268 KB Output is correct
3 Correct 48 ms 104228 KB Output is correct
4 Correct 57 ms 104272 KB Output is correct
5 Correct 49 ms 104240 KB Output is correct
6 Correct 53 ms 104272 KB Output is correct
7 Correct 68 ms 104256 KB Output is correct
8 Correct 71 ms 104396 KB Output is correct
9 Correct 64 ms 104288 KB Output is correct
10 Correct 72 ms 104232 KB Output is correct
11 Correct 74 ms 104264 KB Output is correct
12 Correct 65 ms 104268 KB Output is correct
13 Correct 70 ms 104196 KB Output is correct
14 Correct 59 ms 104256 KB Output is correct
15 Correct 67 ms 104208 KB Output is correct
16 Correct 62 ms 104284 KB Output is correct
17 Correct 72 ms 104168 KB Output is correct
18 Correct 69 ms 104244 KB Output is correct
19 Correct 71 ms 104184 KB Output is correct
20 Correct 66 ms 104160 KB Output is correct
21 Correct 58 ms 104272 KB Output is correct
22 Correct 71 ms 104160 KB Output is correct
23 Correct 57 ms 104168 KB Output is correct
24 Correct 51 ms 104208 KB Output is correct
25 Correct 56 ms 104252 KB Output is correct
26 Correct 57 ms 104272 KB Output is correct
27 Correct 88 ms 109884 KB Output is correct
28 Correct 82 ms 109068 KB Output is correct
29 Correct 332 ms 135340 KB Output is correct
30 Correct 355 ms 136996 KB Output is correct
31 Correct 351 ms 137840 KB Output is correct
32 Correct 359 ms 138420 KB Output is correct
33 Correct 390 ms 133812 KB Output is correct
34 Correct 330 ms 133704 KB Output is correct
35 Correct 333 ms 141132 KB Output is correct
36 Correct 367 ms 138836 KB Output is correct
37 Correct 350 ms 134920 KB Output is correct
38 Correct 311 ms 132876 KB Output is correct
39 Correct 395 ms 141204 KB Output is correct
40 Correct 309 ms 133152 KB Output is correct
41 Correct 323 ms 133104 KB Output is correct
42 Correct 338 ms 144172 KB Output is correct
43 Correct 310 ms 134232 KB Output is correct
44 Correct 313 ms 134028 KB Output is correct
45 Correct 221 ms 122988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 104268 KB Output is correct
2 Correct 50 ms 104268 KB Output is correct
3 Correct 48 ms 104228 KB Output is correct
4 Correct 57 ms 104272 KB Output is correct
5 Correct 49 ms 104240 KB Output is correct
6 Correct 53 ms 104272 KB Output is correct
7 Correct 68 ms 104256 KB Output is correct
8 Correct 71 ms 104396 KB Output is correct
9 Correct 64 ms 104288 KB Output is correct
10 Correct 72 ms 104232 KB Output is correct
11 Correct 74 ms 104264 KB Output is correct
12 Correct 65 ms 104268 KB Output is correct
13 Correct 70 ms 104196 KB Output is correct
14 Correct 59 ms 104256 KB Output is correct
15 Correct 67 ms 104208 KB Output is correct
16 Correct 62 ms 104284 KB Output is correct
17 Correct 72 ms 104168 KB Output is correct
18 Correct 69 ms 104244 KB Output is correct
19 Correct 71 ms 104184 KB Output is correct
20 Correct 66 ms 104160 KB Output is correct
21 Correct 58 ms 104272 KB Output is correct
22 Correct 71 ms 104160 KB Output is correct
23 Correct 57 ms 104168 KB Output is correct
24 Correct 51 ms 104208 KB Output is correct
25 Correct 56 ms 104252 KB Output is correct
26 Correct 57 ms 104272 KB Output is correct
27 Correct 88 ms 109884 KB Output is correct
28 Correct 82 ms 109068 KB Output is correct
29 Correct 332 ms 135340 KB Output is correct
30 Correct 355 ms 136996 KB Output is correct
31 Correct 351 ms 137840 KB Output is correct
32 Correct 359 ms 138420 KB Output is correct
33 Correct 390 ms 133812 KB Output is correct
34 Correct 330 ms 133704 KB Output is correct
35 Correct 333 ms 141132 KB Output is correct
36 Correct 367 ms 138836 KB Output is correct
37 Correct 350 ms 134920 KB Output is correct
38 Correct 311 ms 132876 KB Output is correct
39 Correct 395 ms 141204 KB Output is correct
40 Correct 309 ms 133152 KB Output is correct
41 Correct 323 ms 133104 KB Output is correct
42 Correct 338 ms 144172 KB Output is correct
43 Correct 310 ms 134232 KB Output is correct
44 Correct 313 ms 134028 KB Output is correct
45 Correct 221 ms 122988 KB Output is correct
46 Correct 232 ms 111572 KB Output is correct
47 Correct 2392 ms 178880 KB Output is correct
48 Correct 3164 ms 224240 KB Output is correct
49 Correct 3769 ms 265676 KB Output is correct
50 Correct 3845 ms 271044 KB Output is correct
51 Correct 3657 ms 272024 KB Output is correct
52 Correct 3966 ms 296516 KB Output is correct
53 Correct 4070 ms 295820 KB Output is correct
54 Correct 4130 ms 295312 KB Output is correct
55 Correct 4737 ms 292660 KB Output is correct
56 Correct 4449 ms 316648 KB Output is correct
57 Correct 4722 ms 342232 KB Output is correct
58 Correct 4844 ms 361824 KB Output is correct
59 Correct 5223 ms 385360 KB Output is correct
60 Correct 5492 ms 407292 KB Output is correct
61 Correct 5727 ms 430840 KB Output is correct
62 Correct 6128 ms 458360 KB Output is correct
63 Correct 6015 ms 488860 KB Output is correct
64 Correct 3830 ms 368360 KB Output is correct
65 Correct 4088 ms 368368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 104268 KB Output is correct
2 Correct 50 ms 104268 KB Output is correct
3 Correct 48 ms 104228 KB Output is correct
4 Correct 57 ms 104272 KB Output is correct
5 Correct 49 ms 104240 KB Output is correct
6 Correct 53 ms 104272 KB Output is correct
7 Correct 68 ms 104256 KB Output is correct
8 Correct 71 ms 104396 KB Output is correct
9 Correct 64 ms 104288 KB Output is correct
10 Correct 72 ms 104232 KB Output is correct
11 Correct 74 ms 104264 KB Output is correct
12 Correct 65 ms 104268 KB Output is correct
13 Correct 70 ms 104196 KB Output is correct
14 Correct 59 ms 104256 KB Output is correct
15 Correct 67 ms 104208 KB Output is correct
16 Correct 62 ms 104284 KB Output is correct
17 Correct 72 ms 104168 KB Output is correct
18 Correct 69 ms 104244 KB Output is correct
19 Correct 71 ms 104184 KB Output is correct
20 Correct 66 ms 104160 KB Output is correct
21 Correct 58 ms 104272 KB Output is correct
22 Correct 71 ms 104160 KB Output is correct
23 Correct 57 ms 104168 KB Output is correct
24 Correct 51 ms 104208 KB Output is correct
25 Correct 56 ms 104252 KB Output is correct
26 Correct 57 ms 104272 KB Output is correct
27 Correct 88 ms 109884 KB Output is correct
28 Correct 82 ms 109068 KB Output is correct
29 Correct 332 ms 135340 KB Output is correct
30 Correct 355 ms 136996 KB Output is correct
31 Correct 351 ms 137840 KB Output is correct
32 Correct 359 ms 138420 KB Output is correct
33 Correct 390 ms 133812 KB Output is correct
34 Correct 330 ms 133704 KB Output is correct
35 Correct 333 ms 141132 KB Output is correct
36 Correct 367 ms 138836 KB Output is correct
37 Correct 350 ms 134920 KB Output is correct
38 Correct 311 ms 132876 KB Output is correct
39 Correct 395 ms 141204 KB Output is correct
40 Correct 309 ms 133152 KB Output is correct
41 Correct 323 ms 133104 KB Output is correct
42 Correct 338 ms 144172 KB Output is correct
43 Correct 310 ms 134232 KB Output is correct
44 Correct 313 ms 134028 KB Output is correct
45 Correct 221 ms 122988 KB Output is correct
46 Correct 232 ms 111572 KB Output is correct
47 Correct 2392 ms 178880 KB Output is correct
48 Correct 3164 ms 224240 KB Output is correct
49 Correct 3769 ms 265676 KB Output is correct
50 Correct 3845 ms 271044 KB Output is correct
51 Correct 3657 ms 272024 KB Output is correct
52 Correct 3966 ms 296516 KB Output is correct
53 Correct 4070 ms 295820 KB Output is correct
54 Correct 4130 ms 295312 KB Output is correct
55 Correct 4737 ms 292660 KB Output is correct
56 Correct 4449 ms 316648 KB Output is correct
57 Correct 4722 ms 342232 KB Output is correct
58 Correct 4844 ms 361824 KB Output is correct
59 Correct 5223 ms 385360 KB Output is correct
60 Correct 5492 ms 407292 KB Output is correct
61 Correct 5727 ms 430840 KB Output is correct
62 Correct 6128 ms 458360 KB Output is correct
63 Correct 6015 ms 488860 KB Output is correct
64 Correct 3830 ms 368360 KB Output is correct
65 Correct 4088 ms 368368 KB Output is correct
66 Correct 251 ms 130240 KB Output is correct
67 Correct 763 ms 139116 KB Output is correct
68 Correct 2316 ms 236192 KB Output is correct
69 Correct 2626 ms 240560 KB Output is correct
70 Correct 2727 ms 244040 KB Output is correct
71 Correct 6987 ms 182460 KB Output is correct
72 Correct 7677 ms 232136 KB Output is correct
73 Correct 7507 ms 301772 KB Output is correct
74 Correct 7840 ms 303228 KB Output is correct
75 Correct 7526 ms 299924 KB Output is correct
76 Correct 8077 ms 265304 KB Output is correct
77 Correct 8299 ms 276424 KB Output is correct
78 Correct 8446 ms 276428 KB Output is correct
79 Correct 7956 ms 345832 KB Output is correct
80 Correct 8329 ms 369780 KB Output is correct
81 Correct 9222 ms 373092 KB Output is correct
82 Correct 8657 ms 412552 KB Output is correct
83 Correct 9772 ms 438272 KB Output is correct
84 Correct 9285 ms 493048 KB Output is correct
85 Correct 6178 ms 371376 KB Output is correct