/*
* Author: Nonoze
* Created: Monday 06/05/2024
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
#define int long long
#ifdef _IN_LOCAL
#include <bits/DebugTemplate.h>
#else
#define dbg(...) ;
#endif
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) {cout << x << endl; return;}
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;
void solve();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
// cin >> tt;
while(tt--) solve();
return 0;
}
int n, m, q;
vector<pair<int, int>> a;
vector<int> memo, nxt;
int first=0;
vector<vector<int>> sparce;
vector<int> logs;
void init() {
vector<int> vec;
for (int i=0; i<n; i++) vec.pb(a[i].fi);
logs.clear(), logs.resize(n+1); logs[0]=0, logs[1]=0;
for (int i=2; i<=n; i++) logs[i]=logs[i/2]+1;
int lg=logs[n]+1;
sparce.clear(), sparce.resize(lg, vector<int>(n, inf));
for (int i=0; i<n; i++) sparce[0][i]=vec[i];
for (int i=1; i<lg; i++) {
for (int j=0; j+(1<<i)<=n; j++) {
sparce[i][j]=min(sparce[i-1][j], sparce[i-1][j+(1<<(i-1))]);
}
}
}
int query(int l, int r) {
int lg=logs[r-l+1];
return min(sparce[lg][l], sparce[lg][r-(1<<lg)+1]);
}
vector<vector<int>> up;
vector<int> depth;
vector<bool> did;
void dfs(int s) {
if (s>=n || did[s]) return;
did[s]=1;
dfs(nxt[s]);
up[s][0]=nxt[s];
depth[s]=depth[nxt[s]]+1;
for (int i=1; i<LOG; i++) {
up[s][i]=up[up[s][i-1]][i-1];
}
}
int kth(int s, int k) {
for (int i=0; i<LOG; i++) {
if (k&(1<<i)) s=up[s][i];
}
return s;
}
void solve() {
cin >> n >> m;
a.clear(), a.resize(n), nxt.resize(n);
for (auto &u: a) {
cin >> u.fi >> u.se;
if (u.se<u.fi) u.se+=m;
}
sort(all(a), [&](pair<int, int> &x, pair<int, int> &y) {
return x.se < y.se;
});
init();
for (int i=n-1; i>=0; i--) {
int l=i+1, r=n-1, ans=n;
while (l<=r) {
int mid=(l+r)/2;
if (query(mid, r)<=a[i].se) {
ans=mid;
l=mid+1;
} else r=mid-1;
}
nxt[i]=ans;
}
up.resize(n+1, vector<int>(LOG, n)), did.resize(n, 0), depth.resize(n+1, 0);
for (int i=0; i<n; i++) dfs(i);
int ans=inf;
for (int i=0; i<n; i++) {
int objective=a[i].fi+m;
int l=1, r=depth[i]-1, res=inf;
while (l<=r) {
int mid=(l+r)/2;
if (a[kth(i, mid)].se>=objective) {
res=mid;
r=mid-1;
} else l=mid+1;
}
cmin(ans, res+1);
}
if (ans!=inf) cout << ans << endl;
else cout << -1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 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 |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
452 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 |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
600 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 |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 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 |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
600 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 |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
2392 KB |
Output is correct |
40 |
Correct |
4 ms |
2264 KB |
Output is correct |
41 |
Correct |
4 ms |
2392 KB |
Output is correct |
42 |
Correct |
4 ms |
2396 KB |
Output is correct |
43 |
Correct |
2 ms |
2396 KB |
Output is correct |
44 |
Correct |
5 ms |
2468 KB |
Output is correct |
45 |
Correct |
5 ms |
2392 KB |
Output is correct |
46 |
Correct |
5 ms |
2396 KB |
Output is correct |
47 |
Correct |
3 ms |
2396 KB |
Output is correct |
48 |
Correct |
3 ms |
2396 KB |
Output is correct |
49 |
Correct |
3 ms |
2412 KB |
Output is correct |
50 |
Correct |
3 ms |
2136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
456 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 |
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 |
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 |
3 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
2260 KB |
Output is correct |
17 |
Correct |
2 ms |
2140 KB |
Output is correct |
18 |
Correct |
5 ms |
2392 KB |
Output is correct |
19 |
Correct |
4 ms |
2396 KB |
Output is correct |
20 |
Correct |
72 ms |
82580 KB |
Output is correct |
21 |
Correct |
131 ms |
85616 KB |
Output is correct |
22 |
Correct |
123 ms |
85612 KB |
Output is correct |
23 |
Correct |
286 ms |
91028 KB |
Output is correct |
24 |
Correct |
235 ms |
88580 KB |
Output is correct |
25 |
Correct |
83 ms |
85160 KB |
Output is correct |
26 |
Correct |
221 ms |
87832 KB |
Output is correct |
27 |
Correct |
275 ms |
91796 KB |
Output is correct |
28 |
Correct |
138 ms |
85852 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 |
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 |
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 |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
3 ms |
2396 KB |
Output is correct |
13 |
Correct |
4 ms |
2396 KB |
Output is correct |
14 |
Correct |
3 ms |
2140 KB |
Output is correct |
15 |
Correct |
65 ms |
82520 KB |
Output is correct |
16 |
Correct |
278 ms |
91024 KB |
Output is correct |
17 |
Correct |
156 ms |
85648 KB |
Output is correct |
18 |
Correct |
111 ms |
85596 KB |
Output is correct |
19 |
Correct |
257 ms |
89744 KB |
Output is correct |
20 |
Correct |
218 ms |
85648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 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 |
0 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 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
600 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 |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 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 |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
3 ms |
2392 KB |
Output is correct |
40 |
Correct |
4 ms |
2264 KB |
Output is correct |
41 |
Correct |
4 ms |
2392 KB |
Output is correct |
42 |
Correct |
4 ms |
2396 KB |
Output is correct |
43 |
Correct |
2 ms |
2396 KB |
Output is correct |
44 |
Correct |
5 ms |
2468 KB |
Output is correct |
45 |
Correct |
5 ms |
2392 KB |
Output is correct |
46 |
Correct |
5 ms |
2396 KB |
Output is correct |
47 |
Correct |
3 ms |
2396 KB |
Output is correct |
48 |
Correct |
3 ms |
2396 KB |
Output is correct |
49 |
Correct |
3 ms |
2412 KB |
Output is correct |
50 |
Correct |
3 ms |
2136 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
460 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
456 KB |
Output is correct |
55 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
3 ms |
2396 KB |
Output is correct |
66 |
Correct |
4 ms |
2260 KB |
Output is correct |
67 |
Correct |
2 ms |
2140 KB |
Output is correct |
68 |
Correct |
5 ms |
2392 KB |
Output is correct |
69 |
Correct |
4 ms |
2396 KB |
Output is correct |
70 |
Correct |
72 ms |
82580 KB |
Output is correct |
71 |
Correct |
131 ms |
85616 KB |
Output is correct |
72 |
Correct |
123 ms |
85612 KB |
Output is correct |
73 |
Correct |
286 ms |
91028 KB |
Output is correct |
74 |
Correct |
235 ms |
88580 KB |
Output is correct |
75 |
Correct |
83 ms |
85160 KB |
Output is correct |
76 |
Correct |
221 ms |
87832 KB |
Output is correct |
77 |
Correct |
275 ms |
91796 KB |
Output is correct |
78 |
Correct |
138 ms |
85852 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 |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
344 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
0 ms |
348 KB |
Output is correct |
85 |
Correct |
0 ms |
348 KB |
Output is correct |
86 |
Correct |
0 ms |
348 KB |
Output is correct |
87 |
Correct |
0 ms |
348 KB |
Output is correct |
88 |
Correct |
0 ms |
460 KB |
Output is correct |
89 |
Correct |
0 ms |
348 KB |
Output is correct |
90 |
Correct |
3 ms |
2396 KB |
Output is correct |
91 |
Correct |
4 ms |
2396 KB |
Output is correct |
92 |
Correct |
3 ms |
2140 KB |
Output is correct |
93 |
Correct |
65 ms |
82520 KB |
Output is correct |
94 |
Correct |
278 ms |
91024 KB |
Output is correct |
95 |
Correct |
156 ms |
85648 KB |
Output is correct |
96 |
Correct |
111 ms |
85596 KB |
Output is correct |
97 |
Correct |
257 ms |
89744 KB |
Output is correct |
98 |
Correct |
218 ms |
85648 KB |
Output is correct |
99 |
Correct |
134 ms |
85644 KB |
Output is correct |
100 |
Correct |
135 ms |
85644 KB |
Output is correct |
101 |
Correct |
287 ms |
91024 KB |
Output is correct |
102 |
Correct |
270 ms |
90000 KB |
Output is correct |
103 |
Correct |
280 ms |
91792 KB |
Output is correct |
104 |
Correct |
157 ms |
86224 KB |
Output is correct |
105 |
Correct |
225 ms |
87444 KB |
Output is correct |