#include <bits/stdc++.h>
#define ff first
#define ss second
#define int long long
#define pii pair<int, int>
#define ff first
#define ss second
using namespace std;
const int inf = 1e17;
const int mod = 998244353;
void solve()
{
int n,m;
cin >> n >> m;
vector<pii> v(n);
for (int i = 0; i< n; i++)
{
cin >> v[i].ff >> v[i].ss;
while(v[i].ff>v[i].ss)
v[i].ss+=m;
}
sort(v.begin(),v.end());
{
vector<bool> del(n);
int maxi = 0;
for (int i = 0; i < n;i ++)
{
if (v[i].ss <= maxi)
del[i] = 1;
maxi = max(maxi,v[i].ss);
}
vector<pii> neu;
for (int i = 0; i < n; i++)
if (!del[i])
neu.push_back(v[i]);
n = neu.size();
v = neu;
}
{
vector<bool> del(n);
int mini = v[n-1].ss;
for (int i = n-1; i >= 0;i--)
{
if (v[i].ff >= mini)
del[i] = 1;
mini = min(mini,v[i].ff);
}
vector<pii> neu;
for (int i = 0; i < n; i++)
if (!del[i])
neu.push_back(v[i]);
n = neu.size();
v = neu;
}
for (int i = 0; i< n;i++)
v[i].ss%=m;
{
int bigie = 0;
for (int i = 0; i < n; i++)
if (v[i].ff > v[i].ss)
bigie = max(bigie,v[i].ss);
vector<bool> del(n);
for (int i = 0; i < n; i++)
if (v[i].ff < v[i].ss && v[i].ss < bigie)
del[i] = 1;
vector<pii> neu;
for (int i = 0; i < n; i++)
if (!del[i])
neu.push_back(v[i]);
n = neu.size();
v = neu;
}
int oldn = n;
vector<pii> normalni;
for (int i = 0; i < n; i++)
if (v[i].ff < v[i].ss)
normalni.push_back(v[i]);
n = normalni.size();
vector<int> nxt(n);
for (int i = 0; i < n-1; i++)
{
int l = i;
int r = n;
while (r-l>1)
{
int mid = l+r>>1;
if (normalni[mid].ff > normalni[i].ss)
r = mid;
else
l = mid;
}
if (l == i)
{
cout << -1 << endl;
return;
}
nxt[i] = l;
}
nxt[n-1] = -1;
vector<vector<int>> lifts(20,vector<int>(n));
lifts[0] = nxt;
for (int j = 1; j < 20; j++)
for (int i = 0; i < n; i++)
{
if (lifts[j-1][i] == -1)
lifts[j][i] = -1;
else
lifts[j][i] = lifts[j-1][lifts[j-1][i]];
}
n = oldn;
int normala = normalni.size();
int mini= inf;
for (int fix = 0; fix < n; fix++)
{
int f = v[fix].ff, s = v[fix].ss;
int beg;
if (v[fix].ff > v[fix].ss)
{
int L = 0, R = normala-1;
while (R-L>1)
{
int mid = L+R>>1;
if (normalni[mid].ff > s)
R = mid;
else
L = mid;
}
if (normalni[R].ff <= s)
beg = R;
else
beg = L;
int cur = beg;
int mask = 0;
for (int bit = 19; bit >= 0; bit--)
{
int cand = lifts[bit][cur];
if (cand != -1 && normalni[cand].ss < f)
{
cur = cand;
mask|=(1<<bit);
}
}
if (nxt[cur] != -1)
{
cur = nxt[cur];
mask++;
}
if (normalni[cur].ss < f)
continue;
mini = min(mask+2,mini);
}
}
if (mini == inf)
cout << -1 << endl;
else
cout << mini << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:93:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
93 | int mid = l+r>>1;
| ~^~
Main.cpp:130:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
130 | int mid = L+R>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |