This documentation is automatically generated by online-judge-tools/verification-helper
// #define PROBLEM "https://atcoder.jp/contests/tdpc/tasks/tdpc_target"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"
/*
* Typical DP Contest K - ターゲット
* https://atcoder.jp/contests/tdpc/submissions/66823999
* (verifyのための非想定解)
*/
#include "../../Src/DataStructure/SegmentTree/OfflineSegmentTree2D.hpp"
using namespace zawa;
#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
#include <ranges>
struct M {
using Element = int;
static Element identity() { return 0; }
static Element operation(Element L, Element R) { return std::max(L, R); }
};
int solve() {
int N;
std::cin >> N;
std::vector<std::pair<int, int>> RX(N);
for (auto& [r, x] : RX) std::cin >> x >> r;
std::ranges::sort(RX);
const int MAX = 100000000;
OfflineSegmentTree2D<int, M> segL(N), segR(N);
for (int i = 0 ; i < N ; i++) {
const auto [r, x] = RX[i];
segL.operation(x, r + MAX - x);
segR.operation(x, r + x);
}
auto exeL = segL.build(), exeR = segR.build();
int ans = 0;
for (const auto& [r, x] : RX) {
const int v = std::max(
exeL.product(0, 0, x, r + MAX - x),
exeR.product(x, 0, 2*MAX+1, r + x)
) + 1;
ans = std::max(v, ans);
exeL.operation(x, r + MAX - x, v);
exeR.operation(x, r + x, v);
}
return ans;
}
int main() {
#ifdef ATCODER
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cout << solve() << '\n';
#else
std::cout << "Hello World\n";
#endif
}
#line 1 "Test/AtCoder/tdpc_target.test.cpp"
// #define PROBLEM "https://atcoder.jp/contests/tdpc/tasks/tdpc_target"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"
/*
* Typical DP Contest K - ターゲット
* https://atcoder.jp/contests/tdpc/submissions/66823999
* (verifyのための非想定解)
*/
#line 2 "Src/DataStructure/SegmentTree/OfflineSegmentTree2D.hpp"
#line 2 "Src/Template/TypeAlias.hpp"
#include <cstdint>
#include <cstddef>
namespace zawa {
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
} // namespace zawa
#line 2 "Src/Algebra/Monoid/MonoidConcept.hpp"
#line 2 "Src/Algebra/Semigroup/SemigroupConcept.hpp"
#include <concepts>
namespace zawa {
namespace concepts {
template <class T>
concept Semigroup = requires {
typename T::Element;
{ T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};
} // namespace concepts
} // namespace zawa
#line 4 "Src/Algebra/Monoid/MonoidConcept.hpp"
#line 6 "Src/Algebra/Monoid/MonoidConcept.hpp"
namespace zawa {
namespace concepts {
template <class T>
concept Identitiable = requires {
typename T::Element;
{ T::identity() } -> std::same_as<typename T::Element>;
};
template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;
} // namespace
} // namespace zawa
#line 2 "Src/Sequence/CompressedSequence.hpp"
#line 4 "Src/Sequence/CompressedSequence.hpp"
#include <vector>
#include <algorithm>
#include <cassert>
#include <iterator>
#include <limits>
namespace zawa {
template <class T>
class CompressedSequence {
public:
static constexpr u32 NotFound = std::numeric_limits<u32>::max();
CompressedSequence() = default;
template <class InputIterator>
CompressedSequence(InputIterator first, InputIterator last) : comped_(first, last), f_{} {
std::sort(comped_.begin(), comped_.end());
comped_.erase(std::unique(comped_.begin(), comped_.end()), comped_.end());
comped_.shrink_to_fit();
f_.reserve(std::distance(first, last));
for (auto it{first} ; it != last ; it++) {
f_.emplace_back(std::distance(comped_.begin(), std::lower_bound(comped_.begin(), comped_.end(), *it)));
}
}
CompressedSequence(const std::vector<T>& A) : CompressedSequence(A.begin(), A.end()) {}
inline usize size() const noexcept {
return comped_.size();
}
u32 operator[](const T& v) const {
return std::distance(comped_.begin(), std::lower_bound(comped_.begin(), comped_.end(), v));
}
u32 upper_bound(const T& v) const {
return std::distance(comped_.begin(), std::upper_bound(comped_.begin(), comped_.end(), v));
}
u32 find(const T& v) const {
u32 i = std::distance(comped_.begin(), std::lower_bound(comped_.begin(), comped_.end(), v));
return i == comped_.size() or comped_[i] != v ? NotFound : i;
}
bool contains(const T& v) const {
u32 i = std::distance(comped_.begin(), std::lower_bound(comped_.begin(), comped_.end(), v));
return i < comped_.size() and comped_[i] == v;
}
u32 at(const T& v) const {
u32 res = find(v);
assert(res != NotFound);
return res;
}
inline u32 map(u32 i) const noexcept {
assert(i < f_.size());
return f_[i];
}
inline T inverse(u32 i) const noexcept {
assert(i < size());
return comped_[i];
}
inline std::vector<T> comped() const noexcept {
return comped_;
}
private:
std::vector<T> comped_;
std::vector<u32> f_;
};
} // namespace zawa
#line 2 "Src/DataStructure/SegmentTree/SegmentTree.hpp"
#line 5 "Src/DataStructure/SegmentTree/SegmentTree.hpp"
#line 8 "Src/DataStructure/SegmentTree/SegmentTree.hpp"
#include <functional>
#include <type_traits>
#include <ostream>
namespace zawa {
template <concepts::Monoid Monoid>
class SegmentTree {
public:
using VM = Monoid;
using V = typename VM::Element;
using OM = Monoid;
using O = typename OM::Element;
SegmentTree() = default;
explicit SegmentTree(usize n) : m_n{ n }, m_dat(n << 1, VM::identity()) {}
explicit SegmentTree(const std::vector<V>& dat) : m_n{ dat.size() }, m_dat(dat.size() << 1, VM::identity()) {
for (usize i{} ; i < m_n ; i++) {
m_dat[i + m_n] = dat[i];
}
for (usize i{m_n} ; i-- ; i) {
m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
}
}
[[nodiscard]] inline usize size() const noexcept {
return m_n;
}
[[nodiscard]] V get(usize i) const {
assert(i < size());
return m_dat[i + m_n];
}
[[nodiscard]] V operator[](usize i) const {
assert(i < size());
return m_dat[i + m_n];
}
void operation(usize i, const O& value) {
assert(i < size());
i += size();
m_dat[i] = OM::operation(m_dat[i], value);
while (i = parent(i), i) {
m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
}
}
void assign(usize i, const V& value) {
assert(i < size());
i += size();
m_dat[i] = value;
while (i = parent(i), i) {
m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
}
}
[[nodiscard]] V product(u32 l, u32 r) const {
assert(l <= r and r <= size());
V L{ VM::identity() }, R{ VM::identity() };
for (l += size(), r += size() ; l < r ; l = parent(l), r = parent(r)) {
if (l & 1) {
L = VM::operation(L, m_dat[l++]);
}
if (r & 1) {
R = VM::operation(m_dat[--r], R);
}
}
return VM::operation(L, R);
}
template <class Function>
[[nodiscard]] usize maxRight(usize l, const Function& f) {
assert(l < size());
static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "maxRight's argument f must be function bool(T)");
assert(f(VM::identity()));
usize res{l}, width{1};
V prod{ VM::identity() };
// 現在の見ている頂点の幅をwidthで持つ
// 境界がある頂点を含む部分木の根を探す
// (折り返す時は必要以上の幅を持つ根になるが、widthを持っているのでオーバーしない)
for (l += size() ; res + width <= size() ; l = parent(l), width <<= 1) if (l & 1) {
if (not f(VM::operation(prod, m_dat[l]))) break;
res += width;
prod = VM::operation(prod, m_dat[l++]);
}
// 根から下って、境界を発見する
while (l = left(l), width >>= 1) {
if (res + width <= size() and f(VM::operation(prod, m_dat[l]))) {
res += width;
prod = VM::operation(prod, m_dat[l++]);
}
}
return res;
}
template <class Function>
[[nodiscard]] usize minLeft(usize r, const Function& f) const {
assert(r <= size());
static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "minLeft's argument f must be function bool(T)");
assert(f(VM::identity()));
usize res{r}, width{1};
V prod{ VM::identity() };
for (r += size() ; res >= width ; r = parent(r), width <<= 1) if (r & 1) {
if (not f(VM::operation(m_dat[r - 1], prod))) break;
res -= width;
prod = VM::operation(prod, m_dat[--r]);
}
while (r = left(r), width >>= 1) {
if (res >= width and f(VM::operation(m_dat[r - 1], prod))) {
res -= width;
prod = VM::operation(m_dat[--r], prod);
}
}
return res;
}
friend std::ostream& operator<<(std::ostream& os, const SegmentTree& st) {
for (usize i{1} ; i < 2 * st.size() ; i++) {
os << st.m_dat[i] << (i + 1 == 2 * st.size() ? "" : " ");
}
return os;
}
private:
constexpr u32 left(u32 v) const {
return v << 1;
}
constexpr u32 right(u32 v) const {
return v << 1 | 1;
}
constexpr u32 parent(u32 v) const {
return v >> 1;
}
usize m_n;
std::vector<V> m_dat;
};
} // namespace zawa
#line 7 "Src/DataStructure/SegmentTree/OfflineSegmentTree2D.hpp"
#line 10 "Src/DataStructure/SegmentTree/OfflineSegmentTree2D.hpp"
namespace zawa {
namespace internal {
template <class T, concepts::Monoid M>
class SegmentTree2D {
public:
using V = M::Element;
SegmentTree2D(const std::vector<T>& xs, const std::vector<T>& ys)
: m_compx{xs}, m_compy(2 * m_compx.size()), m_seg(2 * m_compx.size()) {
std::vector<std::vector<T>> app(2 * m_compx.size());
for (usize i = 0 ; i < xs.size() ; i++) {
T y = ys[i];
for (usize j = m_compx.map(i) + m_compx.size() ; j ; j >>= 1) {
app[j].push_back(y);
}
}
for (usize i = 1 ; i < app.size() ; i++) {
m_compy[i] = CompressedSequence<T>(app[i]);
m_seg[i] = SegmentTree<M>(m_compy[i].size());
}
}
void set(const T& x, const T& y, const V& v) {
auto i = m_compx.at(x);
for (i += m_compx.size() ; i ; i >>= 1) {
m_seg[i].set(m_compy[i].at(y), v);
}
}
void operation(const T& x, const T& y, const V& v) {
auto i = m_compx.at(x);
for (i += m_compx.size() ; i ; i >>= 1) {
m_seg[i].operation(m_compy[i].at(y), v);
}
}
[[nodiscard]] V product(const T& l, const T& d, const T& r, const T& u) const {
assert(l <= r and d <= u);
V L = M::identity(), R = M::identity();
for (usize lidx = m_compx[l] + m_compx.size(), ridx = m_compx[r] + m_compx.size() ; lidx < ridx ; lidx >>= 1, ridx >>= 1) {
if (lidx & 1) {
L = M::operation(L, m_seg[lidx].product(m_compy[lidx][d], m_compy[lidx][u]));
lidx++;
}
if (ridx & 1) {
ridx--;
R = M::operation(m_seg[ridx].product(m_compy[ridx][d], m_compy[ridx][u]), R);
}
}
return M::operation(L, R);
}
private:
CompressedSequence<T> m_compx;
std::vector<CompressedSequence<T>> m_compy;
std::vector<SegmentTree<M>> m_seg;
};
} // namespace internal
template <class T, concepts::Monoid M>
class OfflineSegmentTree2D {
public:
OfflineSegmentTree2D(usize q = 0) {
m_xs.reserve(q);
m_ys.reserve(q);
}
void operation(const T& x, const T& y) {
m_xs.push_back(x);
m_ys.push_back(y);
}
void operation(T&& x, T&& y) {
m_xs.push_back(std::move(x));
m_ys.push_back(std::move(y));
}
[[nodiscard]] internal::SegmentTree2D<T, M> build() const {
return internal::SegmentTree2D<T, M>{m_xs, m_ys};
}
private:
std::vector<T> m_xs{}, m_ys{};
};
} // namespace zawa
#line 11 "Test/AtCoder/tdpc_target.test.cpp"
using namespace zawa;
#line 14 "Test/AtCoder/tdpc_target.test.cpp"
#include <iostream>
#line 16 "Test/AtCoder/tdpc_target.test.cpp"
#include <utility>
#include <ranges>
struct M {
using Element = int;
static Element identity() { return 0; }
static Element operation(Element L, Element R) { return std::max(L, R); }
};
int solve() {
int N;
std::cin >> N;
std::vector<std::pair<int, int>> RX(N);
for (auto& [r, x] : RX) std::cin >> x >> r;
std::ranges::sort(RX);
const int MAX = 100000000;
OfflineSegmentTree2D<int, M> segL(N), segR(N);
for (int i = 0 ; i < N ; i++) {
const auto [r, x] = RX[i];
segL.operation(x, r + MAX - x);
segR.operation(x, r + x);
}
auto exeL = segL.build(), exeR = segR.build();
int ans = 0;
for (const auto& [r, x] : RX) {
const int v = std::max(
exeL.product(0, 0, x, r + MAX - x),
exeR.product(x, 0, 2*MAX+1, r + x)
) + 1;
ans = std::max(v, ans);
exeL.operation(x, r + MAX - x, v);
exeR.operation(x, r + x, v);
}
return ans;
}
int main() {
#ifdef ATCODER
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cout << solve() << '\n';
#else
std::cout << "Hello World\n";
#endif
}