#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <sstream>
#include <cassert>
#include <vector>
#include <numeric>
#include <string>
#include <bitset>
#include <cmath>
#include <complex>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <random>
#include <chrono>
#include <ctime>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
/*
⠄⠄⠄⢰⣧⣼⣯⠄⣸⣠⣶⣶⣦⣾⠄⠄⠄⠄⡀⠄⢀⣿⣿⠄⠄⠄⢸⡇⠄⠄
⠄⠄⠄⣾⣿⠿⠿⠶⠿⢿⣿⣿⣿⣿⣦⣤⣄⢀⡅⢠⣾⣛⡉⠄⠄⠄⠸⢀⣿⠄
⠄⠄⢀⡋⣡⣴⣶⣶⡀⠄⠄⠙⢿⣿⣿⣿⣿⣿⣴⣿⣿⣿⢃⣤⣄⣀⣥⣿⣿⠄
⠄⠄⢸⣇⠻⣿⣿⣿⣧⣀⢀⣠⡌⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠿⠿⣿⣿⣿⠄
⠄⢀⢸⣿⣷⣤⣤⣤⣬⣙⣛⢿⣿⣿⣿⣿⣿⣿⡿⣿⣿⡍⠄⠄⢀⣤⣄⠉⠋⣰
⠄⣼⣖⣿⣿⣿⣿⣿⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿⢇⣿⣿⡷⠶⠶⢿⣿⣿⠇⢀⣤
⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣽⣿⣿⣿⡇⣿⣿⣿⣿⣿⣿⣷⣶⣥⣴⣿⡗
⢀⠈⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠄
⢸⣿⣦⣌⣛⣻⣿⣿⣧⠙⠛⠛⡭⠅⠒⠦⠭⣭⡻⣿⣿⣿⣿⣿⣿⣿⣿⡿⠃⠄
⠘⣿⣿⣿⣿⣿⣿⣿⣿⡆⠄⠄⠄⠄⠄⠄⠄⠄⠹⠈⢋⣽⣿⣿⣿⣿⣵⣾⠃⠄
⠄⠘⣿⣿⣿⣿⣿⣿⣿⣿⠄⣴⣿⣶⣄⠄⣴⣶⠄⢀⣾⣿⣿⣿⣿⣿⣿⠃⠄⠄
⠄⠄⠈⠻⣿⣿⣿⣿⣿⣿⡄⢻⣿⣿⣿⠄⣿⣿⡀⣾⣿⣿⣿⣿⣛⠛⠁⠄⠄⠄
⠄⠄⠄⠄⠈⠛⢿⣿⣿⣿⠁⠞⢿⣿⣿⡄⢿⣿⡇⣸⣿⣿⠿⠛⠁⠄⠄⠄⠄⠄
⠄⠄⠄⠄⠄⠄⠄⠉⠻⣿⣿⣾⣦⡙⠻⣷⣾⣿⠃⠿⠋⠁⠄⠄⠄⠄⠄⢀⣠⣴
⣿⣿⣿⣶⣶⣮⣥⣒⠲⢮⣝⡿⣿⣿⡆⣿⡿⠃⠄⠄⠄⠄⠄⠄⠄⣠⣴⣿⣿⣿
*/
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define all(x) (x).begin(), (x).end()
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cout << "\n"; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << " " << to_string(H);
debug_out(T...);
}
#ifdef CLOWN
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__);
#else
#define dbg(...);
#endif
int test = 0;
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
const int LOG = 20;
#define int ll
void solve() {
int n, q;
cin >> n;
vector<pii> a(n);
set<pii> s;
vector<vector<int>> up(n + 1, vector<int>(LOG + 1, -1));
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
int r = 0;
for (int l = 0; l < n; l++) {
while (r < n) {
auto it = s.lower_bound({a[r].first, -1});
if (it != s.end() && it->first <= a[r].second) break;
if (it != s.begin() && prev(it)->second >= a[r].first) break;
s.insert({a[r]});
r++;
}
up[l][0] = r;
s.erase(a[l]);
}
for (auto i : up) {
dbg(i)
}
dbg("")
for (int i = 1; i < LOG; i++) {
for (int j = 0; j < n; j++) {
if (up[j][i - 1] == -1) continue;
up[j][i] = up[up[j][i - 1]][i - 1];
}
}
for (auto i : up) {
dbg(i)
}
cin >> q;
while (q--) {
int l;
cin >> l >> r;
l--;
r--;
int ans = 1;
for (int i = LOG; i >= 0; i--) {
if (up[l][i] != -1 && up[l][i] <= r) l = up[l][i], ans += (1 << i);
}
cout << ans << "\n";
}
}
int32_t main() {
#ifdef CLOWN
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
fastio();
if (test) cin >> t;
while (t--) { solve(); }
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
39108 KB |
Output is correct |
2 |
Correct |
134 ms |
42176 KB |
Output is correct |
3 |
Correct |
147 ms |
42284 KB |
Output is correct |
4 |
Correct |
128 ms |
42760 KB |
Output is correct |
5 |
Correct |
128 ms |
44712 KB |
Output is correct |
6 |
Correct |
125 ms |
40704 KB |
Output is correct |
7 |
Correct |
113 ms |
41400 KB |
Output is correct |
8 |
Correct |
127 ms |
41776 KB |
Output is correct |
9 |
Correct |
136 ms |
42756 KB |
Output is correct |
10 |
Correct |
125 ms |
41304 KB |
Output is correct |
11 |
Correct |
266 ms |
52488 KB |
Output is correct |
12 |
Correct |
229 ms |
49316 KB |
Output is correct |
13 |
Correct |
260 ms |
49076 KB |
Output is correct |
14 |
Correct |
209 ms |
48560 KB |
Output is correct |
15 |
Correct |
226 ms |
46136 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
128 ms |
43368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1228 KB |
Output is correct |
2 |
Correct |
5 ms |
1352 KB |
Output is correct |
3 |
Correct |
4 ms |
1352 KB |
Output is correct |
4 |
Correct |
5 ms |
1356 KB |
Output is correct |
5 |
Correct |
5 ms |
1352 KB |
Output is correct |
6 |
Correct |
8 ms |
1484 KB |
Output is correct |
7 |
Correct |
7 ms |
1424 KB |
Output is correct |
8 |
Correct |
5 ms |
1356 KB |
Output is correct |
9 |
Correct |
4 ms |
1356 KB |
Output is correct |
10 |
Correct |
5 ms |
1380 KB |
Output is correct |
11 |
Correct |
7 ms |
1632 KB |
Output is correct |
12 |
Correct |
5 ms |
1484 KB |
Output is correct |
13 |
Correct |
5 ms |
1612 KB |
Output is correct |
14 |
Correct |
6 ms |
1420 KB |
Output is correct |
15 |
Correct |
5 ms |
1484 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
4 ms |
1440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1228 KB |
Output is correct |
2 |
Correct |
5 ms |
1352 KB |
Output is correct |
3 |
Correct |
4 ms |
1352 KB |
Output is correct |
4 |
Correct |
5 ms |
1356 KB |
Output is correct |
5 |
Correct |
5 ms |
1352 KB |
Output is correct |
6 |
Correct |
8 ms |
1484 KB |
Output is correct |
7 |
Correct |
7 ms |
1424 KB |
Output is correct |
8 |
Correct |
5 ms |
1356 KB |
Output is correct |
9 |
Correct |
4 ms |
1356 KB |
Output is correct |
10 |
Correct |
5 ms |
1380 KB |
Output is correct |
11 |
Correct |
7 ms |
1632 KB |
Output is correct |
12 |
Correct |
5 ms |
1484 KB |
Output is correct |
13 |
Correct |
5 ms |
1612 KB |
Output is correct |
14 |
Correct |
6 ms |
1420 KB |
Output is correct |
15 |
Correct |
5 ms |
1484 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
4 ms |
1440 KB |
Output is correct |
18 |
Correct |
106 ms |
4036 KB |
Output is correct |
19 |
Correct |
75 ms |
3792 KB |
Output is correct |
20 |
Correct |
98 ms |
4148 KB |
Output is correct |
21 |
Correct |
77 ms |
3780 KB |
Output is correct |
22 |
Correct |
79 ms |
3920 KB |
Output is correct |
23 |
Correct |
85 ms |
3872 KB |
Output is correct |
24 |
Correct |
79 ms |
3996 KB |
Output is correct |
25 |
Correct |
90 ms |
4036 KB |
Output is correct |
26 |
Correct |
73 ms |
3872 KB |
Output is correct |
27 |
Correct |
74 ms |
3700 KB |
Output is correct |
28 |
Correct |
55 ms |
3560 KB |
Output is correct |
29 |
Correct |
53 ms |
3668 KB |
Output is correct |
30 |
Correct |
54 ms |
3660 KB |
Output is correct |
31 |
Correct |
54 ms |
3480 KB |
Output is correct |
32 |
Correct |
52 ms |
3628 KB |
Output is correct |
33 |
Correct |
1 ms |
292 KB |
Output is correct |
34 |
Correct |
53 ms |
3744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
40972 KB |
Output is correct |
2 |
Correct |
123 ms |
43832 KB |
Output is correct |
3 |
Correct |
115 ms |
40844 KB |
Output is correct |
4 |
Correct |
122 ms |
40456 KB |
Output is correct |
5 |
Correct |
129 ms |
41864 KB |
Output is correct |
6 |
Correct |
110 ms |
40932 KB |
Output is correct |
7 |
Correct |
125 ms |
40644 KB |
Output is correct |
8 |
Correct |
115 ms |
40984 KB |
Output is correct |
9 |
Correct |
117 ms |
40616 KB |
Output is correct |
10 |
Correct |
133 ms |
43772 KB |
Output is correct |
11 |
Correct |
248 ms |
48852 KB |
Output is correct |
12 |
Correct |
238 ms |
53612 KB |
Output is correct |
13 |
Correct |
269 ms |
48740 KB |
Output is correct |
14 |
Correct |
206 ms |
45796 KB |
Output is correct |
15 |
Correct |
209 ms |
49220 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
112 ms |
41620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
39108 KB |
Output is correct |
2 |
Correct |
134 ms |
42176 KB |
Output is correct |
3 |
Correct |
147 ms |
42284 KB |
Output is correct |
4 |
Correct |
128 ms |
42760 KB |
Output is correct |
5 |
Correct |
128 ms |
44712 KB |
Output is correct |
6 |
Correct |
125 ms |
40704 KB |
Output is correct |
7 |
Correct |
113 ms |
41400 KB |
Output is correct |
8 |
Correct |
127 ms |
41776 KB |
Output is correct |
9 |
Correct |
136 ms |
42756 KB |
Output is correct |
10 |
Correct |
125 ms |
41304 KB |
Output is correct |
11 |
Correct |
266 ms |
52488 KB |
Output is correct |
12 |
Correct |
229 ms |
49316 KB |
Output is correct |
13 |
Correct |
260 ms |
49076 KB |
Output is correct |
14 |
Correct |
209 ms |
48560 KB |
Output is correct |
15 |
Correct |
226 ms |
46136 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
128 ms |
43368 KB |
Output is correct |
18 |
Correct |
4 ms |
1228 KB |
Output is correct |
19 |
Correct |
5 ms |
1352 KB |
Output is correct |
20 |
Correct |
4 ms |
1352 KB |
Output is correct |
21 |
Correct |
5 ms |
1356 KB |
Output is correct |
22 |
Correct |
5 ms |
1352 KB |
Output is correct |
23 |
Correct |
8 ms |
1484 KB |
Output is correct |
24 |
Correct |
7 ms |
1424 KB |
Output is correct |
25 |
Correct |
5 ms |
1356 KB |
Output is correct |
26 |
Correct |
4 ms |
1356 KB |
Output is correct |
27 |
Correct |
5 ms |
1380 KB |
Output is correct |
28 |
Correct |
7 ms |
1632 KB |
Output is correct |
29 |
Correct |
5 ms |
1484 KB |
Output is correct |
30 |
Correct |
5 ms |
1612 KB |
Output is correct |
31 |
Correct |
6 ms |
1420 KB |
Output is correct |
32 |
Correct |
5 ms |
1484 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
4 ms |
1440 KB |
Output is correct |
35 |
Correct |
106 ms |
4036 KB |
Output is correct |
36 |
Correct |
75 ms |
3792 KB |
Output is correct |
37 |
Correct |
98 ms |
4148 KB |
Output is correct |
38 |
Correct |
77 ms |
3780 KB |
Output is correct |
39 |
Correct |
79 ms |
3920 KB |
Output is correct |
40 |
Correct |
85 ms |
3872 KB |
Output is correct |
41 |
Correct |
79 ms |
3996 KB |
Output is correct |
42 |
Correct |
90 ms |
4036 KB |
Output is correct |
43 |
Correct |
73 ms |
3872 KB |
Output is correct |
44 |
Correct |
74 ms |
3700 KB |
Output is correct |
45 |
Correct |
55 ms |
3560 KB |
Output is correct |
46 |
Correct |
53 ms |
3668 KB |
Output is correct |
47 |
Correct |
54 ms |
3660 KB |
Output is correct |
48 |
Correct |
54 ms |
3480 KB |
Output is correct |
49 |
Correct |
52 ms |
3628 KB |
Output is correct |
50 |
Correct |
1 ms |
292 KB |
Output is correct |
51 |
Correct |
53 ms |
3744 KB |
Output is correct |
52 |
Correct |
133 ms |
40972 KB |
Output is correct |
53 |
Correct |
123 ms |
43832 KB |
Output is correct |
54 |
Correct |
115 ms |
40844 KB |
Output is correct |
55 |
Correct |
122 ms |
40456 KB |
Output is correct |
56 |
Correct |
129 ms |
41864 KB |
Output is correct |
57 |
Correct |
110 ms |
40932 KB |
Output is correct |
58 |
Correct |
125 ms |
40644 KB |
Output is correct |
59 |
Correct |
115 ms |
40984 KB |
Output is correct |
60 |
Correct |
117 ms |
40616 KB |
Output is correct |
61 |
Correct |
133 ms |
43772 KB |
Output is correct |
62 |
Correct |
248 ms |
48852 KB |
Output is correct |
63 |
Correct |
238 ms |
53612 KB |
Output is correct |
64 |
Correct |
269 ms |
48740 KB |
Output is correct |
65 |
Correct |
206 ms |
45796 KB |
Output is correct |
66 |
Correct |
209 ms |
49220 KB |
Output is correct |
67 |
Correct |
1 ms |
204 KB |
Output is correct |
68 |
Correct |
112 ms |
41620 KB |
Output is correct |
69 |
Correct |
443 ms |
45320 KB |
Output is correct |
70 |
Correct |
491 ms |
47480 KB |
Output is correct |
71 |
Correct |
432 ms |
43972 KB |
Output is correct |
72 |
Correct |
443 ms |
46224 KB |
Output is correct |
73 |
Correct |
440 ms |
45680 KB |
Output is correct |
74 |
Correct |
465 ms |
47872 KB |
Output is correct |
75 |
Correct |
465 ms |
44512 KB |
Output is correct |
76 |
Correct |
494 ms |
48228 KB |
Output is correct |
77 |
Correct |
392 ms |
44492 KB |
Output is correct |
78 |
Correct |
401 ms |
45256 KB |
Output is correct |
79 |
Correct |
371 ms |
56048 KB |
Output is correct |
80 |
Correct |
316 ms |
51940 KB |
Output is correct |
81 |
Correct |
328 ms |
51660 KB |
Output is correct |
82 |
Correct |
296 ms |
49580 KB |
Output is correct |
83 |
Correct |
319 ms |
47480 KB |
Output is correct |
84 |
Correct |
1 ms |
204 KB |
Output is correct |
85 |
Correct |
211 ms |
44452 KB |
Output is correct |