Submission #241895

# Submission time Handle Problem Language Result Execution time Memory
241895 2020-06-26T09:29:13 Z osaaateiasavtnl Construction of Highway (JOI18_construction) C++17
16 / 100
2000 ms 31648 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 1e5+7, LG = 20;
int n, a[N], par[N];
ii d[N];
vector <int> tree[N];

int to[N][LG], h[N];
int l[N], r[N], ptr = -1;
void dfs(int u) {
    to[u][0] = par[u];
    for (int i = 1; i < LG; ++i)
        to[u][i] = to[to[u][i - 1]][i - 1];

    l[u] = ++ptr;    

    for (int v : tree[u]) {
        h[v] = h[u]+1;
        dfs(v);        
    }   

    r[u] = ptr;
}   

int mx[N << 2];
void upd(int v, int tl, int tr, int i, int x) {
    mx[v] = x;
    if (tl == tr) 
        return;
    int tm = (tl + tr) >> 1;
    if (i <= tm)
        upd(v * 2 + 1, tl, tm, i, x);
    else
        upd(v * 2 + 2, tm + 1, tr, i, x);
}
int get(int v, int tl, int tr, int l, int r) {
    if (tr < l || r < tl)
        return -1;
    if (l <= tl && tr <= r)
        return mx[v];
    int tm = (tl + tr) >> 1;
    return max(get(v * 2 + 1, tl, tm, l, r), get(v * 2 + 2, tm + 1, tr, l, r));
}

int val[N];

int last(int u) {
    return get(0, 0, N, l[u], r[u]);
}

int getval(int u, int ls) {
    if (ls == -1)
        return a[u];
    else
        return val[ls];
}

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    for (int i = 0; i < (N << 2); ++i)
        mx[i] = -1;

    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }   

    for (int t = 0; t < n - 1; ++t) {
        int u, v;
        cin >> u >> v;
        tree[u].app(v);
        par[v] = u;
        d[t] = mp(u,v);
    }   

    par[1] = 1;
    dfs(1);
    for (int t = 0; t < n - 1; ++t) {
        int u, v;
        tie(u, v) = d[t];

        vector <ii> path;
        while (1) {
            int up = u;
            for (int i = LG - 1; i >= 0; --i)
                if (last(to[up][i]) == last(u))
                    up = to[up][i];
            path.app(mp(getval(u, last(u)), h[u] - h[up] + 1));

            //cout << "u " << u << endl;
            //cout << "up " << up << endl;

            if (up == 1)
                break;
            else
                u = par[up];
        }   

        val[t] = a[v];
        upd(0, 0, N, l[v], t);

        /*
        cout << t << " : ";
        for (auto e : path)
            cout << e.f << ' ' << e.s << ", ";
        cout << endl;
        */

        int ans = 0;
        for (int i = 0; i < path.size(); ++i)
            for (int j = i + 1; j < path.size(); ++j)
                if (path[i].f < path[j].f)
                    ans += path[i].s * path[j].s;
        cout << ans << endl;
    }   
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:127:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < path.size(); ++i)
                         ~~^~~~~~~~~~~~~
construction.cpp:128:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int j = i + 1; j < path.size(); ++j)
                                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5888 KB Output is correct
2 Correct 8 ms 5888 KB Output is correct
3 Correct 9 ms 5888 KB Output is correct
4 Correct 12 ms 5888 KB Output is correct
5 Correct 16 ms 6016 KB Output is correct
6 Correct 16 ms 6016 KB Output is correct
7 Correct 16 ms 6016 KB Output is correct
8 Correct 10 ms 6016 KB Output is correct
9 Correct 12 ms 6016 KB Output is correct
10 Correct 12 ms 6016 KB Output is correct
11 Correct 12 ms 6016 KB Output is correct
12 Correct 14 ms 6016 KB Output is correct
13 Correct 12 ms 6016 KB Output is correct
14 Correct 13 ms 6048 KB Output is correct
15 Correct 26 ms 6016 KB Output is correct
16 Correct 28 ms 6016 KB Output is correct
17 Correct 27 ms 6016 KB Output is correct
18 Correct 29 ms 6016 KB Output is correct
19 Correct 13 ms 6016 KB Output is correct
20 Correct 13 ms 6016 KB Output is correct
21 Correct 12 ms 6144 KB Output is correct
22 Correct 16 ms 6016 KB Output is correct
23 Correct 17 ms 6016 KB Output is correct
24 Correct 16 ms 6016 KB Output is correct
25 Correct 18 ms 6016 KB Output is correct
26 Correct 11 ms 6016 KB Output is correct
27 Correct 13 ms 6016 KB Output is correct
28 Correct 14 ms 6016 KB Output is correct
29 Correct 12 ms 6016 KB Output is correct
30 Correct 26 ms 6016 KB Output is correct
31 Correct 26 ms 6016 KB Output is correct
32 Correct 12 ms 6016 KB Output is correct
33 Correct 12 ms 6016 KB Output is correct
34 Correct 13 ms 6016 KB Output is correct
35 Correct 12 ms 6016 KB Output is correct
36 Correct 13 ms 6016 KB Output is correct
37 Correct 12 ms 6016 KB Output is correct
38 Correct 12 ms 6016 KB Output is correct
39 Correct 15 ms 6144 KB Output is correct
40 Correct 13 ms 6016 KB Output is correct
41 Correct 14 ms 6016 KB Output is correct
42 Correct 14 ms 6016 KB Output is correct
43 Correct 13 ms 6016 KB Output is correct
44 Correct 13 ms 6016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5888 KB Output is correct
2 Correct 8 ms 5888 KB Output is correct
3 Correct 9 ms 5888 KB Output is correct
4 Correct 12 ms 5888 KB Output is correct
5 Correct 16 ms 6016 KB Output is correct
6 Correct 16 ms 6016 KB Output is correct
7 Correct 16 ms 6016 KB Output is correct
8 Correct 10 ms 6016 KB Output is correct
9 Correct 12 ms 6016 KB Output is correct
10 Correct 12 ms 6016 KB Output is correct
11 Correct 12 ms 6016 KB Output is correct
12 Correct 14 ms 6016 KB Output is correct
13 Correct 12 ms 6016 KB Output is correct
14 Correct 13 ms 6048 KB Output is correct
15 Correct 26 ms 6016 KB Output is correct
16 Correct 28 ms 6016 KB Output is correct
17 Correct 27 ms 6016 KB Output is correct
18 Correct 29 ms 6016 KB Output is correct
19 Correct 13 ms 6016 KB Output is correct
20 Correct 13 ms 6016 KB Output is correct
21 Correct 12 ms 6144 KB Output is correct
22 Correct 16 ms 6016 KB Output is correct
23 Correct 17 ms 6016 KB Output is correct
24 Correct 16 ms 6016 KB Output is correct
25 Correct 18 ms 6016 KB Output is correct
26 Correct 11 ms 6016 KB Output is correct
27 Correct 13 ms 6016 KB Output is correct
28 Correct 14 ms 6016 KB Output is correct
29 Correct 12 ms 6016 KB Output is correct
30 Correct 26 ms 6016 KB Output is correct
31 Correct 26 ms 6016 KB Output is correct
32 Correct 12 ms 6016 KB Output is correct
33 Correct 12 ms 6016 KB Output is correct
34 Correct 13 ms 6016 KB Output is correct
35 Correct 12 ms 6016 KB Output is correct
36 Correct 13 ms 6016 KB Output is correct
37 Correct 12 ms 6016 KB Output is correct
38 Correct 12 ms 6016 KB Output is correct
39 Correct 15 ms 6144 KB Output is correct
40 Correct 13 ms 6016 KB Output is correct
41 Correct 14 ms 6016 KB Output is correct
42 Correct 14 ms 6016 KB Output is correct
43 Correct 13 ms 6016 KB Output is correct
44 Correct 13 ms 6016 KB Output is correct
45 Correct 34 ms 6144 KB Output is correct
46 Correct 102 ms 6912 KB Output is correct
47 Correct 103 ms 6912 KB Output is correct
48 Correct 96 ms 7032 KB Output is correct
49 Correct 34 ms 7040 KB Output is correct
50 Correct 32 ms 7040 KB Output is correct
51 Correct 33 ms 7040 KB Output is correct
52 Correct 47 ms 7032 KB Output is correct
53 Correct 51 ms 7032 KB Output is correct
54 Correct 52 ms 7032 KB Output is correct
55 Correct 51 ms 7032 KB Output is correct
56 Correct 47 ms 6912 KB Output is correct
57 Correct 218 ms 6904 KB Output is correct
58 Correct 236 ms 7032 KB Output is correct
59 Correct 223 ms 6908 KB Output is correct
60 Correct 220 ms 6904 KB Output is correct
61 Correct 48 ms 6912 KB Output is correct
62 Correct 46 ms 6912 KB Output is correct
63 Correct 49 ms 6912 KB Output is correct
64 Correct 104 ms 6904 KB Output is correct
65 Correct 103 ms 6904 KB Output is correct
66 Correct 104 ms 6912 KB Output is correct
67 Correct 92 ms 6904 KB Output is correct
68 Correct 32 ms 7080 KB Output is correct
69 Correct 53 ms 7032 KB Output is correct
70 Correct 51 ms 7032 KB Output is correct
71 Correct 49 ms 6912 KB Output is correct
72 Correct 220 ms 7032 KB Output is correct
73 Correct 233 ms 6904 KB Output is correct
74 Correct 48 ms 6912 KB Output is correct
75 Correct 47 ms 6912 KB Output is correct
76 Correct 46 ms 6912 KB Output is correct
77 Correct 43 ms 6912 KB Output is correct
78 Correct 43 ms 6912 KB Output is correct
79 Correct 44 ms 6912 KB Output is correct
80 Correct 46 ms 6904 KB Output is correct
81 Correct 55 ms 6912 KB Output is correct
82 Correct 54 ms 6912 KB Output is correct
83 Correct 55 ms 6912 KB Output is correct
84 Correct 54 ms 6912 KB Output is correct
85 Correct 51 ms 6904 KB Output is correct
86 Correct 53 ms 6904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5888 KB Output is correct
2 Correct 8 ms 5888 KB Output is correct
3 Correct 9 ms 5888 KB Output is correct
4 Correct 12 ms 5888 KB Output is correct
5 Correct 16 ms 6016 KB Output is correct
6 Correct 16 ms 6016 KB Output is correct
7 Correct 16 ms 6016 KB Output is correct
8 Correct 10 ms 6016 KB Output is correct
9 Correct 12 ms 6016 KB Output is correct
10 Correct 12 ms 6016 KB Output is correct
11 Correct 12 ms 6016 KB Output is correct
12 Correct 14 ms 6016 KB Output is correct
13 Correct 12 ms 6016 KB Output is correct
14 Correct 13 ms 6048 KB Output is correct
15 Correct 26 ms 6016 KB Output is correct
16 Correct 28 ms 6016 KB Output is correct
17 Correct 27 ms 6016 KB Output is correct
18 Correct 29 ms 6016 KB Output is correct
19 Correct 13 ms 6016 KB Output is correct
20 Correct 13 ms 6016 KB Output is correct
21 Correct 12 ms 6144 KB Output is correct
22 Correct 16 ms 6016 KB Output is correct
23 Correct 17 ms 6016 KB Output is correct
24 Correct 16 ms 6016 KB Output is correct
25 Correct 18 ms 6016 KB Output is correct
26 Correct 11 ms 6016 KB Output is correct
27 Correct 13 ms 6016 KB Output is correct
28 Correct 14 ms 6016 KB Output is correct
29 Correct 12 ms 6016 KB Output is correct
30 Correct 26 ms 6016 KB Output is correct
31 Correct 26 ms 6016 KB Output is correct
32 Correct 12 ms 6016 KB Output is correct
33 Correct 12 ms 6016 KB Output is correct
34 Correct 13 ms 6016 KB Output is correct
35 Correct 12 ms 6016 KB Output is correct
36 Correct 13 ms 6016 KB Output is correct
37 Correct 12 ms 6016 KB Output is correct
38 Correct 12 ms 6016 KB Output is correct
39 Correct 15 ms 6144 KB Output is correct
40 Correct 13 ms 6016 KB Output is correct
41 Correct 14 ms 6016 KB Output is correct
42 Correct 14 ms 6016 KB Output is correct
43 Correct 13 ms 6016 KB Output is correct
44 Correct 13 ms 6016 KB Output is correct
45 Correct 34 ms 6144 KB Output is correct
46 Correct 102 ms 6912 KB Output is correct
47 Correct 103 ms 6912 KB Output is correct
48 Correct 96 ms 7032 KB Output is correct
49 Correct 34 ms 7040 KB Output is correct
50 Correct 32 ms 7040 KB Output is correct
51 Correct 33 ms 7040 KB Output is correct
52 Correct 47 ms 7032 KB Output is correct
53 Correct 51 ms 7032 KB Output is correct
54 Correct 52 ms 7032 KB Output is correct
55 Correct 51 ms 7032 KB Output is correct
56 Correct 47 ms 6912 KB Output is correct
57 Correct 218 ms 6904 KB Output is correct
58 Correct 236 ms 7032 KB Output is correct
59 Correct 223 ms 6908 KB Output is correct
60 Correct 220 ms 6904 KB Output is correct
61 Correct 48 ms 6912 KB Output is correct
62 Correct 46 ms 6912 KB Output is correct
63 Correct 49 ms 6912 KB Output is correct
64 Correct 104 ms 6904 KB Output is correct
65 Correct 103 ms 6904 KB Output is correct
66 Correct 104 ms 6912 KB Output is correct
67 Correct 92 ms 6904 KB Output is correct
68 Correct 32 ms 7080 KB Output is correct
69 Correct 53 ms 7032 KB Output is correct
70 Correct 51 ms 7032 KB Output is correct
71 Correct 49 ms 6912 KB Output is correct
72 Correct 220 ms 7032 KB Output is correct
73 Correct 233 ms 6904 KB Output is correct
74 Correct 48 ms 6912 KB Output is correct
75 Correct 47 ms 6912 KB Output is correct
76 Correct 46 ms 6912 KB Output is correct
77 Correct 43 ms 6912 KB Output is correct
78 Correct 43 ms 6912 KB Output is correct
79 Correct 44 ms 6912 KB Output is correct
80 Correct 46 ms 6904 KB Output is correct
81 Correct 55 ms 6912 KB Output is correct
82 Correct 54 ms 6912 KB Output is correct
83 Correct 55 ms 6912 KB Output is correct
84 Correct 54 ms 6912 KB Output is correct
85 Correct 51 ms 6904 KB Output is correct
86 Correct 53 ms 6904 KB Output is correct
87 Correct 268 ms 8412 KB Output is correct
88 Correct 996 ms 14072 KB Output is correct
89 Execution timed out 2078 ms 31648 KB Time limit exceeded
90 Halted 0 ms 0 KB -