Submission #1067666

# Submission time Handle Problem Language Result Execution time Memory
1067666 2024-08-20T23:41:51 Z jerzyk Sky Walking (IOI19_walk) C++17
100 / 100
711 ms 193588 KB
#include <bits/stdc++.h>
#include "walk.h"

using namespace std;
#define pb push_back
#define st first
#define nd second
typedef long long ll;
typedef long double ld;
const ll I = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL;
const int II = 2 * 1000 * 1000 * 1000;
const ll M = 1000LL * 1000LL * 1000LL + 7LL;
const int N = 1<<18;
const int NN = 1<<20;
int tab[NN], hei[NN];
int drz[2 * N], drz2[2 * N], drz3[2 * N];

int cnt = 0;
pair<int, int> co[NN];
vector<pair<int, ll>> ed[NN];
ll dis[NN];
pair<pair<int, int>, int> wal[NN];
bool vis[NN];

vector<int> poi[NN];
vector<int> sky[NN];


void Sett(int a, int b, int x)
{
    a += N - 1; b += N + 1;
    while(a / 2 != b / 2)
    {
        if(a % 2 == 0) drz3[a + 1] = x;
        if(b % 2 == 1) drz3[b - 1] = x;
        a /= 2;
        b /= 2;
    }
}

int Ma(int v)
{
    v += N;
    int ans = drz3[v];
    while(v > 0)
        {ans = max(ans, drz3[v]); v /= 2;}
    return ans;
}

void DoDrz(int n)
{
    for(int i = 1; i <= n; ++i)
        drz[i + N] = hei[i];
    for(int i = 1; i <= n; ++i)
        drz2[i + N] = 1;
    for(int i = N - 1; i >= 1; --i)
        drz[i] = max(drz[i * 2], drz[i * 2 + 1]);
    for(int i = N - 1; i >= 1; --i)
        drz2[i] = drz2[i * 2] + drz2[i * 2 + 1];
}

int Find(int a, int b, int x, int r)
{
    a += N - 1; b += N + 1;
    int a1 = 0, a2 = 0, b1 = 0, b2 = 0;
    while(a / 2 != b / 2)
    {
        if(a % 2 == 0 && drz[a + 1] >= x)
        {
            if(a1 == 0)
                a1 = a + 1;
            a2 = a + 1;
        }
        if(b % 2 == 1 && drz[b - 1] >= x)
        {
            if(b1 == 0)
                b1 = b - 1;

            b2 = b - 1;
        }
        a /= 2; b /= 2;
    }
    if(r == 0)
        a = a1;
    if(a == 0)
        a = b2;
    if(r == 1)
        a = b1;
    if(a == 0)
        a = a2;
    while(a < N)
    {
        a = a * 2 + r;
        if(drz[a] < x) a ^= 1;
    }
    return a - N;
}

bool C(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b)
{
    return (make_pair(a.nd, a.st) < make_pair(b.nd, b.st));
}

void Clr(int v, int a, int b, int pz, int kz, int id)
{
    if(drz2[v] <= 0 || a > kz || b < pz) return;
    if(v >= N)
    {
        v -= N;
        //cerr << "clr: " << v << " " << id << "\n";
        if(hei[v] >= wal[id].nd)
            sky[v].pb(id);
        --drz2[v + N];
        return;
    }
    Clr(v * 2, a, (a + b) / 2, pz, kz, id);
    Clr(v * 2 + 1, (a + b) / 2 + 1, b, pz, kz, id);
    drz2[v] = drz2[v * 2] + drz2[v * 2 + 1];
}

inline ll D(int i, int j)
{
    ll d1 = max(co[i].st - co[j].st, co[j].st - co[i].st);
    ll d2 = max(co[i].nd - co[j].nd, co[j].nd - co[i].nd);
    return d1 + d2;
}

inline void A(int x, int y)
{
    ll d = D(x, y);
    ed[x].pb(make_pair(y, d)); ed[y].pb(make_pair(x, d));
}

set<pair<int, int>> inter;
set<pair<int, int>>::iterator it;

bool IT(pair<int, int> a, pair<int, int> b)
{
    return (max(a.st, b.st) <= min(a.nd, b.nd));
}

pair<int, int> DI(pair<int, int> a, pair<int, int> b)
{
    return make_pair(min(a.st, b.st), max(a.nd, b.nd));
}

void Add(pair<int, int> a)
{
    it = inter.lower_bound(a);
    if(it != inter.end() && IT(*it, a))
    {
        a = DI(a, *it); inter.erase(it);
    }
    it = inter.lower_bound(a);
    if(it != inter.begin())
    {
        --it;
        if(IT(*it, a))
        {
            a = DI(a, *it); inter.erase(it);
        }
    }
    inter.insert(a);
}

void Do(int &k)
{
    vector<pair<pair<int, int>, int>> nxt;
    for(int i = 1; i <= k; ++i)
    {
        Add(wal[i].st);
        //cerr << "xd " << i << "\n";
        if(i == k || wal[i].nd != wal[i + 1].nd)
        {
            for(it = inter.begin(); it != inter.end(); ++it)
                nxt.pb(make_pair(*it, wal[i].nd));
            inter.clear();
        }
    }
    k = nxt.size();
    for(int i = 1; i <= k; ++i)
        wal[i] = nxt[i - 1];
}

void SanInput(int n, int &k, int u, int v)
{
    int dod = 0;
    sort(wal + 1, wal + 1 + k, C);
    Do(k);
    for(int i = 1; i <= k; ++i)
    {
        //cerr << "skypath: " << i << " " << wal[i].st.st << " " << wal[i].st.nd << " " << wal[i].nd << "\n";
        vector<int> cur = {wal[i].st.st, wal[i].st.nd}, hl;
        if(wal[i].st.st < u && wal[i].st.nd > u)
        {
            int v1 = Find(1, u, wal[i].nd, 1), v2 = Find(u, n, wal[i].nd, 0);
            cur.pb(v1); cur.pb(v2);
        }
        if(wal[i].st.st < v && wal[i].st.nd > v)
        {
            int v1 = Find(1, v, wal[i].nd, 1), v2 = Find(v, n, wal[i].nd, 0);
            cur.pb(v1); cur.pb(v2);
        }
        sort(cur.begin(), cur.end());

        for(int j = 0; j < (int)cur.size(); ++j)
            if(j == 0 || cur[j] != cur[j - 1]) hl.pb(cur[j]);
        cur = hl;
        wal[i].st.nd = cur[1];

        for(int j = 1; j < (int)cur.size() - 1; ++j)
        {
            ++dod; 
            wal[k + dod] = make_pair(make_pair(cur[j], cur[j + 1]), wal[i].nd);
        }
    }
    k += dod;
    sort(wal + 1, wal + 1 + k, C);
    for(int i = 1; i <= k; ++i)
    {
        Clr(1, 0, N - 1, wal[i].st.st, wal[i].st.nd, i);

        int a1 = Ma(wal[i].st.st), a2 = Ma(wal[i].st.nd);
        sky[wal[i].st.st].pb(i);
        if(a1 != 0)
            sky[wal[i].st.st].pb(a1);
        sky[wal[i].st.nd].pb(i);
        if(a2 != 0)
            sky[wal[i].st.nd].pb(a2);
        Sett(wal[i].st.st, wal[i].st.nd, i);
    }

    cnt = n;
    for(int i = 1; i <= n; ++i)
    {
        vector<int> hlp;
        sort(sky[i].begin(), sky[i].end());
        for(int j = 0; j < (int)sky[i].size(); ++j)
            if(j == 0 || sky[i][j] != sky[i][j - 1]) hlp.pb(sky[i][j]);
        sky[i] = hlp;
        co[i] = make_pair(tab[i], 0);
        int pr = i;
        //cerr << "Sky: " << i << " " << "\n";
        for(int j = 0; j < (int)sky[i].size(); ++j)
        {
            if(j != 0) pr = cnt;
            if(j == 0 || wal[sky[i][j]].nd != wal[sky[i][j - 1]].nd)
            {++cnt; co[cnt] = make_pair(tab[i], wal[sky[i][j]].nd); A(cnt, pr);}
            poi[sky[i][j]].pb(cnt);
            //cerr << sky[i][j] << " ";
        }
        //cerr << "\n";
    }
    for(int i = 1; i <= k; ++i)
        for(int j = 1; j < (int)poi[i].size(); ++j)
            A(poi[i][j - 1], poi[i][j]);
}

void Dijkstra(int s)
{
    int n = cnt;
    priority_queue<pair<ll, int>> q;
    for(int i = 1; i <= n; ++i) dis[i] = I;
    dis[s] = 0LL;
    q.push(make_pair(0LL, s));
    while((int)q.size() > 0)
    {
        int v = q.top().nd; q.pop();
        if(vis[v]) continue;
        vis[v] = true;
        //cerr << "Dijkstra: " << v << " " << co[v].st << " " << co[v].nd << " " << dis[v] << "\n";
        for(int i = 0; i < (int)ed[v].size(); ++i)
        {
            //cout << "ed: " << ed[v][i].st << " " << ed[v][i].nd << "\n";
            ll d = dis[v] + ed[v][i].nd;
            if(d < dis[ed[v][i].st])
            {
                dis[ed[v][i].st] = d;
                q.push(make_pair(-d, ed[v][i].st));
            }
        }
    }
}

long long min_distance(vector<int> _x, vector<int> _h, vector<int> _l, vector<int> _r, vector<int> _y, int _s, int _g)
{
    int u = _s + 1, v = _g + 1;
    int n = _x.size(), k = _l.size();
    for(int i = 1; i <= n; ++i)
        {tab[i] = _x[i - 1]; hei[i] = _h[i - 1]; ++cnt;}
    for(int i = 1; i <= k; ++i)
        wal[i] = make_pair(make_pair(_l[i - 1] + 1, _r[i - 1] + 1), _y[i - 1]);
    DoDrz(n);
    SanInput(n, k, u, v);
    Dijkstra(u);
    if(dis[v] == I) return -1;
    return dis[v];
}
# Verdict Execution time Memory Grader output
1 Correct 35 ms 76380 KB Output is correct
2 Correct 34 ms 76376 KB Output is correct
3 Correct 35 ms 76220 KB Output is correct
4 Correct 35 ms 76372 KB Output is correct
5 Correct 33 ms 76380 KB Output is correct
6 Correct 35 ms 76368 KB Output is correct
7 Correct 35 ms 76368 KB Output is correct
8 Correct 34 ms 76376 KB Output is correct
9 Correct 34 ms 76384 KB Output is correct
10 Correct 34 ms 76376 KB Output is correct
11 Correct 33 ms 76228 KB Output is correct
12 Correct 36 ms 76372 KB Output is correct
13 Correct 32 ms 76372 KB Output is correct
14 Correct 32 ms 76380 KB Output is correct
15 Correct 35 ms 76372 KB Output is correct
16 Correct 37 ms 76380 KB Output is correct
17 Correct 36 ms 76380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 76380 KB Output is correct
2 Correct 34 ms 76380 KB Output is correct
3 Correct 301 ms 124768 KB Output is correct
4 Correct 388 ms 139828 KB Output is correct
5 Correct 238 ms 128084 KB Output is correct
6 Correct 214 ms 125628 KB Output is correct
7 Correct 235 ms 128212 KB Output is correct
8 Correct 313 ms 126512 KB Output is correct
9 Correct 277 ms 133600 KB Output is correct
10 Correct 439 ms 142380 KB Output is correct
11 Correct 288 ms 122160 KB Output is correct
12 Correct 247 ms 116156 KB Output is correct
13 Correct 396 ms 143152 KB Output is correct
14 Correct 252 ms 114652 KB Output is correct
15 Correct 180 ms 117188 KB Output is correct
16 Correct 191 ms 117788 KB Output is correct
17 Correct 185 ms 115572 KB Output is correct
18 Correct 144 ms 119092 KB Output is correct
19 Correct 40 ms 78136 KB Output is correct
20 Correct 114 ms 96144 KB Output is correct
21 Correct 176 ms 114728 KB Output is correct
22 Correct 176 ms 116776 KB Output is correct
23 Correct 221 ms 127388 KB Output is correct
24 Correct 178 ms 117128 KB Output is correct
25 Correct 191 ms 114332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 81232 KB Output is correct
2 Correct 284 ms 128296 KB Output is correct
3 Correct 320 ms 132404 KB Output is correct
4 Correct 487 ms 154664 KB Output is correct
5 Correct 542 ms 157216 KB Output is correct
6 Correct 490 ms 153644 KB Output is correct
7 Correct 286 ms 127348 KB Output is correct
8 Correct 231 ms 116012 KB Output is correct
9 Correct 494 ms 149300 KB Output is correct
10 Correct 196 ms 127368 KB Output is correct
11 Correct 43 ms 80716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 81232 KB Output is correct
2 Correct 284 ms 128296 KB Output is correct
3 Correct 320 ms 132404 KB Output is correct
4 Correct 487 ms 154664 KB Output is correct
5 Correct 542 ms 157216 KB Output is correct
6 Correct 490 ms 153644 KB Output is correct
7 Correct 286 ms 127348 KB Output is correct
8 Correct 231 ms 116012 KB Output is correct
9 Correct 494 ms 149300 KB Output is correct
10 Correct 196 ms 127368 KB Output is correct
11 Correct 43 ms 80716 KB Output is correct
12 Correct 344 ms 132400 KB Output is correct
13 Correct 393 ms 152372 KB Output is correct
14 Correct 519 ms 156852 KB Output is correct
15 Correct 403 ms 137728 KB Output is correct
16 Correct 425 ms 145980 KB Output is correct
17 Correct 465 ms 153548 KB Output is correct
18 Correct 382 ms 137680 KB Output is correct
19 Correct 412 ms 145964 KB Output is correct
20 Correct 279 ms 126588 KB Output is correct
21 Correct 92 ms 103628 KB Output is correct
22 Correct 277 ms 142084 KB Output is correct
23 Correct 265 ms 139700 KB Output is correct
24 Correct 240 ms 124344 KB Output is correct
25 Correct 274 ms 134636 KB Output is correct
26 Correct 188 ms 114852 KB Output is correct
27 Correct 486 ms 156204 KB Output is correct
28 Correct 333 ms 152116 KB Output is correct
29 Correct 472 ms 151976 KB Output is correct
30 Correct 289 ms 126956 KB Output is correct
31 Correct 443 ms 147900 KB Output is correct
32 Correct 181 ms 120368 KB Output is correct
33 Correct 182 ms 122144 KB Output is correct
34 Correct 212 ms 126384 KB Output is correct
35 Correct 226 ms 126492 KB Output is correct
36 Correct 209 ms 118388 KB Output is correct
37 Correct 178 ms 114772 KB Output is correct
38 Correct 182 ms 116756 KB Output is correct
39 Correct 221 ms 127296 KB Output is correct
40 Correct 178 ms 117040 KB Output is correct
41 Correct 184 ms 114500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 76380 KB Output is correct
2 Correct 34 ms 76376 KB Output is correct
3 Correct 35 ms 76220 KB Output is correct
4 Correct 35 ms 76372 KB Output is correct
5 Correct 33 ms 76380 KB Output is correct
6 Correct 35 ms 76368 KB Output is correct
7 Correct 35 ms 76368 KB Output is correct
8 Correct 34 ms 76376 KB Output is correct
9 Correct 34 ms 76384 KB Output is correct
10 Correct 34 ms 76376 KB Output is correct
11 Correct 33 ms 76228 KB Output is correct
12 Correct 36 ms 76372 KB Output is correct
13 Correct 32 ms 76372 KB Output is correct
14 Correct 32 ms 76380 KB Output is correct
15 Correct 35 ms 76372 KB Output is correct
16 Correct 37 ms 76380 KB Output is correct
17 Correct 36 ms 76380 KB Output is correct
18 Correct 34 ms 76380 KB Output is correct
19 Correct 34 ms 76380 KB Output is correct
20 Correct 301 ms 124768 KB Output is correct
21 Correct 388 ms 139828 KB Output is correct
22 Correct 238 ms 128084 KB Output is correct
23 Correct 214 ms 125628 KB Output is correct
24 Correct 235 ms 128212 KB Output is correct
25 Correct 313 ms 126512 KB Output is correct
26 Correct 277 ms 133600 KB Output is correct
27 Correct 439 ms 142380 KB Output is correct
28 Correct 288 ms 122160 KB Output is correct
29 Correct 247 ms 116156 KB Output is correct
30 Correct 396 ms 143152 KB Output is correct
31 Correct 252 ms 114652 KB Output is correct
32 Correct 180 ms 117188 KB Output is correct
33 Correct 191 ms 117788 KB Output is correct
34 Correct 185 ms 115572 KB Output is correct
35 Correct 144 ms 119092 KB Output is correct
36 Correct 40 ms 78136 KB Output is correct
37 Correct 114 ms 96144 KB Output is correct
38 Correct 176 ms 114728 KB Output is correct
39 Correct 176 ms 116776 KB Output is correct
40 Correct 221 ms 127388 KB Output is correct
41 Correct 178 ms 117128 KB Output is correct
42 Correct 191 ms 114332 KB Output is correct
43 Correct 53 ms 81232 KB Output is correct
44 Correct 284 ms 128296 KB Output is correct
45 Correct 320 ms 132404 KB Output is correct
46 Correct 487 ms 154664 KB Output is correct
47 Correct 542 ms 157216 KB Output is correct
48 Correct 490 ms 153644 KB Output is correct
49 Correct 286 ms 127348 KB Output is correct
50 Correct 231 ms 116012 KB Output is correct
51 Correct 494 ms 149300 KB Output is correct
52 Correct 196 ms 127368 KB Output is correct
53 Correct 43 ms 80716 KB Output is correct
54 Correct 344 ms 132400 KB Output is correct
55 Correct 393 ms 152372 KB Output is correct
56 Correct 519 ms 156852 KB Output is correct
57 Correct 403 ms 137728 KB Output is correct
58 Correct 425 ms 145980 KB Output is correct
59 Correct 465 ms 153548 KB Output is correct
60 Correct 382 ms 137680 KB Output is correct
61 Correct 412 ms 145964 KB Output is correct
62 Correct 279 ms 126588 KB Output is correct
63 Correct 92 ms 103628 KB Output is correct
64 Correct 277 ms 142084 KB Output is correct
65 Correct 265 ms 139700 KB Output is correct
66 Correct 240 ms 124344 KB Output is correct
67 Correct 274 ms 134636 KB Output is correct
68 Correct 188 ms 114852 KB Output is correct
69 Correct 486 ms 156204 KB Output is correct
70 Correct 333 ms 152116 KB Output is correct
71 Correct 472 ms 151976 KB Output is correct
72 Correct 289 ms 126956 KB Output is correct
73 Correct 443 ms 147900 KB Output is correct
74 Correct 181 ms 120368 KB Output is correct
75 Correct 182 ms 122144 KB Output is correct
76 Correct 212 ms 126384 KB Output is correct
77 Correct 226 ms 126492 KB Output is correct
78 Correct 209 ms 118388 KB Output is correct
79 Correct 178 ms 114772 KB Output is correct
80 Correct 182 ms 116756 KB Output is correct
81 Correct 221 ms 127296 KB Output is correct
82 Correct 178 ms 117040 KB Output is correct
83 Correct 184 ms 114500 KB Output is correct
84 Correct 55 ms 80724 KB Output is correct
85 Correct 370 ms 134964 KB Output is correct
86 Correct 623 ms 172340 KB Output is correct
87 Correct 106 ms 106408 KB Output is correct
88 Correct 112 ms 107432 KB Output is correct
89 Correct 100 ms 106396 KB Output is correct
90 Correct 42 ms 79696 KB Output is correct
91 Correct 37 ms 76624 KB Output is correct
92 Correct 48 ms 80724 KB Output is correct
93 Correct 225 ms 119476 KB Output is correct
94 Correct 89 ms 103888 KB Output is correct
95 Correct 328 ms 144572 KB Output is correct
96 Correct 270 ms 139828 KB Output is correct
97 Correct 232 ms 124860 KB Output is correct
98 Correct 242 ms 134504 KB Output is correct
99 Correct 711 ms 193588 KB Output is correct
100 Correct 487 ms 155076 KB Output is correct
101 Correct 557 ms 164912 KB Output is correct
102 Correct 274 ms 127588 KB Output is correct
103 Correct 170 ms 120380 KB Output is correct
104 Correct 177 ms 121912 KB Output is correct
105 Correct 204 ms 125744 KB Output is correct
106 Correct 180 ms 122160 KB Output is correct
107 Correct 189 ms 121404 KB Output is correct
108 Correct 58 ms 82688 KB Output is correct
109 Correct 416 ms 141080 KB Output is correct
110 Correct 331 ms 152872 KB Output is correct
111 Correct 339 ms 154932 KB Output is correct
112 Correct 224 ms 126484 KB Output is correct
113 Correct 225 ms 122916 KB Output is correct