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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= int (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> int (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < int (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
template<class X, class Y>
bool minz(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} return false;
}
template<class X, class Y>
bool maxz(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} return false;
}
const int N = 5e5 + 5;
const ll oo = (ll) 1e16;
const ll mod = 1e9 + 7; // 998244353;
int n, m;
pii a[N];
namespace sub6 {
map <int, vector<pii>> qq;
set <pii> s;
int ans = mod, up[20][N];
void solve(){
forr (i, 1, n){
qq[a[i].nd].pb({1, i});
qq[a[i].st].pb({0, i});
}
for (pair<int, vector<pii>> ww : qq){
int u = ww.st;
vector <pii> v = ww.nd;
sort(all(v));
for (pii e : v){
if (e.st == 0){
s.insert(mp(a[e.nd].nd, e.nd));
} else {
s.erase(s.find(mp(a[e.nd].nd, e.nd)));
if (s.size()){
up[0][e.nd] = (*s.rbegin()).nd;
} else {
up[0][e.nd] = n + 1;
}
}
}
}
a[n + 1].nd = 2 * m + 1;
forr (i, 0, 17){
up[i][n + 1] = n + 1;
}
ford (i, n, 1){
if (up[0][i] < i) continue;
forr (j, 1, 17){
up[j][i] = up[j - 1][up[j - 1][i]];
}
int u = i, res = 2;
ford (j, 17, 0){
if (a[up[j][u]].nd < a[i].st + m){
res += mask(j);
u = up[j][u];
}
}
if (up[0][u] != n + 1 && a[up[0][u]].nd >= a[i].st + m){
minz(ans, res);
}
}
cout << (ans <= n ? ans : -1) << "\n";
}
}
namespace sub2 {
int f[500], ans = mod;
void solve(){
forr (i, 1, n){
memset (f, 63, sizeof f);
forr (j, 1, n){
if (a[j].st == a[i].st){
f[j] = 1;
} else {
ford (k, j - 1, 1){
if (a[j].st >= a[k].st && a[j].st <= a[k].nd){
minz(f[j], f[k] + 1);
}
}
}
if (a[j].nd >= a[i].st + m){
minz(ans, f[j]);
}
}
}
cout << (ans <= n ? ans : -1) << "\n";
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef kaguya
freopen(file".inp", "r", stdin); freopen(file".ans", "w", stdout);
#endif
cin >> n >> m;
forr (i, 1, n){
cin >> a[i].st >> a[i].nd;
if (a[i].nd < a[i].st)
a[i].nd += m;
}
sort(a + 1, a + 1 + n);
if (n <= 300){
sub2::solve();
} else {
sub6::solve();
}
return 0;
}
/*
*/
Compilation message (stderr)
Main.cpp: In function 'void sub6::solve()':
Main.cpp:55:8: warning: unused variable 'u' [-Wunused-variable]
55 | int u = ww.st;
| ^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |