답안 #212740

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
212740 2020-03-24T08:37:07 Z Alexa2001 별자리 3 (JOI20_constellation3) C++17
100 / 100
408 ms 63224 KB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 2e5 + 5;

typedef long long ll;
const ll inf = 1e18;

int i, root, L[Nmax], R[Nmax], t[Nmax], H[Nmax];
multiset<pair<int,ll>> v[Nmax];

ll lazy[Nmax];

int n, m;



void cartesian_tree()
{
    int i;

    t[1] = 0; root = 1;
    for(i=2; i<=n; ++i)
    {
        int node = i-1;
        while(t[node] && H[node] < H[i])
            node = t[node];

        if(H[node] >= H[i])
        {
            if(R[node])
            {
                L[i] = R[node];
                t[L[i]] = i;
            }

            R[node] = i;
            t[i] = node;
        }
        else
        {
            root = i;
            L[i] = node;
            t[i] = 0;
            t[node] = i;
        }
    }
}

void Merge(int node, int son, int H)
{
    /*
    cerr << "#\n";
    for(auto it : v[node]) cerr << it.first << ' ' << it.second << endl;
    cerr << "#\n";
    for(auto it : v[son]) cerr << it.first << ' ' << it.second << endl;
    cerr << "#\n";
    */

    ll val1 = inf, val2 = inf;
    while(v[son].size() && v[son].begin()->first <= H)
    {
        val1 = min(val1, v[son].begin()->second + lazy[son]);
        v[son].erase(v[son].begin());
    }

    while(v[node].size() && v[node].begin()->first <= H)
    {
        val2 = min(val2, v[node].begin()->second + lazy[node]);
        v[node].erase(v[node].begin());
    }

    if(v[node].size() < v[son].size())
    {
        swap(v[node], v[son]);
        swap(lazy[node], lazy[son]);
        swap(val1, val2);
    }

    assert(val1 != inf && val2 != inf);
    if(val1 == inf)
    {
        v[node].insert({0, val2 - lazy[node]});
        return;
    }

    lazy[node] += val1;
    for(auto it : v[son])
        v[node].insert({it.first, it.second + lazy[son] - lazy[node] + val2});

    v[node].insert({0, val1 + val2 - lazy[node]});

   // for(auto it : v[node]) cerr << it.first << ' ' << it.second + lazy[node] << endl;
}

void solve(int node)
{
    int son1 = L[node], son2 = R[node];
    if(!son1 && !son2) return;

    if(son1) solve(son1);
    if(son2) solve(son2);

    if(son1) Merge(node, son1, H[node]);
    if(son2) Merge(node, son2, H[node]);
}

int main()
{
   // freopen("input", "r", stdin);
    cin.sync_with_stdio(false); cin.tie(0);

    cin >> n;

    int i;
    for(i=1; i<=n; ++i) cin >> H[i];

    cartesian_tree();

    cin >> m;
    for(i=1; i<=m; ++i)
    {
        int x, y, cost;
        cin >> x >> y >> cost;
        v[x].insert({y, cost});
    }

    for(i=1; i<=n; ++i)
    {
        ll sum = 0;
        for(auto it : v[i])
            sum += it.second;

        multiset<pair<int, ll>> S;
        for(auto it : v[i])
            S.insert({it.first, sum - it.second});

        S.insert({0, sum});

        swap(S, v[i]);
        lazy[i] = 0;
    }

    solve(root);

    ll ans = inf;
    for(auto it : v[root])
        ans = min(ans, it.second);
    cout << ans + lazy[root] << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 ms 9856 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 11 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 11 ms 9856 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 11 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 10 ms 9856 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 10 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 ms 9856 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 11 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 11 ms 9856 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 11 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 10 ms 9856 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 10 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
23 Correct 12 ms 10112 KB Output is correct
24 Correct 12 ms 10112 KB Output is correct
25 Correct 12 ms 10112 KB Output is correct
26 Correct 12 ms 10112 KB Output is correct
27 Correct 12 ms 10112 KB Output is correct
28 Correct 12 ms 10112 KB Output is correct
29 Correct 12 ms 10112 KB Output is correct
30 Correct 12 ms 10112 KB Output is correct
31 Correct 12 ms 10112 KB Output is correct
32 Correct 12 ms 10112 KB Output is correct
33 Correct 12 ms 10112 KB Output is correct
34 Correct 12 ms 10112 KB Output is correct
35 Correct 12 ms 10112 KB Output is correct
36 Correct 12 ms 10112 KB Output is correct
37 Correct 12 ms 10240 KB Output is correct
38 Correct 11 ms 10240 KB Output is correct
39 Correct 13 ms 10112 KB Output is correct
40 Correct 12 ms 10112 KB Output is correct
41 Correct 13 ms 10112 KB Output is correct
42 Correct 12 ms 10112 KB Output is correct
43 Correct 13 ms 10112 KB Output is correct
44 Correct 12 ms 10112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9856 KB Output is correct
2 Correct 10 ms 9856 KB Output is correct
3 Correct 10 ms 9856 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 11 ms 9856 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9856 KB Output is correct
8 Correct 11 ms 9856 KB Output is correct
9 Correct 10 ms 9856 KB Output is correct
10 Correct 10 ms 9856 KB Output is correct
11 Correct 10 ms 9856 KB Output is correct
12 Correct 11 ms 9856 KB Output is correct
13 Correct 10 ms 9856 KB Output is correct
14 Correct 11 ms 9856 KB Output is correct
15 Correct 11 ms 9856 KB Output is correct
16 Correct 10 ms 9856 KB Output is correct
17 Correct 10 ms 9856 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 10 ms 9856 KB Output is correct
20 Correct 10 ms 9856 KB Output is correct
21 Correct 10 ms 9856 KB Output is correct
22 Correct 10 ms 9856 KB Output is correct
23 Correct 12 ms 10112 KB Output is correct
24 Correct 12 ms 10112 KB Output is correct
25 Correct 12 ms 10112 KB Output is correct
26 Correct 12 ms 10112 KB Output is correct
27 Correct 12 ms 10112 KB Output is correct
28 Correct 12 ms 10112 KB Output is correct
29 Correct 12 ms 10112 KB Output is correct
30 Correct 12 ms 10112 KB Output is correct
31 Correct 12 ms 10112 KB Output is correct
32 Correct 12 ms 10112 KB Output is correct
33 Correct 12 ms 10112 KB Output is correct
34 Correct 12 ms 10112 KB Output is correct
35 Correct 12 ms 10112 KB Output is correct
36 Correct 12 ms 10112 KB Output is correct
37 Correct 12 ms 10240 KB Output is correct
38 Correct 11 ms 10240 KB Output is correct
39 Correct 13 ms 10112 KB Output is correct
40 Correct 12 ms 10112 KB Output is correct
41 Correct 13 ms 10112 KB Output is correct
42 Correct 12 ms 10112 KB Output is correct
43 Correct 13 ms 10112 KB Output is correct
44 Correct 12 ms 10112 KB Output is correct
45 Correct 271 ms 44640 KB Output is correct
46 Correct 271 ms 44152 KB Output is correct
47 Correct 280 ms 44536 KB Output is correct
48 Correct 274 ms 44380 KB Output is correct
49 Correct 269 ms 43896 KB Output is correct
50 Correct 271 ms 43768 KB Output is correct
51 Correct 274 ms 44024 KB Output is correct
52 Correct 277 ms 44804 KB Output is correct
53 Correct 273 ms 44408 KB Output is correct
54 Correct 394 ms 51064 KB Output is correct
55 Correct 408 ms 49016 KB Output is correct
56 Correct 387 ms 47608 KB Output is correct
57 Correct 404 ms 47480 KB Output is correct
58 Correct 372 ms 49400 KB Output is correct
59 Correct 388 ms 49776 KB Output is correct
60 Correct 166 ms 52600 KB Output is correct
61 Correct 376 ms 59384 KB Output is correct
62 Correct 387 ms 51448 KB Output is correct
63 Correct 373 ms 58620 KB Output is correct
64 Correct 354 ms 57592 KB Output is correct
65 Correct 374 ms 51320 KB Output is correct
66 Correct 382 ms 63224 KB Output is correct