Submission #659842

# Submission time Handle Problem Language Result Execution time Memory
659842 2022-11-19T12:06:23 Z 600Mihnea Event Hopping (BOI22_events) C++17
10 / 100
1500 ms 2024 KB
bool home = 0;
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = (int) 1e5 + 7;
int n;
int q;

struct T {
  int l;
  int r;
  int init;
};

bool operator < (T a, T b) {
  if (a.r != b.r) {
    return a.r < b.r;
  } else {
    return a.l < b.l;
  }
}

T v[N];
int dp[N];
int nxt[N];
int ant[N];
int inv[N];
int low[N];
int high[N];

int cnt_rs_under(int limit) {
  int low = 1, high = n, cnt = 0;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (v[mid].r <= limit) {
      cnt = mid;
      low = mid + 1;
    } else {
      high = mid - 1;
    }
  }
  return cnt;
}

int main() {
  if (!home) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  } else {
    freopen("input.txt", "r", stdin);
  }

  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> v[i].l >> v[i].r;
    v[i].init = i;
  }
  sort(v + 1, v + n + 1);
  for (int i = 1; i <= n; i++) {
    int x = cnt_rs_under(v[i].l - 1) + 1;
    int y = cnt_rs_under(v[i].r);
    low[i] = x;
    high[i] = y;
    for (int j = 1; j <= n; j++) {
      assert((v[i].l <= v[j].r && v[j].r <= v[i].r) == (x <= j && j <= y));
    }
  }
  for (int i = 1; i <= n; i++) {
    inv[v[i].init] = i;
  }
  for (int i = 1; i <= n; i++) {
    nxt[i] = -1;
    for (int j = 1; j <= n; j++) {
      if (v[j].l <= v[i].r) {
        if (nxt[i] == -1) {
          nxt[i] = j;
        } else {
          if (v[j].r > v[nxt[i]].r) {
            nxt[i] = j;
          }
        }
      }
    }
    if (nxt[i] != -1 && v[i].r >= v[nxt[i]].r) {
      nxt[i] = -1;
    }
  }
  for (int i = 1; i <= n; i++) {
    ant[i] = -1;
    for (int j = 1; j <= n; j++) {
      if (v[i].l <= v[j].r && v[j].r <= v[i].l) {
        if (ant[i] == -1) {
          ant[i] = j;
        }
      } else {
        if (v[j].l < v[ant[i]].l) {
          ant[i] = j;
        }
      }
    }
  }
  for (int iq = 1; iq <= q; iq++) {
    int ff, ss;
    cin >> ff >> ss;
    ff = inv[ff];
    ss = inv[ss];
    int L = ss, R = ss, cost = 0;
    while (!(L <= ff && ff <= R)) {
      int L2 = L, R2 = R;
      for (int j = L; j <= R; j++) {
        if (low[j] <= high[j]) {
          L2 = min(L2, low[j]);
          R2 = max(R2, high[j]);
        }
      }
      if (L2 == L && R2 == R) {
        break;
      }
      L = L2;
      R = R2;
      cost++;
    }
    if (!(L <= ff && ff <= R)) {
      cout << "impossible\n";
    } else {
      cout << cost << "\n";
    }
    continue;
    for (int i = 1; i <= n; i++) {
      dp[i] = -1;
    }
    int co = 0;
    while (nxt[ff] != -1 && v[nxt[ff]].r <= v[ss].l) {
      ff = nxt[ff];
      co++;
    }
    while (ant[ss] != -1 && v[ant[ss]].l >= v[ss].r) {
      ss = ant[ss];
      co++;
    }
    queue<int> q;
    q.push(ff);
    dp[ff] = 0;
    while (!q.empty()) {
      int a = q.front();
      q.pop();
      for (int b = 1; b <= n; b++) {
        if (v[b].l <= v[a].r && v[a].r <= v[b].r && dp[b] == -1) {
          dp[b] = 1 + dp[a];
          q.push(b);
        }
      }
    }
    if (dp[ss] == -1) {
      cout << "impossible\n";
      continue;
    }
    cout << co + dp[ss] << "\n";
  }

  return 0;
}
/**

**/

Compilation message

events.cpp: In function 'int main()':
events.cpp:51:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Execution timed out 1574 ms 1512 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 57 ms 340 KB Output is correct
4 Correct 8 ms 380 KB Output is correct
5 Correct 12 ms 380 KB Output is correct
6 Correct 5 ms 340 KB Output is correct
7 Correct 5 ms 380 KB Output is correct
8 Correct 4 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 57 ms 340 KB Output is correct
4 Correct 8 ms 380 KB Output is correct
5 Correct 12 ms 380 KB Output is correct
6 Correct 5 ms 340 KB Output is correct
7 Correct 5 ms 380 KB Output is correct
8 Correct 4 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 57 ms 340 KB Output is correct
13 Correct 7 ms 340 KB Output is correct
14 Correct 12 ms 380 KB Output is correct
15 Correct 5 ms 372 KB Output is correct
16 Correct 5 ms 340 KB Output is correct
17 Correct 4 ms 340 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Execution timed out 1589 ms 456 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 57 ms 340 KB Output is correct
4 Correct 8 ms 380 KB Output is correct
5 Correct 12 ms 380 KB Output is correct
6 Correct 5 ms 340 KB Output is correct
7 Correct 5 ms 380 KB Output is correct
8 Correct 4 ms 340 KB Output is correct
9 Correct 4 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 57 ms 368 KB Output is correct
13 Correct 8 ms 392 KB Output is correct
14 Correct 13 ms 340 KB Output is correct
15 Correct 5 ms 340 KB Output is correct
16 Correct 5 ms 340 KB Output is correct
17 Correct 4 ms 340 KB Output is correct
18 Correct 6 ms 380 KB Output is correct
19 Execution timed out 1590 ms 2024 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1543 ms 1692 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Execution timed out 1574 ms 1512 KB Time limit exceeded
3 Halted 0 ms 0 KB -