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>
#include <algorithm>
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];
int query(int L, int R) //
{
// cerr << "\n\n";
// cerr << "query: " << L << ' ' << R << '\n';
int ans = 0;
for(int e = lg; e >= 0; e--)
{
if(jmp[L][e] <= R)
{
ans += (1 << e);
L = jmp[L][e];
if(L >= R) return ans;
}
}
// cerr << "returning " << ans << "\n";
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';
}
// 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++)
{
auto y2 = events.lower_bound({L[i], R[i]});
if(R[i] > y2->first) continue;
auto y1 = y2;
y1--;
if(L[i] < y1->second) continue;
int change = query(y1->second, L[i]) + query(R[i], y2->first) + 1 - query(y1->second, y2->first);
if(max_events + change < K) continue;
max_events += change;
events.insert({L[i], R[i]});
res.push_back(i);
}
for(int r: res) cout << r << '\n';
// cout << '\n';
}
# | 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... |