Submission #825336

# Submission time Handle Problem Language Result Execution time Memory
825336 2023-08-14T17:56:29 Z vjudge1 Designated Cities (JOI19_designated_cities) C++17
100 / 100
510 ms 74892 KB
#include <bits/stdc++.h>
using namespace std;

// 123

class segtree_t {
       public:
        segtree_t *left, *right;
        int l, r, m, id;
        int64_t val, lazy;

        segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), id(l), val(0), lazy(0) {
                if (l == r) return;
                left = new segtree_t(l, m);
                right = new segtree_t(m + 1, r);
        }

        void Update(int s, int t, int64_t x) {
                if (r < s or l > t) return;
                if (s <= l && r <= t) {
                        val += x;
                        lazy += x;
                        return;
                }
                Down();
                left->Update(s, t, x);
                right->Update(s, t, x);
                Up();
        }

        pair<int64_t, int> Get(int s, int t) {
                if (r < s or l > t) return pair<int64_t, int>(-1, -1);
                if (s <= l && r <= t) return pair<int64_t, int>(val, id);
                Down();
                return max(left->Get(s, t), right->Get(s, t));
        }

        void Up() {
                if (left->val < right->val) {
                        val = right->val;
                        id = right->id;
                } else {
                        val = left->val;
                        id = left->id;
                }
        }

        void Down() {
                left->lazy += lazy;
                right->lazy += lazy;
                right->val += lazy;
                left->val += lazy;
                lazy = 0;
        }
};

int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        int n;
        cin >> n;
        vector<vector<tuple<int, int, int>>> adj(n);
        vector<int> deg(n, 0);
        for (int i = 0; i < n - 1; i++) {
                int u, v, a, b;
                cin >> u >> v >> a >> b;
                u--, v--;
                deg[u]++, deg[v]++;
                adj[u].emplace_back(v, a, b);
                adj[v].emplace_back(u, b, a);
        }

        vector<int64_t> f(n, 0);
        function<void(int, int)> dfs1 = [&](int u, int p) {
                for (auto [v, a, b] : adj[u]) {
                        if (v == p) continue;
                        dfs1(v, u);
                        f[u] += a + f[v];
                }
        };

        vector<int64_t> g(n, 0);
        function<void(int, int)> dfs2 = [&](int u, int p) {
                for (auto [v, a, b] : adj[u]) {
                        if (v == p) continue;
                        g[v] = g[u] - a + b;
                        dfs2(v, u);
                }
        };

        dfs1(0, -1);
        g[0] = f[0];
        dfs2(0, -1);
        int root = min_element(g.begin(), g.end()) - g.begin();

        vector<int64_t> h(n, 0);
        function<void(int, int)> dfs3 = [&](int u, int p) {
                for (auto [v, a, b] : adj[u]) {
                        if (v == p) continue;
                        h[v] = h[u] + a;
                        dfs3(v, u);
                }
        };

        dfs3(root, -1);
        int root2 = max_element(h.begin(), h.end()) - h.begin();
        h[root2] = 0;
        dfs3(root2, -1);
        int64_t best = *max_element(h.begin(), h.end());

        int time = 0;
        vector<int> tin(n), tout(n), par(n, -1), costp(n);

        function<void(int, int)> dfs4 = [&](int u, int p) {
                tin[u] = time++;
                for (auto [v, a, b] : adj[u]) {
                        if (v == p) continue;
                        par[v] = u;
                        costp[v] = a;
                        dfs4(v, u);
                }
                tout[u] = time;
        };
        dfs4(root2, -1);

        vector<int> node(n);
        for (int i = 0; i < n; i++) {
                node[tin[i]] = i;
        }

        segtree_t *tree = new segtree_t(0, time);
        for (int i = 0; i < n; i++) {
                tree->Update(tin[i], tin[i], h[i]);
        }

        vector<int64_t> pref(n);
        pref[0] = g[root2];
        vector<int> vis(n, 0);
        vis[root2] = 1;

        for (int i = 1; i < n; i++) {
                pref[i] = pref[i - 1] - tree->val;
                int x = node[tree->id];
                while (!vis[x]) {
                        vis[x] = 1;
                        tree->Update(tin[x], tout[x] - 1, -costp[x]);
                        x = par[x];
                }
        }
        pref[0] = g[root];

        int q;
        cin >> q;

        while (q--) {
                int e;
                cin >> e;
                e--;
                cout << pref[e] << '\n';
        }
}

Compilation message

designated_cities.cpp: In constructor 'segtree_t::segtree_t(int, int)':
designated_cities.cpp:12:51: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 |         segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), id(l), val(0), lazy(0) {
      |                                                 ~~^~~
designated_cities.cpp: In function 'int32_t main()':
designated_cities.cpp:110:17: warning: unused variable 'best' [-Wunused-variable]
  110 |         int64_t best = *max_element(h.begin(), h.end());
      |                 ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 224 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 478 ms 49988 KB Output is correct
3 Correct 421 ms 66816 KB Output is correct
4 Correct 429 ms 50004 KB Output is correct
5 Correct 392 ms 50076 KB Output is correct
6 Correct 416 ms 52344 KB Output is correct
7 Correct 404 ms 49936 KB Output is correct
8 Correct 425 ms 66116 KB Output is correct
9 Correct 381 ms 50424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 420 ms 50036 KB Output is correct
3 Correct 480 ms 66760 KB Output is correct
4 Correct 340 ms 49996 KB Output is correct
5 Correct 420 ms 49944 KB Output is correct
6 Correct 442 ms 53108 KB Output is correct
7 Correct 363 ms 50232 KB Output is correct
8 Correct 422 ms 61284 KB Output is correct
9 Correct 378 ms 50420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 224 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 2 ms 724 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 3 ms 820 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
19 Correct 2 ms 816 KB Output is correct
20 Correct 2 ms 724 KB Output is correct
21 Correct 2 ms 724 KB Output is correct
22 Correct 2 ms 724 KB Output is correct
23 Correct 2 ms 724 KB Output is correct
24 Correct 2 ms 852 KB Output is correct
25 Correct 2 ms 852 KB Output is correct
26 Correct 2 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 478 ms 49988 KB Output is correct
3 Correct 421 ms 66816 KB Output is correct
4 Correct 429 ms 50004 KB Output is correct
5 Correct 392 ms 50076 KB Output is correct
6 Correct 416 ms 52344 KB Output is correct
7 Correct 404 ms 49936 KB Output is correct
8 Correct 425 ms 66116 KB Output is correct
9 Correct 381 ms 50424 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 420 ms 50036 KB Output is correct
12 Correct 480 ms 66760 KB Output is correct
13 Correct 340 ms 49996 KB Output is correct
14 Correct 420 ms 49944 KB Output is correct
15 Correct 442 ms 53108 KB Output is correct
16 Correct 363 ms 50232 KB Output is correct
17 Correct 422 ms 61284 KB Output is correct
18 Correct 378 ms 50420 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 389 ms 50036 KB Output is correct
21 Correct 429 ms 73272 KB Output is correct
22 Correct 327 ms 55064 KB Output is correct
23 Correct 510 ms 56544 KB Output is correct
24 Correct 468 ms 55256 KB Output is correct
25 Correct 397 ms 56528 KB Output is correct
26 Correct 397 ms 55320 KB Output is correct
27 Correct 414 ms 56312 KB Output is correct
28 Correct 429 ms 59052 KB Output is correct
29 Correct 400 ms 56392 KB Output is correct
30 Correct 377 ms 55504 KB Output is correct
31 Correct 359 ms 56408 KB Output is correct
32 Correct 492 ms 68332 KB Output is correct
33 Correct 347 ms 56892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 224 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 478 ms 49988 KB Output is correct
14 Correct 421 ms 66816 KB Output is correct
15 Correct 429 ms 50004 KB Output is correct
16 Correct 392 ms 50076 KB Output is correct
17 Correct 416 ms 52344 KB Output is correct
18 Correct 404 ms 49936 KB Output is correct
19 Correct 425 ms 66116 KB Output is correct
20 Correct 381 ms 50424 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 420 ms 50036 KB Output is correct
23 Correct 480 ms 66760 KB Output is correct
24 Correct 340 ms 49996 KB Output is correct
25 Correct 420 ms 49944 KB Output is correct
26 Correct 442 ms 53108 KB Output is correct
27 Correct 363 ms 50232 KB Output is correct
28 Correct 422 ms 61284 KB Output is correct
29 Correct 378 ms 50420 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 2 ms 724 KB Output is correct
32 Correct 2 ms 980 KB Output is correct
33 Correct 2 ms 724 KB Output is correct
34 Correct 3 ms 724 KB Output is correct
35 Correct 3 ms 820 KB Output is correct
36 Correct 2 ms 724 KB Output is correct
37 Correct 2 ms 816 KB Output is correct
38 Correct 2 ms 724 KB Output is correct
39 Correct 2 ms 724 KB Output is correct
40 Correct 2 ms 724 KB Output is correct
41 Correct 2 ms 724 KB Output is correct
42 Correct 2 ms 852 KB Output is correct
43 Correct 2 ms 852 KB Output is correct
44 Correct 2 ms 852 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 389 ms 50036 KB Output is correct
47 Correct 429 ms 73272 KB Output is correct
48 Correct 327 ms 55064 KB Output is correct
49 Correct 510 ms 56544 KB Output is correct
50 Correct 468 ms 55256 KB Output is correct
51 Correct 397 ms 56528 KB Output is correct
52 Correct 397 ms 55320 KB Output is correct
53 Correct 414 ms 56312 KB Output is correct
54 Correct 429 ms 59052 KB Output is correct
55 Correct 400 ms 56392 KB Output is correct
56 Correct 377 ms 55504 KB Output is correct
57 Correct 359 ms 56408 KB Output is correct
58 Correct 492 ms 68332 KB Output is correct
59 Correct 347 ms 56892 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 446 ms 59116 KB Output is correct
62 Correct 507 ms 74892 KB Output is correct
63 Correct 354 ms 57748 KB Output is correct
64 Correct 426 ms 59188 KB Output is correct
65 Correct 425 ms 57736 KB Output is correct
66 Correct 435 ms 59156 KB Output is correct
67 Correct 407 ms 57792 KB Output is correct
68 Correct 407 ms 59056 KB Output is correct
69 Correct 427 ms 61516 KB Output is correct
70 Correct 405 ms 58900 KB Output is correct
71 Correct 415 ms 58352 KB Output is correct
72 Correct 456 ms 59624 KB Output is correct
73 Correct 480 ms 70412 KB Output is correct
74 Correct 360 ms 61052 KB Output is correct