#include <bits/stdc++.h>
const int N = 2e5 + 5;
struct segtree{
int t[4*N] = {0};
int query(int v, int tl, int tr, int l, int r) {
if(tl > r || tr < l) {
return 0;
}
if(tl >= l && tr <= r) {
return t[v];
}
int tm = (tl + tr) / 2;
return std::max(query(v * 2, tl, tm, l, r), query(v * 2 + 1, tm + 1, tr, l, r));
}
void update(int v, int tl, int tr, int index, int value) {
if(tl == tr) {
t[v] = std::max(t[v], value);
}
else {
int tm = (tl + tr) / 2;
if(index <= tm) {
update(v * 2, tl, tm, index, value);
}
else {
update(v * 2 + 1, tm + 1, tr, index, value);
}
t[v] = std::max(t[v * 2], t[v * 2 + 1]);
}
}
}seg;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> l(n + 1), r(n + 1);
for(int i = 1; i <= n; i++) {
std::cin >> l[i] >> r[i];
}
std::vector<int> dp(n + 1, 1);
std::vector<int> get[n + 1];
std::vector<int> update[n + 1];
for(int i = 1; i <= n; i++) {
/**
for(int j = i - l[i] - 1; j > 0; j--) {
if(i > j + r[j]) {
dp[i] = std::max(dp[i], dp[j] + 1);
}
}**/
if(i - l[i] - 1 > 0)
get[i - l[i] - 1].push_back(i);
}
for(int i = 1; i <= n; i++) {
seg.update(1, 1, N - 1, i + r[i], dp[i]);
for(auto x : get[i]) {
dp[x] = std::max(dp[x], seg.query(1, 1, N - 1, 1, x - 1) + 1);
}
}
int ans = 0;
for(int i = 1; i <= n; i++) {
ans = std::max(ans, dp[i]);
}
std::cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |