#include <bits/stdc++.h>
#define ll long long
#define pb push_back
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) (int)x.size()
#define fi first
#define sd second
#define all(x) x.begin(), x.end()
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = (int)2e5 + 5;
int n, l[N], r[N], q, nxt[N], mn[N][22], lg[N], t[8 * N], mod[8 * N], mx;
vector<int> vec;
bool intersect(const pair<int, int> &a, const pair<int, int> &b)
{
int L = max(a.fi, b.fi);
int R = min(a.sd, b.sd);
return R >= L;
}
int get(int l, int r)
{
int t = lg[r - l + 1];
return min(mn[l][t], mn[r - (1 << t) + 1][t]);
}
void push(int v, int tl, int tr)
{
if (tl == tr) return;
mod[v + v] = min(mod[v + v], mod[v]);
mod[v + v + 1] = min(mod[v + v + 1], mod[v]);
t[v + v] = min(t[v + v], mod[v]);
t[v + v + 1] = min(t[v + v + 1], mod[v]);
}
void build(int v, int tl, int tr)
{
if (tl == tr)
{
t[v] = n + 1;
mod[v] = n + 1;
return;
}
int m = (tl + tr) >> 1;
build(v + v, tl, m);
build(v + v + 1, m + 1, tr);
t[v] = min(t[v + v], t[v + v + 1]);
mod[v] = n + 1;
}
int get(int v, int tl, int tr, int l, int r)
{
push(v, tl, tr);
if (tl > tr || tl > r || tr < l) return n + 1;
if (tl >= l && tr <= r)
return t[v];
//push(v, tl, tr);
int m = (tl + tr) >> 1;
return min(get(v + v, tl, m, l, r), get(v + v + 1, m + 1, tr, l, r));
}
void upd(int v, int tl, int tr, int l, int r, int val)
{
push(v, tl, tr);
if (tl > tr || tl > r || tr < l) return;
if (tl >= l && tr <= r)
{
t[v] = min(t[v], val);
mod[v] = min(mod[v], val);
//push(v, tl, tr);
return;
}
int m = (tl + tr) >> 1;
//push(v, tl, tr);
upd(v + v, tl, m, l, r, val);
upd(v + v + 1, m + 1, tr, l, r, val);
t[v] = min(t[v + v], t[v + v + 1]);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
lg[1] = 0;
for(int i = 2; i <= n; i++) lg[i] = lg[i / 2] + 1;
for(int i = 1; i <= n; i++)
{
cin >> l[i] >> r[i];
vec.pb(l[i]);
vec.pb(r[i]);
}
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
for(int i = 1; i <= n; i++)
{
l[i] = lower_bound(all(vec), l[i]) - vec.begin(); l[i]++;
r[i] = lower_bound(all(vec), r[i]) - vec.begin(); r[i]++;
mx = max(mx, l[i]);
mx = max(mx, r[i]);
}
build(1, 1, mx);
for(int i = n; i >= 1; i--)
{
int val = get(1, 1, mx, l[i], r[i]);
nxt[i] = val;
mn[i][0] = nxt[i];
upd(1, 1, mx, l[i], r[i], i);
}
for(int i = 1; (1 << i) <= n; i++)
{
for(int j = 1; j + (1 << i) - 1 <= n; j++)
mn[j][i] = min(mn[j][i - 1], mn[j + (1 << (i - 1))][i - 1]);
}
for(int i = 1; i <= n; i++) nxt[i] = get(i, nxt[i] - 1);
cin >> q;
while(q--)
{
int le, ri;
cin >> le >> ri;
int i = le, ans = 0;
while(i <= ri)
{
i = nxt[i];
ans++;
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
469 ms |
29724 KB |
Output is correct |
2 |
Correct |
405 ms |
29244 KB |
Output is correct |
3 |
Correct |
415 ms |
29432 KB |
Output is correct |
4 |
Correct |
420 ms |
29496 KB |
Output is correct |
5 |
Correct |
446 ms |
30440 KB |
Output is correct |
6 |
Correct |
74 ms |
20276 KB |
Output is correct |
7 |
Correct |
78 ms |
20572 KB |
Output is correct |
8 |
Correct |
84 ms |
20792 KB |
Output is correct |
9 |
Correct |
92 ms |
21304 KB |
Output is correct |
10 |
Correct |
100 ms |
20648 KB |
Output is correct |
11 |
Correct |
316 ms |
30104 KB |
Output is correct |
12 |
Correct |
296 ms |
28844 KB |
Output is correct |
13 |
Correct |
293 ms |
28644 KB |
Output is correct |
14 |
Correct |
300 ms |
30180 KB |
Output is correct |
15 |
Correct |
305 ms |
29540 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
76 ms |
21700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1228 KB |
Output is correct |
2 |
Correct |
20 ms |
1220 KB |
Output is correct |
3 |
Correct |
18 ms |
1208 KB |
Output is correct |
4 |
Correct |
19 ms |
1228 KB |
Output is correct |
5 |
Correct |
19 ms |
1208 KB |
Output is correct |
6 |
Correct |
19 ms |
972 KB |
Output is correct |
7 |
Correct |
16 ms |
952 KB |
Output is correct |
8 |
Correct |
12 ms |
972 KB |
Output is correct |
9 |
Correct |
9 ms |
844 KB |
Output is correct |
10 |
Correct |
8 ms |
844 KB |
Output is correct |
11 |
Correct |
6 ms |
1228 KB |
Output is correct |
12 |
Correct |
6 ms |
1100 KB |
Output is correct |
13 |
Correct |
6 ms |
1100 KB |
Output is correct |
14 |
Correct |
9 ms |
1192 KB |
Output is correct |
15 |
Correct |
6 ms |
1228 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
50 ms |
956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1228 KB |
Output is correct |
2 |
Correct |
20 ms |
1220 KB |
Output is correct |
3 |
Correct |
18 ms |
1208 KB |
Output is correct |
4 |
Correct |
19 ms |
1228 KB |
Output is correct |
5 |
Correct |
19 ms |
1208 KB |
Output is correct |
6 |
Correct |
19 ms |
972 KB |
Output is correct |
7 |
Correct |
16 ms |
952 KB |
Output is correct |
8 |
Correct |
12 ms |
972 KB |
Output is correct |
9 |
Correct |
9 ms |
844 KB |
Output is correct |
10 |
Correct |
8 ms |
844 KB |
Output is correct |
11 |
Correct |
6 ms |
1228 KB |
Output is correct |
12 |
Correct |
6 ms |
1100 KB |
Output is correct |
13 |
Correct |
6 ms |
1100 KB |
Output is correct |
14 |
Correct |
9 ms |
1192 KB |
Output is correct |
15 |
Correct |
6 ms |
1228 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
50 ms |
956 KB |
Output is correct |
18 |
Correct |
528 ms |
2128 KB |
Output is correct |
19 |
Correct |
474 ms |
2148 KB |
Output is correct |
20 |
Correct |
537 ms |
2236 KB |
Output is correct |
21 |
Correct |
501 ms |
2072 KB |
Output is correct |
22 |
Correct |
487 ms |
2144 KB |
Output is correct |
23 |
Correct |
612 ms |
1840 KB |
Output is correct |
24 |
Correct |
463 ms |
1888 KB |
Output is correct |
25 |
Correct |
371 ms |
1772 KB |
Output is correct |
26 |
Correct |
289 ms |
1860 KB |
Output is correct |
27 |
Correct |
228 ms |
1704 KB |
Output is correct |
28 |
Correct |
42 ms |
1604 KB |
Output is correct |
29 |
Correct |
43 ms |
1596 KB |
Output is correct |
30 |
Correct |
44 ms |
1596 KB |
Output is correct |
31 |
Correct |
54 ms |
1588 KB |
Output is correct |
32 |
Correct |
47 ms |
1668 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Execution timed out |
1081 ms |
1484 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
504 ms |
30556 KB |
Output is correct |
2 |
Correct |
451 ms |
30152 KB |
Output is correct |
3 |
Correct |
394 ms |
28616 KB |
Output is correct |
4 |
Correct |
490 ms |
28420 KB |
Output is correct |
5 |
Correct |
413 ms |
29068 KB |
Output is correct |
6 |
Correct |
84 ms |
20408 KB |
Output is correct |
7 |
Correct |
90 ms |
20276 KB |
Output is correct |
8 |
Correct |
89 ms |
20492 KB |
Output is correct |
9 |
Correct |
98 ms |
20152 KB |
Output is correct |
10 |
Correct |
105 ms |
21944 KB |
Output is correct |
11 |
Correct |
275 ms |
28588 KB |
Output is correct |
12 |
Correct |
376 ms |
30580 KB |
Output is correct |
13 |
Correct |
267 ms |
28584 KB |
Output is correct |
14 |
Correct |
314 ms |
29084 KB |
Output is correct |
15 |
Correct |
309 ms |
30356 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
107 ms |
20876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
469 ms |
29724 KB |
Output is correct |
2 |
Correct |
405 ms |
29244 KB |
Output is correct |
3 |
Correct |
415 ms |
29432 KB |
Output is correct |
4 |
Correct |
420 ms |
29496 KB |
Output is correct |
5 |
Correct |
446 ms |
30440 KB |
Output is correct |
6 |
Correct |
74 ms |
20276 KB |
Output is correct |
7 |
Correct |
78 ms |
20572 KB |
Output is correct |
8 |
Correct |
84 ms |
20792 KB |
Output is correct |
9 |
Correct |
92 ms |
21304 KB |
Output is correct |
10 |
Correct |
100 ms |
20648 KB |
Output is correct |
11 |
Correct |
316 ms |
30104 KB |
Output is correct |
12 |
Correct |
296 ms |
28844 KB |
Output is correct |
13 |
Correct |
293 ms |
28644 KB |
Output is correct |
14 |
Correct |
300 ms |
30180 KB |
Output is correct |
15 |
Correct |
305 ms |
29540 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
76 ms |
21700 KB |
Output is correct |
18 |
Correct |
23 ms |
1228 KB |
Output is correct |
19 |
Correct |
20 ms |
1220 KB |
Output is correct |
20 |
Correct |
18 ms |
1208 KB |
Output is correct |
21 |
Correct |
19 ms |
1228 KB |
Output is correct |
22 |
Correct |
19 ms |
1208 KB |
Output is correct |
23 |
Correct |
19 ms |
972 KB |
Output is correct |
24 |
Correct |
16 ms |
952 KB |
Output is correct |
25 |
Correct |
12 ms |
972 KB |
Output is correct |
26 |
Correct |
9 ms |
844 KB |
Output is correct |
27 |
Correct |
8 ms |
844 KB |
Output is correct |
28 |
Correct |
6 ms |
1228 KB |
Output is correct |
29 |
Correct |
6 ms |
1100 KB |
Output is correct |
30 |
Correct |
6 ms |
1100 KB |
Output is correct |
31 |
Correct |
9 ms |
1192 KB |
Output is correct |
32 |
Correct |
6 ms |
1228 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
50 ms |
956 KB |
Output is correct |
35 |
Correct |
528 ms |
2128 KB |
Output is correct |
36 |
Correct |
474 ms |
2148 KB |
Output is correct |
37 |
Correct |
537 ms |
2236 KB |
Output is correct |
38 |
Correct |
501 ms |
2072 KB |
Output is correct |
39 |
Correct |
487 ms |
2144 KB |
Output is correct |
40 |
Correct |
612 ms |
1840 KB |
Output is correct |
41 |
Correct |
463 ms |
1888 KB |
Output is correct |
42 |
Correct |
371 ms |
1772 KB |
Output is correct |
43 |
Correct |
289 ms |
1860 KB |
Output is correct |
44 |
Correct |
228 ms |
1704 KB |
Output is correct |
45 |
Correct |
42 ms |
1604 KB |
Output is correct |
46 |
Correct |
43 ms |
1596 KB |
Output is correct |
47 |
Correct |
44 ms |
1596 KB |
Output is correct |
48 |
Correct |
54 ms |
1588 KB |
Output is correct |
49 |
Correct |
47 ms |
1668 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Execution timed out |
1081 ms |
1484 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |