Submission #1096659

# Submission time Handle Problem Language Result Execution time Memory
1096659 2024-10-05T00:01:17 Z azberjibiou Sky Walking (IOI19_walk) C++17
15 / 100
783 ms 100728 KB
#include "walk.h"
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define lb lower_bound
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
typedef long long ll;
using namespace std;
const int mxN=100050;
const int mxM=10000;
const int mxK=60;
const ll INF=1e18;
struct line{
    int l, r, h;
};
int N, M;
int pos[mxN], A[mxN];
line B[mxN];
int st, en;
int sidx, eidx;
map <pii, int> vtx;
pii V[12*mxN];
int vidx;
vector <pll> adj[12*mxN];
/*
void input(){
    cin >> N >> M;
    for(int i=0;i<N;i++) cin >> pos[i];
    for(int i=0;i<N;i++) cin >> A[i];
    for(int i=1;i<=M;i++) cin >> B[i].l >> B[i].r >> B[i].h;
    for(int i=1;i<=M;i++) B[i].l=pos[B[i].l], B[i].r=pos[B[i].r];
    cin >> st >> en;
}
*/
void no_endpoint(){
    vector <line> v;
    for(int i=1;i<=M;i++) v.push_back(B[i]);
    sort(all(v), [](line a, line b){return a.h!=b.h ? a.h<b.h : a.l<b.l;});
    vector <line> res;
    line prev={-1, -1, -1};
    for(line x : v){
        if(x.h!=prev.h || prev.r<x.l){
            if(prev.h!=-1) res.push_back(prev);
            prev=x;
        }
        else prev.r=x.r;
    }
    if(prev.h!=-1) res.push_back(prev);
    M=res.size();
    for(int i=1;i<=M;i++) B[i]=res[i-1];
}
void init(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<N;i++) pos[i]=x[i];
    for(int i=0;i<N;i++) A[i]=h[i];
    for(int i=1;i<=M;i++) B[i].l=l[i-1], B[i].r=r[i-1], B[i].h=y[i-1];
  	for(int i=1;i<=M;i++) B[i].l=pos[B[i].l], B[i].r=pos[B[i].r];
    no_endpoint();
    st=s, en=g;
}
void add_edge(int a, int b, int c){
    adj[a].emplace_back(b, c);
    adj[b].emplace_back(a, c);
}
void add(vector <int> &v1, int val, pii rng){
    if(val==-1) return;
    if(val>rng.se || val<rng.fi) return;
    v1.push_back(val);
}
int lb(set <int> &s, int val){
    auto it=s.lower_bound(val);
    return (it==s.end() ? -1 : *it);
}
int rb(set <int> &s, int val){
    auto it=s.lower_bound(val+1);
    if(it==s.begin()) return -1;
    it--;
    return *it;
}
void make_graph(){
    set <int> xcr;
    set <pii> fall; // x값, 끝점 index
    vector <pii> hv;
    for(int i=0;i<N;i++) hv.emplace_back(A[i], i);
    sort(all(hv));
    vector <int> turn;
    for(int i=1;i<=M;i++) turn.push_back(i);
    sort(all(turn), [&](int a, int b){return B[a].h>B[b].h;});
    for(int now : turn){
        auto [nl, nr, nh]=B[now];
        while(hv.size() && hv.back().fi>=nh){
            int idx=hv.back().se;
            hv.pop_back();
            xcr.insert(pos[idx]);
        }
        vector <int> cr;
        vector <pii> up; // x값, 끝점 index
        vector <int> down;
        //get up edges
        while(true){
            if(fall.empty()) break;
            auto it=fall.lower_bound(pii(nl, 0));
            if(it==fall.end() || it->fi>nr) break;
            up.push_back(*it);
            fall.erase(it);
        }
        // add down vertices
        add(down, lb(xcr, nl), pii(nl, nr));
        if(nl<=pos[st] && pos[st]<=nr){
            add(down, lb(xcr, pos[st]), pii(nl, nr));
            add(down, rb(xcr, pos[st]), pii(nl, nr));
        }
        if(nl<=pos[en] && pos[en]<=nr){
            add(down, lb(xcr, pos[en]), pii(nl, nr));
            add(down, rb(xcr, pos[en]), pii(nl, nr));
        }
        add(down, rb(xcr, nr), pii(nl, nr));
        sort(all(down));
        down.erase(unique(all(down)), down.end());
        /*
        printf("nl, nr, nh=%d %d %d\n", nl, nr, nh);
        for(int x : down) printf("%d ", x);
        printf("\n");
        printf("xcr: ");
        for(int x : xcr) printf("%d ", x);
        printf("\n");
        */
        //make vertices
        for(int x : down) cr.push_back(x);
        for(auto [x, idx] : up) cr.push_back(x);
        sort(all(cr));
        cr.erase(unique(all(cr)), cr.end());
        for(int i=0;i<cr.size();i++){
            int x=cr[i];
            if(vtx.count(pii(x, nh))==0){
              	V[vidx]=pii(x, nh);
            	vtx[pii(x, nh)]=vidx++;
            }
            //also make horizontal edges
            if(i-1>=0) add_edge(vtx[pii(x, nh)], vtx[pii(cr[i-1], nh)], cr[i]-cr[i-1]);
        }
        //make up edges
        for(auto [x, num1] : up){
            int num2=vtx[pii(x, nh)];
            add_edge(num1, num2, V[num1].se-nh);
        }
        //make down edges
        for(int x : down){
            fall.insert(pii(x, vtx[pii(x, nh)]));
        }
    }
    //make st and en
    V[vidx]=pii(pos[st], 0);
    vtx[pii(pos[st], 0)]=vidx;
    sidx=vidx++;
    V[vidx]=pii(pos[en], 0);
    vtx[pii(pos[en], 0)]=vidx;
    eidx=vidx++;
    //adding edge for st and en
    auto itst=fall.lower_bound(pii(pos[st], 0));
    if(itst!=fall.end() || itst->fi==pos[st]){
        int nxt=itst->se;
        add_edge(sidx, nxt, V[nxt].se);
    }
    auto iten=fall.lower_bound(pii(pos[en], 0));
    if(iten!=fall.end() || iten->fi==pos[en]){
        int nxt=iten->se;
        add_edge(eidx, nxt, V[nxt].se);
    }
    /*
    for(int i=0;i<vidx;i++) printf("V[%d]=%d %d\n", i, V[i].fi, V[i].se);
    for(int i=0;i<vidx;i++){
        printf("i=%d: ", i);
        for(auto [x, y] : adj[i]) printf("(%lld %lld) ", x, y);
        printf("\n");
    }
    */
}
ll dist[12*mxN];
ll ans;
void dijkstra(){
    for(int i=0;i<vidx;i++) dist[i]=-1;
    priority_queue <pll> pq;
    pq.emplace(0, sidx);
    while(pq.size()){
        auto [x, now]=pq.top();
        pq.pop();
        if(dist[now]!=-1) continue;
        dist[now]=-x;
        for(auto [nxt, val] : adj[now]) pq.emplace(x-val, nxt);
    }
    //for(int i=0;i<vidx;i++) printf("dist[%d]=%lld\n", i, dist[i]);
    ans=dist[eidx];
}
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) {
	init(x, h, l, r, y, s, g);
    make_graph();
    dijkstra();
    return ans;
}
/*
int main(){
    gibon
    input();
    make_graph();
    dijkstra();
    cout << ans;
}
*/
/*
7 7
0 3 5 7 10 12 14
8 7 9 7 6 6 9
0 1 1
0 2 6
0 6 8
2 3 1
2 6 7
3 4 2
4 6 5
1 5
*/

Compilation message

walk.cpp: In function 'void make_graph()':
walk.cpp:139:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |         for(int i=0;i<cr.size();i++){
      |                     ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 28508 KB Output is correct
2 Correct 15 ms 28668 KB Output is correct
3 Correct 10 ms 28656 KB Output is correct
4 Correct 10 ms 28508 KB Output is correct
5 Correct 11 ms 28508 KB Output is correct
6 Correct 11 ms 28632 KB Output is correct
7 Correct 11 ms 28504 KB Output is correct
8 Correct 11 ms 28508 KB Output is correct
9 Correct 11 ms 28480 KB Output is correct
10 Correct 11 ms 28508 KB Output is correct
11 Correct 11 ms 28656 KB Output is correct
12 Correct 11 ms 28508 KB Output is correct
13 Correct 11 ms 28508 KB Output is correct
14 Correct 14 ms 28508 KB Output is correct
15 Incorrect 11 ms 28656 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28508 KB Output is correct
2 Correct 11 ms 28508 KB Output is correct
3 Correct 593 ms 82876 KB Output is correct
4 Correct 657 ms 92532 KB Output is correct
5 Correct 395 ms 78652 KB Output is correct
6 Correct 388 ms 76672 KB Output is correct
7 Correct 413 ms 78888 KB Output is correct
8 Correct 609 ms 85336 KB Output is correct
9 Correct 511 ms 88072 KB Output is correct
10 Correct 633 ms 93056 KB Output is correct
11 Correct 508 ms 78980 KB Output is correct
12 Correct 455 ms 75116 KB Output is correct
13 Correct 682 ms 94832 KB Output is correct
14 Incorrect 337 ms 71384 KB Output isn't correct
15 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 32136 KB Output is correct
2 Correct 621 ms 89468 KB Output is correct
3 Correct 659 ms 91520 KB Output is correct
4 Correct 720 ms 100684 KB Output is correct
5 Correct 783 ms 100728 KB Output is correct
6 Correct 688 ms 97676 KB Output is correct
7 Correct 337 ms 69832 KB Output is correct
8 Correct 419 ms 75368 KB Output is correct
9 Correct 640 ms 94652 KB Output is correct
10 Correct 255 ms 75888 KB Output is correct
11 Correct 31 ms 33476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 32136 KB Output is correct
2 Correct 621 ms 89468 KB Output is correct
3 Correct 659 ms 91520 KB Output is correct
4 Correct 720 ms 100684 KB Output is correct
5 Correct 783 ms 100728 KB Output is correct
6 Correct 688 ms 97676 KB Output is correct
7 Correct 337 ms 69832 KB Output is correct
8 Correct 419 ms 75368 KB Output is correct
9 Correct 640 ms 94652 KB Output is correct
10 Correct 255 ms 75888 KB Output is correct
11 Correct 31 ms 33476 KB Output is correct
12 Correct 664 ms 91528 KB Output is correct
13 Incorrect 742 ms 100712 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 28508 KB Output is correct
2 Correct 15 ms 28668 KB Output is correct
3 Correct 10 ms 28656 KB Output is correct
4 Correct 10 ms 28508 KB Output is correct
5 Correct 11 ms 28508 KB Output is correct
6 Correct 11 ms 28632 KB Output is correct
7 Correct 11 ms 28504 KB Output is correct
8 Correct 11 ms 28508 KB Output is correct
9 Correct 11 ms 28480 KB Output is correct
10 Correct 11 ms 28508 KB Output is correct
11 Correct 11 ms 28656 KB Output is correct
12 Correct 11 ms 28508 KB Output is correct
13 Correct 11 ms 28508 KB Output is correct
14 Correct 14 ms 28508 KB Output is correct
15 Incorrect 11 ms 28656 KB Output isn't correct
16 Halted 0 ms 0 KB -