#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
const int N = 4e5 + 10;
int t[4 * N], p[4 * N];
inline void mg(int v) { t[v] = max(t[2 * v + 1], t[2 * v + 2]); }
inline void push(int v) {
if (!p[v]) return;
p[2 * v + 1] += p[v]; t[2 * v + 1] += p[v];
p[2 * v + 2] += p[v]; t[2 * v + 2] += p[v];
p[v] = 0;
}
inline void upd(int v, int vl, int vr, int l, int r, int x) {
if (l > r) return;
else if (vl == l && vr == r) {
t[v] += x, p[v] += x;
return;
}
push(v);
int m = vl + vr >> 1;
upd(2 * v + 1, vl, m, l, min(r, m), x);
upd(2 * v + 2, m + 1, vr, max(l, m + 1), r, x);
mg(v);
}
inline int get(int v, int vl, int vr, int l, int r) {
// cout << v << " [" << vl << ", " << vr << "] and seg [" << l << ", " << r << "]" << '\n';
if (l > r) return -iinf;
else if (vl == l && vr == r) return t[v];
push(v);
int m = vl + vr >> 1;
return max(get(2*v+1,vl,m,l,min(r,m)),get(2*v+2,m+1,vr,max(l,m+1),r));
}
const int lg = 19;
const int M = 2e5 + 10;
int up[M][lg];
inline void solve() {
int n;
cin >> n;
ve<pii> a(n);
for (auto &[l, r] : a) cin >> l >> r;
ve<int> al;
for (auto &[l, r] : a) al.pb(l), al.pb(r);
sort(all(al)), al.erase(unique(all(al)), al.end());
for (auto &[l, r] : a) {
l = lower_bound(all(al), l) - al.begin();
r = lower_bound(all(al), r) - al.begin();
}
int m = sz(al);
ve<int> border(n);
for (int l = 0, r = -1; l < n; ++l) {
auto &[le, re] = a[l];
// cout << get(0, 0, m - 1, le, re) << " !" << '\n';
while (r + 1 < n) {
auto &[lf, rg] = a[r + 1];
if (get(0, 0, m - 1, lf, rg) > 0) break;
upd(0, 0, m - 1, lf, rg, +1);
++r;
}
border[l] = r;
upd(0, 0, m - 1, le, re, -1);
}
// for (int i = 0; i < n; ++i) {
// cout << i + 1 << ": " << border[i] + 1 << '\n';
// }
for (int k = 0; k < lg; ++k) up[n][k] = up[n + 1][k] = n;
for (int l = 0; l < n; ++l) up[l][0] = border[l];
for (int k = 1; k < lg; ++k) {
for (int l = 0; l < n; ++l) {
up[l][k] = up[up[l][k - 1] + 1][k - 1];
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r, --l, --r;
int ans = 0;
for (int i = lg - 1; ~i; --i) {
if (up[l][i] < r) {
ans += (1 << i);
l = up[l][i] + 1;
}
}
cout << ans + 1 << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
Compilation message
Main.cpp: In function 'void upd(int, int, int, int, int, int)':
Main.cpp:39:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
39 | int m = vl + vr >> 1;
| ~~~^~~~
Main.cpp: In function 'int get(int, int, int, int, int)':
Main.cpp:49:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int m = vl + vr >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
480 ms |
26500 KB |
Output is correct |
2 |
Correct |
496 ms |
29816 KB |
Output is correct |
3 |
Correct |
464 ms |
29960 KB |
Output is correct |
4 |
Correct |
487 ms |
30200 KB |
Output is correct |
5 |
Correct |
505 ms |
31136 KB |
Output is correct |
6 |
Correct |
72 ms |
20836 KB |
Output is correct |
7 |
Correct |
82 ms |
21164 KB |
Output is correct |
8 |
Correct |
88 ms |
21368 KB |
Output is correct |
9 |
Correct |
124 ms |
21828 KB |
Output is correct |
10 |
Correct |
108 ms |
21084 KB |
Output is correct |
11 |
Correct |
286 ms |
29568 KB |
Output is correct |
12 |
Correct |
280 ms |
28232 KB |
Output is correct |
13 |
Correct |
292 ms |
28212 KB |
Output is correct |
14 |
Correct |
353 ms |
29552 KB |
Output is correct |
15 |
Correct |
303 ms |
28984 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
77 ms |
22260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1108 KB |
Output is correct |
2 |
Correct |
10 ms |
1140 KB |
Output is correct |
3 |
Correct |
9 ms |
1236 KB |
Output is correct |
4 |
Correct |
10 ms |
1236 KB |
Output is correct |
5 |
Correct |
10 ms |
1224 KB |
Output is correct |
6 |
Correct |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
3 ms |
980 KB |
Output is correct |
8 |
Correct |
6 ms |
936 KB |
Output is correct |
9 |
Correct |
5 ms |
996 KB |
Output is correct |
10 |
Correct |
4 ms |
980 KB |
Output is correct |
11 |
Correct |
7 ms |
1108 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
6 ms |
1124 KB |
Output is correct |
14 |
Correct |
7 ms |
1108 KB |
Output is correct |
15 |
Correct |
8 ms |
1228 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1108 KB |
Output is correct |
2 |
Correct |
10 ms |
1140 KB |
Output is correct |
3 |
Correct |
9 ms |
1236 KB |
Output is correct |
4 |
Correct |
10 ms |
1236 KB |
Output is correct |
5 |
Correct |
10 ms |
1224 KB |
Output is correct |
6 |
Correct |
4 ms |
1116 KB |
Output is correct |
7 |
Correct |
3 ms |
980 KB |
Output is correct |
8 |
Correct |
6 ms |
936 KB |
Output is correct |
9 |
Correct |
5 ms |
996 KB |
Output is correct |
10 |
Correct |
4 ms |
980 KB |
Output is correct |
11 |
Correct |
7 ms |
1108 KB |
Output is correct |
12 |
Correct |
7 ms |
1108 KB |
Output is correct |
13 |
Correct |
6 ms |
1124 KB |
Output is correct |
14 |
Correct |
7 ms |
1108 KB |
Output is correct |
15 |
Correct |
8 ms |
1228 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
988 KB |
Output is correct |
18 |
Correct |
74 ms |
3920 KB |
Output is correct |
19 |
Correct |
66 ms |
3588 KB |
Output is correct |
20 |
Correct |
72 ms |
3944 KB |
Output is correct |
21 |
Correct |
65 ms |
3708 KB |
Output is correct |
22 |
Correct |
76 ms |
3784 KB |
Output is correct |
23 |
Correct |
60 ms |
3372 KB |
Output is correct |
24 |
Correct |
64 ms |
3480 KB |
Output is correct |
25 |
Correct |
68 ms |
3616 KB |
Output is correct |
26 |
Correct |
61 ms |
3424 KB |
Output is correct |
27 |
Correct |
68 ms |
3276 KB |
Output is correct |
28 |
Correct |
57 ms |
3168 KB |
Output is correct |
29 |
Correct |
48 ms |
3296 KB |
Output is correct |
30 |
Correct |
49 ms |
3364 KB |
Output is correct |
31 |
Correct |
50 ms |
3252 KB |
Output is correct |
32 |
Correct |
54 ms |
3392 KB |
Output is correct |
33 |
Correct |
1 ms |
388 KB |
Output is correct |
34 |
Correct |
57 ms |
3276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
500 ms |
27316 KB |
Output is correct |
2 |
Correct |
491 ms |
30660 KB |
Output is correct |
3 |
Correct |
469 ms |
29116 KB |
Output is correct |
4 |
Correct |
464 ms |
28992 KB |
Output is correct |
5 |
Correct |
471 ms |
29508 KB |
Output is correct |
6 |
Correct |
70 ms |
20980 KB |
Output is correct |
7 |
Correct |
88 ms |
20800 KB |
Output is correct |
8 |
Correct |
92 ms |
20968 KB |
Output is correct |
9 |
Correct |
93 ms |
20804 KB |
Output is correct |
10 |
Correct |
111 ms |
22456 KB |
Output is correct |
11 |
Correct |
322 ms |
28044 KB |
Output is correct |
12 |
Correct |
296 ms |
29528 KB |
Output is correct |
13 |
Correct |
274 ms |
28060 KB |
Output is correct |
14 |
Correct |
311 ms |
28624 KB |
Output is correct |
15 |
Correct |
308 ms |
29348 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
76 ms |
21348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
480 ms |
26500 KB |
Output is correct |
2 |
Correct |
496 ms |
29816 KB |
Output is correct |
3 |
Correct |
464 ms |
29960 KB |
Output is correct |
4 |
Correct |
487 ms |
30200 KB |
Output is correct |
5 |
Correct |
505 ms |
31136 KB |
Output is correct |
6 |
Correct |
72 ms |
20836 KB |
Output is correct |
7 |
Correct |
82 ms |
21164 KB |
Output is correct |
8 |
Correct |
88 ms |
21368 KB |
Output is correct |
9 |
Correct |
124 ms |
21828 KB |
Output is correct |
10 |
Correct |
108 ms |
21084 KB |
Output is correct |
11 |
Correct |
286 ms |
29568 KB |
Output is correct |
12 |
Correct |
280 ms |
28232 KB |
Output is correct |
13 |
Correct |
292 ms |
28212 KB |
Output is correct |
14 |
Correct |
353 ms |
29552 KB |
Output is correct |
15 |
Correct |
303 ms |
28984 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
77 ms |
22260 KB |
Output is correct |
18 |
Correct |
13 ms |
1108 KB |
Output is correct |
19 |
Correct |
10 ms |
1140 KB |
Output is correct |
20 |
Correct |
9 ms |
1236 KB |
Output is correct |
21 |
Correct |
10 ms |
1236 KB |
Output is correct |
22 |
Correct |
10 ms |
1224 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
3 ms |
980 KB |
Output is correct |
25 |
Correct |
6 ms |
936 KB |
Output is correct |
26 |
Correct |
5 ms |
996 KB |
Output is correct |
27 |
Correct |
4 ms |
980 KB |
Output is correct |
28 |
Correct |
7 ms |
1108 KB |
Output is correct |
29 |
Correct |
7 ms |
1108 KB |
Output is correct |
30 |
Correct |
6 ms |
1124 KB |
Output is correct |
31 |
Correct |
7 ms |
1108 KB |
Output is correct |
32 |
Correct |
8 ms |
1228 KB |
Output is correct |
33 |
Correct |
0 ms |
340 KB |
Output is correct |
34 |
Correct |
3 ms |
988 KB |
Output is correct |
35 |
Correct |
74 ms |
3920 KB |
Output is correct |
36 |
Correct |
66 ms |
3588 KB |
Output is correct |
37 |
Correct |
72 ms |
3944 KB |
Output is correct |
38 |
Correct |
65 ms |
3708 KB |
Output is correct |
39 |
Correct |
76 ms |
3784 KB |
Output is correct |
40 |
Correct |
60 ms |
3372 KB |
Output is correct |
41 |
Correct |
64 ms |
3480 KB |
Output is correct |
42 |
Correct |
68 ms |
3616 KB |
Output is correct |
43 |
Correct |
61 ms |
3424 KB |
Output is correct |
44 |
Correct |
68 ms |
3276 KB |
Output is correct |
45 |
Correct |
57 ms |
3168 KB |
Output is correct |
46 |
Correct |
48 ms |
3296 KB |
Output is correct |
47 |
Correct |
49 ms |
3364 KB |
Output is correct |
48 |
Correct |
50 ms |
3252 KB |
Output is correct |
49 |
Correct |
54 ms |
3392 KB |
Output is correct |
50 |
Correct |
1 ms |
388 KB |
Output is correct |
51 |
Correct |
57 ms |
3276 KB |
Output is correct |
52 |
Correct |
500 ms |
27316 KB |
Output is correct |
53 |
Correct |
491 ms |
30660 KB |
Output is correct |
54 |
Correct |
469 ms |
29116 KB |
Output is correct |
55 |
Correct |
464 ms |
28992 KB |
Output is correct |
56 |
Correct |
471 ms |
29508 KB |
Output is correct |
57 |
Correct |
70 ms |
20980 KB |
Output is correct |
58 |
Correct |
88 ms |
20800 KB |
Output is correct |
59 |
Correct |
92 ms |
20968 KB |
Output is correct |
60 |
Correct |
93 ms |
20804 KB |
Output is correct |
61 |
Correct |
111 ms |
22456 KB |
Output is correct |
62 |
Correct |
322 ms |
28044 KB |
Output is correct |
63 |
Correct |
296 ms |
29528 KB |
Output is correct |
64 |
Correct |
274 ms |
28060 KB |
Output is correct |
65 |
Correct |
311 ms |
28624 KB |
Output is correct |
66 |
Correct |
308 ms |
29348 KB |
Output is correct |
67 |
Correct |
0 ms |
340 KB |
Output is correct |
68 |
Correct |
76 ms |
21348 KB |
Output is correct |
69 |
Correct |
616 ms |
33236 KB |
Output is correct |
70 |
Correct |
697 ms |
34248 KB |
Output is correct |
71 |
Correct |
630 ms |
32332 KB |
Output is correct |
72 |
Correct |
674 ms |
33544 KB |
Output is correct |
73 |
Correct |
670 ms |
33236 KB |
Output is correct |
74 |
Correct |
254 ms |
26364 KB |
Output is correct |
75 |
Correct |
256 ms |
24636 KB |
Output is correct |
76 |
Correct |
265 ms |
26440 KB |
Output is correct |
77 |
Correct |
264 ms |
24400 KB |
Output is correct |
78 |
Correct |
240 ms |
24992 KB |
Output is correct |
79 |
Correct |
371 ms |
32252 KB |
Output is correct |
80 |
Correct |
321 ms |
31060 KB |
Output is correct |
81 |
Correct |
328 ms |
30924 KB |
Output is correct |
82 |
Correct |
372 ms |
32408 KB |
Output is correct |
83 |
Correct |
369 ms |
31060 KB |
Output is correct |
84 |
Correct |
0 ms |
340 KB |
Output is correct |
85 |
Correct |
149 ms |
24656 KB |
Output is correct |