# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
494823 | blue | Event Hopping 2 (JOI21_event2) | C++17 | 0 ms | 0 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 <iostream>
#include <vector>
#include <map>
#include <set>
#include <cassert>
using namespace std;
const int mx = 200'000;
const int INF = 1'000'000'001;
const int lg = 19;
using vi = vector<int>;
using pii = pair<int, int>;
#define sz(x) int(x.size())
int jmp[1+mx+1][1+lg];
vector<pii> events;
int query(int L, int R) //
{
int ans = 0;
for(auto f: events)
{
if(f.first < L) continue;
if(f.second > R) continue;
ans++;
L = f.second;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
set<int> V;
int L[1+N], R[1+N];
for(int i = 1; i <= N; i++)
{
cin >> L[i] >> R[i];
V.insert(L[i]);
V.insert(R[i]);
}
map<int, int> mp;
int ct = 0;
for(int v: V) mp[v] = ++ct;
// cerr << "normalized\n";
// for(auto y: mp) cerr << y.first << " : " << y.second << '\n';
for(int i = 1; i <= N; i++)
{
L[i] = mp[L[i]];
R[i] = mp[R[i]];
// cerr << L[i] << ' ' << R[i] << '\n';
events.push_back({L[i], R[i]});
}
sort(events.begin(), events.end());
// cerr << "\n\n";
vi jmp_val(2+mx, 1+mx);
jmp_val[mx+1] = mx+1;
for(int i = 1; i <= N; i++) jmp_val[L[i]] = min(jmp_val[L[i]], R[i]);
for(int p = mx; p >= 1; p--) jmp_val[p] = min(jmp_val[p], jmp_val[p+1]);
for(int i = 1; i <= mx+1; i++)
jmp[i][0] = jmp_val[i];
for(int e = 1; e <= lg; e++)
{
for(int i = 1; i <= mx+1; i++)
{
jmp[i][e] = jmp[jmp[i][e-1]][e-1];
}
}
// jmp[i][e] = jmp[ jmp[i][e-1] ][e-1];
int max_events = query(1, mx);
if(max_events < K)
{
cout << "-1\n";
return 0;
}
// cerr << "max events = " << max_events << '\n';
vi res;
set<pii> events;
events.insert({1, 1});
events.insert({mx, mx});
// for(int i = 1; i <= 20; i++) cerr << jmp_val[i] << ' ';
// cerr << '\n';
for(int i = 1; i <= N && sz(res) < K; i++)
{
// cerr << "\n\n\n\n\n";
// cerr << "i = " << i << '\n';
auto y2 = events.lower_bound({L[i], R[i]});
auto y1 = y2;
y1--;
if(L[i] < y1->second || y2->first < R[i]) continue;
// assert(y1->first <= L[i]);
// assert(y1->second <= L[i]);
// assert(y2->first >= R[i]);
// assert(y2->second >= R[i]);
// cerr << "bounds = " << y1->second << ' ' << y2->first << '\n';
// cerr << query(y1->second, L[i]) << ' ' << query(R[i], y2->first) << ' ' << query(y1->second, y2->first) << '\n';
int change = query(y1->second, L[i]) + 1 + query(R[i], y2->first) - query(y1->second, y2->first);
if(max_events + change >= K)
{
// cerr << "added\n";
events.insert({L[i], R[i]});
res.push_back(i);
}
max_events += change;
// cerr << "new max events = " << max_events << '\n';
}
for(int r: res) cout << r << '\n';
// cout << '\n';
}