제출 #1341894

#제출 시각아이디문제언어결과실행 시간메모리
1341894ramzialoulouFire (BOI24_fire)C++20
0 / 100
1 ms344 KiB
#include <bits/stdc++.h>
 
using namespace std;

const int inf = int(1e9);
const int N = 5009 * 50;
int id = 1;
int sum[N];
int lazy[N];
int L[N];
int R[N];

void push(int nd, int l, int r) {
  if (!L[nd]) {
    L[nd] = ++id;
    R[nd] = ++id;
  }
  if (lazy[nd] == 0) return;
  int mid = (l + r) / 2;
  sum[L[nd]] = (mid - l + 1) * lazy[nd];
  sum[R[nd]] = (r - mid) * lazy[nd];
  lazy[L[nd]] = lazy[nd];
  lazy[R[nd]] = lazy[nd];
}

void upd(int nd, int l, int r, int x, int y) {
  push(nd, l, r);
  if (x <= l && r <= y) {
    sum[nd] = r - l + 1;
    lazy[nd] = 1;
    return;
  }
  if (l > y || r < x) {
    return;
  }
  int mid = (l + r) / 2;
  upd(L[nd], l, mid, x, y);
  upd(R[nd], mid + 1, r, x, y);
  sum[nd] = sum[L[nd]] + sum[R[nd]];
}

int get(int nd, int l, int r, int x, int y) {
  push(nd, l, r);
  if (x <= l && r <= y) {
    return sum[nd];
  }
  if (l > y || r < x) {
    return 0;
  }
  int mid = (l + r) / 2;
  return get(L[nd], l, mid, x, y) + get(R[nd], mid + 1, r, x, y);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> s(n), e(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> e[i];
    e[i]--;
  }
  int ans = -1;
  for (int z = 1; z <= n; z++) {
    int mx = 0;
    int idx = -1;
    for (int i = 0; i < n; i++) {
      if (s[i] <= e[i]) {
        int x = get(1, 0, inf, s[i], e[i]);
        if (e[i] - s[i] + 1 - x > mx) {
          mx = e[i] - s[i] + 1 - x;
          idx = i;
        }
        //cout << "z: " << z << ' ' << "i: " << i << ' ' << "points: " << e[i] - s[i] + 1 - x << ' ' << "mx: " << mx << ' ' << "idx: " << idx << '\n';
      } else {
        int x = get(1, 0, inf, 0, e[i]) + get(1, 0, inf, s[i], m - 1);
        //cout << "z: " << z << ' ' << "i: " << i << ' ' << "x: " << x << ' ' << "res: " << s[i] + 1 + m - e[i] + 1 << '\n';
        if (e[i] + 1 + m - s[i] - x > mx) {
          mx = e[i] + 1 + m - s[i] - x;
          idx = i;
        }
        //cout << "z: " << z << ' ' << "i: " << i << ' ' << "points: " <<  s[i] + 1 + m - e[i] + 1 - x << ' ' << "mx: " << mx << ' ' << "idx: " << idx << '\n';
      }
    }
    if (idx == -1) break;
    if (s[idx] > e[idx]) {
      upd(1, 0, inf, 0, e[idx]);
      upd(1, 0, inf, s[idx], m - 1);
    } else {
      upd(1, 0, inf, s[idx], e[idx]);
    }
    if (sum[1] == m) {
      ans = z;
      break;
    }
    //cout << "idx: " << idx << ' ' << "sum[1]: " << sum[1] << '\n';
  }
  cout << ans << '\n';
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...