Submission #1347167

#TimeUsernameProblemLanguageResultExecution timeMemory
1347167LIAEvent Hopping 2 (JOI21_event2)C++17
0 / 100
115 ms51264 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define v vector
#define lp(i, s, e) for (int i = s; i < e; ++i)
ll logi = 20;
v<v<ll>> bl;
ll query(ll a, ll b) {
  if (a >= b)
    return 0;
  ll ans = 0;
  for (int j = logi; j >= 0; --j) {
    if (bl[a][j] <= b) {
      ans += (1LL << j);
      a = bl[a][j];
    }
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, k;
  cin >> n >> k;
  v<pair<ll, ll>> rg(n);
  v<ll> V;

  lp(i, 0, n) {
    cin >> rg[i].first >> rg[i].second;
    V.push_back(rg[i].first);
    V.push_back(rg[i].second);
  }

  sort(V.begin(), V.end());
  V.erase(unique(V.begin(), V.end()), V.end());
  ll m = V.size();

  bl.assign(m + 2, v<ll>(logi + 1, m + 1));
  v<ll> min_r(m + 2, m + 1);

  lp(i, 0, n) {
    ll l = lower_bound(V.begin(), V.end(), rg[i].first) - V.begin() + 1;
    ll r = lower_bound(V.begin(), V.end(), rg[i].second) - V.begin() + 1;
    min_r[l] = min(min_r[l], r);
    rg[i] = {l, r};
  }

  for (ll i = m; i >= 1; --i) {
    min_r[i] = min(min_r[i], min_r[i + 1]);
    bl[i][0] = min_r[i];
  }

  lp(j, 1, logi + 1) {
    lp(i, 1, m + 2) {
      if (bl[i][j - 1] <= m) {
        bl[i][j] = bl[bl[i][j - 1]][j - 1];
      }
    }
  }

  set<pair<ll, ll>> st;
  st.insert({1, m});
  ll total = query(1, m);

  if (total < k) {
    cout << -1 << '\n';
    return 0;
  }

  v<ll> path;
  lp(i, 0, n) {
    if (k == 0)
      break;
    ll l = rg[i].first;
    ll r = rg[i].second;

    auto it = st.upper_bound({l, 2e9});
    if (it == st.begin())
      continue;
    --it;
    if (it->first <= l && r <= it->second) {
      ll X = it->first;
      ll Y = it->second;
      ll old_val = query(X, Y);
      ll new_val = query(X, l) + 1 + query(r, Y);

      if (total - old_val + new_val >= k-1) {
        total = total - old_val + new_val;
        k--;
        path.push_back(i + 1);
        st.erase(it);
        if (X < l)
          st.insert({X, l});
        if (r < Y)
          st.insert({r, Y});
      }
    }
  }

  for (auto it : path) {
    cout << it << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...