#include <bits/stdc++.h>
using namespace std;
#define ll long long
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int nx=2e5+5, kx=18;
ll n, m, pa[nx][kx], sm[nx][kx], t, vl[nx][2], sh[nx][2], svl[nx][2], ssh[nx][2], ans=1e9;
map<ll, ll> mp;
struct segtree
{
tuple<ll, ll, ll> d[16*nx];
void update(int l, int r, int i, int idx, tuple<ll, ll, ll> vl)
{
if (idx<l||r<idx) return;
if (l==r) return d[i]=max(d[i], vl), void();
int md=(l+r)/2;
update(l, md, 2*i, idx, vl);
update(md+1, r, 2*i+1, idx, vl);
d[i]=max(d[2*i], d[2*i+1]);
}
tuple<ll, ll, ll> query(int l, int r, int i, int ql, int qr)
{
if (qr<l||r<ql) return {0, 0, 0};
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return max(query(l, md, 2*i, ql, qr), query(md+1, r, 2*i+1, ql, qr));
}
} s;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++)
{
cin>>vl[i][0]>>vl[i][1];
if (vl[i][1]<vl[i][0]) vl[i][1]+=m;
sh[i][0]=vl[i][0]+m;
sh[i][1]=vl[i][1]+m;
svl[i][0]=vl[i][0], svl[i][1]=vl[i][1], ssh[i][0]=sh[i][0], ssh[i][1]=sh[i][1];
mp[vl[i][0]]=mp[vl[i][1]]=mp[sh[i][0]]=mp[sh[i][1]]=0;
}
for (auto &[x, y]:mp) y=++t;
for (int i=1; i<=n ;i++) vl[i][0]=mp[vl[i][0]], vl[i][1]=mp[vl[i][1]], sh[i][0]=mp[sh[i][0]], sh[i][1]=mp[sh[i][1]];
for (int i=1; i<=n; i++) s.update(1, 4*n, 1, vl[i][0], {vl[i][1], svl[i][1], i}), s.update(1, 4*n, 1, sh[i][0], {sh[i][1], ssh[i][1], i});
for (int i=1; i<=n; i++)
{
auto [mx, sv, idx]=s.query(1, 4*n, 1, vl[i][0], vl[i][1]);
auto tmp=svl[i][1];
if (sv<=tmp) continue;
pa[i][0]=idx;
sm[i][0]=sv-tmp;
//cout<<"pa "<<i<<' '<<pa[i][0]<<' '<<sm[i][0]<<'\n';
}
for (int j=1; j<kx; j++) for (int i=1; i<=n; i++) pa[i][j]=pa[pa[i][j-1]][j-1], sm[i][j]=sm[i][j-1]+sm[pa[i][j-1]][j-1];
for (int i=1; i<=n; i++)
{
ll cur=svl[i][1]-svl[i][0], u=i, cnt=1;
for (int j=kx-1; j>=0; j--) if (cur+sm[u][j]<m) cnt+=1<<j, cur+=sm[u][j], u=pa[u][j];
//cout<<"cur "<<cur<<' '<<cnt<<' '<<u<<' '<<cur+sm[u][0]<<'\n';
if (cur+sm[u][0]<m||u==0) continue;
ans=min(ans, cnt+1);
}
if (ans==1e9) cout<<-1;
else cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10592 KB |
Output is correct |
6 |
Correct |
2 ms |
10744 KB |
Output is correct |
7 |
Correct |
2 ms |
10576 KB |
Output is correct |
8 |
Correct |
2 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
10576 KB |
Output is correct |
11 |
Correct |
2 ms |
10748 KB |
Output is correct |
12 |
Correct |
2 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
10576 KB |
Output is correct |
14 |
Correct |
3 ms |
10744 KB |
Output is correct |
15 |
Correct |
2 ms |
10576 KB |
Output is correct |
16 |
Correct |
2 ms |
10576 KB |
Output is correct |
17 |
Correct |
2 ms |
10576 KB |
Output is correct |
18 |
Correct |
2 ms |
10744 KB |
Output is correct |
19 |
Correct |
2 ms |
10576 KB |
Output is correct |
20 |
Correct |
2 ms |
10576 KB |
Output is correct |
21 |
Correct |
2 ms |
10576 KB |
Output is correct |
22 |
Correct |
2 ms |
10576 KB |
Output is correct |
23 |
Correct |
2 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10592 KB |
Output is correct |
6 |
Correct |
2 ms |
10744 KB |
Output is correct |
7 |
Correct |
2 ms |
10576 KB |
Output is correct |
8 |
Correct |
2 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
10576 KB |
Output is correct |
11 |
Correct |
2 ms |
10748 KB |
Output is correct |
12 |
Correct |
2 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
10576 KB |
Output is correct |
14 |
Correct |
3 ms |
10744 KB |
Output is correct |
15 |
Correct |
2 ms |
10576 KB |
Output is correct |
16 |
Correct |
2 ms |
10576 KB |
Output is correct |
17 |
Correct |
2 ms |
10576 KB |
Output is correct |
18 |
Correct |
2 ms |
10744 KB |
Output is correct |
19 |
Correct |
2 ms |
10576 KB |
Output is correct |
20 |
Correct |
2 ms |
10576 KB |
Output is correct |
21 |
Correct |
2 ms |
10576 KB |
Output is correct |
22 |
Correct |
2 ms |
10576 KB |
Output is correct |
23 |
Correct |
2 ms |
10576 KB |
Output is correct |
24 |
Correct |
2 ms |
10576 KB |
Output is correct |
25 |
Correct |
2 ms |
10832 KB |
Output is correct |
26 |
Correct |
2 ms |
10832 KB |
Output is correct |
27 |
Correct |
3 ms |
10832 KB |
Output is correct |
28 |
Correct |
2 ms |
10832 KB |
Output is correct |
29 |
Correct |
2 ms |
10832 KB |
Output is correct |
30 |
Correct |
2 ms |
10684 KB |
Output is correct |
31 |
Correct |
2 ms |
10832 KB |
Output is correct |
32 |
Correct |
3 ms |
10832 KB |
Output is correct |
33 |
Correct |
3 ms |
10832 KB |
Output is correct |
34 |
Correct |
2 ms |
10832 KB |
Output is correct |
35 |
Correct |
2 ms |
10832 KB |
Output is correct |
36 |
Correct |
3 ms |
10832 KB |
Output is correct |
37 |
Correct |
3 ms |
10832 KB |
Output is correct |
38 |
Correct |
2 ms |
10676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10592 KB |
Output is correct |
6 |
Correct |
2 ms |
10744 KB |
Output is correct |
7 |
Correct |
2 ms |
10576 KB |
Output is correct |
8 |
Correct |
2 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
10576 KB |
Output is correct |
11 |
Correct |
2 ms |
10748 KB |
Output is correct |
12 |
Correct |
2 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
10576 KB |
Output is correct |
14 |
Correct |
3 ms |
10744 KB |
Output is correct |
15 |
Correct |
2 ms |
10576 KB |
Output is correct |
16 |
Correct |
2 ms |
10576 KB |
Output is correct |
17 |
Correct |
2 ms |
10576 KB |
Output is correct |
18 |
Correct |
2 ms |
10744 KB |
Output is correct |
19 |
Correct |
2 ms |
10576 KB |
Output is correct |
20 |
Correct |
2 ms |
10576 KB |
Output is correct |
21 |
Correct |
2 ms |
10576 KB |
Output is correct |
22 |
Correct |
2 ms |
10576 KB |
Output is correct |
23 |
Correct |
2 ms |
10576 KB |
Output is correct |
24 |
Correct |
2 ms |
10576 KB |
Output is correct |
25 |
Correct |
2 ms |
10832 KB |
Output is correct |
26 |
Correct |
2 ms |
10832 KB |
Output is correct |
27 |
Correct |
3 ms |
10832 KB |
Output is correct |
28 |
Correct |
2 ms |
10832 KB |
Output is correct |
29 |
Correct |
2 ms |
10832 KB |
Output is correct |
30 |
Correct |
2 ms |
10684 KB |
Output is correct |
31 |
Correct |
2 ms |
10832 KB |
Output is correct |
32 |
Correct |
3 ms |
10832 KB |
Output is correct |
33 |
Correct |
3 ms |
10832 KB |
Output is correct |
34 |
Correct |
2 ms |
10832 KB |
Output is correct |
35 |
Correct |
2 ms |
10832 KB |
Output is correct |
36 |
Correct |
3 ms |
10832 KB |
Output is correct |
37 |
Correct |
3 ms |
10832 KB |
Output is correct |
38 |
Correct |
2 ms |
10676 KB |
Output is correct |
39 |
Correct |
17 ms |
14160 KB |
Output is correct |
40 |
Correct |
14 ms |
14160 KB |
Output is correct |
41 |
Correct |
14 ms |
14148 KB |
Output is correct |
42 |
Correct |
12 ms |
12624 KB |
Output is correct |
43 |
Correct |
11 ms |
12368 KB |
Output is correct |
44 |
Correct |
11 ms |
12792 KB |
Output is correct |
45 |
Correct |
11 ms |
12624 KB |
Output is correct |
46 |
Correct |
14 ms |
12880 KB |
Output is correct |
47 |
Correct |
13 ms |
14160 KB |
Output is correct |
48 |
Correct |
15 ms |
14160 KB |
Output is correct |
49 |
Correct |
11 ms |
14160 KB |
Output is correct |
50 |
Correct |
11 ms |
12676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
10744 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
10748 KB |
Output is correct |
7 |
Correct |
2 ms |
10576 KB |
Output is correct |
8 |
Correct |
2 ms |
10576 KB |
Output is correct |
9 |
Correct |
3 ms |
10832 KB |
Output is correct |
10 |
Correct |
3 ms |
10936 KB |
Output is correct |
11 |
Correct |
3 ms |
10832 KB |
Output is correct |
12 |
Correct |
3 ms |
10832 KB |
Output is correct |
13 |
Correct |
2 ms |
10832 KB |
Output is correct |
14 |
Correct |
2 ms |
10832 KB |
Output is correct |
15 |
Correct |
13 ms |
13976 KB |
Output is correct |
16 |
Correct |
10 ms |
12624 KB |
Output is correct |
17 |
Correct |
10 ms |
12460 KB |
Output is correct |
18 |
Correct |
10 ms |
12624 KB |
Output is correct |
19 |
Correct |
12 ms |
14160 KB |
Output is correct |
20 |
Correct |
123 ms |
78612 KB |
Output is correct |
21 |
Correct |
1004 ms |
169544 KB |
Output is correct |
22 |
Correct |
1051 ms |
169872 KB |
Output is correct |
23 |
Correct |
713 ms |
124516 KB |
Output is correct |
24 |
Correct |
1093 ms |
169784 KB |
Output is correct |
25 |
Correct |
851 ms |
118824 KB |
Output is correct |
26 |
Correct |
680 ms |
126280 KB |
Output is correct |
27 |
Correct |
860 ms |
126328 KB |
Output is correct |
28 |
Correct |
991 ms |
167524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8528 KB |
Output is correct |
2 |
Correct |
2 ms |
10576 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10576 KB |
Output is correct |
6 |
Correct |
2 ms |
10744 KB |
Output is correct |
7 |
Correct |
2 ms |
10576 KB |
Output is correct |
8 |
Correct |
2 ms |
10944 KB |
Output is correct |
9 |
Correct |
2 ms |
10832 KB |
Output is correct |
10 |
Correct |
3 ms |
10832 KB |
Output is correct |
11 |
Correct |
2 ms |
10832 KB |
Output is correct |
12 |
Correct |
13 ms |
14020 KB |
Output is correct |
13 |
Correct |
14 ms |
12740 KB |
Output is correct |
14 |
Correct |
11 ms |
12624 KB |
Output is correct |
15 |
Correct |
133 ms |
78616 KB |
Output is correct |
16 |
Correct |
748 ms |
124488 KB |
Output is correct |
17 |
Correct |
1142 ms |
169716 KB |
Output is correct |
18 |
Correct |
1168 ms |
168596 KB |
Output is correct |
19 |
Correct |
966 ms |
173628 KB |
Output is correct |
20 |
Correct |
793 ms |
127428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10576 KB |
Output is correct |
2 |
Correct |
2 ms |
8528 KB |
Output is correct |
3 |
Correct |
2 ms |
10576 KB |
Output is correct |
4 |
Correct |
2 ms |
10576 KB |
Output is correct |
5 |
Correct |
2 ms |
10592 KB |
Output is correct |
6 |
Correct |
2 ms |
10744 KB |
Output is correct |
7 |
Correct |
2 ms |
10576 KB |
Output is correct |
8 |
Correct |
2 ms |
10576 KB |
Output is correct |
9 |
Correct |
2 ms |
10576 KB |
Output is correct |
10 |
Correct |
2 ms |
10576 KB |
Output is correct |
11 |
Correct |
2 ms |
10748 KB |
Output is correct |
12 |
Correct |
2 ms |
10576 KB |
Output is correct |
13 |
Correct |
2 ms |
10576 KB |
Output is correct |
14 |
Correct |
3 ms |
10744 KB |
Output is correct |
15 |
Correct |
2 ms |
10576 KB |
Output is correct |
16 |
Correct |
2 ms |
10576 KB |
Output is correct |
17 |
Correct |
2 ms |
10576 KB |
Output is correct |
18 |
Correct |
2 ms |
10744 KB |
Output is correct |
19 |
Correct |
2 ms |
10576 KB |
Output is correct |
20 |
Correct |
2 ms |
10576 KB |
Output is correct |
21 |
Correct |
2 ms |
10576 KB |
Output is correct |
22 |
Correct |
2 ms |
10576 KB |
Output is correct |
23 |
Correct |
2 ms |
10576 KB |
Output is correct |
24 |
Correct |
2 ms |
10576 KB |
Output is correct |
25 |
Correct |
2 ms |
10832 KB |
Output is correct |
26 |
Correct |
2 ms |
10832 KB |
Output is correct |
27 |
Correct |
3 ms |
10832 KB |
Output is correct |
28 |
Correct |
2 ms |
10832 KB |
Output is correct |
29 |
Correct |
2 ms |
10832 KB |
Output is correct |
30 |
Correct |
2 ms |
10684 KB |
Output is correct |
31 |
Correct |
2 ms |
10832 KB |
Output is correct |
32 |
Correct |
3 ms |
10832 KB |
Output is correct |
33 |
Correct |
3 ms |
10832 KB |
Output is correct |
34 |
Correct |
2 ms |
10832 KB |
Output is correct |
35 |
Correct |
2 ms |
10832 KB |
Output is correct |
36 |
Correct |
3 ms |
10832 KB |
Output is correct |
37 |
Correct |
3 ms |
10832 KB |
Output is correct |
38 |
Correct |
2 ms |
10676 KB |
Output is correct |
39 |
Correct |
17 ms |
14160 KB |
Output is correct |
40 |
Correct |
14 ms |
14160 KB |
Output is correct |
41 |
Correct |
14 ms |
14148 KB |
Output is correct |
42 |
Correct |
12 ms |
12624 KB |
Output is correct |
43 |
Correct |
11 ms |
12368 KB |
Output is correct |
44 |
Correct |
11 ms |
12792 KB |
Output is correct |
45 |
Correct |
11 ms |
12624 KB |
Output is correct |
46 |
Correct |
14 ms |
12880 KB |
Output is correct |
47 |
Correct |
13 ms |
14160 KB |
Output is correct |
48 |
Correct |
15 ms |
14160 KB |
Output is correct |
49 |
Correct |
11 ms |
14160 KB |
Output is correct |
50 |
Correct |
11 ms |
12676 KB |
Output is correct |
51 |
Correct |
1 ms |
8528 KB |
Output is correct |
52 |
Correct |
2 ms |
10744 KB |
Output is correct |
53 |
Correct |
2 ms |
10576 KB |
Output is correct |
54 |
Correct |
2 ms |
10576 KB |
Output is correct |
55 |
Correct |
2 ms |
10576 KB |
Output is correct |
56 |
Correct |
2 ms |
10748 KB |
Output is correct |
57 |
Correct |
2 ms |
10576 KB |
Output is correct |
58 |
Correct |
2 ms |
10576 KB |
Output is correct |
59 |
Correct |
3 ms |
10832 KB |
Output is correct |
60 |
Correct |
3 ms |
10936 KB |
Output is correct |
61 |
Correct |
3 ms |
10832 KB |
Output is correct |
62 |
Correct |
3 ms |
10832 KB |
Output is correct |
63 |
Correct |
2 ms |
10832 KB |
Output is correct |
64 |
Correct |
2 ms |
10832 KB |
Output is correct |
65 |
Correct |
13 ms |
13976 KB |
Output is correct |
66 |
Correct |
10 ms |
12624 KB |
Output is correct |
67 |
Correct |
10 ms |
12460 KB |
Output is correct |
68 |
Correct |
10 ms |
12624 KB |
Output is correct |
69 |
Correct |
12 ms |
14160 KB |
Output is correct |
70 |
Correct |
123 ms |
78612 KB |
Output is correct |
71 |
Correct |
1004 ms |
169544 KB |
Output is correct |
72 |
Correct |
1051 ms |
169872 KB |
Output is correct |
73 |
Correct |
713 ms |
124516 KB |
Output is correct |
74 |
Correct |
1093 ms |
169784 KB |
Output is correct |
75 |
Correct |
851 ms |
118824 KB |
Output is correct |
76 |
Correct |
680 ms |
126280 KB |
Output is correct |
77 |
Correct |
860 ms |
126328 KB |
Output is correct |
78 |
Correct |
991 ms |
167524 KB |
Output is correct |
79 |
Correct |
2 ms |
8528 KB |
Output is correct |
80 |
Correct |
2 ms |
10576 KB |
Output is correct |
81 |
Correct |
2 ms |
10576 KB |
Output is correct |
82 |
Correct |
2 ms |
10576 KB |
Output is correct |
83 |
Correct |
2 ms |
10576 KB |
Output is correct |
84 |
Correct |
2 ms |
10744 KB |
Output is correct |
85 |
Correct |
2 ms |
10576 KB |
Output is correct |
86 |
Correct |
2 ms |
10944 KB |
Output is correct |
87 |
Correct |
2 ms |
10832 KB |
Output is correct |
88 |
Correct |
3 ms |
10832 KB |
Output is correct |
89 |
Correct |
2 ms |
10832 KB |
Output is correct |
90 |
Correct |
13 ms |
14020 KB |
Output is correct |
91 |
Correct |
14 ms |
12740 KB |
Output is correct |
92 |
Correct |
11 ms |
12624 KB |
Output is correct |
93 |
Correct |
133 ms |
78616 KB |
Output is correct |
94 |
Correct |
748 ms |
124488 KB |
Output is correct |
95 |
Correct |
1142 ms |
169716 KB |
Output is correct |
96 |
Correct |
1168 ms |
168596 KB |
Output is correct |
97 |
Correct |
966 ms |
173628 KB |
Output is correct |
98 |
Correct |
793 ms |
127428 KB |
Output is correct |
99 |
Correct |
1076 ms |
173428 KB |
Output is correct |
100 |
Correct |
1164 ms |
173576 KB |
Output is correct |
101 |
Correct |
983 ms |
173552 KB |
Output is correct |
102 |
Correct |
998 ms |
173600 KB |
Output is correct |
103 |
Correct |
757 ms |
128568 KB |
Output is correct |
104 |
Correct |
982 ms |
169288 KB |
Output is correct |
105 |
Correct |
513 ms |
172212 KB |
Output is correct |