# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
386292 | model_code | Event Hopping 2 (JOI21_event2) | C++17 | 258 ms | 28800 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.
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
constexpr int MAX_N = 100'000;
constexpr int MAX_logN = 18;
constexpr int MAX_LR = 1'000'000'000;
int N, K;
int L[MAX_N], R[MAX_N];
vector<int> z;
vector<int> invL[MAX_N * 2 + 1];
int doubling[MAX_N * 2 + 1][MAX_logN];
int count(int l, int r) {
int res = 0;
for (int j = MAX_logN - 1; j >= 0; j--) {
if (doubling[l][j] <= r) {
l = doubling[l][j];
res += 1 << j;
}
}
return res;
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d%d", L + i, R + i);
z.push_back(L[i]);
z.push_back(R[i]);
}
sort(z.begin(), z.end());
z.erase(unique(z.begin(), z.end()), z.end());
for (int i = 0; i < N; i++) {
L[i] = lower_bound(z.begin(), z.end(), L[i]) - z.begin();
R[i] = lower_bound(z.begin(), z.end(), R[i]) - z.begin();
}
for (int i = 0; i < N; i++) invL[L[i]].push_back(i);
int r = z.size();
for (int i = z.size(); i >= 0; i--) {
for (int j = 0; j < invL[i].size(); j++) r = min(r, R[invL[i][j]]);
doubling[i][0] = r;
}
for (int i = 1; i < MAX_logN; i++) {
for (int j = 0; j <= z.size(); j++) {
doubling[j][i] = doubling[doubling[j][i - 1]][i - 1];
}
}
int now = count(0, z.size() - 1);
if (now < K) {
printf("-1\n");
return 0;
}
set<pair<int, int>> used = {{0, 0}, {z.size() - 1, z.size() - 1}};
for (int i = 0; used.size() - 2 < K; i++) {
int l = prev(used.lower_bound({R[i], 0}))->second;
int r = used.lower_bound({R[i], 0})->first;
if (L[i] < l) continue;
int tmp = now - count(l, r) + count(l, L[i]) + 1 + count(R[i], r);
if (tmp >= K) {
printf("%d\n", i + 1);
now = tmp;
used.insert({L[i], R[i]});
}
}
}
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... |