Submission #731248

# Submission time Handle Problem Language Result Execution time Memory
731248 2023-04-27T08:15:41 Z danikoynov Sky Walking (IOI19_walk) C++14
33 / 100
208 ms 23288 KB
#include "walk.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 10;

struct skywalk
{
    int left, right;
    ll height;

    skywalk(int _left = 0, int _right = 0, ll _height = 0)
    {
        left = _left;
        right = _right;
        height = _height;
    }
}sky[maxn];

bool cmp(const skywalk &sk1, const skywalk &sk2)
{
    return sk1.height < sk2.height;
}

const ll inf = 1e18;
struct node
{
    ll down_sum, up_sum;

    node(ll _down_sum = inf, ll _up_sum = inf)
    {
        down_sum = _down_sum;
        up_sum = _up_sum;
    }
};

node merge_nodes(node lf, node rf)
{
    node nd;
    nd.down_sum = min(lf.down_sum, rf.down_sum);
    nd.up_sum = min(lf.up_sum, rf.up_sum);
    return nd;
}

node tree[4 * maxn];
int is_active[maxn];
ll dp[maxn];
void toggle(int root, int left, int right, int pos)
{
    if (left == right)
    {
        is_active[left] ^= 1;
        ///cout << root << " : " << left << " : " << right << " : " << pos << " " << is_active[left] << endl;
        if (is_active[left])
        {
            tree[root].down_sum = - sky[left].height + dp[left];
            tree[root].up_sum = sky[left].height + dp[left];
            ///cout << "activate " << left << " " << sky[left].height << endl;
        }
        else
        {
            tree[root] = node();
        }
        return;
    }

    int mid = (left + right) / 2;

    if (pos <= mid)
        toggle(root * 2, left, mid, pos);
    else
        toggle(root * 2 + 1, mid + 1, right, pos);


    tree[root] = merge_nodes(tree[root * 2], tree[root * 2 + 1]);
    ///cout << "here " << left << " " << right << " " << tree[root].up_sum << endl;
}

node query(int root, int left, int right, int qleft, int qright)
{
    if (left > qright || right < qleft)
    return node();

    if (left >= qleft && right <= qright)
        return tree[root];

    int mid = (left + right) / 2;
    return merge_nodes(query(root * 2, left, mid, qleft, qright),
                       query(root * 2 + 1, mid + 1, right, qleft, qright));
}

int n, m;
vector < pair < int, int > > upd[maxn];
long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g)
{
    n = x.size();
    m = l.size();
    for (int i = 0; i < m; i ++)
    {
        sky[i] = skywalk(l[i], r[i], y[i]);
    }
    sort(sky, sky + m, cmp);
    if (s != 0 || g != n - 1)
        return 0;

         for (int i = 0; i < m; i ++)
        {
            upd[sky[i].left].push_back({i, 1});
        }
        for (int i = 0; i < m; i ++)
        {

            upd[sky[i].right].push_back({i, -1});
        }



    for (pair < int, int > cur : upd[s])
    {
        dp[cur.first] = sky[cur.first].height;
        toggle(1, 0, m - 1, cur.first);
        ///cout << "here" << endl;
    }


    ///cout << tree[1].up_sum << endl;
    for (int i = 1; i < g; i ++)
    {
        for (pair < int, int > cur : upd[i])
        {

            if (cur.second == 1)
            {
                node under = node(), above = node();
                if (cur.first != 0)
                    under = query(1, 0, m - 1, 0, cur.first - 1);
                if (cur.first != m - 1)
                    above = query(1, 0, m - 1, cur.first + 1, m - 1);
            //cout << under.down_sum << " " << above.up_sum << endl;

                if (under.down_sum == inf && above.up_sum == inf)
                    dp[cur.first] = inf;
                else
                    dp[cur.first] = min(sky[cur.first].height + under.down_sum, above.up_sum - sky[cur.first].height);
                ///cout << cur.first << " :: " << dp[cur.first] << endl;
            }


            toggle(1, 0, m - 1, cur.first);
        }
    }

    node ans = tree[1];
    if (ans.up_sum == inf)
        return -1;
    return ans.up_sum + x[g] - x[s];
}
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 10452 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 10452 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 62 ms 13712 KB Output is correct
2 Correct 122 ms 18060 KB Output is correct
3 Correct 135 ms 19020 KB Output is correct
4 Correct 176 ms 23036 KB Output is correct
5 Correct 181 ms 22860 KB Output is correct
6 Correct 195 ms 23112 KB Output is correct
7 Correct 100 ms 19048 KB Output is correct
8 Correct 184 ms 22740 KB Output is correct
9 Correct 172 ms 23288 KB Output is correct
10 Correct 127 ms 21360 KB Output is correct
11 Correct 21 ms 12008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 13712 KB Output is correct
2 Correct 122 ms 18060 KB Output is correct
3 Correct 135 ms 19020 KB Output is correct
4 Correct 176 ms 23036 KB Output is correct
5 Correct 181 ms 22860 KB Output is correct
6 Correct 195 ms 23112 KB Output is correct
7 Correct 100 ms 19048 KB Output is correct
8 Correct 184 ms 22740 KB Output is correct
9 Correct 172 ms 23288 KB Output is correct
10 Correct 127 ms 21360 KB Output is correct
11 Correct 21 ms 12008 KB Output is correct
12 Correct 129 ms 18884 KB Output is correct
13 Correct 194 ms 23088 KB Output is correct
14 Correct 165 ms 22764 KB Output is correct
15 Correct 168 ms 23024 KB Output is correct
16 Correct 208 ms 23020 KB Output is correct
17 Correct 157 ms 23116 KB Output is correct
18 Correct 158 ms 23004 KB Output is correct
19 Correct 165 ms 23244 KB Output is correct
20 Correct 85 ms 18928 KB Output is correct
21 Correct 27 ms 14084 KB Output is correct
22 Correct 130 ms 21600 KB Output is correct
23 Correct 141 ms 21896 KB Output is correct
24 Correct 137 ms 22072 KB Output is correct
25 Correct 141 ms 21756 KB Output is correct
26 Correct 133 ms 22220 KB Output is correct
27 Correct 164 ms 22924 KB Output is correct
28 Correct 179 ms 23116 KB Output is correct
29 Correct 167 ms 22880 KB Output is correct
30 Correct 102 ms 19140 KB Output is correct
31 Correct 155 ms 23152 KB Output is correct
32 Correct 134 ms 21536 KB Output is correct
33 Correct 134 ms 21736 KB Output is correct
34 Correct 116 ms 21872 KB Output is correct
35 Correct 142 ms 22088 KB Output is correct
36 Correct 162 ms 22004 KB Output is correct
37 Correct 153 ms 21416 KB Output is correct
38 Correct 153 ms 21852 KB Output is correct
39 Correct 118 ms 22036 KB Output is correct
40 Correct 149 ms 21836 KB Output is correct
41 Correct 145 ms 21636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 10452 KB Output isn't correct
2 Halted 0 ms 0 KB -