답안 #295251

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
295251 2020-09-09T14:42:21 Z Bruteforceman Sky Walking (IOI19_walk) C++14
100 / 100
2005 ms 152152 KB
#include "bits/stdc++.h"
#include "walk.h"
using namespace std;
const int maxNode = 2e6 + 10;
const int maxn = 1e5 + 10;
const long long inf = 1e16;
int node = 0;
vector <pair <int, int>> g[maxNode];
vector <int> start[maxn], fin[maxn];
vector <pair <int, int>> bridge[maxn];
vector <pair <int, int>> line[maxn];

struct vertex {
  int node;
  long long dist;
  vertex() {}
  vertex(int node, long long dist) : node(node), dist(dist) {}
  bool operator < (vertex v) const {
    return dist > v.dist;
  }
};

void addEdge(int i, int j, int cost) {
//   cout << i << " " << j << " " << cost << endl;
  g[i].emplace_back(j, cost);
  g[j].emplace_back(i, cost);
}
long long shortest_path(int src, int des) {
  priority_queue <vertex> Q;
  vector <long long> dist (node, inf);

  Q.emplace(src, 0);
  dist[src] = 0;
  while(not Q.empty()) {
    auto x = Q.top();
    Q.pop();
    if(x.dist != dist[x.node]) continue;
    for(auto i : g[x.node]) {
      if(i.second + x.dist < dist[i.first]) {
        dist[i.first] = i.second + x.dist;
        Q.emplace(i.first, dist[i.first]);
      }
    }
  }
  return dist[des];
}
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 from, int to) {
  for(int i = 0; i < l.size(); i++) {
    start[l[i]].push_back(i);
    fin[r[i]].push_back(i);
  }
  set <pair <int, int>> cont;
  int src = 0, des = 0;
  for(int i = 0; i < x.size(); i++) {
    for(int j : start[i]) {
      cont.insert(make_pair(y[j], j)); 
    }
    vector <pair <int, int>> can;
    if(from == i) {
      can.push_back(make_pair(0, -1));
    }
    if(to == i) {
      can.push_back(make_pair(0, -1));
    }
    for(int j : start[i]) {
      auto it = cont.find(make_pair(y[j], j));
      can.emplace_back(y[j], j);
      if(it != cont.begin()) {
        can.push_back(*prev(it));
      }
      if(next(it) != cont.end() && next(it) -> first <= h[i]) {
        can.push_back(*next(it));
      }
    }
    for(int j : fin[i]) {
      auto it = cont.find(make_pair(y[j], j));
      can.emplace_back(y[j], j);
      if(it != cont.begin()) {
        can.push_back(*prev(it));
      }
      if(next(it) != cont.end() && next(it) -> first <= h[i]) {
        can.push_back(*next(it));
      }
    }
    line[i] = can;
    for(int j : fin[i]) {
      cont.erase(make_pair(y[j], j));
    }
  }
  auto rightSide = [&] (int piv) {
    set <pair <int, int>> alive;
    for(int i = 0; i < x.size(); i++) {
      for(int j : start[i]) {
        alive.insert(make_pair(y[j], j));
      }
      if(i >= piv) {
        while(not alive.empty() && alive.begin() -> first <= h[i]) {
          line[i].emplace_back(*alive.begin());
          alive.erase(alive.begin());
        }
      }
      for(int j : fin[i]) {
        alive.erase(make_pair(y[j], j));
      }
    }
  };
  auto leftSide = [&] (int piv) {
    set <pair <int, int>> alive;
    for(int i = x.size() - 1; i >= 0; i--) {
      for(int j : fin[i]) {
        alive.insert(make_pair(y[j], j));
      }
      if(i <= piv) {
        while(not alive.empty() && alive.begin() -> first <= h[i]) {
          line[i].emplace_back(*alive.begin());
          alive.erase(alive.begin());
        }
      }
      for(int j : start[i]) {
        alive.erase(make_pair(y[j], j));
      }
    } 
  };
  leftSide(from);
  leftSide(to);
  rightSide(from);
  rightSide(to);
  for(int i = 0; i < x.size(); i++) {
    for(int j = 0; j < line[i].size(); j++) {
      if(line[i][j].second == -1) {
        if(i == from) src = node;
        else des = node;
      } else {
        bridge[line[i][j].second].emplace_back(x[i], node);
      }
      line[i][j].second = node++; 
    }
  }
  for(int i = 0; i < x.size(); i++) {
    sort(line[i].begin(), line[i].end());
    for(int j = 1; j < line[i].size(); j++) {
      addEdge(line[i][j - 1].second, line[i][j].second,
          line[i][j].first - line[i][j - 1].first);
    }
  }
  for(int i = 0; i < l.size(); i++) {
    sort(bridge[i].begin(), bridge[i].end());
    for(int j = 1; j < bridge[i].size(); j++) {
      addEdge(bridge[i][j - 1].second, bridge[i][j].second, 
          bridge[i][j].first - bridge[i][j - 1].first);
    }
  }
  auto ans = shortest_path(src, des);
  return ans >= inf ? -1 : ans;
}

Compilation message

walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:48:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |   for(int i = 0; i < l.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp:54:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for(int i = 0; i < x.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp: In lambda function:
walk.cpp:92:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |     for(int i = 0; i < x.size(); i++) {
      |                    ~~^~~~~~~~~~
walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:128:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  128 |   for(int i = 0; i < x.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp:129:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |     for(int j = 0; j < line[i].size(); j++) {
      |                    ~~^~~~~~~~~~~~~~~~
walk.cpp:139:20: 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 < x.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp:141:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  141 |     for(int j = 1; j < line[i].size(); j++) {
      |                    ~~^~~~~~~~~~~~~~~~
walk.cpp:146:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |   for(int i = 0; i < l.size(); i++) {
      |                  ~~^~~~~~~~~~
walk.cpp:148:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |     for(int j = 1; j < bridge[i].size(); j++) {
      |                    ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 56696 KB Output is correct
2 Correct 38 ms 56704 KB Output is correct
3 Correct 39 ms 56696 KB Output is correct
4 Correct 39 ms 56704 KB Output is correct
5 Correct 39 ms 56696 KB Output is correct
6 Correct 40 ms 56700 KB Output is correct
7 Correct 39 ms 56696 KB Output is correct
8 Correct 39 ms 56704 KB Output is correct
9 Correct 39 ms 56696 KB Output is correct
10 Correct 39 ms 56696 KB Output is correct
11 Correct 39 ms 56696 KB Output is correct
12 Correct 39 ms 56696 KB Output is correct
13 Correct 38 ms 56704 KB Output is correct
14 Correct 39 ms 56700 KB Output is correct
15 Correct 39 ms 56696 KB Output is correct
16 Correct 39 ms 56704 KB Output is correct
17 Correct 39 ms 56696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 56700 KB Output is correct
2 Correct 39 ms 56704 KB Output is correct
3 Correct 939 ms 129784 KB Output is correct
4 Correct 822 ms 133068 KB Output is correct
5 Correct 561 ms 113656 KB Output is correct
6 Correct 584 ms 113528 KB Output is correct
7 Correct 562 ms 113920 KB Output is correct
8 Correct 962 ms 129912 KB Output is correct
9 Correct 766 ms 130680 KB Output is correct
10 Correct 906 ms 132756 KB Output is correct
11 Correct 777 ms 127736 KB Output is correct
12 Correct 581 ms 121208 KB Output is correct
13 Correct 836 ms 134856 KB Output is correct
14 Correct 642 ms 118648 KB Output is correct
15 Correct 723 ms 122780 KB Output is correct
16 Correct 638 ms 120876 KB Output is correct
17 Correct 621 ms 118520 KB Output is correct
18 Correct 1357 ms 148096 KB Output is correct
19 Correct 57 ms 59896 KB Output is correct
20 Correct 266 ms 88728 KB Output is correct
21 Correct 581 ms 121428 KB Output is correct
22 Correct 597 ms 120472 KB Output is correct
23 Correct 942 ms 128568 KB Output is correct
24 Correct 599 ms 121332 KB Output is correct
25 Correct 589 ms 119936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 337 ms 90744 KB Output is correct
2 Correct 1070 ms 130424 KB Output is correct
3 Correct 1122 ms 131552 KB Output is correct
4 Correct 1292 ms 138228 KB Output is correct
5 Correct 1537 ms 137964 KB Output is correct
6 Correct 1495 ms 140492 KB Output is correct
7 Correct 579 ms 100980 KB Output is correct
8 Correct 566 ms 119676 KB Output is correct
9 Correct 1437 ms 143008 KB Output is correct
10 Correct 913 ms 132692 KB Output is correct
11 Correct 55 ms 58360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 337 ms 90744 KB Output is correct
2 Correct 1070 ms 130424 KB Output is correct
3 Correct 1122 ms 131552 KB Output is correct
4 Correct 1292 ms 138228 KB Output is correct
5 Correct 1537 ms 137964 KB Output is correct
6 Correct 1495 ms 140492 KB Output is correct
7 Correct 579 ms 100980 KB Output is correct
8 Correct 566 ms 119676 KB Output is correct
9 Correct 1437 ms 143008 KB Output is correct
10 Correct 913 ms 132692 KB Output is correct
11 Correct 55 ms 58360 KB Output is correct
12 Correct 1123 ms 131380 KB Output is correct
13 Correct 1028 ms 137464 KB Output is correct
14 Correct 1479 ms 137964 KB Output is correct
15 Correct 994 ms 123396 KB Output is correct
16 Correct 1050 ms 134768 KB Output is correct
17 Correct 1085 ms 136044 KB Output is correct
18 Correct 1018 ms 123500 KB Output is correct
19 Correct 1061 ms 135156 KB Output is correct
20 Correct 609 ms 100472 KB Output is correct
21 Correct 85 ms 60280 KB Output is correct
22 Correct 745 ms 122416 KB Output is correct
23 Correct 681 ms 123896 KB Output is correct
24 Correct 613 ms 117880 KB Output is correct
25 Correct 622 ms 120120 KB Output is correct
26 Correct 599 ms 112248 KB Output is correct
27 Correct 1577 ms 138864 KB Output is correct
28 Correct 909 ms 137208 KB Output is correct
29 Correct 1444 ms 140632 KB Output is correct
30 Correct 563 ms 100904 KB Output is correct
31 Correct 1443 ms 142908 KB Output is correct
32 Correct 773 ms 126500 KB Output is correct
33 Correct 776 ms 124404 KB Output is correct
34 Correct 931 ms 128724 KB Output is correct
35 Correct 833 ms 125952 KB Output is correct
36 Correct 675 ms 122080 KB Output is correct
37 Correct 605 ms 120568 KB Output is correct
38 Correct 604 ms 119672 KB Output is correct
39 Correct 1010 ms 127908 KB Output is correct
40 Correct 653 ms 120308 KB Output is correct
41 Correct 629 ms 119240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 56696 KB Output is correct
2 Correct 38 ms 56704 KB Output is correct
3 Correct 39 ms 56696 KB Output is correct
4 Correct 39 ms 56704 KB Output is correct
5 Correct 39 ms 56696 KB Output is correct
6 Correct 40 ms 56700 KB Output is correct
7 Correct 39 ms 56696 KB Output is correct
8 Correct 39 ms 56704 KB Output is correct
9 Correct 39 ms 56696 KB Output is correct
10 Correct 39 ms 56696 KB Output is correct
11 Correct 39 ms 56696 KB Output is correct
12 Correct 39 ms 56696 KB Output is correct
13 Correct 38 ms 56704 KB Output is correct
14 Correct 39 ms 56700 KB Output is correct
15 Correct 39 ms 56696 KB Output is correct
16 Correct 39 ms 56704 KB Output is correct
17 Correct 39 ms 56696 KB Output is correct
18 Correct 40 ms 56700 KB Output is correct
19 Correct 39 ms 56704 KB Output is correct
20 Correct 939 ms 129784 KB Output is correct
21 Correct 822 ms 133068 KB Output is correct
22 Correct 561 ms 113656 KB Output is correct
23 Correct 584 ms 113528 KB Output is correct
24 Correct 562 ms 113920 KB Output is correct
25 Correct 962 ms 129912 KB Output is correct
26 Correct 766 ms 130680 KB Output is correct
27 Correct 906 ms 132756 KB Output is correct
28 Correct 777 ms 127736 KB Output is correct
29 Correct 581 ms 121208 KB Output is correct
30 Correct 836 ms 134856 KB Output is correct
31 Correct 642 ms 118648 KB Output is correct
32 Correct 723 ms 122780 KB Output is correct
33 Correct 638 ms 120876 KB Output is correct
34 Correct 621 ms 118520 KB Output is correct
35 Correct 1357 ms 148096 KB Output is correct
36 Correct 57 ms 59896 KB Output is correct
37 Correct 266 ms 88728 KB Output is correct
38 Correct 581 ms 121428 KB Output is correct
39 Correct 597 ms 120472 KB Output is correct
40 Correct 942 ms 128568 KB Output is correct
41 Correct 599 ms 121332 KB Output is correct
42 Correct 589 ms 119936 KB Output is correct
43 Correct 337 ms 90744 KB Output is correct
44 Correct 1070 ms 130424 KB Output is correct
45 Correct 1122 ms 131552 KB Output is correct
46 Correct 1292 ms 138228 KB Output is correct
47 Correct 1537 ms 137964 KB Output is correct
48 Correct 1495 ms 140492 KB Output is correct
49 Correct 579 ms 100980 KB Output is correct
50 Correct 566 ms 119676 KB Output is correct
51 Correct 1437 ms 143008 KB Output is correct
52 Correct 913 ms 132692 KB Output is correct
53 Correct 55 ms 58360 KB Output is correct
54 Correct 1123 ms 131380 KB Output is correct
55 Correct 1028 ms 137464 KB Output is correct
56 Correct 1479 ms 137964 KB Output is correct
57 Correct 994 ms 123396 KB Output is correct
58 Correct 1050 ms 134768 KB Output is correct
59 Correct 1085 ms 136044 KB Output is correct
60 Correct 1018 ms 123500 KB Output is correct
61 Correct 1061 ms 135156 KB Output is correct
62 Correct 609 ms 100472 KB Output is correct
63 Correct 85 ms 60280 KB Output is correct
64 Correct 745 ms 122416 KB Output is correct
65 Correct 681 ms 123896 KB Output is correct
66 Correct 613 ms 117880 KB Output is correct
67 Correct 622 ms 120120 KB Output is correct
68 Correct 599 ms 112248 KB Output is correct
69 Correct 1577 ms 138864 KB Output is correct
70 Correct 909 ms 137208 KB Output is correct
71 Correct 1444 ms 140632 KB Output is correct
72 Correct 563 ms 100904 KB Output is correct
73 Correct 1443 ms 142908 KB Output is correct
74 Correct 773 ms 126500 KB Output is correct
75 Correct 776 ms 124404 KB Output is correct
76 Correct 931 ms 128724 KB Output is correct
77 Correct 833 ms 125952 KB Output is correct
78 Correct 675 ms 122080 KB Output is correct
79 Correct 605 ms 120568 KB Output is correct
80 Correct 604 ms 119672 KB Output is correct
81 Correct 1010 ms 127908 KB Output is correct
82 Correct 653 ms 120308 KB Output is correct
83 Correct 629 ms 119240 KB Output is correct
84 Correct 280 ms 83448 KB Output is correct
85 Correct 1162 ms 132856 KB Output is correct
86 Correct 1648 ms 146292 KB Output is correct
87 Correct 122 ms 66424 KB Output is correct
88 Correct 135 ms 67704 KB Output is correct
89 Correct 119 ms 66300 KB Output is correct
90 Correct 92 ms 64320 KB Output is correct
91 Correct 39 ms 56960 KB Output is correct
92 Correct 71 ms 59768 KB Output is correct
93 Correct 409 ms 87416 KB Output is correct
94 Correct 86 ms 61304 KB Output is correct
95 Correct 765 ms 124412 KB Output is correct
96 Correct 706 ms 123768 KB Output is correct
97 Correct 638 ms 119936 KB Output is correct
98 Correct 653 ms 119336 KB Output is correct
99 Correct 2005 ms 152152 KB Output is correct
100 Correct 1306 ms 138776 KB Output is correct
101 Correct 1720 ms 143528 KB Output is correct
102 Correct 636 ms 100824 KB Output is correct
103 Correct 828 ms 126232 KB Output is correct
104 Correct 780 ms 124080 KB Output is correct
105 Correct 1043 ms 128092 KB Output is correct
106 Correct 868 ms 127436 KB Output is correct
107 Correct 874 ms 124304 KB Output is correct
108 Correct 104 ms 63224 KB Output is correct
109 Correct 1206 ms 123552 KB Output is correct
110 Correct 988 ms 134392 KB Output is correct
111 Correct 956 ms 133580 KB Output is correct
112 Correct 829 ms 126048 KB Output is correct
113 Correct 733 ms 123128 KB Output is correct