Submission #941967

# Submission time Handle Problem Language Result Execution time Memory
941967 2024-03-09T20:52:48 Z juliany2 Construction of Highway (JOI18_construction) C++17
100 / 100
895 ms 23628 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) (x).begin(), (x).end()

template<class T> struct BIT {
    int n;
    vector<T> bit;

    void init(int sz) { bit = vector<T> (n = sz + 7); }

    void upd(int i, T x) { 
        for (i++; i < n; i += i & -i) bit[i] += x;
    }

    T query(int i) {
        T ret = 0;
        for (i++; i > 0; i -= i & -i) ret += bit[i];
        return ret;
    }

    T query(int l, int r) { return query(r) - query(l - 1); };
};

const int N = 1e5 + 7;
int n;
vector<int> adj[N];
array<int, 2> e[N];
int c[N], sz[N], head[N], pos[N], timer;
int parent[N];
set<array<int, 3>> s;
vector<array<int, 3>> cur;
BIT<int> bit;

void dfs(int v = 1, int p = 0) {
    parent[v] = p;
    sz[v] = 1;

    for (int &u : adj[v]) {
        if (u == p)
            continue;

        dfs(u, v);
        sz[v] += sz[u];

        if (adj[v][0] == p || sz[u] > sz[adj[v][0]])
            swap(u, adj[v][0]);
    }
}

void dfs_hld(int v = 1, int p = 0) {
    pos[v] = ++timer;

    for (int u : adj[v]) {
        if (u == p)
            continue;

        head[u] = (u == adj[v][0] ? head[v] : u);
        dfs_hld(u, v);
    }
}

void add(int l, int r, int x) {
    vector<array<int, 3>> tmp;
    while (s.lower_bound({l, -1, -1}) != s.end() && (*s.lower_bound({l, -1, -1}))[0] <= r) {
        auto [a, b, y] = *s.lower_bound({l, -1, -1});

        if (b <= r) {
            tmp.push_back({a, b, y});
            s.erase({a, b, y});
        }
        else {
            tmp.push_back({a, r, y});
            s.insert({r + 1, b, y});
            s.erase({a, b, y});
        }
    }

    s.insert({l, r, x});

    reverse(all(tmp));

    for (auto &[a, b, y] : tmp)
        cur.push_back({a, b, y});
}

void query(int v, int x) {
    for (; v >= 1; v = parent[head[v]])
        add(pos[head[v]], pos[v], x);
}

int main() {
    cin.tie(0)->sync_with_stdio(false);

    cin >> n;

    vector<int> cmp;
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
        cmp.push_back(c[i]);
    }

    sort(all(cmp));
    cmp.erase(unique(all(cmp)), cmp.end());

    for (int i = 1; i <= n; i++)
        c[i] = lower_bound(all(cmp), c[i]) - cmp.begin() + 1;

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;

        adj[u].push_back(v);
        adj[v].push_back(u);
        e[i] = {u, v};
    }

    dfs();

    head[1] = 1;
    dfs_hld();

    s.insert({1, 1, c[1]});

    bit.init(n + 1);

    for (int i = 1; i < n; i++) {
        auto [u, v] = e[i];

        query(v, c[v]);

        ll ans = 0;
        for (auto &[l, r, x] : cur) {
            int cnt = r - l + 1;
            ans += 1LL * bit.query(0, x - 1) * cnt;
            bit.upd(x, cnt);
        }

        for (auto &[l, r, x] : cur)
            bit.upd(x, -(r - l + 1));
        cur.clear();

        cout << ans << '\n';
    }

    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 5208 KB Output is correct
2 Correct 1 ms 5368 KB Output is correct
3 Correct 2 ms 5236 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5468 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 3 ms 5384 KB Output is correct
18 Correct 3 ms 5208 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5396 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5212 KB Output is correct
32 Correct 2 ms 5212 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 2 ms 5212 KB Output is correct
36 Correct 2 ms 5212 KB Output is correct
37 Correct 2 ms 5388 KB Output is correct
38 Correct 2 ms 5212 KB Output is correct
39 Correct 2 ms 5212 KB Output is correct
40 Correct 2 ms 5272 KB Output is correct
41 Correct 2 ms 5212 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 2 ms 5208 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5208 KB Output is correct
2 Correct 1 ms 5368 KB Output is correct
3 Correct 2 ms 5236 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5468 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 3 ms 5384 KB Output is correct
18 Correct 3 ms 5208 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5396 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5212 KB Output is correct
32 Correct 2 ms 5212 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 2 ms 5212 KB Output is correct
36 Correct 2 ms 5212 KB Output is correct
37 Correct 2 ms 5388 KB Output is correct
38 Correct 2 ms 5212 KB Output is correct
39 Correct 2 ms 5212 KB Output is correct
40 Correct 2 ms 5272 KB Output is correct
41 Correct 2 ms 5212 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 2 ms 5208 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
45 Correct 3 ms 5468 KB Output is correct
46 Correct 12 ms 5644 KB Output is correct
47 Correct 12 ms 5680 KB Output is correct
48 Correct 12 ms 5720 KB Output is correct
49 Correct 4 ms 5908 KB Output is correct
50 Correct 4 ms 5724 KB Output is correct
51 Correct 4 ms 5720 KB Output is correct
52 Correct 5 ms 5980 KB Output is correct
53 Correct 5 ms 6020 KB Output is correct
54 Correct 5 ms 5980 KB Output is correct
55 Correct 5 ms 5900 KB Output is correct
56 Correct 6 ms 5724 KB Output is correct
57 Correct 21 ms 5724 KB Output is correct
58 Correct 21 ms 5724 KB Output is correct
59 Correct 21 ms 5756 KB Output is correct
60 Correct 21 ms 5724 KB Output is correct
61 Correct 5 ms 5724 KB Output is correct
62 Correct 5 ms 5724 KB Output is correct
63 Correct 5 ms 5728 KB Output is correct
64 Correct 11 ms 5724 KB Output is correct
65 Correct 12 ms 5720 KB Output is correct
66 Correct 14 ms 5556 KB Output is correct
67 Correct 12 ms 5724 KB Output is correct
68 Correct 3 ms 5724 KB Output is correct
69 Correct 5 ms 5980 KB Output is correct
70 Correct 6 ms 6084 KB Output is correct
71 Correct 5 ms 5720 KB Output is correct
72 Correct 21 ms 5724 KB Output is correct
73 Correct 21 ms 5604 KB Output is correct
74 Correct 5 ms 5720 KB Output is correct
75 Correct 6 ms 5724 KB Output is correct
76 Correct 6 ms 5792 KB Output is correct
77 Correct 6 ms 5724 KB Output is correct
78 Correct 5 ms 5724 KB Output is correct
79 Correct 6 ms 5716 KB Output is correct
80 Correct 6 ms 5724 KB Output is correct
81 Correct 6 ms 5724 KB Output is correct
82 Correct 6 ms 5644 KB Output is correct
83 Correct 6 ms 5724 KB Output is correct
84 Correct 5 ms 5720 KB Output is correct
85 Correct 5 ms 5644 KB Output is correct
86 Correct 5 ms 5724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5208 KB Output is correct
2 Correct 1 ms 5368 KB Output is correct
3 Correct 2 ms 5236 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 1 ms 5212 KB Output is correct
11 Correct 2 ms 5212 KB Output is correct
12 Correct 2 ms 5468 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 3 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 3 ms 5384 KB Output is correct
18 Correct 3 ms 5208 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 2 ms 5212 KB Output is correct
21 Correct 2 ms 5212 KB Output is correct
22 Correct 2 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 2 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 2 ms 5396 KB Output is correct
29 Correct 2 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5212 KB Output is correct
32 Correct 2 ms 5212 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 2 ms 5212 KB Output is correct
36 Correct 2 ms 5212 KB Output is correct
37 Correct 2 ms 5388 KB Output is correct
38 Correct 2 ms 5212 KB Output is correct
39 Correct 2 ms 5212 KB Output is correct
40 Correct 2 ms 5272 KB Output is correct
41 Correct 2 ms 5212 KB Output is correct
42 Correct 2 ms 5212 KB Output is correct
43 Correct 2 ms 5208 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
45 Correct 3 ms 5468 KB Output is correct
46 Correct 12 ms 5644 KB Output is correct
47 Correct 12 ms 5680 KB Output is correct
48 Correct 12 ms 5720 KB Output is correct
49 Correct 4 ms 5908 KB Output is correct
50 Correct 4 ms 5724 KB Output is correct
51 Correct 4 ms 5720 KB Output is correct
52 Correct 5 ms 5980 KB Output is correct
53 Correct 5 ms 6020 KB Output is correct
54 Correct 5 ms 5980 KB Output is correct
55 Correct 5 ms 5900 KB Output is correct
56 Correct 6 ms 5724 KB Output is correct
57 Correct 21 ms 5724 KB Output is correct
58 Correct 21 ms 5724 KB Output is correct
59 Correct 21 ms 5756 KB Output is correct
60 Correct 21 ms 5724 KB Output is correct
61 Correct 5 ms 5724 KB Output is correct
62 Correct 5 ms 5724 KB Output is correct
63 Correct 5 ms 5728 KB Output is correct
64 Correct 11 ms 5724 KB Output is correct
65 Correct 12 ms 5720 KB Output is correct
66 Correct 14 ms 5556 KB Output is correct
67 Correct 12 ms 5724 KB Output is correct
68 Correct 3 ms 5724 KB Output is correct
69 Correct 5 ms 5980 KB Output is correct
70 Correct 6 ms 6084 KB Output is correct
71 Correct 5 ms 5720 KB Output is correct
72 Correct 21 ms 5724 KB Output is correct
73 Correct 21 ms 5604 KB Output is correct
74 Correct 5 ms 5720 KB Output is correct
75 Correct 6 ms 5724 KB Output is correct
76 Correct 6 ms 5792 KB Output is correct
77 Correct 6 ms 5724 KB Output is correct
78 Correct 5 ms 5724 KB Output is correct
79 Correct 6 ms 5716 KB Output is correct
80 Correct 6 ms 5724 KB Output is correct
81 Correct 6 ms 5724 KB Output is correct
82 Correct 6 ms 5644 KB Output is correct
83 Correct 6 ms 5724 KB Output is correct
84 Correct 5 ms 5720 KB Output is correct
85 Correct 5 ms 5644 KB Output is correct
86 Correct 5 ms 5724 KB Output is correct
87 Correct 34 ms 6236 KB Output is correct
88 Correct 117 ms 8464 KB Output is correct
89 Correct 496 ms 15880 KB Output is correct
90 Correct 488 ms 15824 KB Output is correct
91 Correct 484 ms 15648 KB Output is correct
92 Correct 73 ms 19412 KB Output is correct
93 Correct 73 ms 19568 KB Output is correct
94 Correct 69 ms 19584 KB Output is correct
95 Correct 134 ms 23120 KB Output is correct
96 Correct 138 ms 23628 KB Output is correct
97 Correct 142 ms 23584 KB Output is correct
98 Correct 139 ms 23556 KB Output is correct
99 Correct 143 ms 18892 KB Output is correct
100 Correct 860 ms 15572 KB Output is correct
101 Correct 895 ms 15828 KB Output is correct
102 Correct 890 ms 16112 KB Output is correct
103 Correct 888 ms 15956 KB Output is correct
104 Correct 134 ms 18828 KB Output is correct
105 Correct 121 ms 18820 KB Output is correct
106 Correct 124 ms 18904 KB Output is correct
107 Correct 477 ms 15120 KB Output is correct
108 Correct 485 ms 15216 KB Output is correct
109 Correct 489 ms 15312 KB Output is correct
110 Correct 61 ms 18896 KB Output is correct
111 Correct 136 ms 23064 KB Output is correct
112 Correct 130 ms 22952 KB Output is correct
113 Correct 130 ms 18204 KB Output is correct
114 Correct 858 ms 15568 KB Output is correct
115 Correct 891 ms 15852 KB Output is correct
116 Correct 112 ms 18108 KB Output is correct
117 Correct 163 ms 16532 KB Output is correct
118 Correct 164 ms 16080 KB Output is correct
119 Correct 158 ms 15608 KB Output is correct
120 Correct 153 ms 16252 KB Output is correct
121 Correct 149 ms 15564 KB Output is correct
122 Correct 152 ms 15272 KB Output is correct
123 Correct 145 ms 16592 KB Output is correct
124 Correct 141 ms 15828 KB Output is correct
125 Correct 146 ms 15564 KB Output is correct
126 Correct 133 ms 16364 KB Output is correct
127 Correct 131 ms 15560 KB Output is correct
128 Correct 140 ms 15664 KB Output is correct