#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;
int solve(int n,int m,vector<pii> v)
{
for (int i = 0;i < n; i++)
if (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();
if (!n)
{
return -1;
}
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)
{
return -1;
}
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;
if (normalni[cur].ff > s)
continue;
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 && normalni[cur].ss < f)
{
cur = nxt[cur];
mask++;
}
if (normalni[cur].ss < f)
continue;
mini = min(mask+2,mini);
}
}
int lb = 0,rb = inf;
for (int i = 0;i < n; i++)
if (v[i].ff > v[i].ss)
{
lb = max(lb,v[i].ss);
rb = min(rb,v[i].ff);
}
int used = 2;
int maxright = lb;
if (lb >= rb)
mini = min(mini,used);
for (int i = 0; i < n; i++)
{
if (v[i].ff > maxright)
{
used++;
if (i-1 < 0)
{
break;
}
maxright = v[i-1].ss;
if (v[i].ff > maxright)
{
break;
}
if (maxright >= rb)
{
mini = min(used,mini);
}
}
}
if (mini == inf)
return -1;
else
return mini;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m;
cin >> n >> m;
vector<pii> v(n);
for (int i = 0; i < n; i++)
cin >> v[i].ff >> v[i].ss;
cout << solve(n,m,v) << endl;
}
Compilation message
Main.cpp: In function 'long long int solve(long long int, long long int, std::vector<std::pair<long long int, long long int> >)':
Main.cpp:91:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
91 | int mid = l+r>>1;
| ~^~
Main.cpp:129:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
129 | int mid = L+R>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
376 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
460 KB |
Output is correct |
38 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
376 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
460 KB |
Output is correct |
38 |
Correct |
0 ms |
600 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
1412 KB |
Output is correct |
41 |
Correct |
2 ms |
1628 KB |
Output is correct |
42 |
Correct |
2 ms |
1388 KB |
Output is correct |
43 |
Correct |
1 ms |
720 KB |
Output is correct |
44 |
Correct |
2 ms |
1644 KB |
Output is correct |
45 |
Correct |
2 ms |
1520 KB |
Output is correct |
46 |
Correct |
2 ms |
1644 KB |
Output is correct |
47 |
Correct |
1 ms |
632 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
1 ms |
1100 KB |
Output is correct |
50 |
Correct |
2 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
1 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
1388 KB |
Output is correct |
16 |
Correct |
2 ms |
1400 KB |
Output is correct |
17 |
Correct |
2 ms |
616 KB |
Output is correct |
18 |
Correct |
3 ms |
1388 KB |
Output is correct |
19 |
Correct |
1 ms |
552 KB |
Output is correct |
20 |
Correct |
23 ms |
6732 KB |
Output is correct |
21 |
Correct |
58 ms |
11344 KB |
Output is correct |
22 |
Correct |
46 ms |
10084 KB |
Output is correct |
23 |
Correct |
88 ms |
45288 KB |
Output is correct |
24 |
Correct |
66 ms |
36804 KB |
Output is correct |
25 |
Correct |
42 ms |
7764 KB |
Output is correct |
26 |
Correct |
60 ms |
25756 KB |
Output is correct |
27 |
Correct |
74 ms |
46168 KB |
Output is correct |
28 |
Correct |
44 ms |
9300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
1388 KB |
Output is correct |
13 |
Correct |
2 ms |
1388 KB |
Output is correct |
14 |
Correct |
2 ms |
1412 KB |
Output is correct |
15 |
Correct |
20 ms |
6672 KB |
Output is correct |
16 |
Correct |
72 ms |
45096 KB |
Output is correct |
17 |
Correct |
71 ms |
45016 KB |
Output is correct |
18 |
Correct |
71 ms |
42612 KB |
Output is correct |
19 |
Correct |
71 ms |
45884 KB |
Output is correct |
20 |
Correct |
68 ms |
46004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
376 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
460 KB |
Output is correct |
38 |
Correct |
0 ms |
600 KB |
Output is correct |
39 |
Correct |
2 ms |
860 KB |
Output is correct |
40 |
Correct |
2 ms |
1412 KB |
Output is correct |
41 |
Correct |
2 ms |
1628 KB |
Output is correct |
42 |
Correct |
2 ms |
1388 KB |
Output is correct |
43 |
Correct |
1 ms |
720 KB |
Output is correct |
44 |
Correct |
2 ms |
1644 KB |
Output is correct |
45 |
Correct |
2 ms |
1520 KB |
Output is correct |
46 |
Correct |
2 ms |
1644 KB |
Output is correct |
47 |
Correct |
1 ms |
632 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
1 ms |
1100 KB |
Output is correct |
50 |
Correct |
2 ms |
1388 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
352 KB |
Output is correct |
53 |
Correct |
0 ms |
344 KB |
Output is correct |
54 |
Correct |
0 ms |
600 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
352 KB |
Output is correct |
62 |
Correct |
0 ms |
352 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
2 ms |
1388 KB |
Output is correct |
66 |
Correct |
2 ms |
1400 KB |
Output is correct |
67 |
Correct |
2 ms |
616 KB |
Output is correct |
68 |
Correct |
3 ms |
1388 KB |
Output is correct |
69 |
Correct |
1 ms |
552 KB |
Output is correct |
70 |
Correct |
23 ms |
6732 KB |
Output is correct |
71 |
Correct |
58 ms |
11344 KB |
Output is correct |
72 |
Correct |
46 ms |
10084 KB |
Output is correct |
73 |
Correct |
88 ms |
45288 KB |
Output is correct |
74 |
Correct |
66 ms |
36804 KB |
Output is correct |
75 |
Correct |
42 ms |
7764 KB |
Output is correct |
76 |
Correct |
60 ms |
25756 KB |
Output is correct |
77 |
Correct |
74 ms |
46168 KB |
Output is correct |
78 |
Correct |
44 ms |
9300 KB |
Output is correct |
79 |
Correct |
0 ms |
344 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
344 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
344 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
0 ms |
348 KB |
Output is correct |
87 |
Correct |
1 ms |
348 KB |
Output is correct |
88 |
Correct |
0 ms |
348 KB |
Output is correct |
89 |
Correct |
0 ms |
348 KB |
Output is correct |
90 |
Correct |
2 ms |
1388 KB |
Output is correct |
91 |
Correct |
2 ms |
1388 KB |
Output is correct |
92 |
Correct |
2 ms |
1412 KB |
Output is correct |
93 |
Correct |
20 ms |
6672 KB |
Output is correct |
94 |
Correct |
72 ms |
45096 KB |
Output is correct |
95 |
Correct |
71 ms |
45016 KB |
Output is correct |
96 |
Correct |
71 ms |
42612 KB |
Output is correct |
97 |
Correct |
71 ms |
45884 KB |
Output is correct |
98 |
Correct |
68 ms |
46004 KB |
Output is correct |
99 |
Correct |
45 ms |
12452 KB |
Output is correct |
100 |
Correct |
44 ms |
13996 KB |
Output is correct |
101 |
Correct |
74 ms |
49092 KB |
Output is correct |
102 |
Correct |
67 ms |
41420 KB |
Output is correct |
103 |
Correct |
72 ms |
49216 KB |
Output is correct |
104 |
Correct |
46 ms |
15824 KB |
Output is correct |
105 |
Correct |
40 ms |
29088 KB |
Output is correct |