#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";}
#define debug(X) cout<<"["#X"]"<<X<<endl;
#else
#define debug(X) {}
#endif
#define int long long
const int base = 1<<19;
struct segtree
{
vector<pair<int, int> > tree;
segtree(int n){tree.resize(base*2+10, {0, -n});}
void add(int w, pair<int, int> val)
{
w += base;
if(tree[w] < val) tree[w] = val;
w /= 2;
while(w != 0)
{
tree[w] = max(tree[w*2], tree[w*2+1]);
w /= 2;
}
}
pair<int, int> query(int w, int p, int k, int x, int y)
{
if(p > y || k < x) return {0, -1};
if(x <= p && k <= y) return tree[w];
return max(query(w*2, p, (p+k)/2, x, y),
query(w*2+1, (p+k)/2+1, k, x, y));
}
};
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin>>n>>m;
map<int, int> scl;
vector<pair<int, int> > vals(n);
for(auto& v : vals) cin>>v.first>>v.second;
for(auto& v : vals) scl[v.first] = 1, scl[v.second] = 1;
int cnt = 0;
for(auto& v : scl) v.second = cnt++;
for(auto& v : vals) v.first = scl[v.first], v.second = scl[v.second];
debug(vals);
segtree A(n);
for(auto& v : vals)
if(v.second < v.first)
{A.add(v.first, make_pair(v.second + cnt, -(int)(&v - &vals[0])));}
else
{A.add(v.first, make_pair(v.second, -(int)(&v - &vals[0])));}
vector par(n+1, vector(22, n));
for(auto& v : vals)
{
debug((int)(&v - &vals[0]));
if(v.second < v.first)
{
auto a1 = A.query(1, 0, base-1, v.first, base-1);
auto a2 = A.query(1, 0, base-1, 0, v.second);
if(a2.second != -1){par[(int)(&v - &vals[0])][0] = -a2.second;}
else par[(int)(&v - &vals[0])][0] = -a1.second;
}
else
par[(int)(&v - &vals[0])][0] =
-A.query(1, 0, base-1, v.first, v.second).second;
}
int result = 1e9+7;
auto check = [&](int p, int k, int x)
{
if(x < 0) return false;
if(k < p)
return x >= p || x <= k;
else
return p <= x && x <= k;
};
vals.push_back({-1, -1});
for(int i=1;i<22;i++)
for(int j=0;j<n;j++)
{
par[j][i] = par[par[j][i-1]][i-1];
if(check(vals[j].first, vals[par[j][i-1]].second, vals[par[j][i]].second))
{
// if(par[j][i] != j)
// result = min(result, (1LL<<(i+1)));
par[j][i] = n;
}
}
debug(par);
for(int i=0;i<n;i++)
{
int b = i;
int res = 1;
bool e = true;
while(e)
{
debug(b);
e = false;
int d = 0;
for(;d<22;d++)
{
if(par[b][d] == n) break;
if(par[b][d] == b) break;
if(!check((vals[b].second)%cnt, (vals[i].first-1+cnt)%cnt, vals[par[b][d]].second))
break;
}
if(d == 0) break;
debug(d);
d--;
b = par[b][d];
res += (1<<d);
e = true;
}
debug(b);
debug(par[b][0]);
debug(res);
if(par[b][0] != n && par[b][0] != b)
result = min(result, res+1);
}
if(result > n)
cout<<-1;
else
cout<<result;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16816 KB |
Output is correct |
3 |
Correct |
7 ms |
16732 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
5 ms |
16732 KB |
Output is correct |
6 |
Correct |
6 ms |
16676 KB |
Output is correct |
7 |
Correct |
6 ms |
16660 KB |
Output is correct |
8 |
Correct |
6 ms |
16752 KB |
Output is correct |
9 |
Correct |
7 ms |
16728 KB |
Output is correct |
10 |
Correct |
6 ms |
16732 KB |
Output is correct |
11 |
Correct |
6 ms |
16732 KB |
Output is correct |
12 |
Correct |
6 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16668 KB |
Output is correct |
14 |
Correct |
6 ms |
16880 KB |
Output is correct |
15 |
Correct |
6 ms |
16732 KB |
Output is correct |
16 |
Correct |
8 ms |
16732 KB |
Output is correct |
17 |
Correct |
6 ms |
16732 KB |
Output is correct |
18 |
Correct |
6 ms |
16988 KB |
Output is correct |
19 |
Correct |
6 ms |
16732 KB |
Output is correct |
20 |
Correct |
6 ms |
16732 KB |
Output is correct |
21 |
Correct |
9 ms |
16840 KB |
Output is correct |
22 |
Correct |
6 ms |
16732 KB |
Output is correct |
23 |
Correct |
6 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16816 KB |
Output is correct |
3 |
Correct |
7 ms |
16732 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
5 ms |
16732 KB |
Output is correct |
6 |
Correct |
6 ms |
16676 KB |
Output is correct |
7 |
Correct |
6 ms |
16660 KB |
Output is correct |
8 |
Correct |
6 ms |
16752 KB |
Output is correct |
9 |
Correct |
7 ms |
16728 KB |
Output is correct |
10 |
Correct |
6 ms |
16732 KB |
Output is correct |
11 |
Correct |
6 ms |
16732 KB |
Output is correct |
12 |
Correct |
6 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16668 KB |
Output is correct |
14 |
Correct |
6 ms |
16880 KB |
Output is correct |
15 |
Correct |
6 ms |
16732 KB |
Output is correct |
16 |
Correct |
8 ms |
16732 KB |
Output is correct |
17 |
Correct |
6 ms |
16732 KB |
Output is correct |
18 |
Correct |
6 ms |
16988 KB |
Output is correct |
19 |
Correct |
6 ms |
16732 KB |
Output is correct |
20 |
Correct |
6 ms |
16732 KB |
Output is correct |
21 |
Correct |
9 ms |
16840 KB |
Output is correct |
22 |
Correct |
6 ms |
16732 KB |
Output is correct |
23 |
Correct |
6 ms |
16732 KB |
Output is correct |
24 |
Correct |
6 ms |
16732 KB |
Output is correct |
25 |
Correct |
7 ms |
16800 KB |
Output is correct |
26 |
Correct |
6 ms |
16988 KB |
Output is correct |
27 |
Correct |
7 ms |
16732 KB |
Output is correct |
28 |
Correct |
6 ms |
16984 KB |
Output is correct |
29 |
Correct |
7 ms |
16988 KB |
Output is correct |
30 |
Correct |
7 ms |
16732 KB |
Output is correct |
31 |
Correct |
6 ms |
16732 KB |
Output is correct |
32 |
Correct |
6 ms |
16840 KB |
Output is correct |
33 |
Correct |
7 ms |
16732 KB |
Output is correct |
34 |
Correct |
7 ms |
16824 KB |
Output is correct |
35 |
Correct |
7 ms |
16988 KB |
Output is correct |
36 |
Correct |
6 ms |
16988 KB |
Output is correct |
37 |
Correct |
7 ms |
17240 KB |
Output is correct |
38 |
Correct |
6 ms |
16988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16816 KB |
Output is correct |
3 |
Correct |
7 ms |
16732 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
5 ms |
16732 KB |
Output is correct |
6 |
Correct |
6 ms |
16676 KB |
Output is correct |
7 |
Correct |
6 ms |
16660 KB |
Output is correct |
8 |
Correct |
6 ms |
16752 KB |
Output is correct |
9 |
Correct |
7 ms |
16728 KB |
Output is correct |
10 |
Correct |
6 ms |
16732 KB |
Output is correct |
11 |
Correct |
6 ms |
16732 KB |
Output is correct |
12 |
Correct |
6 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16668 KB |
Output is correct |
14 |
Correct |
6 ms |
16880 KB |
Output is correct |
15 |
Correct |
6 ms |
16732 KB |
Output is correct |
16 |
Correct |
8 ms |
16732 KB |
Output is correct |
17 |
Correct |
6 ms |
16732 KB |
Output is correct |
18 |
Correct |
6 ms |
16988 KB |
Output is correct |
19 |
Correct |
6 ms |
16732 KB |
Output is correct |
20 |
Correct |
6 ms |
16732 KB |
Output is correct |
21 |
Correct |
9 ms |
16840 KB |
Output is correct |
22 |
Correct |
6 ms |
16732 KB |
Output is correct |
23 |
Correct |
6 ms |
16732 KB |
Output is correct |
24 |
Correct |
6 ms |
16732 KB |
Output is correct |
25 |
Correct |
7 ms |
16800 KB |
Output is correct |
26 |
Correct |
6 ms |
16988 KB |
Output is correct |
27 |
Correct |
7 ms |
16732 KB |
Output is correct |
28 |
Correct |
6 ms |
16984 KB |
Output is correct |
29 |
Correct |
7 ms |
16988 KB |
Output is correct |
30 |
Correct |
7 ms |
16732 KB |
Output is correct |
31 |
Correct |
6 ms |
16732 KB |
Output is correct |
32 |
Correct |
6 ms |
16840 KB |
Output is correct |
33 |
Correct |
7 ms |
16732 KB |
Output is correct |
34 |
Correct |
7 ms |
16824 KB |
Output is correct |
35 |
Correct |
7 ms |
16988 KB |
Output is correct |
36 |
Correct |
6 ms |
16988 KB |
Output is correct |
37 |
Correct |
7 ms |
17240 KB |
Output is correct |
38 |
Correct |
6 ms |
16988 KB |
Output is correct |
39 |
Correct |
12 ms |
18780 KB |
Output is correct |
40 |
Correct |
12 ms |
18780 KB |
Output is correct |
41 |
Correct |
12 ms |
18676 KB |
Output is correct |
42 |
Correct |
11 ms |
18268 KB |
Output is correct |
43 |
Correct |
10 ms |
18268 KB |
Output is correct |
44 |
Correct |
12 ms |
18264 KB |
Output is correct |
45 |
Correct |
12 ms |
18524 KB |
Output is correct |
46 |
Correct |
13 ms |
18300 KB |
Output is correct |
47 |
Correct |
12 ms |
18776 KB |
Output is correct |
48 |
Correct |
13 ms |
18712 KB |
Output is correct |
49 |
Correct |
11 ms |
18640 KB |
Output is correct |
50 |
Correct |
15 ms |
18524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16728 KB |
Output is correct |
2 |
Correct |
6 ms |
16732 KB |
Output is correct |
3 |
Correct |
6 ms |
16732 KB |
Output is correct |
4 |
Correct |
6 ms |
16764 KB |
Output is correct |
5 |
Correct |
6 ms |
16732 KB |
Output is correct |
6 |
Correct |
6 ms |
16704 KB |
Output is correct |
7 |
Correct |
6 ms |
16732 KB |
Output is correct |
8 |
Correct |
6 ms |
16732 KB |
Output is correct |
9 |
Correct |
7 ms |
16732 KB |
Output is correct |
10 |
Correct |
8 ms |
16988 KB |
Output is correct |
11 |
Correct |
7 ms |
16732 KB |
Output is correct |
12 |
Correct |
7 ms |
16988 KB |
Output is correct |
13 |
Correct |
7 ms |
16988 KB |
Output is correct |
14 |
Correct |
6 ms |
16988 KB |
Output is correct |
15 |
Correct |
12 ms |
18704 KB |
Output is correct |
16 |
Correct |
12 ms |
18364 KB |
Output is correct |
17 |
Correct |
14 ms |
18524 KB |
Output is correct |
18 |
Correct |
12 ms |
18268 KB |
Output is correct |
19 |
Correct |
12 ms |
18780 KB |
Output is correct |
20 |
Correct |
117 ms |
66032 KB |
Output is correct |
21 |
Correct |
590 ms |
92496 KB |
Output is correct |
22 |
Correct |
542 ms |
92756 KB |
Output is correct |
23 |
Correct |
701 ms |
79696 KB |
Output is correct |
24 |
Correct |
639 ms |
92752 KB |
Output is correct |
25 |
Correct |
345 ms |
77356 KB |
Output is correct |
26 |
Correct |
527 ms |
80168 KB |
Output is correct |
27 |
Correct |
658 ms |
80020 KB |
Output is correct |
28 |
Correct |
410 ms |
92216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16728 KB |
Output is correct |
2 |
Correct |
7 ms |
16728 KB |
Output is correct |
3 |
Correct |
6 ms |
16732 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
6 ms |
16732 KB |
Output is correct |
6 |
Correct |
6 ms |
16732 KB |
Output is correct |
7 |
Correct |
6 ms |
16728 KB |
Output is correct |
8 |
Correct |
6 ms |
16988 KB |
Output is correct |
9 |
Correct |
6 ms |
16900 KB |
Output is correct |
10 |
Correct |
6 ms |
16984 KB |
Output is correct |
11 |
Correct |
7 ms |
16988 KB |
Output is correct |
12 |
Correct |
13 ms |
18776 KB |
Output is correct |
13 |
Correct |
12 ms |
18268 KB |
Output is correct |
14 |
Correct |
11 ms |
18316 KB |
Output is correct |
15 |
Correct |
115 ms |
66128 KB |
Output is correct |
16 |
Correct |
660 ms |
79808 KB |
Output is correct |
17 |
Correct |
620 ms |
92568 KB |
Output is correct |
18 |
Correct |
546 ms |
92676 KB |
Output is correct |
19 |
Correct |
598 ms |
92752 KB |
Output is correct |
20 |
Correct |
701 ms |
79700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16728 KB |
Output is correct |
2 |
Correct |
5 ms |
16816 KB |
Output is correct |
3 |
Correct |
7 ms |
16732 KB |
Output is correct |
4 |
Correct |
6 ms |
16732 KB |
Output is correct |
5 |
Correct |
5 ms |
16732 KB |
Output is correct |
6 |
Correct |
6 ms |
16676 KB |
Output is correct |
7 |
Correct |
6 ms |
16660 KB |
Output is correct |
8 |
Correct |
6 ms |
16752 KB |
Output is correct |
9 |
Correct |
7 ms |
16728 KB |
Output is correct |
10 |
Correct |
6 ms |
16732 KB |
Output is correct |
11 |
Correct |
6 ms |
16732 KB |
Output is correct |
12 |
Correct |
6 ms |
16732 KB |
Output is correct |
13 |
Correct |
6 ms |
16668 KB |
Output is correct |
14 |
Correct |
6 ms |
16880 KB |
Output is correct |
15 |
Correct |
6 ms |
16732 KB |
Output is correct |
16 |
Correct |
8 ms |
16732 KB |
Output is correct |
17 |
Correct |
6 ms |
16732 KB |
Output is correct |
18 |
Correct |
6 ms |
16988 KB |
Output is correct |
19 |
Correct |
6 ms |
16732 KB |
Output is correct |
20 |
Correct |
6 ms |
16732 KB |
Output is correct |
21 |
Correct |
9 ms |
16840 KB |
Output is correct |
22 |
Correct |
6 ms |
16732 KB |
Output is correct |
23 |
Correct |
6 ms |
16732 KB |
Output is correct |
24 |
Correct |
6 ms |
16732 KB |
Output is correct |
25 |
Correct |
7 ms |
16800 KB |
Output is correct |
26 |
Correct |
6 ms |
16988 KB |
Output is correct |
27 |
Correct |
7 ms |
16732 KB |
Output is correct |
28 |
Correct |
6 ms |
16984 KB |
Output is correct |
29 |
Correct |
7 ms |
16988 KB |
Output is correct |
30 |
Correct |
7 ms |
16732 KB |
Output is correct |
31 |
Correct |
6 ms |
16732 KB |
Output is correct |
32 |
Correct |
6 ms |
16840 KB |
Output is correct |
33 |
Correct |
7 ms |
16732 KB |
Output is correct |
34 |
Correct |
7 ms |
16824 KB |
Output is correct |
35 |
Correct |
7 ms |
16988 KB |
Output is correct |
36 |
Correct |
6 ms |
16988 KB |
Output is correct |
37 |
Correct |
7 ms |
17240 KB |
Output is correct |
38 |
Correct |
6 ms |
16988 KB |
Output is correct |
39 |
Correct |
12 ms |
18780 KB |
Output is correct |
40 |
Correct |
12 ms |
18780 KB |
Output is correct |
41 |
Correct |
12 ms |
18676 KB |
Output is correct |
42 |
Correct |
11 ms |
18268 KB |
Output is correct |
43 |
Correct |
10 ms |
18268 KB |
Output is correct |
44 |
Correct |
12 ms |
18264 KB |
Output is correct |
45 |
Correct |
12 ms |
18524 KB |
Output is correct |
46 |
Correct |
13 ms |
18300 KB |
Output is correct |
47 |
Correct |
12 ms |
18776 KB |
Output is correct |
48 |
Correct |
13 ms |
18712 KB |
Output is correct |
49 |
Correct |
11 ms |
18640 KB |
Output is correct |
50 |
Correct |
15 ms |
18524 KB |
Output is correct |
51 |
Correct |
6 ms |
16728 KB |
Output is correct |
52 |
Correct |
6 ms |
16732 KB |
Output is correct |
53 |
Correct |
6 ms |
16732 KB |
Output is correct |
54 |
Correct |
6 ms |
16764 KB |
Output is correct |
55 |
Correct |
6 ms |
16732 KB |
Output is correct |
56 |
Correct |
6 ms |
16704 KB |
Output is correct |
57 |
Correct |
6 ms |
16732 KB |
Output is correct |
58 |
Correct |
6 ms |
16732 KB |
Output is correct |
59 |
Correct |
7 ms |
16732 KB |
Output is correct |
60 |
Correct |
8 ms |
16988 KB |
Output is correct |
61 |
Correct |
7 ms |
16732 KB |
Output is correct |
62 |
Correct |
7 ms |
16988 KB |
Output is correct |
63 |
Correct |
7 ms |
16988 KB |
Output is correct |
64 |
Correct |
6 ms |
16988 KB |
Output is correct |
65 |
Correct |
12 ms |
18704 KB |
Output is correct |
66 |
Correct |
12 ms |
18364 KB |
Output is correct |
67 |
Correct |
14 ms |
18524 KB |
Output is correct |
68 |
Correct |
12 ms |
18268 KB |
Output is correct |
69 |
Correct |
12 ms |
18780 KB |
Output is correct |
70 |
Correct |
117 ms |
66032 KB |
Output is correct |
71 |
Correct |
590 ms |
92496 KB |
Output is correct |
72 |
Correct |
542 ms |
92756 KB |
Output is correct |
73 |
Correct |
701 ms |
79696 KB |
Output is correct |
74 |
Correct |
639 ms |
92752 KB |
Output is correct |
75 |
Correct |
345 ms |
77356 KB |
Output is correct |
76 |
Correct |
527 ms |
80168 KB |
Output is correct |
77 |
Correct |
658 ms |
80020 KB |
Output is correct |
78 |
Correct |
410 ms |
92216 KB |
Output is correct |
79 |
Correct |
6 ms |
16728 KB |
Output is correct |
80 |
Correct |
7 ms |
16728 KB |
Output is correct |
81 |
Correct |
6 ms |
16732 KB |
Output is correct |
82 |
Correct |
6 ms |
16732 KB |
Output is correct |
83 |
Correct |
6 ms |
16732 KB |
Output is correct |
84 |
Correct |
6 ms |
16732 KB |
Output is correct |
85 |
Correct |
6 ms |
16728 KB |
Output is correct |
86 |
Correct |
6 ms |
16988 KB |
Output is correct |
87 |
Correct |
6 ms |
16900 KB |
Output is correct |
88 |
Correct |
6 ms |
16984 KB |
Output is correct |
89 |
Correct |
7 ms |
16988 KB |
Output is correct |
90 |
Correct |
13 ms |
18776 KB |
Output is correct |
91 |
Correct |
12 ms |
18268 KB |
Output is correct |
92 |
Correct |
11 ms |
18316 KB |
Output is correct |
93 |
Correct |
115 ms |
66128 KB |
Output is correct |
94 |
Correct |
660 ms |
79808 KB |
Output is correct |
95 |
Correct |
620 ms |
92568 KB |
Output is correct |
96 |
Correct |
546 ms |
92676 KB |
Output is correct |
97 |
Correct |
598 ms |
92752 KB |
Output is correct |
98 |
Correct |
701 ms |
79700 KB |
Output is correct |
99 |
Correct |
504 ms |
94288 KB |
Output is correct |
100 |
Correct |
520 ms |
94332 KB |
Output is correct |
101 |
Correct |
724 ms |
94316 KB |
Output is correct |
102 |
Correct |
635 ms |
94292 KB |
Output is correct |
103 |
Correct |
577 ms |
81784 KB |
Output is correct |
104 |
Correct |
403 ms |
93012 KB |
Output is correct |
105 |
Correct |
253 ms |
92968 KB |
Output is correct |