#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define fir first
#define sec second
#define pdd pair<long double, long double>
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
const int mxN=200010;
const int MOD=1e9+7;
int N, Q, M;
pii A[mxN];
vector <int> coor;
int sps[mxN][20];
int mins[mxN];
int ans[mxN];
pii rng[mxN];
pii qry[mxN];
void make_coor()
{
for(int i=1;i<=N;i++) coor.push_back(A[i].fir), coor.push_back(A[i].sec);
sort(coor.begin(), coor.end());
coor.erase(unique(coor.begin(), coor.end()), coor.end());
for(int i=1;i<=N;i++) A[i].fir=lower_bound(coor.begin(), coor.end(), A[i].fir)-coor.begin();
for(int i=1;i<=N;i++) A[i].sec=lower_bound(coor.begin(), coor.end(), A[i].sec)-coor.begin();
M=coor.size();
}
int seg[4*mxN];
void init(int idx, int s, int e)
{
if(s==e)
{
seg[idx]=mins[s];
return;
}
int mid=(s+e)/2;
init(2*idx, s, mid);
init(2*idx+1, mid+1, e);
seg[idx]=min(seg[2*idx], seg[2*idx+1]);
}
int solv(int idx, int s1, int e1, int s2, int e2)
{
if(s2>e1 || s1>e2) return M;
if(s2<=s1 && e1<=e2) return seg[idx];
int mid=(s1+e1)/2;
return min(solv(2*idx, s1, mid, s2, e2), solv(2*idx+1, mid+1, e1, s2, e2));
}
int main()
{
gibon
cin >> N >> Q;
for(int i=1;i<=N;i++) cin >> A[i].fir >> A[i].sec;
make_coor();
for(int i=1;i<=N;i++) sps[i][0]=A[i].fir;
for(int i=1;i<20;i++)
{
for(int j=0;j<M;j++) mins[j]=M;
for(int j=1;j<=N;j++) mins[A[j].sec]=min(mins[A[j].sec], sps[j][i-1]);
init(1, 0, M-1);
for(int j=1;j<=N;j++) sps[j][i]=solv(1, 0, M-1, sps[j][i-1], A[j].sec);
}
for(int i=1;i<=Q;i++) cin >> qry[i].fir >> qry[i].sec;
for(int i=1;i<=Q;i++) rng[i]=A[qry[i].sec];
for(int i=19;i>=0;i--)
{
for(int j=0;j<M;j++) mins[j]=M;
for(int j=1;j<=N;j++) mins[A[j].sec]=min(mins[A[j].sec], sps[j][i]);
init(1, 0, M-1);
for(int j=1;j<=Q;j++)
{
int res=solv(1, 0, M-1, rng[j].fir, rng[j].sec);
if(res>A[qry[j].fir].sec) ans[j]+=(1<<i), rng[j].fir=res;
}
}
for(int i=1;i<=Q;i++)
{
if(sps[qry[i].sec][19]>A[qry[i].fir].sec || A[qry[i].sec].sec<A[qry[i].fir].sec) cout << "impossible\n";
else if(qry[i].sec==qry[i].fir) cout << "0\n";
else if(A[qry[i].sec].fir<=A[qry[i].fir].sec) cout << "1\n";
else cout << ans[i]+2 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
756 ms |
16892 KB |
Output is correct |
3 |
Correct |
818 ms |
16940 KB |
Output is correct |
4 |
Correct |
840 ms |
16832 KB |
Output is correct |
5 |
Correct |
861 ms |
16960 KB |
Output is correct |
6 |
Correct |
855 ms |
17052 KB |
Output is correct |
7 |
Correct |
806 ms |
17104 KB |
Output is correct |
8 |
Correct |
659 ms |
18776 KB |
Output is correct |
9 |
Correct |
652 ms |
18668 KB |
Output is correct |
10 |
Correct |
895 ms |
17268 KB |
Output is correct |
11 |
Correct |
944 ms |
18352 KB |
Output is correct |
12 |
Correct |
450 ms |
16548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
480 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
480 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
476 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
4 ms |
488 KB |
Output is correct |
17 |
Correct |
4 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
276 ms |
4300 KB |
Output is correct |
20 |
Correct |
235 ms |
4328 KB |
Output is correct |
21 |
Correct |
337 ms |
4668 KB |
Output is correct |
22 |
Correct |
325 ms |
4700 KB |
Output is correct |
23 |
Correct |
492 ms |
4300 KB |
Output is correct |
24 |
Correct |
435 ms |
4276 KB |
Output is correct |
25 |
Correct |
260 ms |
4288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
480 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
484 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
5 ms |
480 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
476 KB |
Output is correct |
16 |
Correct |
4 ms |
496 KB |
Output is correct |
17 |
Correct |
4 ms |
488 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
527 ms |
13208 KB |
Output is correct |
20 |
Correct |
489 ms |
12360 KB |
Output is correct |
21 |
Correct |
463 ms |
12988 KB |
Output is correct |
22 |
Correct |
523 ms |
14224 KB |
Output is correct |
23 |
Correct |
425 ms |
14636 KB |
Output is correct |
24 |
Correct |
535 ms |
14612 KB |
Output is correct |
25 |
Correct |
61 ms |
11080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
642 ms |
16864 KB |
Output is correct |
2 |
Correct |
846 ms |
16840 KB |
Output is correct |
3 |
Correct |
871 ms |
16632 KB |
Output is correct |
4 |
Correct |
649 ms |
18596 KB |
Output is correct |
5 |
Correct |
867 ms |
17144 KB |
Output is correct |
6 |
Correct |
1072 ms |
18376 KB |
Output is correct |
7 |
Correct |
1113 ms |
18428 KB |
Output is correct |
8 |
Correct |
1213 ms |
18508 KB |
Output is correct |
9 |
Correct |
470 ms |
14572 KB |
Output is correct |
10 |
Correct |
872 ms |
18048 KB |
Output is correct |
11 |
Correct |
874 ms |
17832 KB |
Output is correct |
12 |
Correct |
926 ms |
18052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
756 ms |
16892 KB |
Output is correct |
3 |
Correct |
818 ms |
16940 KB |
Output is correct |
4 |
Correct |
840 ms |
16832 KB |
Output is correct |
5 |
Correct |
861 ms |
16960 KB |
Output is correct |
6 |
Correct |
855 ms |
17052 KB |
Output is correct |
7 |
Correct |
806 ms |
17104 KB |
Output is correct |
8 |
Correct |
659 ms |
18776 KB |
Output is correct |
9 |
Correct |
652 ms |
18668 KB |
Output is correct |
10 |
Correct |
895 ms |
17268 KB |
Output is correct |
11 |
Correct |
944 ms |
18352 KB |
Output is correct |
12 |
Correct |
450 ms |
16548 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
468 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
480 KB |
Output is correct |
18 |
Correct |
3 ms |
468 KB |
Output is correct |
19 |
Correct |
3 ms |
484 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
468 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
476 KB |
Output is correct |
27 |
Correct |
3 ms |
468 KB |
Output is correct |
28 |
Correct |
4 ms |
488 KB |
Output is correct |
29 |
Correct |
4 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
276 ms |
4300 KB |
Output is correct |
32 |
Correct |
235 ms |
4328 KB |
Output is correct |
33 |
Correct |
337 ms |
4668 KB |
Output is correct |
34 |
Correct |
325 ms |
4700 KB |
Output is correct |
35 |
Correct |
492 ms |
4300 KB |
Output is correct |
36 |
Correct |
435 ms |
4276 KB |
Output is correct |
37 |
Correct |
260 ms |
4288 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
5 ms |
480 KB |
Output is correct |
42 |
Correct |
3 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
476 KB |
Output is correct |
44 |
Correct |
4 ms |
496 KB |
Output is correct |
45 |
Correct |
4 ms |
488 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
527 ms |
13208 KB |
Output is correct |
48 |
Correct |
489 ms |
12360 KB |
Output is correct |
49 |
Correct |
463 ms |
12988 KB |
Output is correct |
50 |
Correct |
523 ms |
14224 KB |
Output is correct |
51 |
Correct |
425 ms |
14636 KB |
Output is correct |
52 |
Correct |
535 ms |
14612 KB |
Output is correct |
53 |
Correct |
61 ms |
11080 KB |
Output is correct |
54 |
Correct |
642 ms |
16864 KB |
Output is correct |
55 |
Correct |
846 ms |
16840 KB |
Output is correct |
56 |
Correct |
871 ms |
16632 KB |
Output is correct |
57 |
Correct |
649 ms |
18596 KB |
Output is correct |
58 |
Correct |
867 ms |
17144 KB |
Output is correct |
59 |
Correct |
1072 ms |
18376 KB |
Output is correct |
60 |
Correct |
1113 ms |
18428 KB |
Output is correct |
61 |
Correct |
1213 ms |
18508 KB |
Output is correct |
62 |
Correct |
470 ms |
14572 KB |
Output is correct |
63 |
Correct |
872 ms |
18048 KB |
Output is correct |
64 |
Correct |
874 ms |
17832 KB |
Output is correct |
65 |
Correct |
926 ms |
18052 KB |
Output is correct |
66 |
Correct |
0 ms |
340 KB |
Output is correct |
67 |
Correct |
657 ms |
16824 KB |
Output is correct |
68 |
Correct |
907 ms |
16880 KB |
Output is correct |
69 |
Correct |
907 ms |
16856 KB |
Output is correct |
70 |
Correct |
912 ms |
17028 KB |
Output is correct |
71 |
Correct |
813 ms |
17008 KB |
Output is correct |
72 |
Correct |
831 ms |
17092 KB |
Output is correct |
73 |
Correct |
724 ms |
18780 KB |
Output is correct |
74 |
Correct |
649 ms |
18816 KB |
Output is correct |
75 |
Correct |
887 ms |
17232 KB |
Output is correct |
76 |
Correct |
922 ms |
18384 KB |
Output is correct |
77 |
Correct |
425 ms |
16596 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
3 ms |
468 KB |
Output is correct |
80 |
Correct |
3 ms |
484 KB |
Output is correct |
81 |
Correct |
4 ms |
468 KB |
Output is correct |
82 |
Correct |
3 ms |
468 KB |
Output is correct |
83 |
Correct |
5 ms |
468 KB |
Output is correct |
84 |
Correct |
3 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
265 ms |
4280 KB |
Output is correct |
87 |
Correct |
225 ms |
4328 KB |
Output is correct |
88 |
Correct |
334 ms |
4668 KB |
Output is correct |
89 |
Correct |
340 ms |
4700 KB |
Output is correct |
90 |
Correct |
487 ms |
4364 KB |
Output is correct |
91 |
Correct |
412 ms |
4268 KB |
Output is correct |
92 |
Correct |
249 ms |
4392 KB |
Output is correct |
93 |
Correct |
482 ms |
13196 KB |
Output is correct |
94 |
Correct |
527 ms |
12496 KB |
Output is correct |
95 |
Correct |
457 ms |
13112 KB |
Output is correct |
96 |
Correct |
507 ms |
14388 KB |
Output is correct |
97 |
Correct |
463 ms |
14616 KB |
Output is correct |
98 |
Correct |
553 ms |
14588 KB |
Output is correct |
99 |
Correct |
53 ms |
11096 KB |
Output is correct |
100 |
Correct |
1145 ms |
18440 KB |
Output is correct |
101 |
Correct |
1347 ms |
18356 KB |
Output is correct |
102 |
Correct |
1388 ms |
18456 KB |
Output is correct |
103 |
Correct |
997 ms |
18080 KB |
Output is correct |
104 |
Correct |
899 ms |
17628 KB |
Output is correct |
105 |
Correct |
948 ms |
18096 KB |
Output is correct |
106 |
Correct |
851 ms |
16212 KB |
Output is correct |
107 |
Correct |
1071 ms |
16324 KB |
Output is correct |
108 |
Correct |
1020 ms |
16992 KB |
Output is correct |
109 |
Correct |
934 ms |
16944 KB |
Output is correct |
110 |
Correct |
1317 ms |
17928 KB |
Output is correct |
111 |
Correct |
1156 ms |
17980 KB |
Output is correct |