#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
int L[400001];
int R[400001];
int L2[400001];
int jump[400001][20];
int nxt_[200001];
vi add[400004];
vi del[400004];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random_start();
int n,m;
cin >> n >> m;
rep(i,n) cin >> L[i] >> R[i];
int n2 = n;
rep(i,n2)
{
L2[i] = m;
if(R[i] < L[i])
{
L[n] = L[i];
R[n] = R[i]+m;
L2[i] = L[i];
L[i] = 0;
n++;
}
}
map<int,int> scale;
scale[m] = 1;
rep(i,n)
{
scale[L[i]] = 1;
scale[R[i]] = 1;
scale[L2[i]] = 1;
}
int cur = 0;
forall(it,scale)
{
scale[it.ff] = cur++;
}
rep(i,n)
{
L2[i] = scale[L2[i]];
L[i] = scale[L[i]];
R[i] = scale[R[i]];
}
rep(i,n)
{
add[L[i]].pb(i);
del[R[i]+1].pb(i);
}
set<pii> s;
rep(i,cur)
{
forall(it,add[i])
{
if(L[i] < R[i])
{
s.insert({R[it],it});
}
else
{
if(i == 0) s.insert({R[it],it});
else s.insert({R[it]+cur,it});
}
}
forall(it,del[i])
{
if(L[i] < R[i])
{
s.erase({R[it],it});
}
else
{
if(i == R[i]+1) s.erase({R[it],it});
else s.erase({R[it]+cur,it});
}
}
if(siz(s) == 0) nxt_[i] = -1;
else nxt_[i] = (*(--s.end())).ss;
}
rep(i,n)
{
jump[i][0] = nxt_[R[i]];
}
rep2(bit,1,19)
{
rep(i,n)
{
jump[i][bit] = jump[jump[i][bit-1]][bit-1];
}
}
int ans = 1e9;
rep(i,n)
{
if(L[i] == 0)
{
int t = jump[i][19];
if(R[t] < L2[i]) continue;
int ans2 = 1;
t = i;
for(int bit = 18; bit >= 0; bit--)
{
if(R[jump[t][bit]] < L2[i])
{
ans2 += (1 << bit);
t = jump[t][bit];
}
}
ans = min(ans,ans2+1);
}
}
if(ans != 1e9) cout << ans << "\n";
else cout << -1 << "\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |