#include <bits/stdc++.h>
// cut here
#ifdef _MSC_VER
#include <intrin.h>
#endif
namespace atcoder {
namespace internal {
int ceil_pow2(int n) {
int x = 0;
while ((1U << x) < (unsigned int)(n)) x++;
return x;
}
int bsf(unsigned int n) {
#ifdef _MSC_VER
unsigned long index;
_BitScanForward(&index, n);
return index;
#else
return __builtin_ctz(n);
#endif
}
} // namespace internal
} // namespace atcoder
namespace atcoder {
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
public:
segtree() : segtree(0) {}
segtree(int n) : segtree(std::vector<S>(n, e())) {}
segtree(const std::vector<S>& v) : _n(int(v.size())) {
log = internal::ceil_pow2(_n);
size = 1 << log;
d = std::vector<S>(2 * size, e());
for (int i = 0; i < _n; i++) d[size + i] = v[i];
for (int i = size - 1; i >= 1; i--) {
update(i);
}
}
void set(int p, S x) {
assert(0 <= p && p < _n);
p += size;
d[p] = x;
for (int i = 1; i <= log; i++) update(p >> i);
}
S get(int p) {
assert(0 <= p && p < _n);
return d[p + size];
}
S prod(int l, int r) {
assert(0 <= l && l <= r && r <= _n);
S sml = e(), smr = e();
l += size;
r += size;
while (l < r) {
if (l & 1) sml = op(sml, d[l++]);
if (r & 1) smr = op(d[--r], smr);
l >>= 1;
r >>= 1;
}
return op(sml, smr);
}
S all_prod() { return d[1]; }
template <bool (*f)(S)> int max_right(int l) {
return max_right(l, [](S x) { return f(x); });
}
template <class F> int max_right(int l, F f) {
assert(0 <= l && l <= _n);
assert(f(e()));
if (l == _n) return _n;
l += size;
S sm = e();
do {
while (l % 2 == 0) l >>= 1;
if (!f(op(sm, d[l]))) {
while (l < size) {
l = (2 * l);
if (f(op(sm, d[l]))) {
sm = op(sm, d[l]);
l++;
}
}
return l - size;
}
sm = op(sm, d[l]);
l++;
} while ((l & -l) != l);
return _n;
}
template <bool (*f)(S)> int min_left(int r) {
return min_left(r, [](S x) { return f(x); });
}
template <class F> int min_left(int r, F f) {
assert(0 <= r && r <= _n);
assert(f(e()));
if (r == 0) return 0;
r += size;
S sm = e();
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!f(op(d[r], sm))) {
while (r < size) {
r = (2 * r + 1);
if (f(op(d[r], sm))) {
sm = op(d[r], sm);
r--;
}
}
return r + 1 - size;
}
sm = op(d[r], sm);
} while ((r & -r) != r);
return 0;
}
private:
int _n, size, log;
std::vector<S> d;
void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};
} // namespace atcoder
// cut here
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3CZAtRo ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
const int inf=1e9;
// struct segtree{
// int n;
// vi seg;
// segtree(int _n=0){init(_n);}
// void init(int _n){
// n=_n;
// int si=1;
// while(si<=n) si*=2;
// si*=2;
// seg.clear();
// seg=vi(si,inf);
// }
// void set(int node,int l,int r,int pos,int x){
// if(l==r-1){
// seg[node]=x;
// return;
// }
// int m=(l+r)/2;
// if(pos<m){
// set(node*2+1,l,m,pos,x);
// }else{
// set(node*2+2,m,r,pos,x);
// }
// seg[node]=min(seg[node*2+1],seg[node*2+2]);
// }
// void set(int pos,int x){
// set(0,0,n,pos,x);
// }
// int prod(int node,int l,int r,int _l,int _r){
// if(l>=_r or r<=_l){
// // _e
// return inf;
// }
// if(_l<=l and r<=_r){
// return seg[node];
// }
// int m=(l+r)/2;
// return min(prod(node*2+1,l,m,_l,_r),prod(node*2+2,m,r,_l,_r));
// }
// int prod(int l,int r){
// return prod(0,0,n,l,r);
// }
// int get(int i){
// return prod(i,i+1);
// }
// };
int op(int l,int r){return min(l,r);}
int e(){return inf;}
struct dat{
int l,r,id;
dat(int _l=-1,int _r=-1,int _id=-1):
l(_l),r(_r),id(_id){}
};
signed main(){
_3CZAtRo;
int n,q;
cin>>n>>q;
vec(dat) a(n);
rep(i,n){
cin>>a[i].l>>a[i].r;
a[i].id=i;
}
vec(vec(pii)) qry(n);
rep(_,q){
int s,t;
cin>>s>>t;
qry[t-1].pb(pii(s-1,_));
}
vi tmp;
rep(i,n){
tmp.pb(a[i].l);
tmp.pb(a[i].r);
}
sort(tmp.begin(),tmp.end());
tmp.erase(unique(tmp.begin(),tmp.end()),tmp.end());
int si=sz(tmp);
rep(i,n){
a[i].l=lower_bound(tmp.begin(),tmp.end(),a[i].l)-tmp.begin();
a[i].r=lower_bound(tmp.begin(),tmp.end(),a[i].r)-tmp.begin();
}
const int ln=17;
vec(atcoder::segtree<int,op,e>) segs;
rep(j,ln){
atcoder::segtree<int,op,e> curseg(si);
rep(i,si){
curseg.set(i,inf);
}
segs.pb(curseg);
}
rep(i,n){
int l=a[i].l,r=a[i].r;
int val=segs[0].get(r);
segs[0].set(r,min(val,l));
}
rng(j,1,ln){
rep(i,n){
int r=a[i].r;
int l=segs[j-1].get(r);
int val=segs[j-1].prod(l,r+1);
int old_val=segs[j-1].get(r);
segs[j].set(r,min(old_val,val));
}
}
vec(atcoder::segtree<int,op,e>) adsegs;
rep(j,ln){
atcoder::segtree<int,op,e> curseg(si);
rep(i,si){
curseg.set(i,inf);
}
adsegs.pb(curseg);
}
rep(j,ln){
rep(i,n){
int r=a[i].r;
if(j==0){
adsegs[j].set(r,segs[j].get(r));
}else{
int l=adsegs[j-1].get(r);
adsegs[j].set(r,segs[j].prod(l,r+1));
}
}
}
// print(" output points ");
// for(auto p:a){
// print(p.l,p.r);
// }
// print(".....");
auto af=[&](int i,int need)->int{
int res=1;
int go=a[i].l;
int r=a[i].r;
// print(need,r);
if(r<need) return inf;
if(go<=need and need<=r) return 1;
// print(segs[1].prod(go,r+1));
// rep(_,4){
// res+=1;
// int ngo=segs[0].prod(go,r+1);
// if(go<=need) break;
// if(ngo==go) break;
// go=ngo;
// }
// print(go);
for(int j=ln-1;j>=0;j--){
int pok=go<=need;
if(j){
int val=adsegs[j-1].prod(go,r+1);
if(val<=need) pok=1;
}
// for(int nj=j-1;nj>=0;nj--){
// int val=segs[nj].prod(ngo,r+1);
// if(val<=need)pok=1;
// ngo=val;
// }
if(!pok){
res+=1<<j;
go=segs[j].prod(go,r+1);
}
}
if(go>need) return inf;
// print(go);
return res;
};
// af(5,0);
vi pns(q,inf);
rep(i,n){
for(auto p:qry[i]){
if(p.fi==i){
pns[p.se]=0;
}else{
pns[p.se]=af(i,a[p.fi].r);
}
}
}
rep(i,q){
if(pns[i]==inf){
cout<<"impossible\n";
}else{
cout<<pns[i]<<"\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
690 ms |
42696 KB |
Output is correct |
3 |
Correct |
754 ms |
43152 KB |
Output is correct |
4 |
Correct |
870 ms |
43404 KB |
Output is correct |
5 |
Correct |
804 ms |
43068 KB |
Output is correct |
6 |
Correct |
767 ms |
43324 KB |
Output is correct |
7 |
Correct |
797 ms |
43248 KB |
Output is correct |
8 |
Correct |
673 ms |
80268 KB |
Output is correct |
9 |
Correct |
719 ms |
80196 KB |
Output is correct |
10 |
Correct |
781 ms |
44352 KB |
Output is correct |
11 |
Correct |
888 ms |
80428 KB |
Output is correct |
12 |
Correct |
374 ms |
43200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
6 ms |
852 KB |
Output is correct |
8 |
Correct |
6 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
6 ms |
852 KB |
Output is correct |
8 |
Correct |
6 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
216 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
596 KB |
Output is correct |
14 |
Correct |
4 ms |
596 KB |
Output is correct |
15 |
Correct |
4 ms |
596 KB |
Output is correct |
16 |
Correct |
6 ms |
928 KB |
Output is correct |
17 |
Correct |
5 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
372 KB |
Output is correct |
19 |
Correct |
106 ms |
4776 KB |
Output is correct |
20 |
Correct |
94 ms |
4728 KB |
Output is correct |
21 |
Correct |
87 ms |
5436 KB |
Output is correct |
22 |
Correct |
52 ms |
5524 KB |
Output is correct |
23 |
Correct |
67 ms |
7540 KB |
Output is correct |
24 |
Correct |
59 ms |
7528 KB |
Output is correct |
25 |
Correct |
86 ms |
6948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
4 ms |
596 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
6 ms |
852 KB |
Output is correct |
8 |
Correct |
6 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
596 KB |
Output is correct |
13 |
Correct |
4 ms |
596 KB |
Output is correct |
14 |
Correct |
4 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
6 ms |
852 KB |
Output is correct |
17 |
Correct |
6 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
602 ms |
41284 KB |
Output is correct |
20 |
Correct |
565 ms |
41444 KB |
Output is correct |
21 |
Correct |
581 ms |
41360 KB |
Output is correct |
22 |
Correct |
709 ms |
77480 KB |
Output is correct |
23 |
Correct |
880 ms |
78036 KB |
Output is correct |
24 |
Correct |
943 ms |
78016 KB |
Output is correct |
25 |
Correct |
47 ms |
4964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
729 ms |
42696 KB |
Output is correct |
2 |
Correct |
745 ms |
43144 KB |
Output is correct |
3 |
Correct |
930 ms |
43428 KB |
Output is correct |
4 |
Correct |
693 ms |
80452 KB |
Output is correct |
5 |
Correct |
807 ms |
44360 KB |
Output is correct |
6 |
Correct |
1069 ms |
80880 KB |
Output is correct |
7 |
Correct |
1052 ms |
80764 KB |
Output is correct |
8 |
Correct |
1280 ms |
80908 KB |
Output is correct |
9 |
Correct |
898 ms |
78236 KB |
Output is correct |
10 |
Correct |
1044 ms |
79644 KB |
Output is correct |
11 |
Correct |
1034 ms |
79388 KB |
Output is correct |
12 |
Correct |
994 ms |
79644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
690 ms |
42696 KB |
Output is correct |
3 |
Correct |
754 ms |
43152 KB |
Output is correct |
4 |
Correct |
870 ms |
43404 KB |
Output is correct |
5 |
Correct |
804 ms |
43068 KB |
Output is correct |
6 |
Correct |
767 ms |
43324 KB |
Output is correct |
7 |
Correct |
797 ms |
43248 KB |
Output is correct |
8 |
Correct |
673 ms |
80268 KB |
Output is correct |
9 |
Correct |
719 ms |
80196 KB |
Output is correct |
10 |
Correct |
781 ms |
44352 KB |
Output is correct |
11 |
Correct |
888 ms |
80428 KB |
Output is correct |
12 |
Correct |
374 ms |
43200 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
4 ms |
596 KB |
Output is correct |
16 |
Correct |
4 ms |
596 KB |
Output is correct |
17 |
Correct |
4 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
6 ms |
852 KB |
Output is correct |
20 |
Correct |
6 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
216 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
4 ms |
596 KB |
Output is correct |
25 |
Correct |
4 ms |
596 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
4 ms |
596 KB |
Output is correct |
28 |
Correct |
6 ms |
928 KB |
Output is correct |
29 |
Correct |
5 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
372 KB |
Output is correct |
31 |
Correct |
106 ms |
4776 KB |
Output is correct |
32 |
Correct |
94 ms |
4728 KB |
Output is correct |
33 |
Correct |
87 ms |
5436 KB |
Output is correct |
34 |
Correct |
52 ms |
5524 KB |
Output is correct |
35 |
Correct |
67 ms |
7540 KB |
Output is correct |
36 |
Correct |
59 ms |
7528 KB |
Output is correct |
37 |
Correct |
86 ms |
6948 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
4 ms |
596 KB |
Output is correct |
41 |
Correct |
4 ms |
596 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
596 KB |
Output is correct |
44 |
Correct |
6 ms |
852 KB |
Output is correct |
45 |
Correct |
6 ms |
852 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
602 ms |
41284 KB |
Output is correct |
48 |
Correct |
565 ms |
41444 KB |
Output is correct |
49 |
Correct |
581 ms |
41360 KB |
Output is correct |
50 |
Correct |
709 ms |
77480 KB |
Output is correct |
51 |
Correct |
880 ms |
78036 KB |
Output is correct |
52 |
Correct |
943 ms |
78016 KB |
Output is correct |
53 |
Correct |
47 ms |
4964 KB |
Output is correct |
54 |
Correct |
729 ms |
42696 KB |
Output is correct |
55 |
Correct |
745 ms |
43144 KB |
Output is correct |
56 |
Correct |
930 ms |
43428 KB |
Output is correct |
57 |
Correct |
693 ms |
80452 KB |
Output is correct |
58 |
Correct |
807 ms |
44360 KB |
Output is correct |
59 |
Correct |
1069 ms |
80880 KB |
Output is correct |
60 |
Correct |
1052 ms |
80764 KB |
Output is correct |
61 |
Correct |
1280 ms |
80908 KB |
Output is correct |
62 |
Correct |
898 ms |
78236 KB |
Output is correct |
63 |
Correct |
1044 ms |
79644 KB |
Output is correct |
64 |
Correct |
1034 ms |
79388 KB |
Output is correct |
65 |
Correct |
994 ms |
79644 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
734 ms |
42748 KB |
Output is correct |
68 |
Correct |
751 ms |
43072 KB |
Output is correct |
69 |
Correct |
862 ms |
43400 KB |
Output is correct |
70 |
Correct |
793 ms |
46232 KB |
Output is correct |
71 |
Correct |
772 ms |
46324 KB |
Output is correct |
72 |
Correct |
793 ms |
46508 KB |
Output is correct |
73 |
Correct |
649 ms |
83448 KB |
Output is correct |
74 |
Correct |
688 ms |
83236 KB |
Output is correct |
75 |
Correct |
765 ms |
47408 KB |
Output is correct |
76 |
Correct |
863 ms |
83532 KB |
Output is correct |
77 |
Correct |
380 ms |
45520 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
4 ms |
596 KB |
Output is correct |
80 |
Correct |
4 ms |
592 KB |
Output is correct |
81 |
Correct |
4 ms |
600 KB |
Output is correct |
82 |
Correct |
4 ms |
612 KB |
Output is correct |
83 |
Correct |
6 ms |
844 KB |
Output is correct |
84 |
Correct |
6 ms |
900 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
110 ms |
5736 KB |
Output is correct |
87 |
Correct |
92 ms |
5760 KB |
Output is correct |
88 |
Correct |
88 ms |
6336 KB |
Output is correct |
89 |
Correct |
51 ms |
6476 KB |
Output is correct |
90 |
Correct |
64 ms |
8572 KB |
Output is correct |
91 |
Correct |
57 ms |
8556 KB |
Output is correct |
92 |
Correct |
85 ms |
8020 KB |
Output is correct |
93 |
Correct |
558 ms |
43316 KB |
Output is correct |
94 |
Correct |
551 ms |
42484 KB |
Output is correct |
95 |
Correct |
554 ms |
43312 KB |
Output is correct |
96 |
Correct |
697 ms |
79660 KB |
Output is correct |
97 |
Correct |
887 ms |
80028 KB |
Output is correct |
98 |
Correct |
886 ms |
80020 KB |
Output is correct |
99 |
Correct |
47 ms |
6224 KB |
Output is correct |
100 |
Correct |
1002 ms |
84012 KB |
Output is correct |
101 |
Correct |
1050 ms |
83784 KB |
Output is correct |
102 |
Correct |
1189 ms |
83856 KB |
Output is correct |
103 |
Correct |
1014 ms |
82672 KB |
Output is correct |
104 |
Correct |
1014 ms |
82528 KB |
Output is correct |
105 |
Correct |
976 ms |
82544 KB |
Output is correct |
106 |
Correct |
625 ms |
46268 KB |
Output is correct |
107 |
Correct |
779 ms |
46396 KB |
Output is correct |
108 |
Correct |
639 ms |
47180 KB |
Output is correct |
109 |
Correct |
623 ms |
47084 KB |
Output is correct |
110 |
Correct |
1058 ms |
83932 KB |
Output is correct |
111 |
Correct |
979 ms |
83752 KB |
Output is correct |