This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif
struct Lisan: vector<int> {
void done() { sort(AI()); erase(unique(AI()), end()); }
int operator () (int v) { return lower_bound(AI(), v) - begin(); }
};
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N, K; cin >> N >> K;
vector<int> L(N + 2), R(N + 2);
for (int i = 1; i <= N; ++i)
cin >> L[i] >> R[i];
L[0] = -2; R[0] = -1;
L[N + 1] = 1'000'000'007;
R[N + 1] = L[N + 1] + 1;
Lisan lisan;
for (int i = 0; i <= N + 1; ++i) {
lisan.pb(L[i]);
lisan.pb(R[i]);
}
lisan.done();
for (int i = 0; i <= N + 1; ++i) {
L[i] = lisan(L[i]);
R[i] = lisan(R[i]);
}
vector<vector<int>> in(lisan.size());
for (int i = 0; i <= N + 1; ++i)
in[L[i]].pb(i);
vector<int> go(lisan.size());
int mnr = 1'000'000'009, mni = -1;
for (int i = lisan.size() - 1; i >= 0; --i) {
for (int j: in[i])
if (R[j] < mnr) {
mnr = R[j];
mni = j;
}
go[i] = mni;
}
int lg = __lg(N);
vector<vector<int>> jmp(lg + 1, vector<int>(N + 2, -1));
for (int i = 0; i <= N; ++i)
jmp[0][i] = go[R[i]];
jmp[0][N + 1] = N + 1;
for (int l = 1; l <= lg; ++l) {
for (int i = 0; i <= N + 1; ++i)
jmp[l][i] = jmp[l - 1][jmp[l - 1][i]];
}
auto solve = [&](int l, int r) {
int ans = 0;
for (int d = lg; d >= 0; --d)
if (R[jmp[d][l]] <= L[r]) {
ans += (1 << d);
l = jmp[d][l];
}
return ans;
};
int tot = solve(0, N + 1);
if (tot < K) {
cout << -1 << '\n';
return 0;
}
auto cmp = [&](const int &a, const int &b) { return L[a] < L[b]; };
set<int, decltype(cmp)> segs(cmp);
segs.insert(0);
segs.insert(N + 1);
vector<int> use;
for (int i = 1; i <= N; ++i) {
auto it = segs.lower_bound(i);
if (R[*prev(it)] > L[i] or R[i] > L[*it]) continue;
int cur = solve(*prev(it), *it);
int curl = solve(*prev(it), i);
int curr = solve(i, *it);
if (tot - cur + curl + curr + 1 >= K) {
tot = tot - cur + curl + curr + 1;
use.pb(i);
segs.insert(i);
}
}
while (use.size() > K) use.pop_back();
for (int &i: use)
cout << i << '\n';
return 0;
}
Compilation message (stderr)
event2.cpp: In function 'int main()':
event2.cpp:110:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
110 | while (use.size() > K) use.pop_back();
| ~~~~~~~~~~~^~~
# | 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... |