Submission #1075666

# Submission time Handle Problem Language Result Execution time Memory
1075666 2024-08-26T08:27:53 Z vjudge1 Construction of Highway (JOI18_construction) C++17
100 / 100
193 ms 20176 KB
#include <bits/stdc++.h>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL

#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Construction of Highway"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 1e5 + 7

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

int n;
int a[maxn], h[maxn], par[maxn];
int nex[maxn], In[maxn], siz[maxn];
vector<int> edges[maxn];
vector<pair<int, int>> P[maxn];
pair<int, int> E[maxn];

/// DATA STRUCTURE
struct BIT {
    int bit[maxn];

    void update(int x, int val) {
        for(; x < maxn; x += (x & -x)) bit[x] += val;
    }

    int get(int x, int ans = 0) {
        for(; x > 0; x -= (x & -x)) ans += bit[x];
        return ans;
    }

} Bit;

/// PRE DFS
int dfs_siz(int u) {
    siz[u] = 1;
    for(int &v : edges[u]) {
        edges[v].erase(find(all(edges[v]), u));

        h[v] = h[u] + 1, par[v] = u;
        siz[u] += dfs_siz(v);
        if(siz[v] > siz[edges[u][0]]) swap(v, edges[u][0]);
    }
    return siz[u];
}

void dfs_hld(int u) {
    static int Time = 0;
    In[u] = ++Time;

    for(int v : edges[u]) {
        if(v == edges[u][0]) nex[v] = nex[u];
        else nex[v] = v;
        dfs_hld(v);
    }
}

/// HLD DECOMP <3
ll get(int u, int val) {

    ll ans = 0;
    vector<pair<int, int>> S, tmp;

    auto prog = [&](int u, int p) {
        int prv = h[p] - 1;
        tmp.clear();

        while(!P[p].empty() && h[P[p].back().first] < h[u]) {
            tmp.push_back({P[p].back().second, h[P[p].back().first] - prv});
            prv = h[P[p].back().first];
            P[p].pop_back();
        }

        if(!P[p].empty()) tmp.push_back({P[p].back().second, h[u] - prv});
        P[p].push_back({u, val});

        reverse(all(tmp));
        for(auto x : tmp) S.push_back(x);
    };

    for(; u != 0; u = par[nex[u]]) {
        prog(u, nex[u]);
    }


    for(auto [cl, w] : S) ans += 1ll * w * Bit.get(cl - 1), Bit.update(cl, w);
    for(auto [cl, w] : S) Bit.update(cl, -w);
    return ans;
}

void solve()
{
    vector<int> Comp;

    cin >> n;
    fi(i, 1, n) cin >> a[i], Comp.push_back(a[i]);
    fi(i, 1, n - 1) {
        int x, y; cin >> x >> y;

        E[i] = {x, y};
        edges[x].push_back(y), edges[y].push_back(x);
    }

    resp(Comp);
    fi(i, 1, n) a[i] = upper_bound(all(Comp), a[i]) - Comp.begin();

    nex[1] = h[1] = 1;
    dfs_siz(1), dfs_hld(1);
    get(1, a[1]);


    fi(i, 1, n - 1) {
        int x, y; tie(x, y) = E[i];
        cout << get(y, a[y]) << '\n';
    }

}


int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen(Neco".inp", "r")) {
        freopen(Neco".inp", "r", stdin);
        freopen(Neco".out", "w", stdout);
    }


    int nTest = 1;
//    cin >> nTest;


    while(nTest--)
    {
        solve();
    }


    return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  139 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5008 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 3 ms 5116 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 3 ms 5212 KB Output is correct
11 Correct 4 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5208 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 3 ms 5268 KB Output is correct
25 Correct 3 ms 5088 KB Output is correct
26 Correct 2 ms 5256 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5172 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 3 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 3 ms 5056 KB Output is correct
38 Correct 2 ms 5208 KB Output is correct
39 Correct 2 ms 5212 KB Output is correct
40 Correct 2 ms 5212 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 3 ms 5196 KB Output is correct
43 Correct 3 ms 5212 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5008 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 3 ms 5116 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 3 ms 5212 KB Output is correct
11 Correct 4 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5208 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 3 ms 5268 KB Output is correct
25 Correct 3 ms 5088 KB Output is correct
26 Correct 2 ms 5256 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5172 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 3 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 3 ms 5056 KB Output is correct
38 Correct 2 ms 5208 KB Output is correct
39 Correct 2 ms 5212 KB Output is correct
40 Correct 2 ms 5212 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 3 ms 5196 KB Output is correct
43 Correct 3 ms 5212 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
45 Correct 3 ms 5212 KB Output is correct
46 Correct 7 ms 5468 KB Output is correct
47 Correct 6 ms 5560 KB Output is correct
48 Correct 7 ms 5560 KB Output is correct
49 Correct 5 ms 5724 KB Output is correct
50 Correct 5 ms 5724 KB Output is correct
51 Correct 5 ms 5724 KB Output is correct
52 Correct 5 ms 5720 KB Output is correct
53 Correct 5 ms 5724 KB Output is correct
54 Correct 5 ms 5636 KB Output is correct
55 Correct 5 ms 5724 KB Output is correct
56 Correct 5 ms 5568 KB Output is correct
57 Correct 7 ms 5556 KB Output is correct
58 Correct 7 ms 5468 KB Output is correct
59 Correct 6 ms 5468 KB Output is correct
60 Correct 7 ms 5468 KB Output is correct
61 Correct 5 ms 5724 KB Output is correct
62 Correct 6 ms 5724 KB Output is correct
63 Correct 5 ms 5720 KB Output is correct
64 Correct 6 ms 5464 KB Output is correct
65 Correct 6 ms 5468 KB Output is correct
66 Correct 7 ms 5464 KB Output is correct
67 Correct 7 ms 5468 KB Output is correct
68 Correct 4 ms 5724 KB Output is correct
69 Correct 5 ms 5548 KB Output is correct
70 Correct 5 ms 5720 KB Output is correct
71 Correct 5 ms 5468 KB Output is correct
72 Correct 7 ms 5332 KB Output is correct
73 Correct 7 ms 5468 KB Output is correct
74 Correct 4 ms 5548 KB Output is correct
75 Correct 4 ms 5468 KB Output is correct
76 Correct 5 ms 5468 KB Output is correct
77 Correct 4 ms 5468 KB Output is correct
78 Correct 4 ms 5468 KB Output is correct
79 Correct 5 ms 5584 KB Output is correct
80 Correct 4 ms 5464 KB Output is correct
81 Correct 4 ms 5560 KB Output is correct
82 Correct 4 ms 5468 KB Output is correct
83 Correct 5 ms 5468 KB Output is correct
84 Correct 6 ms 5420 KB Output is correct
85 Correct 5 ms 5468 KB Output is correct
86 Correct 4 ms 5468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 5008 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 3 ms 5116 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 3 ms 5212 KB Output is correct
11 Correct 4 ms 5212 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 2 ms 5212 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5208 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 3 ms 5268 KB Output is correct
25 Correct 3 ms 5088 KB Output is correct
26 Correct 2 ms 5256 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5172 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 2 ms 5212 KB Output is correct
34 Correct 3 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 3 ms 5056 KB Output is correct
38 Correct 2 ms 5208 KB Output is correct
39 Correct 2 ms 5212 KB Output is correct
40 Correct 2 ms 5212 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 3 ms 5196 KB Output is correct
43 Correct 3 ms 5212 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
45 Correct 3 ms 5212 KB Output is correct
46 Correct 7 ms 5468 KB Output is correct
47 Correct 6 ms 5560 KB Output is correct
48 Correct 7 ms 5560 KB Output is correct
49 Correct 5 ms 5724 KB Output is correct
50 Correct 5 ms 5724 KB Output is correct
51 Correct 5 ms 5724 KB Output is correct
52 Correct 5 ms 5720 KB Output is correct
53 Correct 5 ms 5724 KB Output is correct
54 Correct 5 ms 5636 KB Output is correct
55 Correct 5 ms 5724 KB Output is correct
56 Correct 5 ms 5568 KB Output is correct
57 Correct 7 ms 5556 KB Output is correct
58 Correct 7 ms 5468 KB Output is correct
59 Correct 6 ms 5468 KB Output is correct
60 Correct 7 ms 5468 KB Output is correct
61 Correct 5 ms 5724 KB Output is correct
62 Correct 6 ms 5724 KB Output is correct
63 Correct 5 ms 5720 KB Output is correct
64 Correct 6 ms 5464 KB Output is correct
65 Correct 6 ms 5468 KB Output is correct
66 Correct 7 ms 5464 KB Output is correct
67 Correct 7 ms 5468 KB Output is correct
68 Correct 4 ms 5724 KB Output is correct
69 Correct 5 ms 5548 KB Output is correct
70 Correct 5 ms 5720 KB Output is correct
71 Correct 5 ms 5468 KB Output is correct
72 Correct 7 ms 5332 KB Output is correct
73 Correct 7 ms 5468 KB Output is correct
74 Correct 4 ms 5548 KB Output is correct
75 Correct 4 ms 5468 KB Output is correct
76 Correct 5 ms 5468 KB Output is correct
77 Correct 4 ms 5468 KB Output is correct
78 Correct 4 ms 5468 KB Output is correct
79 Correct 5 ms 5584 KB Output is correct
80 Correct 4 ms 5464 KB Output is correct
81 Correct 4 ms 5560 KB Output is correct
82 Correct 4 ms 5468 KB Output is correct
83 Correct 5 ms 5468 KB Output is correct
84 Correct 6 ms 5420 KB Output is correct
85 Correct 5 ms 5468 KB Output is correct
86 Correct 4 ms 5468 KB Output is correct
87 Correct 12 ms 5980 KB Output is correct
88 Correct 37 ms 7764 KB Output is correct
89 Correct 148 ms 14284 KB Output is correct
90 Correct 155 ms 14232 KB Output is correct
91 Correct 159 ms 14320 KB Output is correct
92 Correct 66 ms 20128 KB Output is correct
93 Correct 66 ms 20176 KB Output is correct
94 Correct 68 ms 20100 KB Output is correct
95 Correct 68 ms 19284 KB Output is correct
96 Correct 73 ms 19376 KB Output is correct
97 Correct 72 ms 19388 KB Output is correct
98 Correct 75 ms 19288 KB Output is correct
99 Correct 75 ms 17872 KB Output is correct
100 Correct 188 ms 13740 KB Output is correct
101 Correct 193 ms 13704 KB Output is correct
102 Correct 193 ms 13700 KB Output is correct
103 Correct 190 ms 13776 KB Output is correct
104 Correct 75 ms 17796 KB Output is correct
105 Correct 78 ms 17956 KB Output is correct
106 Correct 81 ms 17760 KB Output is correct
107 Correct 129 ms 13860 KB Output is correct
108 Correct 140 ms 14036 KB Output is correct
109 Correct 142 ms 14036 KB Output is correct
110 Correct 62 ms 19668 KB Output is correct
111 Correct 78 ms 19288 KB Output is correct
112 Correct 64 ms 19032 KB Output is correct
113 Correct 72 ms 17548 KB Output is correct
114 Correct 161 ms 13692 KB Output is correct
115 Correct 173 ms 13268 KB Output is correct
116 Correct 65 ms 17620 KB Output is correct
117 Correct 77 ms 16192 KB Output is correct
118 Correct 76 ms 15440 KB Output is correct
119 Correct 80 ms 15288 KB Output is correct
120 Correct 66 ms 15684 KB Output is correct
121 Correct 71 ms 15216 KB Output is correct
122 Correct 64 ms 14864 KB Output is correct
123 Correct 94 ms 16048 KB Output is correct
124 Correct 89 ms 15428 KB Output is correct
125 Correct 82 ms 15160 KB Output is correct
126 Correct 73 ms 15684 KB Output is correct
127 Correct 73 ms 15184 KB Output is correct
128 Correct 81 ms 14860 KB Output is correct