Submission #128056

# Submission time Handle Problem Language Result Execution time Memory
128056 2019-07-10T11:26:05 Z BThero Construction of Highway (JOI18_construction) C++17
16 / 100
2000 ms 86908 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 boss[MAXN];

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(vector<pii> &V, 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));
}

int query(int u, int x) {
    vector<pii> V;

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

    ll ans = 0;

    for (int i = 0; i < V.size(); ++i) {
        for (int j = i + 1; j < V.size(); ++j) {
            if (V[i].fi < V[j].fi) {
                ans += V[i].se * 1ll * V[j].se;
            }
        }
    }

    return ans;
}

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

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

    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:128:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < V.size(); ++i) {
                     ~~^~~~~~~~~~
construction.cpp:129:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = i + 1; j < V.size(); ++j) {
                             ~~^~~~~~~~~~
construction.cpp: In function 'void solve()':
construction.cpp:140:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
construction.cpp:143:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &arr[i]);
         ~~~~~^~~~~~~~~~~~~~~
construction.cpp:148: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 72 ms 70008 KB Output is correct
2 Correct 71 ms 70136 KB Output is correct
3 Correct 71 ms 69980 KB Output is correct
4 Correct 70 ms 70008 KB Output is correct
5 Correct 72 ms 70136 KB Output is correct
6 Correct 72 ms 70136 KB Output is correct
7 Correct 71 ms 70008 KB Output is correct
8 Correct 71 ms 70128 KB Output is correct
9 Correct 72 ms 70068 KB Output is correct
10 Correct 72 ms 70060 KB Output is correct
11 Correct 73 ms 70048 KB Output is correct
12 Correct 71 ms 70280 KB Output is correct
13 Correct 71 ms 70136 KB Output is correct
14 Correct 70 ms 70136 KB Output is correct
15 Correct 71 ms 70008 KB Output is correct
16 Correct 71 ms 70008 KB Output is correct
17 Correct 71 ms 70176 KB Output is correct
18 Correct 73 ms 70064 KB Output is correct
19 Correct 70 ms 70064 KB Output is correct
20 Correct 71 ms 70008 KB Output is correct
21 Correct 90 ms 70136 KB Output is correct
22 Correct 71 ms 69976 KB Output is correct
23 Correct 71 ms 70136 KB Output is correct
24 Correct 72 ms 70008 KB Output is correct
25 Correct 70 ms 70008 KB Output is correct
26 Correct 70 ms 70136 KB Output is correct
27 Correct 71 ms 70088 KB Output is correct
28 Correct 71 ms 70008 KB Output is correct
29 Correct 71 ms 70100 KB Output is correct
30 Correct 72 ms 70108 KB Output is correct
31 Correct 72 ms 70208 KB Output is correct
32 Correct 71 ms 70140 KB Output is correct
33 Correct 70 ms 70008 KB Output is correct
34 Correct 71 ms 70136 KB Output is correct
35 Correct 72 ms 70136 KB Output is correct
36 Correct 71 ms 70008 KB Output is correct
37 Correct 71 ms 70116 KB Output is correct
38 Correct 74 ms 70136 KB Output is correct
39 Correct 71 ms 70136 KB Output is correct
40 Correct 70 ms 70012 KB Output is correct
41 Correct 71 ms 70116 KB Output is correct
42 Correct 71 ms 70136 KB Output is correct
43 Correct 70 ms 70084 KB Output is correct
44 Correct 72 ms 70136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 70008 KB Output is correct
2 Correct 71 ms 70136 KB Output is correct
3 Correct 71 ms 69980 KB Output is correct
4 Correct 70 ms 70008 KB Output is correct
5 Correct 72 ms 70136 KB Output is correct
6 Correct 72 ms 70136 KB Output is correct
7 Correct 71 ms 70008 KB Output is correct
8 Correct 71 ms 70128 KB Output is correct
9 Correct 72 ms 70068 KB Output is correct
10 Correct 72 ms 70060 KB Output is correct
11 Correct 73 ms 70048 KB Output is correct
12 Correct 71 ms 70280 KB Output is correct
13 Correct 71 ms 70136 KB Output is correct
14 Correct 70 ms 70136 KB Output is correct
15 Correct 71 ms 70008 KB Output is correct
16 Correct 71 ms 70008 KB Output is correct
17 Correct 71 ms 70176 KB Output is correct
18 Correct 73 ms 70064 KB Output is correct
19 Correct 70 ms 70064 KB Output is correct
20 Correct 71 ms 70008 KB Output is correct
21 Correct 90 ms 70136 KB Output is correct
22 Correct 71 ms 69976 KB Output is correct
23 Correct 71 ms 70136 KB Output is correct
24 Correct 72 ms 70008 KB Output is correct
25 Correct 70 ms 70008 KB Output is correct
26 Correct 70 ms 70136 KB Output is correct
27 Correct 71 ms 70088 KB Output is correct
28 Correct 71 ms 70008 KB Output is correct
29 Correct 71 ms 70100 KB Output is correct
30 Correct 72 ms 70108 KB Output is correct
31 Correct 72 ms 70208 KB Output is correct
32 Correct 71 ms 70140 KB Output is correct
33 Correct 70 ms 70008 KB Output is correct
34 Correct 71 ms 70136 KB Output is correct
35 Correct 72 ms 70136 KB Output is correct
36 Correct 71 ms 70008 KB Output is correct
37 Correct 71 ms 70116 KB Output is correct
38 Correct 74 ms 70136 KB Output is correct
39 Correct 71 ms 70136 KB Output is correct
40 Correct 70 ms 70012 KB Output is correct
41 Correct 71 ms 70116 KB Output is correct
42 Correct 71 ms 70136 KB Output is correct
43 Correct 70 ms 70084 KB Output is correct
44 Correct 72 ms 70136 KB Output is correct
45 Correct 72 ms 70280 KB Output is correct
46 Correct 78 ms 70392 KB Output is correct
47 Correct 78 ms 70392 KB Output is correct
48 Correct 78 ms 70392 KB Output is correct
49 Correct 75 ms 70648 KB Output is correct
50 Correct 75 ms 70648 KB Output is correct
51 Correct 76 ms 70648 KB Output is correct
52 Correct 89 ms 70648 KB Output is correct
53 Correct 83 ms 70520 KB Output is correct
54 Correct 158 ms 70648 KB Output is correct
55 Correct 83 ms 70520 KB Output is correct
56 Correct 75 ms 70520 KB Output is correct
57 Correct 78 ms 70264 KB Output is correct
58 Correct 81 ms 70388 KB Output is correct
59 Correct 82 ms 70336 KB Output is correct
60 Correct 80 ms 70392 KB Output is correct
61 Correct 91 ms 70484 KB Output is correct
62 Correct 75 ms 70520 KB Output is correct
63 Correct 76 ms 70520 KB Output is correct
64 Correct 109 ms 70452 KB Output is correct
65 Correct 77 ms 70392 KB Output is correct
66 Correct 75 ms 70392 KB Output is correct
67 Correct 78 ms 70264 KB Output is correct
68 Correct 77 ms 70648 KB Output is correct
69 Correct 76 ms 70520 KB Output is correct
70 Correct 81 ms 70520 KB Output is correct
71 Correct 75 ms 70520 KB Output is correct
72 Correct 78 ms 70392 KB Output is correct
73 Correct 80 ms 70364 KB Output is correct
74 Correct 74 ms 70520 KB Output is correct
75 Correct 74 ms 70364 KB Output is correct
76 Correct 75 ms 70392 KB Output is correct
77 Correct 75 ms 70392 KB Output is correct
78 Correct 74 ms 70392 KB Output is correct
79 Correct 76 ms 70392 KB Output is correct
80 Correct 75 ms 70324 KB Output is correct
81 Correct 74 ms 70392 KB Output is correct
82 Correct 76 ms 70364 KB Output is correct
83 Correct 77 ms 70392 KB Output is correct
84 Correct 75 ms 70392 KB Output is correct
85 Correct 76 ms 70392 KB Output is correct
86 Correct 75 ms 70392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 70008 KB Output is correct
2 Correct 71 ms 70136 KB Output is correct
3 Correct 71 ms 69980 KB Output is correct
4 Correct 70 ms 70008 KB Output is correct
5 Correct 72 ms 70136 KB Output is correct
6 Correct 72 ms 70136 KB Output is correct
7 Correct 71 ms 70008 KB Output is correct
8 Correct 71 ms 70128 KB Output is correct
9 Correct 72 ms 70068 KB Output is correct
10 Correct 72 ms 70060 KB Output is correct
11 Correct 73 ms 70048 KB Output is correct
12 Correct 71 ms 70280 KB Output is correct
13 Correct 71 ms 70136 KB Output is correct
14 Correct 70 ms 70136 KB Output is correct
15 Correct 71 ms 70008 KB Output is correct
16 Correct 71 ms 70008 KB Output is correct
17 Correct 71 ms 70176 KB Output is correct
18 Correct 73 ms 70064 KB Output is correct
19 Correct 70 ms 70064 KB Output is correct
20 Correct 71 ms 70008 KB Output is correct
21 Correct 90 ms 70136 KB Output is correct
22 Correct 71 ms 69976 KB Output is correct
23 Correct 71 ms 70136 KB Output is correct
24 Correct 72 ms 70008 KB Output is correct
25 Correct 70 ms 70008 KB Output is correct
26 Correct 70 ms 70136 KB Output is correct
27 Correct 71 ms 70088 KB Output is correct
28 Correct 71 ms 70008 KB Output is correct
29 Correct 71 ms 70100 KB Output is correct
30 Correct 72 ms 70108 KB Output is correct
31 Correct 72 ms 70208 KB Output is correct
32 Correct 71 ms 70140 KB Output is correct
33 Correct 70 ms 70008 KB Output is correct
34 Correct 71 ms 70136 KB Output is correct
35 Correct 72 ms 70136 KB Output is correct
36 Correct 71 ms 70008 KB Output is correct
37 Correct 71 ms 70116 KB Output is correct
38 Correct 74 ms 70136 KB Output is correct
39 Correct 71 ms 70136 KB Output is correct
40 Correct 70 ms 70012 KB Output is correct
41 Correct 71 ms 70116 KB Output is correct
42 Correct 71 ms 70136 KB Output is correct
43 Correct 70 ms 70084 KB Output is correct
44 Correct 72 ms 70136 KB Output is correct
45 Correct 72 ms 70280 KB Output is correct
46 Correct 78 ms 70392 KB Output is correct
47 Correct 78 ms 70392 KB Output is correct
48 Correct 78 ms 70392 KB Output is correct
49 Correct 75 ms 70648 KB Output is correct
50 Correct 75 ms 70648 KB Output is correct
51 Correct 76 ms 70648 KB Output is correct
52 Correct 89 ms 70648 KB Output is correct
53 Correct 83 ms 70520 KB Output is correct
54 Correct 158 ms 70648 KB Output is correct
55 Correct 83 ms 70520 KB Output is correct
56 Correct 75 ms 70520 KB Output is correct
57 Correct 78 ms 70264 KB Output is correct
58 Correct 81 ms 70388 KB Output is correct
59 Correct 82 ms 70336 KB Output is correct
60 Correct 80 ms 70392 KB Output is correct
61 Correct 91 ms 70484 KB Output is correct
62 Correct 75 ms 70520 KB Output is correct
63 Correct 76 ms 70520 KB Output is correct
64 Correct 109 ms 70452 KB Output is correct
65 Correct 77 ms 70392 KB Output is correct
66 Correct 75 ms 70392 KB Output is correct
67 Correct 78 ms 70264 KB Output is correct
68 Correct 77 ms 70648 KB Output is correct
69 Correct 76 ms 70520 KB Output is correct
70 Correct 81 ms 70520 KB Output is correct
71 Correct 75 ms 70520 KB Output is correct
72 Correct 78 ms 70392 KB Output is correct
73 Correct 80 ms 70364 KB Output is correct
74 Correct 74 ms 70520 KB Output is correct
75 Correct 74 ms 70364 KB Output is correct
76 Correct 75 ms 70392 KB Output is correct
77 Correct 75 ms 70392 KB Output is correct
78 Correct 74 ms 70392 KB Output is correct
79 Correct 76 ms 70392 KB Output is correct
80 Correct 75 ms 70324 KB Output is correct
81 Correct 74 ms 70392 KB Output is correct
82 Correct 76 ms 70364 KB Output is correct
83 Correct 77 ms 70392 KB Output is correct
84 Correct 75 ms 70392 KB Output is correct
85 Correct 76 ms 70392 KB Output is correct
86 Correct 75 ms 70392 KB Output is correct
87 Correct 90 ms 70880 KB Output is correct
88 Correct 137 ms 72568 KB Output is correct
89 Correct 328 ms 78556 KB Output is correct
90 Correct 328 ms 78584 KB Output is correct
91 Correct 322 ms 78584 KB Output is correct
92 Correct 222 ms 86776 KB Output is correct
93 Correct 235 ms 86812 KB Output is correct
94 Correct 228 ms 86908 KB Output is correct
95 Correct 1349 ms 83440 KB Output is correct
96 Execution timed out 2068 ms 83824 KB Time limit exceeded
97 Halted 0 ms 0 KB -