#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = pair<int, int>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
const int MAXT = 530000;
const int mod = 1e9 + 7;
struct seg{
int tree[MAXT], lazy[MAXT];
void lazydown(int p){
for(int i = 2*p; i < 2*p+2; i++){
lazy[i] += lazy[p];
tree[i] += lazy[p];
}
lazy[p] = 0;
}
void add(int s, int e, int ps, int pe, int p, int v){
if(e < ps || pe < s) return;
if(s <= ps && pe <= e){
tree[p] += v;
lazy[p] += v;
return;
}
lazydown(p);
int pm = (ps+pe)/2;
add(s, e, ps, pm, 2*p, v);
add(s, e, pm+1, pe, 2*p+1, v);
tree[p] = min(tree[2*p], tree[2*p+1]);
}
int query(int s, int e, int ps, int pe, int p){
if(e < ps || pe < s) return 1e9;
if(s <= ps && pe <= e) return tree[p];
lazydown(p);
int pm = (ps+pe)/2;
return min(query(s, e, ps, pm, 2*p), query(s, e, pm+1, pe, 2*p+1));
}
}seg;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> L(n, 1e9), R(n, -1e9), dp(n + 1), nxt(n, 1e9);
for(int i = 0; i < m; i++){
int u, v; cin >> u >> v;
u--; v--;
if(u > v) swap(u, v);
R[u] = max(R[u], v);
L[v] = min(L[v], u);
}
for(int i = 0; i < n; i++){
nxt[i] = R[i];
if(i) nxt[i] = max(nxt[i], nxt[i - 1]);
}
vector<pi> swp;
for(int i = 0; i < n; i++) swp.emplace_back(min(i, L[i]), i);
sort(all(swp));
for(int i = n - 1; i >= 1; i--){
while(sz(swp) && swp.back().first >= i){
seg.add(swp.back().second + 1, n, 0, n, 1, 1);
swp.pop_back();
}
dp[i] = seg.query(max(i, nxt[i-1]) + 1, n, 0, n, 1);
seg.add(i, i, 0, n, 1, dp[i]);
}
cout << dp[1] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
5 ms |
596 KB |
Output is correct |
26 |
Correct |
5 ms |
596 KB |
Output is correct |
27 |
Correct |
5 ms |
596 KB |
Output is correct |
28 |
Correct |
4 ms |
596 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
8 ms |
596 KB |
Output is correct |
31 |
Correct |
8 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
5 ms |
596 KB |
Output is correct |
26 |
Correct |
5 ms |
596 KB |
Output is correct |
27 |
Correct |
5 ms |
596 KB |
Output is correct |
28 |
Correct |
4 ms |
596 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
8 ms |
596 KB |
Output is correct |
31 |
Correct |
8 ms |
596 KB |
Output is correct |
32 |
Correct |
107 ms |
9260 KB |
Output is correct |
33 |
Correct |
124 ms |
9308 KB |
Output is correct |
34 |
Correct |
125 ms |
10536 KB |
Output is correct |
35 |
Correct |
163 ms |
11780 KB |
Output is correct |
36 |
Correct |
176 ms |
11752 KB |
Output is correct |
37 |
Correct |
167 ms |
11708 KB |
Output is correct |
38 |
Correct |
134 ms |
9216 KB |
Output is correct |
39 |
Correct |
106 ms |
9268 KB |
Output is correct |
40 |
Correct |
80 ms |
5968 KB |
Output is correct |
41 |
Correct |
137 ms |
10436 KB |
Output is correct |
42 |
Correct |
172 ms |
11592 KB |
Output is correct |
43 |
Correct |
166 ms |
11648 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |