Submission #102631

# Submission time Handle Problem Language Result Execution time Memory
102631 2019-03-26T11:04:22 Z forestryks Construction of Highway (JOI18_construction) C++14
100 / 100
1416 ms 21848 KB
///////////////////////////////////////////////////////////////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define FILE_IO(x) freopen((string(x) + ".in").c_str(), "r", stdin); freopen((string(x) + ".out").c_str(), "w", stdout)
#define f first
#define s second
#define x1 x1qwer
#define y1 y1qwer
#define right right123
#define left left123
#define foreach(it, v) for (auto it : v)
#define rep(it, n) for (int it = 0; it < n; ++it)
#define forin(it, l, r) for (int it = l; it < r; ++it)
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const double DINF = numeric_limits<double>::infinity();
const ll MOD = 1e9 + 7;
const double EPS = 1e-7;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
mt19937 mmtw_(MOD);
uniform_int_distribution<ll> rd_;
ll randomll() { return rd_(mmtw_);}
ll rnd(ll x, ll y) { return rd_(mmtw_) % (y - x + 1) + x; }
////////////////////////////////////////////////////////////////////////////////////////////////

const int MAXN = 1e5 + 5;
int n;
int a[MAXN];
vector<int> ord;
int p[MAXN];
vector<int> g[MAXN];
int sz[MAXN];
int top[MAXN];
int tin[MAXN];
int tout[MAXN];
int h[MAXN];
vector<pair<int, int>> seg[MAXN];

void rebuild_graph(int v) {
    sz[v] = 1;
    for (auto to : g[v]) {
        rebuild_graph(to);
        sz[v] += sz[to];
    }

    nth_element(g[v].begin(), g[v].begin(), g[v].end(), [&](int a, int b){
        return sz[a] > sz[b];
    });
}

int tt = 0;
void build_hld(int v) {
    tin[v] = tt++;

    if (!seg[top[v]].empty() && seg[top[v]].back().f == a[v]) {
        seg[top[v]].back().s++;
    } else {
        seg[top[v]].push_back({a[v], 1});
    }

    if (!g[v].empty()) {
        for (auto to : g[v]) {
            top[to] = to;
            h[to] = h[v] + 1;
        }
        top[g[v][0]] = top[v];
        for (auto to : g[v]) {
            build_hld(to);
        }
    }

    reverse(all(seg[v]));

    tout[v] = tt;
}

void compress() {
    vector<int> vals(a, a + n);
    sort(all(vals));
    vals.erase(unique(all(vals)), vals.end());
    rep(i, n) {
        a[i] = lower_bound(all(vals), a[i]) - vals.begin();
    }
}

void prepare() {
    compress();
    rebuild_graph(0);
    build_hld(0);
}

int t[MAXN * 4];

void add(int v, int tl, int tr, int pos, int x) {
    if (tr - tl == 1) {
        t[v] += x;
        return;
    }

    int tm = tl + (tr - tl) / 2;
    if (pos < tm) {
        add(v * 2 + 1, tl, tm, pos, x);
    } else {
        add(v * 2 + 2, tm, tr, pos, x);
    }
    t[v] = t[v * 2 + 1] + t[v * 2 + 2];
}

int get(int v, int tl, int tr, int l, int r) {
    if (l <= tl && tr <= r) return t[v];
    if (r <= tl || tr <= l) return 0;

    int tm = tl + (tr - tl) / 2;
    int a = get(v * 2 + 1, tl, tm, l, r);
    int b = get(v * 2 + 2, tm, tr, l, r);
    return a + b;
}

// void add(int v, int tl, int tr, int pos, int x) {
//     t[pos] += x;
// }

// int get(int v, int tl, int tr, int l, int r) {
//     int res = 0;
//     for (int i = l; i < r; ++i) {
//         res += t[i];
//     }
//     return res;
// }

ll solve(const vector<pair<int, int>> &a) {
    ll res = 0;
    for (int i = 0; i < a.size(); ++i) {
        res += 1LL * get(0, 0, n, a[i].f + 1, n) * a[i].s;
        add(0, 0, n, a[i].f, a[i].s);
    }

    for (int i = 0; i < a.size(); ++i) {
        add(0, 0, n, a[i].f, -a[i].s);
    }

    return res;

    // ll res = 0;
    // for (int i = 0; i < a.size(); ++i) {
    //     for (int j = i + 1; j < a.size(); ++j) {
    //         if (a[i].f > a[j].f) {
    //             res += 1LL * a[i].s * a[j].s;
    //         }
    //     }
    // }
    // return res;
}

ll query(int v) {
    vector<int> vc;
    int w = p[v];
    while (w != -1) {
        vc.push_back(w);
        w = p[top[w]];
    }

    reverse(all(vc));

    vector<pair<int, int>> vec;
    for (auto w : vc) {
        int sz = 0;
        while (true) {
            if (seg[top[w]].empty()) break;

            if (h[top[w]] + sz + seg[top[w]].back().s - 1 <= h[w]) {
                sz += seg[top[w]].back().s;
                vec.push_back(seg[top[w]].back());
                seg[top[w]].pop_back();
            } else {
                if (h[top[w]] + sz <= h[w]) {
                    int our = h[w] - (h[top[w]] + sz) + 1;
                    seg[top[w]].back().s -= our;
                    sz += our;
                    vec.push_back(seg[top[w]].back());
                    vec.back().s = our;
                }
                break;
            }
        }
        seg[top[w]].push_back({a[v], sz});
        // cout << a[v] << ' ' << sz << endl;
    }

    // ll res = 0;
    // for (int i = 0; i < vec.size(); ++i) {
    //     for (int j = i + 1; j < vec.size(); ++j) {
    //         if (vec[i].f > vec[j].f) {
    //             res += 1LL * vec[i].s * vec[j].s;
    //         }
    //     }
    // }

    return solve(vec);

    // cout << "Q " << v + 1 << endl;
    // for (auto it : vec) {
    //     cout << it.f << ' ' << it.s << endl;
    // }

    // cout << "vc" << endl;
    // for (auto it : vc) {
    //     cout << it + 1 << ' ';
    // }
    // cout << endl << endl;
}

int main() {
    FAST_IO;
    cin >> n;
    rep(i, n) {
        cin >> a[i];
    }
    p[0] = -1;
    rep(i, n - 1) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        ord.push_back(y);
        p[y] = x;
        g[x].push_back(y);
    }

    prepare();

    // query(1);
    // query(2);
    // query(3);
    // query(4);
    // query(5);

    // rep(v, n) {
    //     cout << "V " << v + 1 << endl;

    //     cout << "top " << top[v] + 1 << endl;
    //     cout << "seg ";
    //     for (auto it : seg[v]) {
    //         cout << "(" << it.f << ' ' << it.s << ") ";
    //     }
    //     cout << endl;
    //     cout << "h " << h[v] << endl;

    //     cout << endl;
    // }

    for (auto it : ord) {
        cout << query(it) << '\n';
        // query(it);
    }
}

Compilation message

construction.cpp: In function 'll solve(const std::vector<std::pair<int, int> >&)':
construction.cpp:142:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a.size(); ++i) {
                     ~~^~~~~~~~~~
construction.cpp:147:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < a.size(); ++i) {
                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 5092 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 8 ms 5120 KB Output is correct
12 Correct 9 ms 5120 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 10 ms 5248 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 6 ms 5092 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 7 ms 5120 KB Output is correct
26 Correct 8 ms 5120 KB Output is correct
27 Correct 9 ms 5120 KB Output is correct
28 Correct 7 ms 5120 KB Output is correct
29 Correct 6 ms 5120 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 7 ms 5120 KB Output is correct
32 Correct 6 ms 5120 KB Output is correct
33 Correct 7 ms 5120 KB Output is correct
34 Correct 8 ms 5120 KB Output is correct
35 Correct 7 ms 5120 KB Output is correct
36 Correct 7 ms 5092 KB Output is correct
37 Correct 9 ms 5092 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 7 ms 5120 KB Output is correct
40 Correct 9 ms 5120 KB Output is correct
41 Correct 9 ms 5120 KB Output is correct
42 Correct 7 ms 5120 KB Output is correct
43 Correct 9 ms 5120 KB Output is correct
44 Correct 9 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 5092 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 8 ms 5120 KB Output is correct
12 Correct 9 ms 5120 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 10 ms 5248 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 6 ms 5092 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 7 ms 5120 KB Output is correct
26 Correct 8 ms 5120 KB Output is correct
27 Correct 9 ms 5120 KB Output is correct
28 Correct 7 ms 5120 KB Output is correct
29 Correct 6 ms 5120 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 7 ms 5120 KB Output is correct
32 Correct 6 ms 5120 KB Output is correct
33 Correct 7 ms 5120 KB Output is correct
34 Correct 8 ms 5120 KB Output is correct
35 Correct 7 ms 5120 KB Output is correct
36 Correct 7 ms 5092 KB Output is correct
37 Correct 9 ms 5092 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 7 ms 5120 KB Output is correct
40 Correct 9 ms 5120 KB Output is correct
41 Correct 9 ms 5120 KB Output is correct
42 Correct 7 ms 5120 KB Output is correct
43 Correct 9 ms 5120 KB Output is correct
44 Correct 9 ms 5120 KB Output is correct
45 Correct 8 ms 5120 KB Output is correct
46 Correct 18 ms 5376 KB Output is correct
47 Correct 17 ms 5376 KB Output is correct
48 Correct 19 ms 5348 KB Output is correct
49 Correct 13 ms 5888 KB Output is correct
50 Correct 16 ms 5760 KB Output is correct
51 Correct 7 ms 5804 KB Output is correct
52 Correct 13 ms 5504 KB Output is correct
53 Correct 14 ms 5504 KB Output is correct
54 Correct 14 ms 5632 KB Output is correct
55 Correct 14 ms 5504 KB Output is correct
56 Correct 13 ms 5504 KB Output is correct
57 Correct 23 ms 5468 KB Output is correct
58 Correct 27 ms 5424 KB Output is correct
59 Correct 36 ms 5376 KB Output is correct
60 Correct 26 ms 5376 KB Output is correct
61 Correct 16 ms 5632 KB Output is correct
62 Correct 15 ms 5504 KB Output is correct
63 Correct 16 ms 5504 KB Output is correct
64 Correct 15 ms 5376 KB Output is correct
65 Correct 16 ms 5376 KB Output is correct
66 Correct 17 ms 5376 KB Output is correct
67 Correct 24 ms 5416 KB Output is correct
68 Correct 11 ms 5760 KB Output is correct
69 Correct 17 ms 5552 KB Output is correct
70 Correct 13 ms 5632 KB Output is correct
71 Correct 12 ms 5376 KB Output is correct
72 Correct 24 ms 5376 KB Output is correct
73 Correct 22 ms 5376 KB Output is correct
74 Correct 11 ms 5504 KB Output is correct
75 Correct 14 ms 5504 KB Output is correct
76 Correct 13 ms 5504 KB Output is correct
77 Correct 14 ms 5376 KB Output is correct
78 Correct 12 ms 5504 KB Output is correct
79 Correct 13 ms 5504 KB Output is correct
80 Correct 12 ms 5376 KB Output is correct
81 Correct 15 ms 5504 KB Output is correct
82 Correct 14 ms 5376 KB Output is correct
83 Correct 19 ms 5504 KB Output is correct
84 Correct 13 ms 5504 KB Output is correct
85 Correct 15 ms 5504 KB Output is correct
86 Correct 12 ms 5376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 5092 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 7 ms 5120 KB Output is correct
11 Correct 8 ms 5120 KB Output is correct
12 Correct 9 ms 5120 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 7 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 10 ms 5248 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 6 ms 5092 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 7 ms 5120 KB Output is correct
26 Correct 8 ms 5120 KB Output is correct
27 Correct 9 ms 5120 KB Output is correct
28 Correct 7 ms 5120 KB Output is correct
29 Correct 6 ms 5120 KB Output is correct
30 Correct 8 ms 5120 KB Output is correct
31 Correct 7 ms 5120 KB Output is correct
32 Correct 6 ms 5120 KB Output is correct
33 Correct 7 ms 5120 KB Output is correct
34 Correct 8 ms 5120 KB Output is correct
35 Correct 7 ms 5120 KB Output is correct
36 Correct 7 ms 5092 KB Output is correct
37 Correct 9 ms 5092 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 7 ms 5120 KB Output is correct
40 Correct 9 ms 5120 KB Output is correct
41 Correct 9 ms 5120 KB Output is correct
42 Correct 7 ms 5120 KB Output is correct
43 Correct 9 ms 5120 KB Output is correct
44 Correct 9 ms 5120 KB Output is correct
45 Correct 8 ms 5120 KB Output is correct
46 Correct 18 ms 5376 KB Output is correct
47 Correct 17 ms 5376 KB Output is correct
48 Correct 19 ms 5348 KB Output is correct
49 Correct 13 ms 5888 KB Output is correct
50 Correct 16 ms 5760 KB Output is correct
51 Correct 7 ms 5804 KB Output is correct
52 Correct 13 ms 5504 KB Output is correct
53 Correct 14 ms 5504 KB Output is correct
54 Correct 14 ms 5632 KB Output is correct
55 Correct 14 ms 5504 KB Output is correct
56 Correct 13 ms 5504 KB Output is correct
57 Correct 23 ms 5468 KB Output is correct
58 Correct 27 ms 5424 KB Output is correct
59 Correct 36 ms 5376 KB Output is correct
60 Correct 26 ms 5376 KB Output is correct
61 Correct 16 ms 5632 KB Output is correct
62 Correct 15 ms 5504 KB Output is correct
63 Correct 16 ms 5504 KB Output is correct
64 Correct 15 ms 5376 KB Output is correct
65 Correct 16 ms 5376 KB Output is correct
66 Correct 17 ms 5376 KB Output is correct
67 Correct 24 ms 5416 KB Output is correct
68 Correct 11 ms 5760 KB Output is correct
69 Correct 17 ms 5552 KB Output is correct
70 Correct 13 ms 5632 KB Output is correct
71 Correct 12 ms 5376 KB Output is correct
72 Correct 24 ms 5376 KB Output is correct
73 Correct 22 ms 5376 KB Output is correct
74 Correct 11 ms 5504 KB Output is correct
75 Correct 14 ms 5504 KB Output is correct
76 Correct 13 ms 5504 KB Output is correct
77 Correct 14 ms 5376 KB Output is correct
78 Correct 12 ms 5504 KB Output is correct
79 Correct 13 ms 5504 KB Output is correct
80 Correct 12 ms 5376 KB Output is correct
81 Correct 15 ms 5504 KB Output is correct
82 Correct 14 ms 5376 KB Output is correct
83 Correct 19 ms 5504 KB Output is correct
84 Correct 13 ms 5504 KB Output is correct
85 Correct 15 ms 5504 KB Output is correct
86 Correct 12 ms 5376 KB Output is correct
87 Correct 43 ms 5888 KB Output is correct
88 Correct 148 ms 7416 KB Output is correct
89 Correct 644 ms 13128 KB Output is correct
90 Correct 694 ms 13032 KB Output is correct
91 Correct 683 ms 13216 KB Output is correct
92 Correct 271 ms 21732 KB Output is correct
93 Correct 285 ms 21668 KB Output is correct
94 Correct 315 ms 21848 KB Output is correct
95 Correct 238 ms 17640 KB Output is correct
96 Correct 321 ms 17768 KB Output is correct
97 Correct 366 ms 17768 KB Output is correct
98 Correct 314 ms 17964 KB Output is correct
99 Correct 300 ms 17080 KB Output is correct
100 Correct 921 ms 13224 KB Output is correct
101 Correct 1193 ms 13084 KB Output is correct
102 Correct 1416 ms 13160 KB Output is correct
103 Correct 1195 ms 13132 KB Output is correct
104 Correct 331 ms 17072 KB Output is correct
105 Correct 326 ms 17060 KB Output is correct
106 Correct 328 ms 17088 KB Output is correct
107 Correct 545 ms 12136 KB Output is correct
108 Correct 607 ms 12136 KB Output is correct
109 Correct 603 ms 12520 KB Output is correct
110 Correct 257 ms 20708 KB Output is correct
111 Correct 243 ms 17768 KB Output is correct
112 Correct 264 ms 16872 KB Output is correct
113 Correct 240 ms 16132 KB Output is correct
114 Correct 900 ms 13152 KB Output is correct
115 Correct 938 ms 12156 KB Output is correct
116 Correct 247 ms 15980 KB Output is correct
117 Correct 299 ms 15080 KB Output is correct
118 Correct 291 ms 14416 KB Output is correct
119 Correct 272 ms 14032 KB Output is correct
120 Correct 223 ms 13920 KB Output is correct
121 Correct 279 ms 13404 KB Output is correct
122 Correct 269 ms 13032 KB Output is correct
123 Correct 385 ms 15080 KB Output is correct
124 Correct 337 ms 14528 KB Output is correct
125 Correct 336 ms 14056 KB Output is correct
126 Correct 300 ms 14048 KB Output is correct
127 Correct 306 ms 13468 KB Output is correct
128 Correct 292 ms 13160 KB Output is correct