#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int bSize = 100001;
const int bits = 17;
const int NMAX = 100001;
const int VMAX = NMAX * 89;
int dp[NMAX][bits + 1];
pii aint[NMAX * 8];
unordered_map <int, int> mp;
pii v[NMAX];
bool cmp(pii a, pii b){
if(a.second != b.second){
return a.second > b.second;
}
return a.first > b.first;
}
void update(int node, int st, int dr, int a, pii p){
if(st == dr){
aint[node] = min(aint[node], p);
return;
}
int mid = (st + dr) / 2;
if(a <= mid){
update(node * 2, st, mid, a, p);
}else{
update(node * 2 + 1, mid + 1, dr, a, p);
}
aint[node] = min(aint[node * 2], aint[node * 2 + 1]);
}
pii query(int node, int st, int dr, int a, int b){
if(a <= st && dr <= b){
return aint[node];
}
int mid = (st + dr) / 2;
pii maxim = {1e9, 1e9};
if(a <= mid)
maxim = min(maxim, query(node * 2, st, mid, a, b));
if(b > mid)
maxim = min(maxim, query(node * 2 + 1, mid + 1, dr, a, b));
return maxim;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q, i;
cin >> n >> q;
for(i = 0; i < NMAX * 8; i++){
aint[i] = {1e9, 1e9};
}
vector <int> nrm;
for(i = 1; i <= n; i++){
cin >> v[i].first >> v[i].second;
nrm.push_back(v[i].first);
nrm.push_back(v[i].second);
}
int cnt = 0;
sort(nrm.begin(), nrm.end());
for(int i = 0; i < nrm.size(); i++){
if(i == 0 || nrm[i] != nrm[i - 1])
cnt++;
mp[nrm[i]] = cnt;
}
for(i = 1; i <= n; i++){
v[i].first = mp[v[i].first];
v[i].second = mp[v[i].second];
}
for(i = 1; i <= n; i++){
update(1, 1, 2 * n, v[i].second, {v[i].first, i});
}
for(i = 1; i <= n; i++){
pii maxim = query(1, 1, 2 * n, v[i].first, v[i].second);
dp[i][0] = maxim.second;
}
for(int j = 1; j <= bits; j++){
for(int i = 1; i <= n; i++){
dp[i][j] = dp[dp[i][j - 1]][j - 1]; /// Oare cand nu o sa mai uit sa fac stramosii
}
}
while(q--){
int x, y;
cin >> x >> y;
int node = y;
int sol = 0;
int pas = bits;
while(pas >= 0){
int nxt = dp[node][pas];
if(nxt != 0 && v[nxt].first > v[x].second){
node = nxt;
sol += (1 << pas);
}
pas--;
}
if(v[node].first > v[x].second){
node = dp[node][0];
sol++;
}
if(v[node].first <= v[x].second && v[x].second <= v[node].second){
cout << sol + (x != y) << "\n";
}else{
cout << "impossible\n";
}
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:74:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i = 0; i < nrm.size(); i++){
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
116 ms |
23336 KB |
Output is correct |
3 |
Correct |
134 ms |
23380 KB |
Output is correct |
4 |
Correct |
151 ms |
23268 KB |
Output is correct |
5 |
Correct |
127 ms |
23732 KB |
Output is correct |
6 |
Correct |
137 ms |
23948 KB |
Output is correct |
7 |
Correct |
133 ms |
24116 KB |
Output is correct |
8 |
Correct |
140 ms |
28392 KB |
Output is correct |
9 |
Correct |
149 ms |
28292 KB |
Output is correct |
10 |
Correct |
145 ms |
23676 KB |
Output is correct |
11 |
Correct |
134 ms |
25112 KB |
Output is correct |
12 |
Correct |
83 ms |
23128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
4 ms |
6684 KB |
Output is correct |
4 |
Correct |
4 ms |
6740 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6748 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
4 ms |
6684 KB |
Output is correct |
4 |
Correct |
4 ms |
6740 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6748 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
2 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6476 KB |
Output is correct |
12 |
Correct |
3 ms |
6748 KB |
Output is correct |
13 |
Correct |
3 ms |
6740 KB |
Output is correct |
14 |
Correct |
4 ms |
6740 KB |
Output is correct |
15 |
Correct |
4 ms |
6740 KB |
Output is correct |
16 |
Correct |
4 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
3 ms |
6612 KB |
Output is correct |
19 |
Correct |
38 ms |
8780 KB |
Output is correct |
20 |
Correct |
31 ms |
8648 KB |
Output is correct |
21 |
Correct |
33 ms |
8908 KB |
Output is correct |
22 |
Correct |
28 ms |
8996 KB |
Output is correct |
23 |
Correct |
29 ms |
9020 KB |
Output is correct |
24 |
Correct |
29 ms |
9036 KB |
Output is correct |
25 |
Correct |
30 ms |
8732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
4 ms |
6484 KB |
Output is correct |
3 |
Correct |
4 ms |
6684 KB |
Output is correct |
4 |
Correct |
4 ms |
6740 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6748 KB |
Output is correct |
7 |
Correct |
4 ms |
6740 KB |
Output is correct |
8 |
Correct |
4 ms |
6740 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
3 ms |
6488 KB |
Output is correct |
11 |
Correct |
4 ms |
6484 KB |
Output is correct |
12 |
Correct |
4 ms |
6740 KB |
Output is correct |
13 |
Correct |
3 ms |
6740 KB |
Output is correct |
14 |
Correct |
4 ms |
6620 KB |
Output is correct |
15 |
Correct |
5 ms |
6628 KB |
Output is correct |
16 |
Correct |
4 ms |
6744 KB |
Output is correct |
17 |
Correct |
4 ms |
6740 KB |
Output is correct |
18 |
Correct |
3 ms |
6680 KB |
Output is correct |
19 |
Correct |
118 ms |
21584 KB |
Output is correct |
20 |
Correct |
84 ms |
20776 KB |
Output is correct |
21 |
Correct |
112 ms |
21428 KB |
Output is correct |
22 |
Correct |
102 ms |
22756 KB |
Output is correct |
23 |
Correct |
144 ms |
26116 KB |
Output is correct |
24 |
Correct |
149 ms |
26132 KB |
Output is correct |
25 |
Correct |
53 ms |
16560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
23300 KB |
Output is correct |
2 |
Correct |
120 ms |
23332 KB |
Output is correct |
3 |
Correct |
156 ms |
23316 KB |
Output is correct |
4 |
Correct |
149 ms |
28536 KB |
Output is correct |
5 |
Correct |
131 ms |
23732 KB |
Output is correct |
6 |
Correct |
190 ms |
28000 KB |
Output is correct |
7 |
Correct |
176 ms |
28008 KB |
Output is correct |
8 |
Correct |
200 ms |
28124 KB |
Output is correct |
9 |
Correct |
138 ms |
26024 KB |
Output is correct |
10 |
Correct |
157 ms |
27676 KB |
Output is correct |
11 |
Correct |
162 ms |
27396 KB |
Output is correct |
12 |
Correct |
176 ms |
27628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
116 ms |
23336 KB |
Output is correct |
3 |
Correct |
134 ms |
23380 KB |
Output is correct |
4 |
Correct |
151 ms |
23268 KB |
Output is correct |
5 |
Correct |
127 ms |
23732 KB |
Output is correct |
6 |
Correct |
137 ms |
23948 KB |
Output is correct |
7 |
Correct |
133 ms |
24116 KB |
Output is correct |
8 |
Correct |
140 ms |
28392 KB |
Output is correct |
9 |
Correct |
149 ms |
28292 KB |
Output is correct |
10 |
Correct |
145 ms |
23676 KB |
Output is correct |
11 |
Correct |
134 ms |
25112 KB |
Output is correct |
12 |
Correct |
83 ms |
23128 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
4 ms |
6484 KB |
Output is correct |
15 |
Correct |
4 ms |
6684 KB |
Output is correct |
16 |
Correct |
4 ms |
6740 KB |
Output is correct |
17 |
Correct |
4 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6748 KB |
Output is correct |
19 |
Correct |
4 ms |
6740 KB |
Output is correct |
20 |
Correct |
4 ms |
6740 KB |
Output is correct |
21 |
Correct |
3 ms |
6612 KB |
Output is correct |
22 |
Correct |
2 ms |
6484 KB |
Output is correct |
23 |
Correct |
3 ms |
6476 KB |
Output is correct |
24 |
Correct |
3 ms |
6748 KB |
Output is correct |
25 |
Correct |
3 ms |
6740 KB |
Output is correct |
26 |
Correct |
4 ms |
6740 KB |
Output is correct |
27 |
Correct |
4 ms |
6740 KB |
Output is correct |
28 |
Correct |
4 ms |
6740 KB |
Output is correct |
29 |
Correct |
4 ms |
6740 KB |
Output is correct |
30 |
Correct |
3 ms |
6612 KB |
Output is correct |
31 |
Correct |
38 ms |
8780 KB |
Output is correct |
32 |
Correct |
31 ms |
8648 KB |
Output is correct |
33 |
Correct |
33 ms |
8908 KB |
Output is correct |
34 |
Correct |
28 ms |
8996 KB |
Output is correct |
35 |
Correct |
29 ms |
9020 KB |
Output is correct |
36 |
Correct |
29 ms |
9036 KB |
Output is correct |
37 |
Correct |
30 ms |
8732 KB |
Output is correct |
38 |
Correct |
3 ms |
6488 KB |
Output is correct |
39 |
Correct |
4 ms |
6484 KB |
Output is correct |
40 |
Correct |
4 ms |
6740 KB |
Output is correct |
41 |
Correct |
3 ms |
6740 KB |
Output is correct |
42 |
Correct |
4 ms |
6620 KB |
Output is correct |
43 |
Correct |
5 ms |
6628 KB |
Output is correct |
44 |
Correct |
4 ms |
6744 KB |
Output is correct |
45 |
Correct |
4 ms |
6740 KB |
Output is correct |
46 |
Correct |
3 ms |
6680 KB |
Output is correct |
47 |
Correct |
118 ms |
21584 KB |
Output is correct |
48 |
Correct |
84 ms |
20776 KB |
Output is correct |
49 |
Correct |
112 ms |
21428 KB |
Output is correct |
50 |
Correct |
102 ms |
22756 KB |
Output is correct |
51 |
Correct |
144 ms |
26116 KB |
Output is correct |
52 |
Correct |
149 ms |
26132 KB |
Output is correct |
53 |
Correct |
53 ms |
16560 KB |
Output is correct |
54 |
Correct |
115 ms |
23300 KB |
Output is correct |
55 |
Correct |
120 ms |
23332 KB |
Output is correct |
56 |
Correct |
156 ms |
23316 KB |
Output is correct |
57 |
Correct |
149 ms |
28536 KB |
Output is correct |
58 |
Correct |
131 ms |
23732 KB |
Output is correct |
59 |
Correct |
190 ms |
28000 KB |
Output is correct |
60 |
Correct |
176 ms |
28008 KB |
Output is correct |
61 |
Correct |
200 ms |
28124 KB |
Output is correct |
62 |
Correct |
138 ms |
26024 KB |
Output is correct |
63 |
Correct |
157 ms |
27676 KB |
Output is correct |
64 |
Correct |
162 ms |
27396 KB |
Output is correct |
65 |
Correct |
176 ms |
27628 KB |
Output is correct |
66 |
Correct |
3 ms |
6484 KB |
Output is correct |
67 |
Correct |
115 ms |
23484 KB |
Output is correct |
68 |
Correct |
119 ms |
23536 KB |
Output is correct |
69 |
Correct |
151 ms |
23288 KB |
Output is correct |
70 |
Correct |
144 ms |
23684 KB |
Output is correct |
71 |
Correct |
143 ms |
24116 KB |
Output is correct |
72 |
Correct |
136 ms |
24116 KB |
Output is correct |
73 |
Correct |
142 ms |
28408 KB |
Output is correct |
74 |
Correct |
148 ms |
28288 KB |
Output is correct |
75 |
Correct |
139 ms |
23704 KB |
Output is correct |
76 |
Correct |
135 ms |
24984 KB |
Output is correct |
77 |
Correct |
83 ms |
23104 KB |
Output is correct |
78 |
Correct |
3 ms |
6484 KB |
Output is correct |
79 |
Correct |
3 ms |
6740 KB |
Output is correct |
80 |
Correct |
3 ms |
6740 KB |
Output is correct |
81 |
Correct |
4 ms |
6740 KB |
Output is correct |
82 |
Correct |
4 ms |
6740 KB |
Output is correct |
83 |
Correct |
4 ms |
6740 KB |
Output is correct |
84 |
Correct |
4 ms |
6740 KB |
Output is correct |
85 |
Correct |
3 ms |
6616 KB |
Output is correct |
86 |
Correct |
34 ms |
8720 KB |
Output is correct |
87 |
Correct |
31 ms |
8748 KB |
Output is correct |
88 |
Correct |
32 ms |
8908 KB |
Output is correct |
89 |
Correct |
36 ms |
8972 KB |
Output is correct |
90 |
Correct |
29 ms |
9024 KB |
Output is correct |
91 |
Correct |
30 ms |
9040 KB |
Output is correct |
92 |
Correct |
29 ms |
8672 KB |
Output is correct |
93 |
Correct |
90 ms |
21584 KB |
Output is correct |
94 |
Correct |
92 ms |
20712 KB |
Output is correct |
95 |
Correct |
105 ms |
21488 KB |
Output is correct |
96 |
Correct |
105 ms |
22700 KB |
Output is correct |
97 |
Correct |
138 ms |
26084 KB |
Output is correct |
98 |
Correct |
160 ms |
26124 KB |
Output is correct |
99 |
Correct |
55 ms |
16472 KB |
Output is correct |
100 |
Correct |
181 ms |
28140 KB |
Output is correct |
101 |
Correct |
183 ms |
27996 KB |
Output is correct |
102 |
Correct |
156 ms |
28060 KB |
Output is correct |
103 |
Correct |
170 ms |
27796 KB |
Output is correct |
104 |
Correct |
172 ms |
27492 KB |
Output is correct |
105 |
Correct |
180 ms |
27600 KB |
Output is correct |
106 |
Correct |
119 ms |
21320 KB |
Output is correct |
107 |
Correct |
134 ms |
22732 KB |
Output is correct |
108 |
Correct |
134 ms |
23472 KB |
Output is correct |
109 |
Correct |
134 ms |
23464 KB |
Output is correct |
110 |
Correct |
186 ms |
27808 KB |
Output is correct |
111 |
Correct |
180 ms |
27844 KB |
Output is correct |