This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<iii, null_type, greater<iii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int N, K, curr, L[500005], R[500005], dep[500005], anc[500005][25];
bool vis[500005];
ii mx[500005];
multiset<ii> ft[500005];
multiset<ii> untaken;
vector<int> v, adj[500005];
void dfs(int n, int e = -1) {
anc[n][0] = e;
vis[n] = 1;
for (int i = 1; i <= 20; i++)
if (anc[n][i - 1] != -1) anc[n][i] = anc[anc[n][i - 1]][i - 1];
for (auto u : adj[n]) if (u != e) {
dep[u] = dep[n] + 1;
dfs(u, n);
}
}
inline int ls(int x) { return x & -x; }
ii qry(int p) {
ii ret = mp(LLONG_MAX, LLONG_MAX);
for (; p <= 2 * N; p += ls(p))
if (!ft[p].empty()) ret = min(ret, *ft[p].begin());
return ret;
}
void upd(int x) {
for (int p = L[x]; p; p -= ls(p))
ft[p].emplace(R[x], x);
}
int get(int l, int r) {
if (mx[l].second == 0) return 0;
int curr = mx[l].second;
if (R[curr] > r) return 0;
for (int i = 20; i >= 0; i--)
if (anc[curr][i] != -1 && R[anc[curr][i]] <= r) curr = anc[curr][i];
return dep[mx[l].second] - dep[curr] + 1;
}
int forceadd(int x, bool b = 0) {
// add [L[x], R[x]]
auto it = untaken.lower_bound(mp(R[x], 0ll));
if (it == untaken.begin()) return -1;
--it;
if (it->first <= L[x] && R[x] <= it->second) {
if (b == 1) {
untaken.emplace(it->first, L[x]);
untaken.emplace(R[x], it->second);
untaken.erase(it);
}
int tmp = 1 + curr + -get(it->first, it->second) + get(it->first, L[x]) + get(R[x], it->second);
if (b) curr = tmp;
return tmp;
} else return -1;
}
main() {
memset(anc, -1, sizeof anc);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> K;
for (int i = 1; i <= N; i++) cin >> L[i] >> R[i], v.pb(L[i]), v.pb(R[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= N; i++) {
L[i] = lower_bound(v.begin(), v.end(), L[i]) - v.begin() + 1;
R[i] = lower_bound(v.begin(), v.end(), R[i]) - v.begin() + 1;
upd(i);
}
for (int i = 1; i <= N; i++) {
auto tmp = qry(R[i]);
if (tmp.second != LLONG_MAX) adj[tmp.second].pb(i);
}
for (int i = 1; i <= N; i++)
if (!vis[i]) dfs(i);
for (int i = 1; i <= 2 * N; i++)
mx[L[i]] = max(mx[L[i]], mp(dep[i] + 1, i));
for (int i = 2 * N; i >= 1; i--) mx[i] = max(mx[i], mx[i + 1]);
untaken.emplace(1, 2 * N);
curr = get(1, 2 * N);
if (get(1, 2 * N) < K) return cout << "-1\n", 0;
for (int i = 1, cnt = 0; i <= N; i++) {
int x = forceadd(i);
if (x >= K) {
cout << i << '\n';
cnt++;
if (cnt == K) return 0;
forceadd(i, 1);
}
}
}
Compilation message (stderr)
event2.cpp:86:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
86 | main() {
| ^~~~
# | 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... |