Submission #499142

# Submission time Handle Problem Language Result Execution time Memory
499142 2021-12-27T09:44:30 Z 600Mihnea Golf (JOI17_golf) C++17
30 / 100
10000 ms 33300 KB
#include <bits/stdc++.h>

using namespace std;

#define y1 ynot1

typedef long long ll;
typedef long double ld;

struct Box {
  int xmin;
  int xmax;
  int ymin;
  int ymax;

};

const int N = 100000 + 7;
const int INF = (int) 1e9 + 7;

int n;
int x1;
int y1;
int x2;
int y2;
Box boxes[N];

void normalization() {
  map<int, int> mx, my;

  for (int i = 0; i <= n + 1; i++) {
    mx[boxes[i].xmin] = 0;
    mx[boxes[i].xmax] = 0;

    my[boxes[i].ymin] = 0;
    my[boxes[i].ymax] = 0;
  }

  int c = 0;
  for (auto &it : mx) {
    it.second = ++c;
  }

  c = 0;
  for (auto &it : my) {
    it.second = ++c;
  }


  for (int i = 0; i <= n + 1; i++) {
    boxes[i].xmin = mx[boxes[i].xmin];
    boxes[i].xmax = mx[boxes[i].xmax];

    boxes[i].ymin = my[boxes[i].ymin];
    boxes[i].ymax = my[boxes[i].ymax];
  }
}

struct Segment {
  int where;
  int low;
  int high;
  int e_low;
  int e_high;
  int dp;
};

vector<vector<Segment>> segs;


void calculateextensions() {
  vector<Segment> xSegs;
  vector<Segment> ySegs;
  for (int step = 1; step <= 2; step++) {
    /// calculate x segs
    for (int i = 0; i <= n + 1; i++) {
      xSegs.push_back({boxes[i].ymin, boxes[i].xmin, boxes[i].xmax, 0, 2 * N, -1});
      xSegs.push_back({boxes[i].ymax, boxes[i].xmin, boxes[i].xmax, 0, 2 * N, -1});
    }

    for (int i = 0; i <= n + 1; i++) {
      swap(boxes[i].xmin, boxes[i].ymin);
      swap(boxes[i].xmax, boxes[i].ymax);
    }

    swap(xSegs, ySegs);
  }

  assert((int) xSegs.size() == 2 * (n + 2));
  assert((int) ySegs.size() == 2 * (n + 2));

  for (int step = 1; step <= 2; step++) {
    /// compute the extensions
    for (auto &myseg : xSegs) {
      for (auto &othseg : ySegs) {
        if (othseg.low < myseg.where && myseg.where < othseg.high) {
          if (othseg.where < myseg.low) {
            myseg.e_low = max(myseg.e_low, othseg.where);
          } else {
            assert(myseg.high < othseg.where);
            myseg.e_high = min(myseg.e_high, othseg.where);
          }
        }
      }
    }

    /// checker

    for (auto &it : xSegs) {
      assert(it.e_low <= it.low);
      assert(it.high <= it.e_high);
    }

    swap(xSegs, ySegs);
  }
  segs.push_back(xSegs);
  segs.push_back(ySegs);
}

queue<pair<int, int>> q;

void addToQ(int type, int index, int value) {
  assert(0 <= type && type < 2);
  assert(0 <= index && index < (int) segs[type].size());
  assert(segs[type][index].dp == -1);
  segs[type][index].dp = value;
  q.push({type, index});
}


signed main() {
  ios::sync_with_stdio(0); cin.tie(0);

  ///freopen ("input", "r", stdin);

  cin >> x1 >> y1 >> x2 >> y2;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> boxes[i].xmin >> boxes[i].xmax >> boxes[i].ymin >> boxes[i].ymax;
  }

  boxes[0].xmin = boxes[0].xmax = x1;
  boxes[0].ymin = boxes[0].ymax = y1;

  boxes[n + 1].xmin = boxes[n + 1].xmax = x2;
  boxes[n + 1].ymin = boxes[n + 1].ymax = y2;

  normalization(); /// do it later

  calculateextensions();



  addToQ(0, 0, 1);
  addToQ(0, 1, 1);
  addToQ(1, 0, 1);
  addToQ(1, 1, 1);

  while (!q.empty()) {
    auto itQ = q.front();
    q.pop();
    int type = itQ.first;
    int index = itQ.second;


    int dp = segs[type][index].dp;

    assert(2 * (n + 2) == (int) segs[type ^ 1].size());

    for (int j = 0; j < 2 * (n + 2); j++) {
      if (segs[type ^ 1][j].dp == -1
          && segs[type ^ 1][j].e_low <= segs[type][index].where && segs[type][index].where <= segs[type ^ 1][j].e_high
          && segs[type][index].e_low <= segs[type ^ 1][j].where && segs[type ^ 1][j].where <= segs[type][index].e_high) {
        addToQ(type ^ 1, j, segs[type][index].dp + 1);
      }
    }
  }

  for (auto &v : segs) {
    for (auto &seg : v) {
      assert(seg.dp != -1);
    }
  }

  x2 = boxes[n + 1].xmin;
  y2 = boxes[n + 1].ymin;

  int sol = INF;

  swap(x2, y2);

  for (auto &v : segs) {
    for (auto &seg : v) {
      if (x2 == seg.where && seg.e_low <= y2 && y2 <= seg.e_high) {
        sol = min(sol, seg.dp);
      }
    }
    swap(x2, y2);
  }

  cout << sol << "\n";


  return 0;
}

Compilation message

golf.cpp: In function 'int main()':
golf.cpp:166:9: warning: unused variable 'dp' [-Wunused-variable]
  166 |     int dp = segs[type][index].dp;
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 37 ms 584 KB Output is correct
6 Correct 42 ms 552 KB Output is correct
7 Correct 36 ms 548 KB Output is correct
8 Correct 39 ms 592 KB Output is correct
9 Correct 39 ms 628 KB Output is correct
10 Correct 37 ms 556 KB Output is correct
11 Correct 37 ms 536 KB Output is correct
12 Correct 39 ms 592 KB Output is correct
13 Correct 37 ms 592 KB Output is correct
14 Correct 38 ms 608 KB Output is correct
15 Correct 4 ms 336 KB Output is correct
16 Correct 13 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 37 ms 584 KB Output is correct
6 Correct 42 ms 552 KB Output is correct
7 Correct 36 ms 548 KB Output is correct
8 Correct 39 ms 592 KB Output is correct
9 Correct 39 ms 628 KB Output is correct
10 Correct 37 ms 556 KB Output is correct
11 Correct 37 ms 536 KB Output is correct
12 Correct 39 ms 592 KB Output is correct
13 Correct 37 ms 592 KB Output is correct
14 Correct 38 ms 608 KB Output is correct
15 Correct 4 ms 336 KB Output is correct
16 Correct 13 ms 464 KB Output is correct
17 Correct 38 ms 568 KB Output is correct
18 Correct 41 ms 592 KB Output is correct
19 Correct 38 ms 596 KB Output is correct
20 Correct 39 ms 612 KB Output is correct
21 Correct 39 ms 616 KB Output is correct
22 Correct 40 ms 648 KB Output is correct
23 Correct 41 ms 592 KB Output is correct
24 Correct 43 ms 688 KB Output is correct
25 Correct 40 ms 696 KB Output is correct
26 Correct 41 ms 648 KB Output is correct
27 Correct 6 ms 464 KB Output is correct
28 Correct 16 ms 480 KB Output is correct
29 Correct 17 ms 572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 37 ms 584 KB Output is correct
6 Correct 42 ms 552 KB Output is correct
7 Correct 36 ms 548 KB Output is correct
8 Correct 39 ms 592 KB Output is correct
9 Correct 39 ms 628 KB Output is correct
10 Correct 37 ms 556 KB Output is correct
11 Correct 37 ms 536 KB Output is correct
12 Correct 39 ms 592 KB Output is correct
13 Correct 37 ms 592 KB Output is correct
14 Correct 38 ms 608 KB Output is correct
15 Correct 4 ms 336 KB Output is correct
16 Correct 13 ms 464 KB Output is correct
17 Correct 38 ms 568 KB Output is correct
18 Correct 41 ms 592 KB Output is correct
19 Correct 38 ms 596 KB Output is correct
20 Correct 39 ms 612 KB Output is correct
21 Correct 39 ms 616 KB Output is correct
22 Correct 40 ms 648 KB Output is correct
23 Correct 41 ms 592 KB Output is correct
24 Correct 43 ms 688 KB Output is correct
25 Correct 40 ms 696 KB Output is correct
26 Correct 41 ms 648 KB Output is correct
27 Correct 6 ms 464 KB Output is correct
28 Correct 16 ms 480 KB Output is correct
29 Correct 17 ms 572 KB Output is correct
30 Execution timed out 10080 ms 33300 KB Time limit exceeded
31 Halted 0 ms 0 KB -