Submission #890265

# Submission time Handle Problem Language Result Execution time Memory
890265 2023-12-20T21:32:37 Z boris_mihov Construction of Highway (JOI18_construction) C++17
100 / 100
1020 ms 23932 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>

typedef long long llong;
const int MAXN = 100000 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;

int n;
struct SegmentTree
{
    struct Node
    {
        int min;
        int max;
        int lazy;

        Node()
        {
            lazy = 0;
            min = INTINF;
            max = -INTINF;
        }

        Node(int _min, int _max)
        {
            lazy = 0;
            min = _min;
            max = _max;
        }

        friend Node operator + (const Node &left, const Node &right)
        {
            Node result;
            result.min = std::min(left.min, right.min);
            result.max = std::max(left.max, right.max);
            return result;
        }
    };

    Node tree[4*MAXN];
    void push(int node, int l, int r)
    {
        if (tree[node].lazy == 0)
        {
            return;
        }

        tree[node].min = tree[node].max = tree[node].lazy;
        if (l < r)
        {
            tree[2*node].lazy = tree[node].lazy;
            tree[2*node + 1].lazy = tree[node].lazy;
        }

        tree[node].lazy = 0;
    }

    void build(int l, int r, int node, int c[], const std::vector <int> &tour)
    {
        if (l == r)
        {
            tree[node].min = tree[node].max = c[tour[l]];
            return;
        }

        int mid = (l + r) / 2;
        build(l, mid, 2*node, c, tour);
        build(mid + 1, r, 2*node + 1, c, tour);
        tree[node] = tree[2*node] + tree[2*node + 1];
    }

    void update(int l, int r, int node, int queryL, int queryR, int queryVal)
    {
        push(node, l, r);
        if (queryR < l || r < queryL)
        {
            return;
        }

        if (queryL <= l && r <= queryR)
        {
            tree[node].lazy = queryVal;
            push(node, l, r);
            return;
        }

        int mid = (l + r) / 2;
        update(l, mid, 2*node, queryL, queryR, queryVal);
        update(mid + 1, r, 2*node + 1, queryL, queryR, queryVal);
        tree[node] = tree[2*node] + tree[2*node + 1];
    }

    Node query(int l, int r, int node, int queryL, int queryR)
    {
        push(node, l, r); 
        if (queryL <= l && r <= queryR)
        {
            return tree[node];
        }

        Node res;
        int mid = (l + r) / 2;
        if (queryL <= mid) res = res + query(l, mid, 2*node, queryL, queryR);
        if (mid + 1 <= queryR) res = res + query(mid + 1, r, 2*node + 1, queryL, queryR);
        return res;
    }

    int search(int l, int r, int node, int queryL, int queryR, int queryVal)
    {
        push(node, l, r);
        if (queryR < l || r < queryL)
        {
            return -1;
        }

        if (queryL <= l && r <= queryR && tree[node].min == tree[node].max && tree[node].min == queryVal)
        {
            return -1;
        }

        if (l == r)
        {
            return l;
        }

        int mid = (l + r) / 2;
        int res = search(mid + 1, r, 2*node + 1, queryL, queryR, queryVal);
        if (res != -1) return res;
        return search(l, mid, 2*node, queryL, queryR, queryVal);
    }

    void build(int c[], const std::vector <int> &tour)
    {
        build(0, n - 1, 1, c, tour);
    }

    void update(int l, int r, int value)
    {
        update(0, n - 1, 1, l, r, value);
    }

    int getValue(int idx)
    {
        Node res = query(0, n - 1, 1, idx, idx);
        return res.min;
    }

    bool checkIfDifferent(int l, int r)
    {
        Node res = query(0, n - 1, 1, l, r);
        return res.min != res.max;
    }
    
    int search(int l, int r, int value)
    {
        return search(0, n - 1, 1, l, r, value);
    }
};

struct Fenwick
{
    int tree[MAXN];
    std::stack <std::pair <int,int>> updateTracker;
    void update(int idx, int value, bool shouldAdd = true)
    {
        if (shouldAdd) updateTracker.push({idx, value});
        for (int pos = idx ; pos <= n ; pos += pos & (-pos))
        {
            tree[pos] += value;
        }
    }

    int query(int idx)
    {
        int res = 0;
        for (int pos = idx ; pos > 0 ; pos -= pos & (-pos))
        {
            res += tree[pos];
        }

        return res;
    }

    void reset()
    {
        while (updateTracker.size())
        {
            update(updateTracker.top().first, -updateTracker.top().second, false);
            updateTracker.pop();
        }
    }
};

int a[MAXN];
int b[MAXN];
int c[MAXN];
int sz[MAXN];
int in[MAXN];
int head[MAXN];
int heavy[MAXN];
int parent[MAXN];
std::vector <int> tour;
std::vector <int> g[MAXN];
SegmentTree tree;
Fenwick fenwick;

void dfs(int node, int par)
{
    sz[node] = 1;
    parent[node] = par;
    for (const int &u : g[node])
    {
        if (u == par)
        {
            continue;
        }

        dfs(u, node);
        sz[node] += sz[u];
        if (sz[u] > sz[heavy[node]])
        {
            heavy[node] = u;
        }
    }
}

void decompose(int node, int h)
{
    head[node] = h;
    in[node] = tour.size();
    tour.push_back(node);

    if (heavy[node] != 0)
    {
        decompose(heavy[node], h);
    }

    for (const int &u : g[node])
    {
        if (u == parent[node] || u == heavy[node])
        {
            continue;
        }

        decompose(u, u);
    }
}

llong calcCost(int node, int added)
{
    llong ans = 0;
    fenwick.reset();
    while (node != 0)
    {
        int jumpTo = 0;
        int currHead = head[node];
        int myValue = tree.getValue(in[node]);
        if (tree.checkIfDifferent(in[currHead], in[node]))
        {
            jumpTo = tour[tree.search(in[currHead], in[node], myValue) + 1];
        } else
        {
            jumpTo = currHead;
        }
        
        int count = in[node] - in[jumpTo] + 1;
        ans += 1LL * count * fenwick.query(myValue - 1);
        fenwick.update(myValue, count);
        tree.update(in[jumpTo], in[node], c[added]);
        node = parent[jumpTo];
    }   

    return ans;
}

std::pair <int,int> sorted[MAXN];
void solve()
{
    for (int i = 1 ; i <= n ; ++i)
    {
        sorted[i] = {c[i], i};
    }

    std::sort(sorted + 1, sorted + 1 + n);
    int cnt = 0;
    for (int i = 1 ; i <= n ; ++i)
    {
        cnt += (sorted[i].first != sorted[i - 1].first);
        c[sorted[i].second] = cnt;
    }

    dfs(1, 0);
    decompose(1, 1);
    tree.build(c, tour);

    for (int i = 1 ; i < n ; ++i)
    {
        std::cout << calcCost(a[i], b[i]) << '\n';
    }
}

void input()
{
    std::cin >> n;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> c[i];
    }

    for (int i = 1 ; i < n ; ++i)
    {
        std::cin >> a[i] >> b[i];
        g[a[i]].push_back(b[i]);
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}   

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10584 KB Output is correct
4 Correct 4 ms 10680 KB Output is correct
5 Correct 3 ms 10588 KB Output is correct
6 Correct 3 ms 10692 KB Output is correct
7 Correct 3 ms 10588 KB Output is correct
8 Correct 3 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 3 ms 10728 KB Output is correct
13 Correct 3 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 4 ms 10588 KB Output is correct
16 Correct 4 ms 10588 KB Output is correct
17 Correct 3 ms 10588 KB Output is correct
18 Correct 4 ms 10588 KB Output is correct
19 Correct 2 ms 10728 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 3 ms 10588 KB Output is correct
25 Correct 3 ms 10584 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 3 ms 10684 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10588 KB Output is correct
30 Correct 3 ms 10696 KB Output is correct
31 Correct 3 ms 10584 KB Output is correct
32 Correct 3 ms 10588 KB Output is correct
33 Correct 2 ms 10588 KB Output is correct
34 Correct 3 ms 10588 KB Output is correct
35 Correct 3 ms 10584 KB Output is correct
36 Correct 3 ms 10760 KB Output is correct
37 Correct 3 ms 10588 KB Output is correct
38 Correct 3 ms 10588 KB Output is correct
39 Correct 3 ms 10588 KB Output is correct
40 Correct 2 ms 10588 KB Output is correct
41 Correct 3 ms 10728 KB Output is correct
42 Correct 3 ms 10588 KB Output is correct
43 Correct 2 ms 10728 KB Output is correct
44 Correct 3 ms 10732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10584 KB Output is correct
4 Correct 4 ms 10680 KB Output is correct
5 Correct 3 ms 10588 KB Output is correct
6 Correct 3 ms 10692 KB Output is correct
7 Correct 3 ms 10588 KB Output is correct
8 Correct 3 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 3 ms 10728 KB Output is correct
13 Correct 3 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 4 ms 10588 KB Output is correct
16 Correct 4 ms 10588 KB Output is correct
17 Correct 3 ms 10588 KB Output is correct
18 Correct 4 ms 10588 KB Output is correct
19 Correct 2 ms 10728 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 3 ms 10588 KB Output is correct
25 Correct 3 ms 10584 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 3 ms 10684 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10588 KB Output is correct
30 Correct 3 ms 10696 KB Output is correct
31 Correct 3 ms 10584 KB Output is correct
32 Correct 3 ms 10588 KB Output is correct
33 Correct 2 ms 10588 KB Output is correct
34 Correct 3 ms 10588 KB Output is correct
35 Correct 3 ms 10584 KB Output is correct
36 Correct 3 ms 10760 KB Output is correct
37 Correct 3 ms 10588 KB Output is correct
38 Correct 3 ms 10588 KB Output is correct
39 Correct 3 ms 10588 KB Output is correct
40 Correct 2 ms 10588 KB Output is correct
41 Correct 3 ms 10728 KB Output is correct
42 Correct 3 ms 10588 KB Output is correct
43 Correct 2 ms 10728 KB Output is correct
44 Correct 3 ms 10732 KB Output is correct
45 Correct 4 ms 10588 KB Output is correct
46 Correct 11 ms 10980 KB Output is correct
47 Correct 12 ms 10844 KB Output is correct
48 Correct 11 ms 10840 KB Output is correct
49 Correct 6 ms 11100 KB Output is correct
50 Correct 5 ms 11100 KB Output is correct
51 Correct 5 ms 11096 KB Output is correct
52 Correct 6 ms 10844 KB Output is correct
53 Correct 6 ms 10844 KB Output is correct
54 Correct 6 ms 10844 KB Output is correct
55 Correct 6 ms 10844 KB Output is correct
56 Correct 6 ms 11100 KB Output is correct
57 Correct 21 ms 10952 KB Output is correct
58 Correct 20 ms 10840 KB Output is correct
59 Correct 21 ms 10840 KB Output is correct
60 Correct 23 ms 10844 KB Output is correct
61 Correct 7 ms 11096 KB Output is correct
62 Correct 7 ms 10988 KB Output is correct
63 Correct 7 ms 11100 KB Output is correct
64 Correct 9 ms 10840 KB Output is correct
65 Correct 11 ms 10844 KB Output is correct
66 Correct 12 ms 11096 KB Output is correct
67 Correct 12 ms 10956 KB Output is correct
68 Correct 5 ms 11244 KB Output is correct
69 Correct 6 ms 10844 KB Output is correct
70 Correct 6 ms 10844 KB Output is correct
71 Correct 6 ms 10844 KB Output is correct
72 Correct 19 ms 10952 KB Output is correct
73 Correct 21 ms 10844 KB Output is correct
74 Correct 7 ms 10844 KB Output is correct
75 Correct 7 ms 10844 KB Output is correct
76 Correct 7 ms 10844 KB Output is correct
77 Correct 7 ms 10840 KB Output is correct
78 Correct 7 ms 10840 KB Output is correct
79 Correct 7 ms 10844 KB Output is correct
80 Correct 8 ms 10844 KB Output is correct
81 Correct 8 ms 10840 KB Output is correct
82 Correct 8 ms 10844 KB Output is correct
83 Correct 8 ms 10844 KB Output is correct
84 Correct 8 ms 10844 KB Output is correct
85 Correct 8 ms 10844 KB Output is correct
86 Correct 8 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10588 KB Output is correct
2 Correct 2 ms 10584 KB Output is correct
3 Correct 2 ms 10584 KB Output is correct
4 Correct 4 ms 10680 KB Output is correct
5 Correct 3 ms 10588 KB Output is correct
6 Correct 3 ms 10692 KB Output is correct
7 Correct 3 ms 10588 KB Output is correct
8 Correct 3 ms 10588 KB Output is correct
9 Correct 2 ms 10588 KB Output is correct
10 Correct 2 ms 10588 KB Output is correct
11 Correct 2 ms 10588 KB Output is correct
12 Correct 3 ms 10728 KB Output is correct
13 Correct 3 ms 10588 KB Output is correct
14 Correct 2 ms 10588 KB Output is correct
15 Correct 4 ms 10588 KB Output is correct
16 Correct 4 ms 10588 KB Output is correct
17 Correct 3 ms 10588 KB Output is correct
18 Correct 4 ms 10588 KB Output is correct
19 Correct 2 ms 10728 KB Output is correct
20 Correct 3 ms 10588 KB Output is correct
21 Correct 2 ms 10588 KB Output is correct
22 Correct 2 ms 10588 KB Output is correct
23 Correct 2 ms 10588 KB Output is correct
24 Correct 3 ms 10588 KB Output is correct
25 Correct 3 ms 10584 KB Output is correct
26 Correct 2 ms 10588 KB Output is correct
27 Correct 3 ms 10684 KB Output is correct
28 Correct 2 ms 10588 KB Output is correct
29 Correct 2 ms 10588 KB Output is correct
30 Correct 3 ms 10696 KB Output is correct
31 Correct 3 ms 10584 KB Output is correct
32 Correct 3 ms 10588 KB Output is correct
33 Correct 2 ms 10588 KB Output is correct
34 Correct 3 ms 10588 KB Output is correct
35 Correct 3 ms 10584 KB Output is correct
36 Correct 3 ms 10760 KB Output is correct
37 Correct 3 ms 10588 KB Output is correct
38 Correct 3 ms 10588 KB Output is correct
39 Correct 3 ms 10588 KB Output is correct
40 Correct 2 ms 10588 KB Output is correct
41 Correct 3 ms 10728 KB Output is correct
42 Correct 3 ms 10588 KB Output is correct
43 Correct 2 ms 10728 KB Output is correct
44 Correct 3 ms 10732 KB Output is correct
45 Correct 4 ms 10588 KB Output is correct
46 Correct 11 ms 10980 KB Output is correct
47 Correct 12 ms 10844 KB Output is correct
48 Correct 11 ms 10840 KB Output is correct
49 Correct 6 ms 11100 KB Output is correct
50 Correct 5 ms 11100 KB Output is correct
51 Correct 5 ms 11096 KB Output is correct
52 Correct 6 ms 10844 KB Output is correct
53 Correct 6 ms 10844 KB Output is correct
54 Correct 6 ms 10844 KB Output is correct
55 Correct 6 ms 10844 KB Output is correct
56 Correct 6 ms 11100 KB Output is correct
57 Correct 21 ms 10952 KB Output is correct
58 Correct 20 ms 10840 KB Output is correct
59 Correct 21 ms 10840 KB Output is correct
60 Correct 23 ms 10844 KB Output is correct
61 Correct 7 ms 11096 KB Output is correct
62 Correct 7 ms 10988 KB Output is correct
63 Correct 7 ms 11100 KB Output is correct
64 Correct 9 ms 10840 KB Output is correct
65 Correct 11 ms 10844 KB Output is correct
66 Correct 12 ms 11096 KB Output is correct
67 Correct 12 ms 10956 KB Output is correct
68 Correct 5 ms 11244 KB Output is correct
69 Correct 6 ms 10844 KB Output is correct
70 Correct 6 ms 10844 KB Output is correct
71 Correct 6 ms 10844 KB Output is correct
72 Correct 19 ms 10952 KB Output is correct
73 Correct 21 ms 10844 KB Output is correct
74 Correct 7 ms 10844 KB Output is correct
75 Correct 7 ms 10844 KB Output is correct
76 Correct 7 ms 10844 KB Output is correct
77 Correct 7 ms 10840 KB Output is correct
78 Correct 7 ms 10840 KB Output is correct
79 Correct 7 ms 10844 KB Output is correct
80 Correct 8 ms 10844 KB Output is correct
81 Correct 8 ms 10840 KB Output is correct
82 Correct 8 ms 10844 KB Output is correct
83 Correct 8 ms 10844 KB Output is correct
84 Correct 8 ms 10844 KB Output is correct
85 Correct 8 ms 10844 KB Output is correct
86 Correct 8 ms 10844 KB Output is correct
87 Correct 32 ms 11348 KB Output is correct
88 Correct 109 ms 12528 KB Output is correct
89 Correct 503 ms 16320 KB Output is correct
90 Correct 510 ms 16596 KB Output is correct
91 Correct 484 ms 16116 KB Output is correct
92 Correct 102 ms 23932 KB Output is correct
93 Correct 102 ms 23884 KB Output is correct
94 Correct 103 ms 23892 KB Output is correct
95 Correct 151 ms 19148 KB Output is correct
96 Correct 126 ms 19404 KB Output is correct
97 Correct 140 ms 19404 KB Output is correct
98 Correct 124 ms 19404 KB Output is correct
99 Correct 161 ms 19284 KB Output is correct
100 Correct 849 ms 16880 KB Output is correct
101 Correct 963 ms 16912 KB Output is correct
102 Correct 958 ms 16840 KB Output is correct
103 Correct 1020 ms 16852 KB Output is correct
104 Correct 170 ms 19236 KB Output is correct
105 Correct 172 ms 19408 KB Output is correct
106 Correct 164 ms 19296 KB Output is correct
107 Correct 389 ms 15708 KB Output is correct
108 Correct 502 ms 15624 KB Output is correct
109 Correct 486 ms 15916 KB Output is correct
110 Correct 105 ms 23244 KB Output is correct
111 Correct 120 ms 19048 KB Output is correct
112 Correct 123 ms 18896 KB Output is correct
113 Correct 127 ms 18896 KB Output is correct
114 Correct 853 ms 16584 KB Output is correct
115 Correct 964 ms 16428 KB Output is correct
116 Correct 166 ms 18644 KB Output is correct
117 Correct 156 ms 17364 KB Output is correct
118 Correct 187 ms 16976 KB Output is correct
119 Correct 186 ms 16688 KB Output is correct
120 Correct 159 ms 17104 KB Output is correct
121 Correct 169 ms 16524 KB Output is correct
122 Correct 172 ms 16472 KB Output is correct
123 Correct 217 ms 17736 KB Output is correct
124 Correct 222 ms 17104 KB Output is correct
125 Correct 234 ms 16840 KB Output is correct
126 Correct 210 ms 17104 KB Output is correct
127 Correct 208 ms 16588 KB Output is correct
128 Correct 230 ms 16732 KB Output is correct