#include <bits/stdc++.h>
using namespace std;
#define POPCOUNT(n) (__builtin_popcountll((n)))
#define CLZ(n) (__builtin_clzll((n)))
#define CTZ(n) (__builtin_ctzll((n)))
#define LOG(n) (63 - __builtin_clzll((n)))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define MASK(i) (1ll << (i))
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))
#define Int __int128
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;
template <class T1, class T2> bool maximize(T1 &x, T2 y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T1, class T2> bool minimize(T1 &x, T2 y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <class T> void remove_duplicate(vector<T> &ve) {
sort (ve.begin(), ve.end());
ve.resize(unique(ve.begin(), ve.end()) - ve.begin());
}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
long long random(long long l, long long r) {
return uniform_int_distribution<long long>(l, r)(rng);
}
unsigned long long random(unsigned long long l, unsigned long long r) {
return uniform_int_distribution<unsigned long long>(l, r)(rng);
}
template <class T> T random(T r) {
return rng() % r;
}
const int N = 1e5 + 5, LG = 18;
const int MOD = 1e9 + 7;
const int inf = 1e9 + 69;
const long long INF = 1e18;
int n, k;
int L[N], R[N], maxLeft[N], nxt[N][LG];
pii a[N];
set<pii> s;
int best;
int findNext(int curR) {
int low = 1, high = n, pos = 0;
while (low <= high) {
int mid = (low + high) >> 1;
if (maxLeft[mid] >= curR) high = (pos = mid) - 1;
else low = mid + 1;
}
return pos;
}
int count(int l, int r) {
int id = findNext(l);
if (a[id].fi > r) return 0;
int ans = 1;
for (int i = LG - 1; i >= 0; --i) if (a[nxt[id][i]].fi <= r) {
id = nxt[id][i];
ans += MASK(i);
}
return ans;
}
bool add(int need, int l, int r) {
auto it = s.upper_bound(make_pair(l, inf));
if (it == s.begin()) return false;
it = prev(it);
if (it->se < r) return false;
int newBest = best;
newBest -= count(it->fi, it->se);
newBest += count(it->fi, l);
newBest += count(r, it->se);
if (newBest >= need) {
pii tmp = *it; s.erase(it);
s.emplace(tmp.fi, l);
s.emplace(r, tmp.se);
best = newBest;
return true;
}
return false;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> L[i] >> R[i];
for (int i = 1; i <= n; ++i) a[i] = make_pair(R[i], L[i]);
sort (a + 1, a + 1 + n);
a[0] = make_pair(inf, inf);
for (int i = 1; i <= n; ++i)
maxLeft[i] = max(maxLeft[i - 1], a[i].se);
for (int i = 1; i <= n; ++i) nxt[i][0] = findNext(a[i].fi);
for (int i = 1; i < LG; ++i) for (int j = 1; j <= n; ++j) {
nxt[j][i] = nxt[nxt[j][i - 1]][i - 1];
}
s.emplace(1, inf - 1);
best = count(1, inf - 1);
if (best < k) return cout << -1, 0;
vector<int> ans;
for (int i = 1; i <= n; ++i) {
if (add(k - ans.size() - 1, L[i], R[i])) ans.emplace_back(i);
if (ans.size() == k) break;
}
// for (auto &x : ans) cerr << x << ' ';
// cerr << '\n';
assert(ans.size() == k);
for (auto &x : ans) cout << x << '\n';
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |