#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
int n,q;
array<int,3> a[maxn];
pii rmq[maxn][20];
pii minn[maxn][20];
vi nen;
pii getmin(int l,int r)
{
int lg = __lg(r - l + 1);
return min(minn[l][lg],minn[r - (1 << lg) + 1][lg]);
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
fo(i,1,n)
{
cin >> a[i][0] >> a[i][1];
nen.pb(a[i][0]);
nen.pb(a[i][1]);
a[i][2] = i;
}
sort(all(nen));UNIQUE(nen);
fo(i,1,ss(nen)) fo(j,0,19) minn[i][j] = rmq[i][j] = {inf,inf};
fo(i,1,n)
{
a[i][0] = lower_bound(all(nen),a[i][0]) - nen.begin() + 1;
a[i][1] = lower_bound(all(nen),a[i][1]) - nen.begin() + 1;
minimize(minn[a[i][1]][0],make_pair(a[i][0],i));
}
fo(j,1,19) fo(i,1,ss(nen))
minn[i][j] = min(minn[i][j - 1],minn[i + (1 << j - 1)][j - 1]);
fo(i,1,n)
{
rmq[i][0] = getmin(a[i][0],a[i][1]);
}
// cout << rmq[8][0].fi << ' ' << rmq[8][0].se;en;
fo(j,1,19) fo(i,1,n)
{
if(rmq[i][j - 1].fi == inf) continue;
rmq[i][j] = rmq[rmq[i][j - 1].se][j - 1];
}
while(q--)
{
int l,r;
cin >> l >> r;
if(a[r][1] < a[l][1])
{
cout << "impossible";en;
continue;
}
if(l == r)
{
cout << 0;en;
continue;
}
if(a[r][0] <= a[l][1] and a[l][1] <= a[r][1])
{
cout << 1;en;
continue;
}
int ans = 0;
fod(i,19,0) if(rmq[r][i].fi != inf and rmq[r][i].fi > a[l][1])
{
ans += (1 << i);
r = rmq[r][i].se;
if(r == inf) break;
}
// cout << r;en;
if(r != inf)
{
r = rmq[r][0].se;
ans++;
if(r != inf and a[r][0] <= a[l][1] and a[r][1] >= a[l][1])
{
cout << ans + 1;en;
continue;
}
else cout << "impossible \n";
}
else cout << "impossible\n";
}
}
Compilation message
events.cpp:68:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
68 | main()
| ^~~~
events.cpp: In function 'int main()':
events.cpp:95:58: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
95 | minn[i][j] = min(minn[i][j - 1],minn[i + (1 << j - 1)][j - 1]);
| ~~^~~
events.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
events.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
95 ms |
73160 KB |
Output is correct |
3 |
Correct |
94 ms |
73160 KB |
Output is correct |
4 |
Correct |
148 ms |
72384 KB |
Output is correct |
5 |
Correct |
118 ms |
80344 KB |
Output is correct |
6 |
Correct |
117 ms |
86208 KB |
Output is correct |
7 |
Correct |
117 ms |
88152 KB |
Output is correct |
8 |
Correct |
170 ms |
137408 KB |
Output is correct |
9 |
Correct |
165 ms |
137408 KB |
Output is correct |
10 |
Correct |
114 ms |
74432 KB |
Output is correct |
11 |
Correct |
136 ms |
99516 KB |
Output is correct |
12 |
Correct |
74 ms |
73928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
3 ms |
6876 KB |
Output is correct |
4 |
Correct |
2 ms |
6904 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
3 ms |
6876 KB |
Output is correct |
4 |
Correct |
2 ms |
6904 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
2 ms |
6736 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
2 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6736 KB |
Output is correct |
16 |
Correct |
2 ms |
6740 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
25 ms |
10568 KB |
Output is correct |
20 |
Correct |
25 ms |
10380 KB |
Output is correct |
21 |
Correct |
28 ms |
10832 KB |
Output is correct |
22 |
Correct |
23 ms |
10824 KB |
Output is correct |
23 |
Correct |
22 ms |
14684 KB |
Output is correct |
24 |
Correct |
20 ms |
14672 KB |
Output is correct |
25 |
Correct |
24 ms |
14464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
3 ms |
6876 KB |
Output is correct |
4 |
Correct |
2 ms |
6904 KB |
Output is correct |
5 |
Correct |
2 ms |
6736 KB |
Output is correct |
6 |
Correct |
2 ms |
6736 KB |
Output is correct |
7 |
Correct |
3 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
6736 KB |
Output is correct |
9 |
Correct |
2 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Correct |
2 ms |
6736 KB |
Output is correct |
13 |
Correct |
2 ms |
6736 KB |
Output is correct |
14 |
Correct |
2 ms |
6736 KB |
Output is correct |
15 |
Correct |
2 ms |
6908 KB |
Output is correct |
16 |
Correct |
2 ms |
6736 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
86 ms |
72460 KB |
Output is correct |
20 |
Correct |
79 ms |
71680 KB |
Output is correct |
21 |
Correct |
90 ms |
72384 KB |
Output is correct |
22 |
Correct |
101 ms |
95424 KB |
Output is correct |
23 |
Correct |
145 ms |
134904 KB |
Output is correct |
24 |
Correct |
154 ms |
135100 KB |
Output is correct |
25 |
Correct |
27 ms |
40644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
71012 KB |
Output is correct |
2 |
Correct |
111 ms |
71104 KB |
Output is correct |
3 |
Correct |
135 ms |
71044 KB |
Output is correct |
4 |
Correct |
170 ms |
134296 KB |
Output is correct |
5 |
Correct |
109 ms |
73664 KB |
Output is correct |
6 |
Correct |
172 ms |
133712 KB |
Output is correct |
7 |
Correct |
181 ms |
133936 KB |
Output is correct |
8 |
Correct |
179 ms |
134084 KB |
Output is correct |
9 |
Correct |
141 ms |
133056 KB |
Output is correct |
10 |
Correct |
170 ms |
133568 KB |
Output is correct |
11 |
Correct |
167 ms |
133312 KB |
Output is correct |
12 |
Correct |
168 ms |
133476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
95 ms |
73160 KB |
Output is correct |
3 |
Correct |
94 ms |
73160 KB |
Output is correct |
4 |
Correct |
148 ms |
72384 KB |
Output is correct |
5 |
Correct |
118 ms |
80344 KB |
Output is correct |
6 |
Correct |
117 ms |
86208 KB |
Output is correct |
7 |
Correct |
117 ms |
88152 KB |
Output is correct |
8 |
Correct |
170 ms |
137408 KB |
Output is correct |
9 |
Correct |
165 ms |
137408 KB |
Output is correct |
10 |
Correct |
114 ms |
74432 KB |
Output is correct |
11 |
Correct |
136 ms |
99516 KB |
Output is correct |
12 |
Correct |
74 ms |
73928 KB |
Output is correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4432 KB |
Output is correct |
15 |
Correct |
3 ms |
6876 KB |
Output is correct |
16 |
Correct |
2 ms |
6904 KB |
Output is correct |
17 |
Correct |
2 ms |
6736 KB |
Output is correct |
18 |
Correct |
2 ms |
6736 KB |
Output is correct |
19 |
Correct |
3 ms |
6736 KB |
Output is correct |
20 |
Correct |
2 ms |
6736 KB |
Output is correct |
21 |
Correct |
2 ms |
4432 KB |
Output is correct |
22 |
Correct |
2 ms |
4432 KB |
Output is correct |
23 |
Correct |
1 ms |
4432 KB |
Output is correct |
24 |
Correct |
2 ms |
6736 KB |
Output is correct |
25 |
Correct |
2 ms |
6736 KB |
Output is correct |
26 |
Correct |
2 ms |
6736 KB |
Output is correct |
27 |
Correct |
2 ms |
6736 KB |
Output is correct |
28 |
Correct |
2 ms |
6740 KB |
Output is correct |
29 |
Correct |
2 ms |
6736 KB |
Output is correct |
30 |
Correct |
1 ms |
4432 KB |
Output is correct |
31 |
Correct |
25 ms |
10568 KB |
Output is correct |
32 |
Correct |
25 ms |
10380 KB |
Output is correct |
33 |
Correct |
28 ms |
10832 KB |
Output is correct |
34 |
Correct |
23 ms |
10824 KB |
Output is correct |
35 |
Correct |
22 ms |
14684 KB |
Output is correct |
36 |
Correct |
20 ms |
14672 KB |
Output is correct |
37 |
Correct |
24 ms |
14464 KB |
Output is correct |
38 |
Correct |
1 ms |
4432 KB |
Output is correct |
39 |
Correct |
1 ms |
4432 KB |
Output is correct |
40 |
Correct |
2 ms |
6736 KB |
Output is correct |
41 |
Correct |
2 ms |
6736 KB |
Output is correct |
42 |
Correct |
2 ms |
6736 KB |
Output is correct |
43 |
Correct |
2 ms |
6908 KB |
Output is correct |
44 |
Correct |
2 ms |
6736 KB |
Output is correct |
45 |
Correct |
2 ms |
6736 KB |
Output is correct |
46 |
Correct |
1 ms |
4432 KB |
Output is correct |
47 |
Correct |
86 ms |
72460 KB |
Output is correct |
48 |
Correct |
79 ms |
71680 KB |
Output is correct |
49 |
Correct |
90 ms |
72384 KB |
Output is correct |
50 |
Correct |
101 ms |
95424 KB |
Output is correct |
51 |
Correct |
145 ms |
134904 KB |
Output is correct |
52 |
Correct |
154 ms |
135100 KB |
Output is correct |
53 |
Correct |
27 ms |
40644 KB |
Output is correct |
54 |
Correct |
96 ms |
71012 KB |
Output is correct |
55 |
Correct |
111 ms |
71104 KB |
Output is correct |
56 |
Correct |
135 ms |
71044 KB |
Output is correct |
57 |
Correct |
170 ms |
134296 KB |
Output is correct |
58 |
Correct |
109 ms |
73664 KB |
Output is correct |
59 |
Correct |
172 ms |
133712 KB |
Output is correct |
60 |
Correct |
181 ms |
133936 KB |
Output is correct |
61 |
Correct |
179 ms |
134084 KB |
Output is correct |
62 |
Correct |
141 ms |
133056 KB |
Output is correct |
63 |
Correct |
170 ms |
133568 KB |
Output is correct |
64 |
Correct |
167 ms |
133312 KB |
Output is correct |
65 |
Correct |
168 ms |
133476 KB |
Output is correct |
66 |
Correct |
1 ms |
4432 KB |
Output is correct |
67 |
Correct |
109 ms |
74172 KB |
Output is correct |
68 |
Correct |
104 ms |
74072 KB |
Output is correct |
69 |
Correct |
155 ms |
74072 KB |
Output is correct |
70 |
Correct |
117 ms |
80320 KB |
Output is correct |
71 |
Correct |
121 ms |
86348 KB |
Output is correct |
72 |
Correct |
125 ms |
88264 KB |
Output is correct |
73 |
Correct |
170 ms |
137408 KB |
Output is correct |
74 |
Correct |
165 ms |
137292 KB |
Output is correct |
75 |
Correct |
115 ms |
74432 KB |
Output is correct |
76 |
Correct |
154 ms |
99516 KB |
Output is correct |
77 |
Correct |
85 ms |
73920 KB |
Output is correct |
78 |
Correct |
2 ms |
4432 KB |
Output is correct |
79 |
Correct |
2 ms |
6736 KB |
Output is correct |
80 |
Correct |
2 ms |
6736 KB |
Output is correct |
81 |
Correct |
2 ms |
6736 KB |
Output is correct |
82 |
Correct |
2 ms |
6736 KB |
Output is correct |
83 |
Correct |
2 ms |
6736 KB |
Output is correct |
84 |
Correct |
2 ms |
6736 KB |
Output is correct |
85 |
Correct |
1 ms |
4432 KB |
Output is correct |
86 |
Correct |
26 ms |
10568 KB |
Output is correct |
87 |
Correct |
34 ms |
10456 KB |
Output is correct |
88 |
Correct |
28 ms |
10832 KB |
Output is correct |
89 |
Correct |
23 ms |
10824 KB |
Output is correct |
90 |
Correct |
22 ms |
14716 KB |
Output is correct |
91 |
Correct |
21 ms |
14900 KB |
Output is correct |
92 |
Correct |
25 ms |
14416 KB |
Output is correct |
93 |
Correct |
101 ms |
72384 KB |
Output is correct |
94 |
Correct |
109 ms |
71652 KB |
Output is correct |
95 |
Correct |
102 ms |
72376 KB |
Output is correct |
96 |
Correct |
117 ms |
95424 KB |
Output is correct |
97 |
Correct |
164 ms |
135104 KB |
Output is correct |
98 |
Correct |
158 ms |
135104 KB |
Output is correct |
99 |
Correct |
31 ms |
40640 KB |
Output is correct |
100 |
Correct |
206 ms |
136920 KB |
Output is correct |
101 |
Correct |
215 ms |
136788 KB |
Output is correct |
102 |
Correct |
190 ms |
137172 KB |
Output is correct |
103 |
Correct |
189 ms |
136664 KB |
Output is correct |
104 |
Correct |
178 ms |
136384 KB |
Output is correct |
105 |
Correct |
187 ms |
136656 KB |
Output is correct |
106 |
Correct |
125 ms |
65728 KB |
Output is correct |
107 |
Correct |
162 ms |
73420 KB |
Output is correct |
108 |
Correct |
143 ms |
74436 KB |
Output is correct |
109 |
Correct |
120 ms |
74548 KB |
Output is correct |
110 |
Correct |
176 ms |
137616 KB |
Output is correct |
111 |
Correct |
170 ms |
136896 KB |
Output is correct |