# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1097929 | thangdz2k7 | Event Hopping 2 (JOI21_event2) | C++17 | 127 ms | 25296 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : thembululquaUwU
// 3.9.2024
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const int LG = 20;
void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fd(sr, x) (lower_bound(sr.begin(), sr.end(), x) - sr.begin())
#define uq(sr) (sr).erase(unique(all((sr))), (sr).end())
#define szv(v) (int((v).size()))
int n, k, l[N], r[N], to[N][20];
int get(int u, int v){
int ans = 0;
for (int l = LG - 1; l >= 0; -- l){
if (to[u][l] <= v){
ans += (1 << l);
u = to[u][l] + 1;
}
}
return ans;
}
void solve(){
cin >> n >> k; vi sr;
for (int i = 1; i <= n; ++ i){
cin >> l[i] >> r[i];
r[i] --; sr.pb(l[i]); sr.pb(r[i]);
}
sort(all(sr)); uq(sr); int sz = sr.size();
for (int i = 0; i <= sz + 1; ++ i)
for (int j = 0; j < LG; ++ j) to[i][j] = sz + 1;
for (int i = 1; i <= n; ++ i){
l[i] = fd(sr, l[i]) + 1;
r[i] = fd(sr, r[i]) + 1;
minl(to[l[i]][0], r[i]);
}
for (int i = sz; i >= 1; -- i){
minl(to[i][0], to[i + 1][0]);
for (int j = 1; j < LG; ++ j){
int nxt = to[i][j - 1] + 1;
if (nxt > sz) break;
to[i][j] = to[nxt][j - 1];
}
}
set <ii> s; s.insert({0, 0}); s.insert({sz + 1, sz + 1});
vector <int> ans; int T = get(1, sz);
if (T < k){
cout << -1; return;
}
for (int i = 1; i <= n; ++ i){ if (ans.size() >= k) break;
auto pl = *(--s.upper_bound({r[i], r[i]}));
auto pr = *s.lower_bound({l[i], l[i]});
if (pl.se >= l[i] || pr.fi <= r[i]) continue;
int c = get(pl.se + 1, l[i] - 1) + get(r[i] + 1, pr.fi - 1) + 1 - get(pl.se + 1, pr.fi - 1);
if (T + c >= k){
T += c; ans.pb(i);
s.insert({l[i], r[i]});
}
}
for (int i : ans) cout << i << endl;
}
int main(){
if (fopen("pqh.inp", "r")){
freopen("pqh.inp", "r", stdin);
freopen("pqh.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; // cin >> t;
while (t --) solve();
return 0;
}
Compilation message (stderr)
# | 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... |