#define NDEBUG
#ifdef NDEBUG
#define dbg(TXTMSG) if constexpr (false) cerr << "lol"
#define dbgv(VARN) ((void)0)
#define dbgfor(COND) if constexpr (false) for (COND)
#else
#define _GLIBCXX_DEBUG 1
#define _GLIBCXX_DEBUG_PEDANTIC 1
#pragma GCC optimize("trapv")
#define dbg(TXTMSG) cerr << "\n" << TXTMSG
#define dbgv(VARN) cerr << "\n" << #VARN << " = "<< VARN << ", line: " << __LINE__ << "\n"
#define dbgfor(COND) for (COND)
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll,ll>;
#define e0 first
#define e1 second
constexpr ll INFTY = 1e11;
template<class T>
struct sptable {
array<vector<pair<T,ll>>,20> cache;
sptable(vector<T> arr) {
cache.fill(vector<pair<T,ll>>(arr.size()));
for (ll i = 0; i < (ll)arr.size(); ++i)
{
cache[0][i]=make_pair(arr[i],i);
}
for (ll p = 1; p < 20; p++)
{
for (ll i = 0; i <= ((ll)arr.size()) - (1<<p); ++i)
{dbg(p << "f" << i);
cache[p][i] = min(cache[p-1][i], cache[p-1][i+(1<<(p-1))]);
dbgv(cache[p][i].e0.e0);
dbg("h");
}
}
dbg("yay");
}
pair<T,ll> rmq(ll b, ll e) {
ll s = e-b+1;
ll p = 63-__builtin_clzll(s);
return min(cache[p][b],cache[p][e-(1<<p)+1]);
}
};
struct ev {
ll first;
ll second;
ll idx;
bool operator<(const ev& b) const {
return e0 < b.e0;
}
};
ll jumptill(ll ei, ll si, ll maxpow, const vector<ll>* powjmp, const vector<ev>& events) {
dbgv(maxpow);
if (maxpow<=2) {
ll numjmps=1;
while (ei != -1 && events[ei].e0>events[si].e1) {
ei = powjmp[0][ei];
numjmps++;
}
if (ei==-1) return -INFTY;
else return numjmps;
}
ll nei = powjmp[maxpow][ei];
if (nei != -1 && events[nei].e0>events[si].e1) {
return (1<<maxpow) + jumptill(nei,si,maxpow-1,powjmp, events);
}
else {
return jumptill(ei,si,maxpow-1,powjmp, events);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll N,Q;
cin >> N >> Q;
// {s,e}
vector<ev> events(N);
vector<ll> events_indice(N+1);
for (int i = 0; i < N; ++i)
{
cin >> events[i].e0 >> events[i].e1;
events[i].idx = i+1;
}
auto cmpe = [](const ev& a, const ev& b) {
return a.e1 < b.e1;
};
sort(events.begin(),events.end(), cmpe);
for (ll i = 0; i < N; ++i)
{
events_indice[events[i].idx]=i;
}
sptable<ev> jmpfnd(events);
vector<ll> jmpto(N);
auto cmpe2 = [](ll a, const ev& b) {
return a < b.e1;
};
auto cmpe3= [](const ev& a, ll b) {
return a.e1 < b;
};
for (ll i = 0; i < N; ++i)
{
ll lb = lower_bound(events.cbegin(),events.cend(), events[i].e0, cmpe3) - events.cbegin();
ll ub = upper_bound(events.cbegin(),events.cend(), events[i].e1, cmpe2) - events.cbegin() - 1;
dbgv(lb << ub);
if (lb==ub) {
assert(lb==i);
jmpto[i] = -1;
}
else {
jmpto[i] = jmpfnd.rmq(lb, ub).e1;
if (jmpto[i]==i) {
jmpto[i] = -1;
}
}
dbgv(jmpto[i]);
}
array<vector<ll>,20> powjmp;
powjmp[0] = jmpto;
for (ll p = 1; p < 20; p++)
{
powjmp[p].assign(N,-2);
for (ll i = 0; i < N; ++i)
{
if (powjmp[p-1][i]==-1) powjmp[p][i] = -1;
else powjmp[p][i] = powjmp[p-1][powjmp[p-1][i]];
}
}
for (ll qno=0; qno < Q; qno++)
{
ll si,ei;
cin >> si >> ei;
si = events_indice[si];
ei = events_indice[ei];
if (si==ei) {
cout << "0\n";
continue;
}
if (events[si].e1 > events[ei].e1) {
cout << "impossible\n";
continue;
}dbgv(ei);
ll numjmps=jumptill(ei,si,19,powjmp.data(),events);
if (numjmps<=0) {
cout << "impossible\n";
}
else {
cout << numjmps <<"\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
93 ms |
85624 KB |
Output is correct |
3 |
Correct |
83 ms |
86252 KB |
Output is correct |
4 |
Correct |
100 ms |
86468 KB |
Output is correct |
5 |
Correct |
87 ms |
86296 KB |
Output is correct |
6 |
Correct |
89 ms |
86260 KB |
Output is correct |
7 |
Correct |
88 ms |
86512 KB |
Output is correct |
8 |
Correct |
78 ms |
86772 KB |
Output is correct |
9 |
Correct |
78 ms |
86764 KB |
Output is correct |
10 |
Correct |
107 ms |
86536 KB |
Output is correct |
11 |
Correct |
109 ms |
86616 KB |
Output is correct |
12 |
Correct |
72 ms |
86256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
1240 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
1240 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
28 ms |
5660 KB |
Output is correct |
20 |
Correct |
26 ms |
5812 KB |
Output is correct |
21 |
Correct |
32 ms |
6160 KB |
Output is correct |
22 |
Correct |
23 ms |
6072 KB |
Output is correct |
23 |
Correct |
23 ms |
5808 KB |
Output is correct |
24 |
Correct |
23 ms |
5824 KB |
Output is correct |
25 |
Correct |
27 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
1240 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
1276 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1372 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
62 ms |
83884 KB |
Output is correct |
20 |
Correct |
59 ms |
83440 KB |
Output is correct |
21 |
Correct |
67 ms |
83784 KB |
Output is correct |
22 |
Correct |
66 ms |
83952 KB |
Output is correct |
23 |
Correct |
59 ms |
84468 KB |
Output is correct |
24 |
Correct |
62 ms |
83932 KB |
Output is correct |
25 |
Correct |
46 ms |
83448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
85784 KB |
Output is correct |
2 |
Correct |
84 ms |
86260 KB |
Output is correct |
3 |
Correct |
89 ms |
86256 KB |
Output is correct |
4 |
Correct |
78 ms |
86868 KB |
Output is correct |
5 |
Correct |
103 ms |
86604 KB |
Output is correct |
6 |
Correct |
106 ms |
86512 KB |
Output is correct |
7 |
Correct |
113 ms |
86508 KB |
Output is correct |
8 |
Correct |
98 ms |
86512 KB |
Output is correct |
9 |
Correct |
59 ms |
84608 KB |
Output is correct |
10 |
Correct |
83 ms |
86028 KB |
Output is correct |
11 |
Correct |
82 ms |
85780 KB |
Output is correct |
12 |
Correct |
82 ms |
86152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
93 ms |
85624 KB |
Output is correct |
3 |
Correct |
83 ms |
86252 KB |
Output is correct |
4 |
Correct |
100 ms |
86468 KB |
Output is correct |
5 |
Correct |
87 ms |
86296 KB |
Output is correct |
6 |
Correct |
89 ms |
86260 KB |
Output is correct |
7 |
Correct |
88 ms |
86512 KB |
Output is correct |
8 |
Correct |
78 ms |
86772 KB |
Output is correct |
9 |
Correct |
78 ms |
86764 KB |
Output is correct |
10 |
Correct |
107 ms |
86536 KB |
Output is correct |
11 |
Correct |
109 ms |
86616 KB |
Output is correct |
12 |
Correct |
72 ms |
86256 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
1240 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
1112 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1112 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
28 ms |
5660 KB |
Output is correct |
32 |
Correct |
26 ms |
5812 KB |
Output is correct |
33 |
Correct |
32 ms |
6160 KB |
Output is correct |
34 |
Correct |
23 ms |
6072 KB |
Output is correct |
35 |
Correct |
23 ms |
5808 KB |
Output is correct |
36 |
Correct |
23 ms |
5824 KB |
Output is correct |
37 |
Correct |
27 ms |
5564 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
1276 KB |
Output is correct |
41 |
Correct |
1 ms |
1116 KB |
Output is correct |
42 |
Correct |
1 ms |
1116 KB |
Output is correct |
43 |
Correct |
1 ms |
1116 KB |
Output is correct |
44 |
Correct |
1 ms |
1372 KB |
Output is correct |
45 |
Correct |
2 ms |
1116 KB |
Output is correct |
46 |
Correct |
1 ms |
1116 KB |
Output is correct |
47 |
Correct |
62 ms |
83884 KB |
Output is correct |
48 |
Correct |
59 ms |
83440 KB |
Output is correct |
49 |
Correct |
67 ms |
83784 KB |
Output is correct |
50 |
Correct |
66 ms |
83952 KB |
Output is correct |
51 |
Correct |
59 ms |
84468 KB |
Output is correct |
52 |
Correct |
62 ms |
83932 KB |
Output is correct |
53 |
Correct |
46 ms |
83448 KB |
Output is correct |
54 |
Correct |
82 ms |
85784 KB |
Output is correct |
55 |
Correct |
84 ms |
86260 KB |
Output is correct |
56 |
Correct |
89 ms |
86256 KB |
Output is correct |
57 |
Correct |
78 ms |
86868 KB |
Output is correct |
58 |
Correct |
103 ms |
86604 KB |
Output is correct |
59 |
Correct |
106 ms |
86512 KB |
Output is correct |
60 |
Correct |
113 ms |
86508 KB |
Output is correct |
61 |
Correct |
98 ms |
86512 KB |
Output is correct |
62 |
Correct |
59 ms |
84608 KB |
Output is correct |
63 |
Correct |
83 ms |
86028 KB |
Output is correct |
64 |
Correct |
82 ms |
85780 KB |
Output is correct |
65 |
Correct |
82 ms |
86152 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
84 ms |
86352 KB |
Output is correct |
68 |
Correct |
87 ms |
86944 KB |
Output is correct |
69 |
Correct |
91 ms |
86356 KB |
Output is correct |
70 |
Correct |
88 ms |
86284 KB |
Output is correct |
71 |
Correct |
89 ms |
86256 KB |
Output is correct |
72 |
Correct |
94 ms |
86512 KB |
Output is correct |
73 |
Correct |
79 ms |
86768 KB |
Output is correct |
74 |
Correct |
82 ms |
86764 KB |
Output is correct |
75 |
Correct |
109 ms |
86764 KB |
Output is correct |
76 |
Correct |
110 ms |
86664 KB |
Output is correct |
77 |
Correct |
70 ms |
86112 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
1 ms |
1112 KB |
Output is correct |
80 |
Correct |
1 ms |
1116 KB |
Output is correct |
81 |
Correct |
1 ms |
1116 KB |
Output is correct |
82 |
Correct |
1 ms |
1116 KB |
Output is correct |
83 |
Correct |
1 ms |
1112 KB |
Output is correct |
84 |
Correct |
1 ms |
1232 KB |
Output is correct |
85 |
Correct |
1 ms |
1116 KB |
Output is correct |
86 |
Correct |
27 ms |
5948 KB |
Output is correct |
87 |
Correct |
25 ms |
6092 KB |
Output is correct |
88 |
Correct |
30 ms |
6332 KB |
Output is correct |
89 |
Correct |
23 ms |
6320 KB |
Output is correct |
90 |
Correct |
25 ms |
6352 KB |
Output is correct |
91 |
Correct |
22 ms |
6068 KB |
Output is correct |
92 |
Correct |
24 ms |
5820 KB |
Output is correct |
93 |
Correct |
64 ms |
84468 KB |
Output is correct |
94 |
Correct |
61 ms |
83696 KB |
Output is correct |
95 |
Correct |
72 ms |
84468 KB |
Output is correct |
96 |
Correct |
68 ms |
84464 KB |
Output is correct |
97 |
Correct |
62 ms |
84464 KB |
Output is correct |
98 |
Correct |
63 ms |
84504 KB |
Output is correct |
99 |
Correct |
53 ms |
83952 KB |
Output is correct |
100 |
Correct |
110 ms |
86428 KB |
Output is correct |
101 |
Correct |
108 ms |
86512 KB |
Output is correct |
102 |
Correct |
102 ms |
86512 KB |
Output is correct |
103 |
Correct |
87 ms |
86256 KB |
Output is correct |
104 |
Correct |
86 ms |
85744 KB |
Output is correct |
105 |
Correct |
85 ms |
86024 KB |
Output is correct |
106 |
Correct |
106 ms |
85844 KB |
Output is correct |
107 |
Correct |
115 ms |
85780 KB |
Output is correct |
108 |
Correct |
100 ms |
86508 KB |
Output is correct |
109 |
Correct |
109 ms |
86484 KB |
Output is correct |
110 |
Correct |
110 ms |
86288 KB |
Output is correct |
111 |
Correct |
107 ms |
86348 KB |
Output is correct |