Submission #128062

# Submission time Handle Problem Language Result Execution time Memory
128062 2019-07-10T11:34:22 Z BThero Construction of Highway (JOI18_construction) C++17
100 / 100
507 ms 86240 KB
// Why am I so dumb? :c
// chrono::system_clock::now().time_since_epoch().count()

//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define mp make_pair

#define all(x) (x).begin(), (x).end()

#define fi first
#define se second

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;   
typedef pair<int, int> pii;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int MAXN = (int)1e5 + 5;

vector<int> adj[MAXN];

deque<pii> T[MAXN];

int group[MAXN];

int fenw[MAXN];

int boss[MAXN];

vector<pii> V;

int arr[MAXN];

int sub[MAXN];

int par[MAXN];

int h[MAXN];

pii e[MAXN];

int n, m; 

void dfs(int v, int pr = -1) {
    sub[v] = 1;

    for (int to : adj[v]) {
        if (to != pr) {
            h[to] = h[v] + 1;
            par[to] = v;
            dfs(to, v);
            sub[v] += sub[to];
        }
    }
}

void dec(int v, int pr = -1) {
    if (!boss[m]) {
        boss[m] = v;
    }

    group[v] = m;
    T[m].pb(mp(arr[v], 1));

    int mx = -1;

    for (int to : adj[v]) {
        if (to != pr) {
            if (mx == -1 || sub[to] > sub[mx]) {
                mx = to;
            }
        }
    }

    if (mx != -1) {
        dec(mx, v);
    }

    for (int to : adj[v]) {
        if (to != pr && to != mx) {
            ++m;
            dec(to, v);
        }
    }
}

void gather(int col, int cnt, int x) {
    int _cnt = cnt;
    vector<pii> V2;

    while (!T[col].empty()) {
        if (T[col].front().se > cnt) {
            V2.pb(mp(T[col].front().fi, cnt));
            T[col][0].se -= cnt;
            break;
        }
        else {
            V2.pb(T[col].front());
            cnt -= T[col].front().se;
            T[col].pop_front();
        }
    }

    while (!V2.empty()) {
        V.pb(V2.back());
        V2.pop_back();
    }

    T[col].push_front(mp(x, _cnt));
}

void fenwUpd(int p, int x) {
    for (; p <= n; p |= (p + 1)) {
        fenw[p] += x;
    }
}

int prefSum(int p) {
    int ret = 0;

    for (; p > 0; --p) {
        ret += fenw[p];
        p &= (p + 1);
    }

    return ret;
}

int query(int u, int x) {
    V.clear();

    while (u != 0) {
        int my = group[u];
        int cnt = h[u] - h[boss[my]] + 1;
        gather(my, cnt, x);
        u = par[boss[my]];
    }

    ll ans = 0;

    for (int i = 0; i < V.size(); ++i) {
        ans += prefSum(V[i].fi - 1) * 1ll * V[i].se;
        fenwUpd(V[i].fi, V[i].se);
    }

    for (int i = 0; i < V.size(); ++i) {
        fenwUpd(V[i].fi, -V[i].se);
    }

    return ans;
}

void compress() {
    vector<int> vv;

    for (int i = 1; i <= n; ++i) {
        vv.pb(arr[i]);
    }

    sort(all(vv));
    vv.resize(unique(all(vv)) - vv.begin());

    for (int i = 1; i <= n; ++i) {
        arr[i] = upper_bound(all(vv), arr[i]) - vv.begin();
    }
}

void solve() {
    scanf("%d", &n);

    for (int i = 1; i <= n; ++i) {
        scanf("%d", &arr[i]);
    }

    compress();

    for (int i = 1; i < n; ++i) {
        int u, v;
        scanf("%d %d", &u, &v);
        e[i] = mp(u, v);
        adj[u].pb(v);
        adj[v].pb(u);
    }

    dfs(1);
    dec(1);

    for (int i = 1; i < n; ++i) {
        int u = e[i].fi, v = e[i].se;
        printf("%d\n", query(u, arr[v]));
    }
}

int main() {
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int tt = 1;

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

    return 0;
}

Compilation message

construction.cpp: In function 'int query(int, int)':
construction.cpp:149:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < V.size(); ++i) {
                     ~~^~~~~~~~~~
construction.cpp:154:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < V.size(); ++i) {
                     ~~^~~~~~~~~~
construction.cpp: In function 'void solve()':
construction.cpp:177:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
construction.cpp:180:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &arr[i]);
         ~~~~~^~~~~~~~~~~~~~~
construction.cpp:187:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &u, &v);
         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 69 ms 70008 KB Output is correct
2 Correct 69 ms 70008 KB Output is correct
3 Correct 69 ms 70008 KB Output is correct
4 Correct 70 ms 70008 KB Output is correct
5 Correct 70 ms 70060 KB Output is correct
6 Correct 71 ms 70136 KB Output is correct
7 Correct 74 ms 70152 KB Output is correct
8 Correct 75 ms 70268 KB Output is correct
9 Correct 70 ms 70140 KB Output is correct
10 Correct 70 ms 70008 KB Output is correct
11 Correct 71 ms 70008 KB Output is correct
12 Correct 70 ms 70008 KB Output is correct
13 Correct 71 ms 70160 KB Output is correct
14 Correct 71 ms 70136 KB Output is correct
15 Correct 71 ms 70048 KB Output is correct
16 Correct 71 ms 70008 KB Output is correct
17 Correct 71 ms 70096 KB Output is correct
18 Correct 71 ms 70008 KB Output is correct
19 Correct 71 ms 70032 KB Output is correct
20 Correct 72 ms 70136 KB Output is correct
21 Correct 70 ms 70136 KB Output is correct
22 Correct 72 ms 70136 KB Output is correct
23 Correct 70 ms 70000 KB Output is correct
24 Correct 71 ms 70136 KB Output is correct
25 Correct 78 ms 70060 KB Output is correct
26 Correct 87 ms 70008 KB Output is correct
27 Correct 76 ms 70136 KB Output is correct
28 Correct 70 ms 70008 KB Output is correct
29 Correct 70 ms 70140 KB Output is correct
30 Correct 71 ms 70140 KB Output is correct
31 Correct 72 ms 70008 KB Output is correct
32 Correct 70 ms 70136 KB Output is correct
33 Correct 71 ms 70048 KB Output is correct
34 Correct 70 ms 70136 KB Output is correct
35 Correct 71 ms 70136 KB Output is correct
36 Correct 70 ms 70136 KB Output is correct
37 Correct 70 ms 70056 KB Output is correct
38 Correct 69 ms 70020 KB Output is correct
39 Correct 69 ms 70008 KB Output is correct
40 Correct 70 ms 70008 KB Output is correct
41 Correct 73 ms 70016 KB Output is correct
42 Correct 89 ms 70108 KB Output is correct
43 Correct 72 ms 69988 KB Output is correct
44 Correct 69 ms 70008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 70008 KB Output is correct
2 Correct 69 ms 70008 KB Output is correct
3 Correct 69 ms 70008 KB Output is correct
4 Correct 70 ms 70008 KB Output is correct
5 Correct 70 ms 70060 KB Output is correct
6 Correct 71 ms 70136 KB Output is correct
7 Correct 74 ms 70152 KB Output is correct
8 Correct 75 ms 70268 KB Output is correct
9 Correct 70 ms 70140 KB Output is correct
10 Correct 70 ms 70008 KB Output is correct
11 Correct 71 ms 70008 KB Output is correct
12 Correct 70 ms 70008 KB Output is correct
13 Correct 71 ms 70160 KB Output is correct
14 Correct 71 ms 70136 KB Output is correct
15 Correct 71 ms 70048 KB Output is correct
16 Correct 71 ms 70008 KB Output is correct
17 Correct 71 ms 70096 KB Output is correct
18 Correct 71 ms 70008 KB Output is correct
19 Correct 71 ms 70032 KB Output is correct
20 Correct 72 ms 70136 KB Output is correct
21 Correct 70 ms 70136 KB Output is correct
22 Correct 72 ms 70136 KB Output is correct
23 Correct 70 ms 70000 KB Output is correct
24 Correct 71 ms 70136 KB Output is correct
25 Correct 78 ms 70060 KB Output is correct
26 Correct 87 ms 70008 KB Output is correct
27 Correct 76 ms 70136 KB Output is correct
28 Correct 70 ms 70008 KB Output is correct
29 Correct 70 ms 70140 KB Output is correct
30 Correct 71 ms 70140 KB Output is correct
31 Correct 72 ms 70008 KB Output is correct
32 Correct 70 ms 70136 KB Output is correct
33 Correct 71 ms 70048 KB Output is correct
34 Correct 70 ms 70136 KB Output is correct
35 Correct 71 ms 70136 KB Output is correct
36 Correct 70 ms 70136 KB Output is correct
37 Correct 70 ms 70056 KB Output is correct
38 Correct 69 ms 70020 KB Output is correct
39 Correct 69 ms 70008 KB Output is correct
40 Correct 70 ms 70008 KB Output is correct
41 Correct 73 ms 70016 KB Output is correct
42 Correct 89 ms 70108 KB Output is correct
43 Correct 72 ms 69988 KB Output is correct
44 Correct 69 ms 70008 KB Output is correct
45 Correct 71 ms 70132 KB Output is correct
46 Correct 77 ms 70236 KB Output is correct
47 Correct 77 ms 70264 KB Output is correct
48 Correct 97 ms 70264 KB Output is correct
49 Correct 76 ms 70776 KB Output is correct
50 Correct 89 ms 70732 KB Output is correct
51 Correct 86 ms 70648 KB Output is correct
52 Correct 74 ms 70520 KB Output is correct
53 Correct 78 ms 70648 KB Output is correct
54 Correct 76 ms 70504 KB Output is correct
55 Correct 74 ms 70548 KB Output is correct
56 Correct 75 ms 70452 KB Output is correct
57 Correct 79 ms 70264 KB Output is correct
58 Correct 79 ms 70268 KB Output is correct
59 Correct 80 ms 70264 KB Output is correct
60 Correct 80 ms 70264 KB Output is correct
61 Correct 98 ms 70396 KB Output is correct
62 Correct 81 ms 70616 KB Output is correct
63 Correct 77 ms 70520 KB Output is correct
64 Correct 77 ms 70264 KB Output is correct
65 Correct 78 ms 70316 KB Output is correct
66 Correct 78 ms 70264 KB Output is correct
67 Correct 78 ms 70264 KB Output is correct
68 Correct 79 ms 70620 KB Output is correct
69 Correct 88 ms 70520 KB Output is correct
70 Correct 80 ms 70524 KB Output is correct
71 Correct 75 ms 70392 KB Output is correct
72 Correct 89 ms 70268 KB Output is correct
73 Correct 78 ms 70264 KB Output is correct
74 Correct 75 ms 70492 KB Output is correct
75 Correct 75 ms 70524 KB Output is correct
76 Correct 76 ms 70520 KB Output is correct
77 Correct 77 ms 70392 KB Output is correct
78 Correct 79 ms 70364 KB Output is correct
79 Correct 75 ms 70432 KB Output is correct
80 Correct 97 ms 70392 KB Output is correct
81 Correct 108 ms 70552 KB Output is correct
82 Correct 94 ms 70392 KB Output is correct
83 Correct 76 ms 70392 KB Output is correct
84 Correct 74 ms 70392 KB Output is correct
85 Correct 75 ms 70408 KB Output is correct
86 Correct 75 ms 70316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 70008 KB Output is correct
2 Correct 69 ms 70008 KB Output is correct
3 Correct 69 ms 70008 KB Output is correct
4 Correct 70 ms 70008 KB Output is correct
5 Correct 70 ms 70060 KB Output is correct
6 Correct 71 ms 70136 KB Output is correct
7 Correct 74 ms 70152 KB Output is correct
8 Correct 75 ms 70268 KB Output is correct
9 Correct 70 ms 70140 KB Output is correct
10 Correct 70 ms 70008 KB Output is correct
11 Correct 71 ms 70008 KB Output is correct
12 Correct 70 ms 70008 KB Output is correct
13 Correct 71 ms 70160 KB Output is correct
14 Correct 71 ms 70136 KB Output is correct
15 Correct 71 ms 70048 KB Output is correct
16 Correct 71 ms 70008 KB Output is correct
17 Correct 71 ms 70096 KB Output is correct
18 Correct 71 ms 70008 KB Output is correct
19 Correct 71 ms 70032 KB Output is correct
20 Correct 72 ms 70136 KB Output is correct
21 Correct 70 ms 70136 KB Output is correct
22 Correct 72 ms 70136 KB Output is correct
23 Correct 70 ms 70000 KB Output is correct
24 Correct 71 ms 70136 KB Output is correct
25 Correct 78 ms 70060 KB Output is correct
26 Correct 87 ms 70008 KB Output is correct
27 Correct 76 ms 70136 KB Output is correct
28 Correct 70 ms 70008 KB Output is correct
29 Correct 70 ms 70140 KB Output is correct
30 Correct 71 ms 70140 KB Output is correct
31 Correct 72 ms 70008 KB Output is correct
32 Correct 70 ms 70136 KB Output is correct
33 Correct 71 ms 70048 KB Output is correct
34 Correct 70 ms 70136 KB Output is correct
35 Correct 71 ms 70136 KB Output is correct
36 Correct 70 ms 70136 KB Output is correct
37 Correct 70 ms 70056 KB Output is correct
38 Correct 69 ms 70020 KB Output is correct
39 Correct 69 ms 70008 KB Output is correct
40 Correct 70 ms 70008 KB Output is correct
41 Correct 73 ms 70016 KB Output is correct
42 Correct 89 ms 70108 KB Output is correct
43 Correct 72 ms 69988 KB Output is correct
44 Correct 69 ms 70008 KB Output is correct
45 Correct 71 ms 70132 KB Output is correct
46 Correct 77 ms 70236 KB Output is correct
47 Correct 77 ms 70264 KB Output is correct
48 Correct 97 ms 70264 KB Output is correct
49 Correct 76 ms 70776 KB Output is correct
50 Correct 89 ms 70732 KB Output is correct
51 Correct 86 ms 70648 KB Output is correct
52 Correct 74 ms 70520 KB Output is correct
53 Correct 78 ms 70648 KB Output is correct
54 Correct 76 ms 70504 KB Output is correct
55 Correct 74 ms 70548 KB Output is correct
56 Correct 75 ms 70452 KB Output is correct
57 Correct 79 ms 70264 KB Output is correct
58 Correct 79 ms 70268 KB Output is correct
59 Correct 80 ms 70264 KB Output is correct
60 Correct 80 ms 70264 KB Output is correct
61 Correct 98 ms 70396 KB Output is correct
62 Correct 81 ms 70616 KB Output is correct
63 Correct 77 ms 70520 KB Output is correct
64 Correct 77 ms 70264 KB Output is correct
65 Correct 78 ms 70316 KB Output is correct
66 Correct 78 ms 70264 KB Output is correct
67 Correct 78 ms 70264 KB Output is correct
68 Correct 79 ms 70620 KB Output is correct
69 Correct 88 ms 70520 KB Output is correct
70 Correct 80 ms 70524 KB Output is correct
71 Correct 75 ms 70392 KB Output is correct
72 Correct 89 ms 70268 KB Output is correct
73 Correct 78 ms 70264 KB Output is correct
74 Correct 75 ms 70492 KB Output is correct
75 Correct 75 ms 70524 KB Output is correct
76 Correct 76 ms 70520 KB Output is correct
77 Correct 77 ms 70392 KB Output is correct
78 Correct 79 ms 70364 KB Output is correct
79 Correct 75 ms 70432 KB Output is correct
80 Correct 97 ms 70392 KB Output is correct
81 Correct 108 ms 70552 KB Output is correct
82 Correct 94 ms 70392 KB Output is correct
83 Correct 76 ms 70392 KB Output is correct
84 Correct 74 ms 70392 KB Output is correct
85 Correct 75 ms 70408 KB Output is correct
86 Correct 75 ms 70316 KB Output is correct
87 Correct 92 ms 70660 KB Output is correct
88 Correct 139 ms 72064 KB Output is correct
89 Correct 373 ms 76848 KB Output is correct
90 Correct 377 ms 76896 KB Output is correct
91 Correct 376 ms 76896 KB Output is correct
92 Correct 258 ms 85180 KB Output is correct
93 Correct 262 ms 85200 KB Output is correct
94 Correct 299 ms 85168 KB Output is correct
95 Correct 231 ms 82296 KB Output is correct
96 Correct 234 ms 82284 KB Output is correct
97 Correct 250 ms 84504 KB Output is correct
98 Correct 240 ms 84444 KB Output is correct
99 Correct 238 ms 83252 KB Output is correct
100 Correct 434 ms 78452 KB Output is correct
101 Correct 507 ms 79032 KB Output is correct
102 Correct 490 ms 79072 KB Output is correct
103 Correct 489 ms 78892 KB Output is correct
104 Correct 257 ms 83188 KB Output is correct
105 Correct 248 ms 83332 KB Output is correct
106 Correct 239 ms 83296 KB Output is correct
107 Correct 328 ms 77788 KB Output is correct
108 Correct 340 ms 77916 KB Output is correct
109 Correct 360 ms 78432 KB Output is correct
110 Correct 236 ms 86240 KB Output is correct
111 Correct 230 ms 84096 KB Output is correct
112 Correct 246 ms 83572 KB Output is correct
113 Correct 219 ms 82272 KB Output is correct
114 Correct 441 ms 78412 KB Output is correct
115 Correct 506 ms 77916 KB Output is correct
116 Correct 225 ms 82268 KB Output is correct
117 Correct 244 ms 81036 KB Output is correct
118 Correct 240 ms 80348 KB Output is correct
119 Correct 237 ms 79964 KB Output is correct
120 Correct 236 ms 80140 KB Output is correct
121 Correct 223 ms 79580 KB Output is correct
122 Correct 227 ms 79200 KB Output is correct
123 Correct 253 ms 80908 KB Output is correct
124 Correct 280 ms 80448 KB Output is correct
125 Correct 254 ms 80024 KB Output is correct
126 Correct 256 ms 80364 KB Output is correct
127 Correct 236 ms 79672 KB Output is correct
128 Correct 240 ms 79348 KB Output is correct