This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef LOCAL
#define FAST_IO
#endif
// ============
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define OVERRIDE(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32) (n); ++i)
#define REP3(i, m, n) for (i32 i = (i32) (m); i < (i32) (n); ++i)
#define REP(...) OVERRIDE(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER(i, n) for (i32 i = (i32) (n) - 1; i >= 0; --i)
#define ALL(x) begin(x), end(x)
using namespace std;
using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = __uint128_t;
using i32 = signed int;
using i64 = signed long long;
using i128 = __int128_t;
using f64 = double;
using f80 = long double;
template <typename T>
using Vec = vector<T>;
template <typename T>
bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
istream &operator>>(istream &is, i128 &x) {
i64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, i128 x) {
os << (i64) x;
return os;
}
istream &operator>>(istream &is, u128 &x) {
u64 v;
is >> v;
x = v;
return is;
}
ostream &operator<<(ostream &os, u128 x) {
os << (u64) x;
return os;
}
[[maybe_unused]] constexpr i32 INF = 1000000100;
[[maybe_unused]] constexpr i64 INF64 = 3000000000000000100;
struct SetUpIO {
SetUpIO() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
cout << fixed << setprecision(15);
}
} set_up_io;
// ============
#ifdef DEBUGF
#else
#define DBG(x) (void) 0
#endif
// ============
#include <cassert>
#include <utility>
#include <vector>
// ============
#include <limits>
#include <utility>
template <typename T>
struct Add {
using Value = T;
static Value id() {
return T(0);
}
static Value op(const Value &lhs, const Value &rhs) {
return lhs + rhs;
}
static Value inv(const Value &x) {
return -x;
}
};
template <typename T>
struct Mul {
using Value = T;
static Value id() {
return Value(1);
}
static Value op(const Value &lhs, const Value &rhs) {
return lhs * rhs;
}
static Value inv(const Value &x) {
return Value(1) / x;
}
};
template <typename T>
struct Min {
using Value = T;
static Value id() {
return std::numeric_limits<T>::max();
}
static Value op(const Value &lhs, const Value &rhs) {
return std::min(lhs, rhs);
}
};
template <typename T>
struct Max {
using Value = T;
static Value id() {
return std::numeric_limits<Value>::min();
}
static Value op(const Value &lhs, const Value &rhs) {
return std::max(lhs, rhs);
}
};
template <typename T>
struct Xor {
using Value = T;
static Value id() {
return T(0);
}
static Value op(const Value &lhs, const Value &rhs) {
return lhs ^ rhs;
}
static Value inv(const Value &x) {
return x;
}
};
template <typename Monoid>
struct Reversible {
using Value = std::pair<typename Monoid::Value, typename Monoid::Value>;
static Value id() {
return Value(Monoid::id(), Monoid::id());
}
static Value op(const Value &v1, const Value &v2) {
return Value(
Monoid::op(v1.first, v2.first),
Monoid::op(v2.second, v1.second));
}
};
// ============
template <typename Monoid>
class SegmentTree {
public:
using Value = typename Monoid::Value;
private:
int old_length;
int length;
std::vector<Value> node;
static int ceil2(int n) {
int l = 1;
while (l < n) {
l <<= 1;
}
return l;
}
public:
SegmentTree(int n) :
old_length(n),
length(ceil2(old_length)),
node(length << 1, Monoid::id()) {
assert(n >= 0);
}
SegmentTree(const std::vector<Value> &v) :
old_length((int) v.size()),
length(ceil2(old_length)),
node(length << 1, Monoid::id()) {
for (int i = 0; i < old_length; ++i) {
node[i + length] = v[i];
}
for (int i = length - 1; i > 0; --i) {
node[i] = Monoid::op(node[i << 1], node[i << 1 | 1]);
}
}
template <typename F>
SegmentTree(int n, const F &f) :
old_length(n), length(ceil2(n)), node(length << 1, Monoid::id()) {
assert(n >= 0);
for (int i = 0; i < old_length; ++i) {
node[i + length] = f(i);
}
for (int i = length - 1; i > 0; --i) {
node[i] = Monoid::op(node[i << 1], node[i << 1 | 1]);
}
}
const Value &operator[](int idx) const {
assert(idx >= 0 && idx < old_length);
return node[idx + length];
}
void update(int idx, Value val) {
assert(idx >= 0 && idx < old_length);
idx += length;
node[idx] = std::move(val);
while (idx != 1) {
idx >>= 1;
node[idx] = Monoid::op(node[idx << 1], node[idx << 1 | 1]);
}
}
Value prod(int l, int r) const {
assert(l >= 0 && l <= r && r <= old_length);
Value prodl = Monoid::id();
Value prodr = Monoid::id();
l += length;
r += length;
while (l != r) {
if (l & 1) {
prodl = Monoid::op(prodl, node[l++]);
}
if (r & 1) {
prodr = Monoid::op(node[--r], prodr);
}
l >>= 1;
r >>= 1;
}
return Monoid::op(prodl, prodr);
}
Value all_prod() const {
return node[1];
}
};
// ============
struct Range {
i64 l, r, a, b;
Range() : l(-1), r(-1), a(-1), b(-1) {}
Range(i64 l, i64 r, i64 a, i64 b) : l(l), r(r), a(a), b(b) {}
};
struct Ops {
using Value = Range;
static Value id() {
return Value();
}
static Value op(Value x, Value y) {
if (x.l == -1) {
return y;
}
if (y.l == -1) {
return x;
}
i64 yb = y.b + y.l - x.r;
if (yb <= x.a) {
return Range(x.l, y.r, y.a + x.a - yb, x.b);
} else {
return Range(x.l, y.r, y.a, x.b + yb - x.a);
}
}
};
int main() {
i32 n;
i64 d;
cin >> n >> d;
Vec<tuple<i64, i64, i64>> stas(n);
for (auto &[x, a, b] : stas) {
cin >> x >> a >> b;
}
sort(ALL(stas));
Vec<i32> idx(n);
iota(ALL(idx), 0);
sort(ALL(idx), [&](i32 i, i32 j) -> bool {
return get<2>(stas[i]) > get<2>(stas[j]);
});
SegmentTree<Ops> seg(n + 2);
seg.update(0, Range(0, 0, 0, 0));
seg.update(n + 1, Range(d, d, 0, 0));
DBG(seg.all_prod().a);
DBG(seg.all_prod().b);
i64 ans = d;
for (i32 i : idx) {
auto [x, a, b] = stas[i];
seg.update(i + 1, Range(x, x, a, 0));
i64 f = seg.all_prod().b;
if (f <= b) {
chmin(ans, f);
}
DBG(seg.all_prod().a);
DBG(seg.all_prod().b);
DBG(i);
}
cout << ans << '\n';
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |