답안 #147992

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
147992 2019-08-31T10:47:23 Z Xylofo Sky Walking (IOI19_walk) C++14
100 / 100
3425 ms 263292 KB
    #pragma GCC optimize("Ofast")
     
    #include "walk.h"
     
     
    #include <bits/stdc++.h>
    using namespace std;
     
    #define rep(i, a, b) for(int i = a; i < (b); ++i)
    #define trav(a, x) for(auto& a : x)
    #define all(x) begin(x), end(x)
    #define sz(x) (int)(x).size()
    #define eb emplace_back
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef vector<int> vi;
     
    const ll INF =1e18;
     
    #ifdef LOCAL
    #include "../../../../MixedFunc/pretty_debug.h"
    #else
    #define debug(...) //ignore
    #endif
     
    #include <bits/extc++.h> /** keep-include */
    struct chash {
      const uint64_t C = ll(2e18 * M_PI) + 71; // large odd number
      ll operator()(ll x) const { return __builtin_bswap64(x*C); }
    };
    __gnu_pbds::gp_hash_table<ll,int,chash> V({},{},{},{},{1<<16});
     
    struct Graph {
      int n,m;
      vi x,y,h;
      //unordered_map<ll, int> V; // verts, indexed by 2e9*x+h
      vector<vector<pair<int,ll>>> E; // adj-list
      vector<map<ll,int> > perBuild, perWalk;
     
      Graph(vi& x, vi& y, vi& h)
        : n(sz(x)), m(sz(y)), x(x), y(y), h(h), perBuild(n), perWalk(m) {}
     
      int at(ll x, ll h) { // return vertex handle of (x,h)
        auto p = x*(ll(2e9))+ h;
        if (V.find(p) == V.end()) { V[p] = sz(E); E.eb(); }
        return V[p];
      }
     
      void add(int build, int walk) { // pt (x[build], y[walk]) is important
        ll X = x[build], Y = y[walk];
        //assert(h[build] >= Y);
        int q = at(X, Y);
        perBuild[build][Y] = q;
        perWalk[walk][X] = q;
      }
     
      void edge(int a, int b, ll c){ // create edge
        E[a].eb(b,c);
        E[b].eb(a,c);
      }
     
      void edges(map<ll,int>& vert) { // create edges on line
        vector<pair<ll,int> > vv(all(vert));
        rep(i,1,sz(vv))
          edge(vv[i-1].second, vv[i].second, vv[i].first-vv[i-1].first);
      }
     
      void addUpDown(vi& l, vi& r) { // add things directely above/below points added.
        vector<vi> start(n), end(n);
        rep(i,0,m) start[l[i]].eb(i), end[r[i]].eb(i);
        map<ll,int> active;
        rep(i,0,n) {
          trav(w,end[i]) active.erase(y[w]);
          trav(w,start[i]) active[y[w]] = w;
          vi toAdd;
          for(auto [hh, _] : perBuild[i]) {
            auto it = active.upper_bound(hh);
            auto maybeAdd = [&](int w) { if (y[w] <= h[i]) toAdd.eb(w); };
            if(it != active.end()) maybeAdd(it->second);
            it = active.lower_bound(hh);
            if(it != active.begin()) maybeAdd((--it)->second);
          }
          trav(w, toAdd) add(i,w);
        }
      }
     
      void build(){
        trav(v, perBuild) edges(v);
        trav(v, perWalk) edges(v);
      }
    };
     
    template<class T>
    auto dijkstra(int source, vector<vector<pair<int, T>>> &g) {
      int n = sz(g);
      vector<T> dist(n, numeric_limits<T>::max());
      //vector<pair<int,T> > dad(n, {-1, 0});
      priority_queue<pair<T,int> > pq;
      dist[source] = 0;
      pq.emplace(0,source);
      while(!pq.empty()) {
        T d; int x;
        tie(d,x) = pq.top();
        d = -d;
        pq.pop();
        if(d > dist[x]) continue;
        for(auto [y,w] : g[x]) {
          if(dist[y] > d + w) {
            dist[y] = d + w;
            //dad[y] = {x, w};
            pq.emplace(-dist[y], y);
          }
        }
      }
      return dist;
    }
     
     
     
    ll min_distance(vi x, vi h, vi l, vi r, vi y, int s, int g) {
      int n = sz(x), m = sz(l);
      debug(n,m);
     
      auto stALL= [&](){
        vector<map<ll,int>> Q;
        auto pcomp  = [&] (vi idx) {
          Q.eb();
          int hh = 0;
          for(int i : idx) if(h[i] > hh) {
            hh = h[i];
            Q.back()[hh] = i;
          }
        };
        { vi ind; rep(i,s,n) ind.eb(i); pcomp(ind); }
        { vi ind; for(int i = s; i >= 0; --i) ind.eb(i); pcomp(ind); }
        { vi ind; rep(i,g,n) ind.eb(i); pcomp(ind); }
        { vi ind; for(int i = g; i >= 0; --i) ind.eb(i); pcomp(ind); }
        //debug(Q);
     
        Graph G(x,y,h);
     
        int S = G.perBuild[s][0] = G.at(x[s], 0);
        int T = G.perBuild[g][0] = G.at(x[g], 0);
     
        debug("added", s, g);
     
        rep(i,0,m) {
          // add endpoints
          G.add(l[i],i);
          G.add(r[i],i);
     
          //add closest to s and g in each direction.
          for(auto &q: Q) {
            auto it = q.lower_bound(y[i]);
            if(it == q.end()) continue;
            int j = it->second;
            if(l[i] <= j && j <= r[i]) G.add(j, i);
          }
        }
     
        debug("added pts 1");
        debug(sz(G.E));
     
        G.addUpDown(l,r);
     
        debug("added pts 2");
        debug(sz(G.E));
     
        G.build();
     
        debug("added edges");
     
        auto dist = dijkstra(S, G.E);
        ll ans = dist[T];
        if (ans >= INF) ans = -1;
        return ans;
      };
     
      return stALL();
    }

Compilation message

walk.cpp: In member function 'void Graph::addUpDown(vi&, vi&)':
walk.cpp:76:20: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
           for(auto [hh, _] : perBuild[i]) {
                    ^
walk.cpp:76:26: warning: unused variable '_' [-Wunused-variable]
           for(auto [hh, _] : perBuild[i]) {
                          ^
walk.cpp: In function 'auto dijkstra(int, std::vector<std::vector<std::pair<int, T> > >&)':
walk.cpp:107:18: warning: decomposition declaration only available with -std=c++1z or -std=gnu++1z
         for(auto [y,w] : g[x]) {
                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1912 KB Output is correct
4 Correct 3 ms 1912 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 1912 KB Output is correct
7 Correct 4 ms 1912 KB Output is correct
8 Correct 3 ms 1912 KB Output is correct
9 Correct 4 ms 1908 KB Output is correct
10 Correct 4 ms 1912 KB Output is correct
11 Correct 3 ms 1912 KB Output is correct
12 Correct 4 ms 1912 KB Output is correct
13 Correct 4 ms 1912 KB Output is correct
14 Correct 3 ms 1912 KB Output is correct
15 Correct 3 ms 1916 KB Output is correct
16 Correct 3 ms 1912 KB Output is correct
17 Correct 4 ms 2040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 1302 ms 136644 KB Output is correct
4 Correct 1237 ms 141560 KB Output is correct
5 Correct 1010 ms 115872 KB Output is correct
6 Correct 939 ms 109644 KB Output is correct
7 Correct 857 ms 116000 KB Output is correct
8 Correct 1413 ms 146956 KB Output is correct
9 Correct 1109 ms 132400 KB Output is correct
10 Correct 1346 ms 147948 KB Output is correct
11 Correct 946 ms 110088 KB Output is correct
12 Correct 528 ms 69568 KB Output is correct
13 Correct 1300 ms 152000 KB Output is correct
14 Correct 802 ms 75460 KB Output is correct
15 Correct 729 ms 75752 KB Output is correct
16 Correct 593 ms 73148 KB Output is correct
17 Correct 580 ms 70736 KB Output is correct
18 Correct 890 ms 80060 KB Output is correct
19 Correct 18 ms 4916 KB Output is correct
20 Correct 206 ms 37544 KB Output is correct
21 Correct 549 ms 69328 KB Output is correct
22 Correct 531 ms 69428 KB Output is correct
23 Correct 878 ms 83868 KB Output is correct
24 Correct 537 ms 70148 KB Output is correct
25 Correct 542 ms 70228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 25124 KB Output is correct
2 Correct 2075 ms 199256 KB Output is correct
3 Correct 2205 ms 205580 KB Output is correct
4 Correct 2166 ms 211728 KB Output is correct
5 Correct 2385 ms 214612 KB Output is correct
6 Correct 2301 ms 201628 KB Output is correct
7 Correct 963 ms 109956 KB Output is correct
8 Correct 626 ms 69580 KB Output is correct
9 Correct 1954 ms 165944 KB Output is correct
10 Correct 783 ms 90548 KB Output is correct
11 Correct 22 ms 7716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 189 ms 25124 KB Output is correct
2 Correct 2075 ms 199256 KB Output is correct
3 Correct 2205 ms 205580 KB Output is correct
4 Correct 2166 ms 211728 KB Output is correct
5 Correct 2385 ms 214612 KB Output is correct
6 Correct 2301 ms 201628 KB Output is correct
7 Correct 963 ms 109956 KB Output is correct
8 Correct 626 ms 69580 KB Output is correct
9 Correct 1954 ms 165944 KB Output is correct
10 Correct 783 ms 90548 KB Output is correct
11 Correct 22 ms 7716 KB Output is correct
12 Correct 2156 ms 205316 KB Output is correct
13 Correct 1961 ms 210844 KB Output is correct
14 Correct 2508 ms 214528 KB Output is correct
15 Correct 1458 ms 150876 KB Output is correct
16 Correct 1497 ms 154168 KB Output is correct
17 Correct 1868 ms 210588 KB Output is correct
18 Correct 1522 ms 150996 KB Output is correct
19 Correct 1642 ms 153908 KB Output is correct
20 Correct 1089 ms 108080 KB Output is correct
21 Correct 56 ms 14556 KB Output is correct
22 Correct 1258 ms 147652 KB Output is correct
23 Correct 1086 ms 137000 KB Output is correct
24 Correct 707 ms 85932 KB Output is correct
25 Correct 983 ms 128028 KB Output is correct
26 Correct 521 ms 66552 KB Output is correct
27 Correct 2394 ms 214360 KB Output is correct
28 Correct 1741 ms 208396 KB Output is correct
29 Correct 2183 ms 201672 KB Output is correct
30 Correct 934 ms 109824 KB Output is correct
31 Correct 1908 ms 165876 KB Output is correct
32 Correct 639 ms 79892 KB Output is correct
33 Correct 616 ms 79372 KB Output is correct
34 Correct 877 ms 87552 KB Output is correct
35 Correct 876 ms 107472 KB Output is correct
36 Correct 657 ms 79740 KB Output is correct
37 Correct 636 ms 69264 KB Output is correct
38 Correct 596 ms 69568 KB Output is correct
39 Correct 872 ms 83884 KB Output is correct
40 Correct 556 ms 70104 KB Output is correct
41 Correct 568 ms 70136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1912 KB Output is correct
4 Correct 3 ms 1912 KB Output is correct
5 Correct 4 ms 1912 KB Output is correct
6 Correct 4 ms 1912 KB Output is correct
7 Correct 4 ms 1912 KB Output is correct
8 Correct 3 ms 1912 KB Output is correct
9 Correct 4 ms 1908 KB Output is correct
10 Correct 4 ms 1912 KB Output is correct
11 Correct 3 ms 1912 KB Output is correct
12 Correct 4 ms 1912 KB Output is correct
13 Correct 4 ms 1912 KB Output is correct
14 Correct 3 ms 1912 KB Output is correct
15 Correct 3 ms 1916 KB Output is correct
16 Correct 3 ms 1912 KB Output is correct
17 Correct 4 ms 2040 KB Output is correct
18 Correct 3 ms 1912 KB Output is correct
19 Correct 3 ms 1912 KB Output is correct
20 Correct 1302 ms 136644 KB Output is correct
21 Correct 1237 ms 141560 KB Output is correct
22 Correct 1010 ms 115872 KB Output is correct
23 Correct 939 ms 109644 KB Output is correct
24 Correct 857 ms 116000 KB Output is correct
25 Correct 1413 ms 146956 KB Output is correct
26 Correct 1109 ms 132400 KB Output is correct
27 Correct 1346 ms 147948 KB Output is correct
28 Correct 946 ms 110088 KB Output is correct
29 Correct 528 ms 69568 KB Output is correct
30 Correct 1300 ms 152000 KB Output is correct
31 Correct 802 ms 75460 KB Output is correct
32 Correct 729 ms 75752 KB Output is correct
33 Correct 593 ms 73148 KB Output is correct
34 Correct 580 ms 70736 KB Output is correct
35 Correct 890 ms 80060 KB Output is correct
36 Correct 18 ms 4916 KB Output is correct
37 Correct 206 ms 37544 KB Output is correct
38 Correct 549 ms 69328 KB Output is correct
39 Correct 531 ms 69428 KB Output is correct
40 Correct 878 ms 83868 KB Output is correct
41 Correct 537 ms 70148 KB Output is correct
42 Correct 542 ms 70228 KB Output is correct
43 Correct 189 ms 25124 KB Output is correct
44 Correct 2075 ms 199256 KB Output is correct
45 Correct 2205 ms 205580 KB Output is correct
46 Correct 2166 ms 211728 KB Output is correct
47 Correct 2385 ms 214612 KB Output is correct
48 Correct 2301 ms 201628 KB Output is correct
49 Correct 963 ms 109956 KB Output is correct
50 Correct 626 ms 69580 KB Output is correct
51 Correct 1954 ms 165944 KB Output is correct
52 Correct 783 ms 90548 KB Output is correct
53 Correct 22 ms 7716 KB Output is correct
54 Correct 2156 ms 205316 KB Output is correct
55 Correct 1961 ms 210844 KB Output is correct
56 Correct 2508 ms 214528 KB Output is correct
57 Correct 1458 ms 150876 KB Output is correct
58 Correct 1497 ms 154168 KB Output is correct
59 Correct 1868 ms 210588 KB Output is correct
60 Correct 1522 ms 150996 KB Output is correct
61 Correct 1642 ms 153908 KB Output is correct
62 Correct 1089 ms 108080 KB Output is correct
63 Correct 56 ms 14556 KB Output is correct
64 Correct 1258 ms 147652 KB Output is correct
65 Correct 1086 ms 137000 KB Output is correct
66 Correct 707 ms 85932 KB Output is correct
67 Correct 983 ms 128028 KB Output is correct
68 Correct 521 ms 66552 KB Output is correct
69 Correct 2394 ms 214360 KB Output is correct
70 Correct 1741 ms 208396 KB Output is correct
71 Correct 2183 ms 201672 KB Output is correct
72 Correct 934 ms 109824 KB Output is correct
73 Correct 1908 ms 165876 KB Output is correct
74 Correct 639 ms 79892 KB Output is correct
75 Correct 616 ms 79372 KB Output is correct
76 Correct 877 ms 87552 KB Output is correct
77 Correct 876 ms 107472 KB Output is correct
78 Correct 657 ms 79740 KB Output is correct
79 Correct 636 ms 69264 KB Output is correct
80 Correct 596 ms 69568 KB Output is correct
81 Correct 872 ms 83884 KB Output is correct
82 Correct 556 ms 70104 KB Output is correct
83 Correct 568 ms 70136 KB Output is correct
84 Correct 150 ms 21512 KB Output is correct
85 Correct 2146 ms 208316 KB Output is correct
86 Correct 2769 ms 234928 KB Output is correct
87 Correct 113 ms 22640 KB Output is correct
88 Correct 117 ms 22660 KB Output is correct
89 Correct 109 ms 22640 KB Output is correct
90 Correct 47 ms 9816 KB Output is correct
91 Correct 5 ms 2168 KB Output is correct
92 Correct 39 ms 7896 KB Output is correct
93 Correct 658 ms 71500 KB Output is correct
94 Correct 59 ms 14768 KB Output is correct
95 Correct 1404 ms 154608 KB Output is correct
96 Correct 1227 ms 138648 KB Output is correct
97 Correct 868 ms 90108 KB Output is correct
98 Correct 989 ms 127992 KB Output is correct
99 Correct 3425 ms 263292 KB Output is correct
100 Correct 2113 ms 214728 KB Output is correct
101 Correct 2669 ms 222520 KB Output is correct
102 Correct 980 ms 112912 KB Output is correct
103 Correct 663 ms 82832 KB Output is correct
104 Correct 659 ms 81632 KB Output is correct
105 Correct 786 ms 90168 KB Output is correct
106 Correct 711 ms 85480 KB Output is correct
107 Correct 761 ms 83068 KB Output is correct
108 Correct 105 ms 15920 KB Output is correct
109 Correct 1691 ms 152752 KB Output is correct
110 Correct 1799 ms 213312 KB Output is correct
111 Correct 1778 ms 213292 KB Output is correct
112 Correct 877 ms 109588 KB Output is correct
113 Correct 818 ms 103200 KB Output is correct