Submission #222727

# Submission time Handle Problem Language Result Execution time Memory
222727 2020-04-13T22:05:50 Z VEGAnn Sky Walking (IOI19_walk) C++14
15 / 100
313 ms 17256 KB
#include <bits/stdc++.h>
#include "walk.h"
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define pii pair<int,int>
#define ft first
#define sd second
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 100100;
const ll OO = 1e18;
vector<int> vc, real_y;
vector<array<int, 3> > lines;
vector<pii> events[N];
int n, m, pos;
bool fd;
ll st[4 * N], psh[4 * N], ans = OO, gt_vl;
 
void build(int v, int l, int r){
    psh[v] = 0;
    st[v] = OO;
 
    if (l == r) return;
 
    int md = (l + r) >> 1;
    build(v + v, l, md);
    build(v + v + 1, md + 1, r);
}
 
void push(int v){
    if (psh[v] != 0){
        st[v] += psh[v];
 
        if (v + v + 1 < 4 * N){
            psh[v + v] += psh[v];
            psh[v + v + 1] += psh[v];
        }
 
        psh[v] = 0;
    }
}
 
void update(int v, int l, int r, int ps, ll vl){
    push(v);
 
    if (l == r){
        st[v] = vl;
        return;
    }
 
    int md = (l + r) >> 1;
 
    if (ps <= md)
        update(v + v, l, md, ps, vl);
    else update(v + v + 1, md + 1, r, ps, vl);
 
    push(v + v);
    push(v + v + 1);
 
    st[v] = min(st[v + v], st[v + v + 1]);
}
 
void add(int v, int tl, int tr, int l, int r, ll vl){
    push(v);
 
    if (l > r) return;
 
    if (tl == l && tr == r){
        psh[v] += vl;
        push(v);
        return;
    }
 
    int md = (tl + tr) >> 1;
    add(v + v, tl, md, l, min(r, md), vl);
    add(v + v + 1, md + 1, tr, max(md + 1, l), r, vl);
 
    st[v] = min(st[v + v], st[v + v + 1]);
}
 
void real_search_left(int v, int l, int r){
    push(v);
 
    if (l == r){
        fd = 1;
        pos = l;
        gt_vl = st[v];
        return;
    }
 
    push(v + v);
    push(v + v + 1);
 
    int md = (l + r) >> 1;
 
    if (st[v + v + 1] < OO)
        real_search_left(v + v + 1, md + 1, r);
    else real_search_left(v + v, l, md);
}
 
void search_left(int v, int tl, int tr, int l, int r){
    push(v);
 
    if (fd || l > r) return;
 
    if (tl == l && tr == r){
        if (st[v] < OO)
            real_search_left(v, l, r);
        return;
    }
 
    int md = (tl + tr) >> 1;
    search_left(v + v + 1, md + 1, tr, max(md + 1, l), r);
    search_left(v + v, tl, md, l, min(r, md));
}
 
 
void real_search_right(int v, int l, int r){
    push(v);
 
    if (l == r){
        fd = 1;
        pos = l;
        gt_vl = st[v];
        return;
    }
 
    push(v + v);
    push(v + v + 1);
 
    int md = (l + r) >> 1;
 
    if (st[v + v] < OO)
        real_search_right(v + v, l, md);
    else real_search_right(v + v + 1, md + 1, r);
}
 
void search_right(int v, int tl, int tr, int l, int r){
    push(v);
 
    if (fd || l > r) return;
 
    if (tl == l && tr == r){
        if (st[v] < OO)
            real_search_right(v, l, r);
        return;
    }
 
    int md = (tl + tr) >> 1;
    search_right(v + v, tl, md, l, min(r, md));
    search_right(v + v + 1, md + 1, tr, max(md + 1, l), r);
}
 
void get_ans(int v, int l, int r){
    push(v);
    if (l == r){
        if (st[v] < OO)
            ans = min(ans, st[v] + ll(real_y[l]));
        return;
    }
 
    int md = (l + r) >> 1;
    get_ans(v + v, l, md);
    get_ans(v + v + 1, md + 1, r);
}
 
long long min_distance(std::vector<int> x, std::vector<int> h, std::vector<int> l, std::vector<int> r, std::vector<int> y, int s, int g) {
    lines.clear();
 
    n = sz(x); m = sz(l);
 
    vc.clear();
 
    for (int i = 0; i < m; i++)
        real_y.PB(y[i]);
 
    sort(all(real_y));
    real_y.resize(unique(all(real_y)) - real_y.begin());
 
//    for (int cr : real_y)
//        cerr << cr << " "; cerr << '\n';
 
    for (int i = 0; i < m; i++){
        y[i] = lower_bound(all(real_y), y[i]) - real_y.begin();
    }
 
    for (int i = 0; i < m; i++)
        lines.PB({y[i], l[i], r[i]});
 
    sort(all(lines));
 
    for (int i = 0; i < m; ){
        int j = i;
        while (j < m && lines[j][0] == lines[i][0])
            j++;
 
        pii seg = MP(lines[i][1], lines[i][2]);
        int ht = lines[i][0];
 
        for (int I = i + 1; I < j; I++)
            if (seg.sd < lines[I][1]){
                events[seg.ft].PB(MP(-1, ht));
                events[seg.sd].PB(MP(+1, ht));
 
                seg = MP(lines[I][1], lines[I][2]);
            } else seg.sd = max(lines[I][2], seg.sd);
 
        events[seg.ft].PB(MP(-1, ht));
        events[seg.sd].PB(MP(+1, ht));
 
        i = j;
    }
 
    build(1, 0, sz(real_y) - 1);
 
    for (pii cr : events[0]){
        int ht = cr.sd;
 
        update(1, 0, sz(real_y) - 1, ht, real_y[ht]);
    }
 
    add(1, 0, sz(real_y) - 1, 0, sz(real_y) - 1, x[1] - x[0]);
 
    for (int loc = 1; loc < n - 1; loc++){
        sort(all(events[loc]));
 
        for (pii cr : events[loc]){
            int ht = cr.sd;
 
            if (cr.ft > 0){
                update(1, 0, sz(real_y) - 1, ht, OO);
            } else {
                push(1);
//                if (st[1] == OO) return -1;
 
                ll best = OO;
 
                fd = 0; pos = -1; gt_vl = -1;
 
                search_left(1, 0, sz(real_y) - 1, 0, ht - 1);
 
                if (fd){
                    best = min(best, gt_vl + ll(real_y[ht]) - ll(real_y[pos]));
                }
 
                fd = 0; pos = -1; gt_vl = -1;
 
                int lc = upper_bound(all(real_y), h[loc]) - real_y.begin() - 1;
 
                search_right(1, 0, sz(real_y) - 1, ht + 1, lc);
 
                if (fd){
                    best = min(best, gt_vl - ll(real_y[ht]) + ll(real_y[pos]));
                }
 
//                if (best == OO) return -1;
 
//                assert(best != OO);
//                if (best == OO){
//                    while (1) {}
//                }
 
                if (best != OO)
                  update(1, 0, sz(real_y) - 1, ht, best);
            }
        }
 
        add(1, 0, sz(real_y) - 1, 0, sz(real_y) - 1, x[loc + 1] - x[loc]);
    }
 
    get_ans(1, 0, sz(real_y) - 1);
 
    return (ans == OO ? -1 : ans);
}
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 2688 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 2688 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 33 ms 5364 KB Output is correct
2 Correct 223 ms 14312 KB Output is correct
3 Correct 243 ms 14952 KB Output is correct
4 Correct 313 ms 17256 KB Output is correct
5 Correct 285 ms 17128 KB Output is correct
6 Correct 294 ms 17000 KB Output is correct
7 Correct 155 ms 11884 KB Output is correct
8 Correct 287 ms 16688 KB Output is correct
9 Correct 281 ms 17128 KB Output is correct
10 Correct 181 ms 14312 KB Output is correct
11 Correct 21 ms 3712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 5364 KB Output is correct
2 Correct 223 ms 14312 KB Output is correct
3 Correct 243 ms 14952 KB Output is correct
4 Correct 313 ms 17256 KB Output is correct
5 Correct 285 ms 17128 KB Output is correct
6 Correct 294 ms 17000 KB Output is correct
7 Correct 155 ms 11884 KB Output is correct
8 Correct 287 ms 16688 KB Output is correct
9 Correct 281 ms 17128 KB Output is correct
10 Correct 181 ms 14312 KB Output is correct
11 Correct 21 ms 3712 KB Output is correct
12 Correct 240 ms 14824 KB Output is correct
13 Correct 279 ms 17128 KB Output is correct
14 Correct 280 ms 16872 KB Output is correct
15 Correct 205 ms 15312 KB Output is correct
16 Correct 222 ms 15208 KB Output is correct
17 Correct 229 ms 15208 KB Output is correct
18 Correct 249 ms 15300 KB Output is correct
19 Correct 284 ms 15208 KB Output is correct
20 Correct 170 ms 11756 KB Output is correct
21 Correct 42 ms 4600 KB Output is correct
22 Correct 211 ms 15980 KB Output is correct
23 Correct 234 ms 16232 KB Output is correct
24 Correct 215 ms 16232 KB Output is correct
25 Correct 220 ms 16028 KB Output is correct
26 Correct 212 ms 16360 KB Output is correct
27 Correct 289 ms 17000 KB Output is correct
28 Correct 282 ms 17128 KB Output is correct
29 Correct 287 ms 17040 KB Output is correct
30 Correct 178 ms 12008 KB Output is correct
31 Correct 281 ms 17128 KB Output is correct
32 Correct 179 ms 12648 KB Output is correct
33 Incorrect 183 ms 13032 KB Output isn't correct
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 2688 KB Output isn't correct
2 Halted 0 ms 0 KB -