Submission #313451

# Submission time Handle Problem Language Result Execution time Memory
313451 2020-10-16T03:09:32 Z HynDuf Construction of Highway (JOI18_construction) C++11
100 / 100
523 ms 110492 KB
#include <bits/stdc++.h>

#define task "C"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e5 + 2;
int n, a[N], head[N], sz[N], dep[N], bit[N], par[N];
ll cur = 0;
vi g[N], com;
vii edge, rollback;
deque<ii> seg[N];
void upd(int p, int v)
{
    for (; p <= n; p += p & -p) bit[p] += v;
}
int query(int p)
{
    int res = 0;
    for (; p; p -= p & -p) res += bit[p];
    return res;
}
void dfs(int u, int p)
{
    par[u] = p;
    dep[u] = dep[p] + 1;
    sz[u] = 1;
    for (int v : g[u]) if (v != p)
    {
        dfs(v, u);
        sz[u] += sz[v];
    }
}
void hld(int u, int p, int h)
{
    head[u] = h;
    int mx = 0, big = -1;
    for (int v : g[u]) if (v != p && mx < sz[v]) mx = sz[v], big = v;
    if (big != -1) hld(big, u, h);
    for (int v : g[u]) if (v != p && v != big) hld(v, u, v);
}
void solve(int id, int col, int len)
{
    if (seg[id].empty())
    {
        seg[id].push_front({col, len});
        return;
    }
    vii tem;
    int num = 0;
    while (1)
    {
        ii it = seg[id].front();
        seg[id].pop_front();
        num += it.S;
        if (num >= len || seg[id].empty())
        {
            if (num - len > 0) seg[id].push_front({it.F, num - len});
            tem.eb(it.F, it.S - max(0, (num - len)));
            seg[id].push_front({col, len});
            break;
        }
        tem.eb(it);
    }
    Rep(i, SZ(tem) - 1, 0)
    {
        //cerr << tem[i].F << ' ' << tem[i].S << '\n';
        rollback.eb(tem[i]);
        upd(tem[i].F, tem[i].S);
        cur += tem[i].S * 1LL * query(tem[i].F - 1);
    }
}
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    cin >> n;
    rep(i, 1, n) cin >> a[i], com.eb(a[i]);
    sort(all(com));
    com.erase(unique(all(com)), com.end());
    rep(i, 1, n) a[i] = lower_bound(all(com), a[i]) - com.begin() + 1;
    rep(i, 2, n)
    {
        int u, v;
        cin >> u >> v;
        g[u].eb(v);
        g[v].eb(u);
        edge.eb(u, v);
    }
    dfs(1, 0);
    hld(1, 0, 1);
    seg[1].eb(a[1], 1);
    for (ii E : edge)
    {
        int u = E.S;
        while (u)
        {
            solve(head[u], a[E.S], dep[u] - dep[head[u]] + 1);
            u = par[head[u]];
        }
        cout << cur << '\n';
        //for (ii v : seg[1]) cout << '(' << v.F << ", " << v.S << "), ";
        //cout << '\n';
        for (ii v : rollback) upd(v.F, -v.S);
        cur = 0;
        rollback.clear();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 53 ms 70008 KB Output is correct
2 Correct 54 ms 70008 KB Output is correct
3 Correct 58 ms 70008 KB Output is correct
4 Correct 60 ms 70136 KB Output is correct
5 Correct 60 ms 70264 KB Output is correct
6 Correct 61 ms 70136 KB Output is correct
7 Correct 59 ms 70152 KB Output is correct
8 Correct 54 ms 70136 KB Output is correct
9 Correct 54 ms 70144 KB Output is correct
10 Correct 54 ms 70136 KB Output is correct
11 Correct 59 ms 70264 KB Output is correct
12 Correct 58 ms 70264 KB Output is correct
13 Correct 59 ms 70264 KB Output is correct
14 Correct 59 ms 70264 KB Output is correct
15 Correct 60 ms 70272 KB Output is correct
16 Correct 59 ms 70136 KB Output is correct
17 Correct 59 ms 70240 KB Output is correct
18 Correct 59 ms 70136 KB Output is correct
19 Correct 59 ms 70264 KB Output is correct
20 Correct 60 ms 70264 KB Output is correct
21 Correct 58 ms 70236 KB Output is correct
22 Correct 58 ms 70136 KB Output is correct
23 Correct 58 ms 70136 KB Output is correct
24 Correct 58 ms 70136 KB Output is correct
25 Correct 58 ms 70136 KB Output is correct
26 Correct 54 ms 70136 KB Output is correct
27 Correct 58 ms 70268 KB Output is correct
28 Correct 58 ms 70264 KB Output is correct
29 Correct 59 ms 70136 KB Output is correct
30 Correct 59 ms 70136 KB Output is correct
31 Correct 62 ms 70144 KB Output is correct
32 Correct 59 ms 70136 KB Output is correct
33 Correct 59 ms 70136 KB Output is correct
34 Correct 58 ms 70136 KB Output is correct
35 Correct 60 ms 70136 KB Output is correct
36 Correct 61 ms 70140 KB Output is correct
37 Correct 58 ms 70144 KB Output is correct
38 Correct 59 ms 70144 KB Output is correct
39 Correct 58 ms 70136 KB Output is correct
40 Correct 59 ms 70136 KB Output is correct
41 Correct 59 ms 70136 KB Output is correct
42 Correct 58 ms 70136 KB Output is correct
43 Correct 60 ms 70136 KB Output is correct
44 Correct 59 ms 70176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 70008 KB Output is correct
2 Correct 54 ms 70008 KB Output is correct
3 Correct 58 ms 70008 KB Output is correct
4 Correct 60 ms 70136 KB Output is correct
5 Correct 60 ms 70264 KB Output is correct
6 Correct 61 ms 70136 KB Output is correct
7 Correct 59 ms 70152 KB Output is correct
8 Correct 54 ms 70136 KB Output is correct
9 Correct 54 ms 70144 KB Output is correct
10 Correct 54 ms 70136 KB Output is correct
11 Correct 59 ms 70264 KB Output is correct
12 Correct 58 ms 70264 KB Output is correct
13 Correct 59 ms 70264 KB Output is correct
14 Correct 59 ms 70264 KB Output is correct
15 Correct 60 ms 70272 KB Output is correct
16 Correct 59 ms 70136 KB Output is correct
17 Correct 59 ms 70240 KB Output is correct
18 Correct 59 ms 70136 KB Output is correct
19 Correct 59 ms 70264 KB Output is correct
20 Correct 60 ms 70264 KB Output is correct
21 Correct 58 ms 70236 KB Output is correct
22 Correct 58 ms 70136 KB Output is correct
23 Correct 58 ms 70136 KB Output is correct
24 Correct 58 ms 70136 KB Output is correct
25 Correct 58 ms 70136 KB Output is correct
26 Correct 54 ms 70136 KB Output is correct
27 Correct 58 ms 70268 KB Output is correct
28 Correct 58 ms 70264 KB Output is correct
29 Correct 59 ms 70136 KB Output is correct
30 Correct 59 ms 70136 KB Output is correct
31 Correct 62 ms 70144 KB Output is correct
32 Correct 59 ms 70136 KB Output is correct
33 Correct 59 ms 70136 KB Output is correct
34 Correct 58 ms 70136 KB Output is correct
35 Correct 60 ms 70136 KB Output is correct
36 Correct 61 ms 70140 KB Output is correct
37 Correct 58 ms 70144 KB Output is correct
38 Correct 59 ms 70144 KB Output is correct
39 Correct 58 ms 70136 KB Output is correct
40 Correct 59 ms 70136 KB Output is correct
41 Correct 59 ms 70136 KB Output is correct
42 Correct 58 ms 70136 KB Output is correct
43 Correct 60 ms 70136 KB Output is correct
44 Correct 59 ms 70176 KB Output is correct
45 Correct 59 ms 70392 KB Output is correct
46 Correct 67 ms 71416 KB Output is correct
47 Correct 67 ms 71288 KB Output is correct
48 Correct 66 ms 71288 KB Output is correct
49 Correct 58 ms 70648 KB Output is correct
50 Correct 57 ms 70648 KB Output is correct
51 Correct 60 ms 70648 KB Output is correct
52 Correct 63 ms 71544 KB Output is correct
53 Correct 65 ms 71544 KB Output is correct
54 Correct 64 ms 71544 KB Output is correct
55 Correct 63 ms 71592 KB Output is correct
56 Correct 64 ms 71544 KB Output is correct
57 Correct 67 ms 71288 KB Output is correct
58 Correct 68 ms 71288 KB Output is correct
59 Correct 69 ms 71288 KB Output is correct
60 Correct 68 ms 71288 KB Output is correct
61 Correct 66 ms 71500 KB Output is correct
62 Correct 73 ms 71544 KB Output is correct
63 Correct 63 ms 71544 KB Output is correct
64 Correct 65 ms 71416 KB Output is correct
65 Correct 64 ms 71288 KB Output is correct
66 Correct 64 ms 71288 KB Output is correct
67 Correct 66 ms 71288 KB Output is correct
68 Correct 57 ms 70648 KB Output is correct
69 Correct 62 ms 71544 KB Output is correct
70 Correct 62 ms 71544 KB Output is correct
71 Correct 62 ms 71416 KB Output is correct
72 Correct 68 ms 71292 KB Output is correct
73 Correct 66 ms 71288 KB Output is correct
74 Correct 63 ms 71416 KB Output is correct
75 Correct 63 ms 71416 KB Output is correct
76 Correct 64 ms 71416 KB Output is correct
77 Correct 64 ms 71416 KB Output is correct
78 Correct 62 ms 71416 KB Output is correct
79 Correct 64 ms 71416 KB Output is correct
80 Correct 63 ms 71416 KB Output is correct
81 Correct 63 ms 71424 KB Output is correct
82 Correct 64 ms 71416 KB Output is correct
83 Correct 64 ms 71416 KB Output is correct
84 Correct 63 ms 71416 KB Output is correct
85 Correct 62 ms 71416 KB Output is correct
86 Correct 64 ms 71416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 70008 KB Output is correct
2 Correct 54 ms 70008 KB Output is correct
3 Correct 58 ms 70008 KB Output is correct
4 Correct 60 ms 70136 KB Output is correct
5 Correct 60 ms 70264 KB Output is correct
6 Correct 61 ms 70136 KB Output is correct
7 Correct 59 ms 70152 KB Output is correct
8 Correct 54 ms 70136 KB Output is correct
9 Correct 54 ms 70144 KB Output is correct
10 Correct 54 ms 70136 KB Output is correct
11 Correct 59 ms 70264 KB Output is correct
12 Correct 58 ms 70264 KB Output is correct
13 Correct 59 ms 70264 KB Output is correct
14 Correct 59 ms 70264 KB Output is correct
15 Correct 60 ms 70272 KB Output is correct
16 Correct 59 ms 70136 KB Output is correct
17 Correct 59 ms 70240 KB Output is correct
18 Correct 59 ms 70136 KB Output is correct
19 Correct 59 ms 70264 KB Output is correct
20 Correct 60 ms 70264 KB Output is correct
21 Correct 58 ms 70236 KB Output is correct
22 Correct 58 ms 70136 KB Output is correct
23 Correct 58 ms 70136 KB Output is correct
24 Correct 58 ms 70136 KB Output is correct
25 Correct 58 ms 70136 KB Output is correct
26 Correct 54 ms 70136 KB Output is correct
27 Correct 58 ms 70268 KB Output is correct
28 Correct 58 ms 70264 KB Output is correct
29 Correct 59 ms 70136 KB Output is correct
30 Correct 59 ms 70136 KB Output is correct
31 Correct 62 ms 70144 KB Output is correct
32 Correct 59 ms 70136 KB Output is correct
33 Correct 59 ms 70136 KB Output is correct
34 Correct 58 ms 70136 KB Output is correct
35 Correct 60 ms 70136 KB Output is correct
36 Correct 61 ms 70140 KB Output is correct
37 Correct 58 ms 70144 KB Output is correct
38 Correct 59 ms 70144 KB Output is correct
39 Correct 58 ms 70136 KB Output is correct
40 Correct 59 ms 70136 KB Output is correct
41 Correct 59 ms 70136 KB Output is correct
42 Correct 58 ms 70136 KB Output is correct
43 Correct 60 ms 70136 KB Output is correct
44 Correct 59 ms 70176 KB Output is correct
45 Correct 59 ms 70392 KB Output is correct
46 Correct 67 ms 71416 KB Output is correct
47 Correct 67 ms 71288 KB Output is correct
48 Correct 66 ms 71288 KB Output is correct
49 Correct 58 ms 70648 KB Output is correct
50 Correct 57 ms 70648 KB Output is correct
51 Correct 60 ms 70648 KB Output is correct
52 Correct 63 ms 71544 KB Output is correct
53 Correct 65 ms 71544 KB Output is correct
54 Correct 64 ms 71544 KB Output is correct
55 Correct 63 ms 71592 KB Output is correct
56 Correct 64 ms 71544 KB Output is correct
57 Correct 67 ms 71288 KB Output is correct
58 Correct 68 ms 71288 KB Output is correct
59 Correct 69 ms 71288 KB Output is correct
60 Correct 68 ms 71288 KB Output is correct
61 Correct 66 ms 71500 KB Output is correct
62 Correct 73 ms 71544 KB Output is correct
63 Correct 63 ms 71544 KB Output is correct
64 Correct 65 ms 71416 KB Output is correct
65 Correct 64 ms 71288 KB Output is correct
66 Correct 64 ms 71288 KB Output is correct
67 Correct 66 ms 71288 KB Output is correct
68 Correct 57 ms 70648 KB Output is correct
69 Correct 62 ms 71544 KB Output is correct
70 Correct 62 ms 71544 KB Output is correct
71 Correct 62 ms 71416 KB Output is correct
72 Correct 68 ms 71292 KB Output is correct
73 Correct 66 ms 71288 KB Output is correct
74 Correct 63 ms 71416 KB Output is correct
75 Correct 63 ms 71416 KB Output is correct
76 Correct 64 ms 71416 KB Output is correct
77 Correct 64 ms 71416 KB Output is correct
78 Correct 62 ms 71416 KB Output is correct
79 Correct 64 ms 71416 KB Output is correct
80 Correct 63 ms 71416 KB Output is correct
81 Correct 63 ms 71424 KB Output is correct
82 Correct 64 ms 71416 KB Output is correct
83 Correct 64 ms 71416 KB Output is correct
84 Correct 63 ms 71416 KB Output is correct
85 Correct 62 ms 71416 KB Output is correct
86 Correct 64 ms 71416 KB Output is correct
87 Correct 79 ms 73336 KB Output is correct
88 Correct 128 ms 79988 KB Output is correct
89 Correct 397 ms 103020 KB Output is correct
90 Correct 388 ms 105064 KB Output is correct
91 Correct 399 ms 105324 KB Output is correct
92 Correct 171 ms 86764 KB Output is correct
93 Correct 171 ms 86764 KB Output is correct
94 Correct 169 ms 86888 KB Output is correct
95 Correct 219 ms 110060 KB Output is correct
96 Correct 231 ms 110444 KB Output is correct
97 Correct 225 ms 110444 KB Output is correct
98 Correct 247 ms 110492 KB Output is correct
99 Correct 230 ms 109036 KB Output is correct
100 Correct 483 ms 96640 KB Output is correct
101 Correct 514 ms 97004 KB Output is correct
102 Correct 523 ms 96992 KB Output is correct
103 Correct 522 ms 96876 KB Output is correct
104 Correct 258 ms 108912 KB Output is correct
105 Correct 246 ms 108904 KB Output is correct
106 Correct 249 ms 108884 KB Output is correct
107 Correct 362 ms 103916 KB Output is correct
108 Correct 371 ms 104172 KB Output is correct
109 Correct 393 ms 104428 KB Output is correct
110 Correct 159 ms 85868 KB Output is correct
111 Correct 218 ms 110056 KB Output is correct
112 Correct 208 ms 109548 KB Output is correct
113 Correct 219 ms 108008 KB Output is correct
114 Correct 482 ms 96748 KB Output is correct
115 Correct 502 ms 95976 KB Output is correct
116 Correct 232 ms 107884 KB Output is correct
117 Correct 234 ms 106604 KB Output is correct
118 Correct 241 ms 105960 KB Output is correct
119 Correct 253 ms 105732 KB Output is correct
120 Correct 222 ms 105836 KB Output is correct
121 Correct 228 ms 105196 KB Output is correct
122 Correct 227 ms 104940 KB Output is correct
123 Correct 252 ms 106604 KB Output is correct
124 Correct 255 ms 106088 KB Output is correct
125 Correct 251 ms 105836 KB Output is correct
126 Correct 241 ms 106092 KB Output is correct
127 Correct 240 ms 105324 KB Output is correct
128 Correct 240 ms 104936 KB Output is correct