#include <bits/stdc++.h>
using namespace std;
void setup()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, k, head, num, new_num, a, b;
int nxt[20][200000];
array<int, 3> p[200000];
set<pair<int, int>> s;
set<pair<int, int>>::iterator it;
vector<int> v;
inline bool Comp(const array<int, 3> &ina, const array<int, 3> &inb)
{
return ina[1] < inb[1];
}
inline bool Comp1(const array<int, 3> &ina, const array<int, 3> &inb)
{
return ina[2] < inb[2];
}
inline int Count(int ind, int limit)
{
int ret = 1;
if (limit < p[ind][1])
{
return 0;
}
for (int i = 19; i >= 0; --i)
{
if (nxt[i][ind] != -1 && p[nxt[i][ind]][1] <= limit)
{
ret += (1 << i);
ind = nxt[i][ind];
}
}
return ret;
}
int main()
{
setup();
cin >> n >> k;
for (int i = 0; i < n; ++i)
{
cin >> p[i][0] >> p[i][1];
p[i][2] = i;
}
sort(p, p + n, Comp);
head = p[0][2];
for (int i = 0, j = 0; i < n; ++i)
{
j = max(i, j);
while (j < n && p[j][0] < p[i][1])
{
j++;
}
nxt[0][p[i][2]] = (j == n ? -1 : p[j][2]);
}
sort(p, p + n, Comp1);
for (int i = 1; i < 20; ++i)
{
for (int j = 0; j < n; ++j)
{
nxt[i][j] = (nxt[i - 1][j] == -1 ? -1 : nxt[i - 1][nxt[i - 1][j]]);
}
}
num = Count(head, 1e9);
for (int i = 0; i < n && s.size() < k; ++i)
{
s.insert({p[i][1], p[i][2]});
it = s.lower_bound({p[i][1], p[i][2]});
if (it == s.begin())
{
a = head;
}
else
{
a = (*prev(it)).second;
}
if (next(it) == s.end())
{
b = 1e9;
}
else
{
b = (*next(it)).first;
}
if ((it != s.begin() && p[a][1] > p[i][0]) || (p[i][1] > b))
{
s.erase({p[i][1], p[i][2]});
continue;
}
new_num = num - Count(a, b) + Count(a, p[i][0]) + Count(i, b);
if (new_num >= k)
{
num = new_num;
v.push_back(i + 1);
continue;
}
s.erase({p[i][1], p[i][2]});
}
if (v.size() < k)
{
cout << -1;
return 0;
}
for (auto & i : v)
{
cout << i << "\n";
}
return 0;
}
| # | 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... |