結果
| 問題 |
No.631 Noelちゃんと電車旅行
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-01-05 23:00:23 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 375 ms / 2,000 ms |
| コード長 | 7,897 bytes |
| コンパイル時間 | 2,532 ms |
| コンパイル使用メモリ | 202,568 KB |
| 最終ジャッジ日時 | 2025-01-05 07:05:23 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
#include <bits/stdc++.h>
#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
vector<T> Vec(int n, T v)
{
return vector<T>(n, v);
}
template <class... Args>
auto Vec(int n, Args... args)
{
auto val = Vec(args...);
return vector<decltype(val)>(n, move(val));
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
os << "sz:" << v.size() << "\n[";
for (const auto& p : v) {
os << p << ",";
}
os << "]\n";
return os;
}
template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
os << "(" << p.first << "," << p.second
<< ")";
return os;
}
constexpr ll MOD = (ll)1e9 + 7LL;
constexpr ld PI = static_cast<ld>(3.1415926535898);
template <typename T>
constexpr T INF = numeric_limits<T>::max() / 10;
template <typename Base>
class SegmentTree
{
public:
using BaseAlgebra = Base;
using AccMonoid = typename BaseAlgebra::AccMonoid;
using OpMonoid = typename BaseAlgebra::OpMonoid;
using T = typename BaseAlgebra::T;
using F = typename BaseAlgebra::OpMonoid::T;
SegmentTree(const int n) : data_num(n), size(1 << (1 + __lg(2 * data_num - 1))), half(size >> 1), value(size, AccMonoid::identity()), action(size, OpMonoid::identity()) { assert(n > 0); }
SegmentTree(const std::vector<T>& val) : data_num(val.size()), size(1 << (1 + __lg(2 * data_num - 1))), half(size >> 1), value(size), action(size, OpMonoid::identity())
{
for (int data = 0; data < half; data++) {
if (data < data_num) {
value[data + half] = val[data];
} else {
value[data + half] = AccMonoid::identity();
}
}
for (int node = half - 1; node >= 1; node--) {
value[node] = acc(value[2 * node], value[2 * node + 1]);
}
}
void initialize()
{
for (int i = 0; i < size; i++) {
value[i] = AccMonoid::identity();
action[i] = OpMonoid::identity();
}
}
void initialize(const vector<T>& val)
{
for (int data = 0; data < half; data++) {
if (data < data_num) {
value[data + half] = val[data];
} else {
value[data + half] = AccMonoid::identity();
}
}
for (int node = half - 1; node >= 1; node--) {
value[node] = acc(value[2 * node], value[2 * node + 1]);
}
}
T get(const int a) const
{
assert(0 <= a and a < data_num);
return accumulate(a, a + 1);
}
void set(const int a, const T val)
{
assert(0 <= a and a < data_num);
const int node = a + half;
value[node] = val;
for (int i = node / 2; i > 0; i /= 2) {
value[i] = acc(value[2 * i], value[2 * i + 1]);
}
}
T accumulate(const int a, const int b) const // Accumulate (a,b]
{
assert(0 <= a and a < b and b <= data_num);
return accumulateRec(1, 0, half, a, b);
}
void modify(const int a, const int b, const F& f) // Apply f on (a,b]
{
assert(0 <= a and a < b and b <= data_num);
if (f == OpMonoid::identity()) {
return;
}
modifyRec(1, 0, half, a, b, f);
}
int query(const int a, const int b) const // query (a,b]
{
assert(0 <= a and a < b and b <= data_num);
return queryRec(1, 0, half, a, b);
}
private:
void modifyRec(const int range_index, const int range_left, const int range_right, const int op_left, const int op_right, const F& f)
{
if (op_left <= range_left and range_right <= op_right) {
value[range_index] = act(f, value[range_index]);
action[range_index] = compose(f, action[range_index]);
} else if (range_right <= op_left or op_right <= range_left) {
// Do nothing
} else {
modifyRec(2 * range_index, range_left, (range_left + range_right) / 2, 0, half, action[range_index]);
modifyRec(2 * range_index, range_left, (range_left + range_right) / 2, op_left, op_right, f);
modifyRec(2 * range_index + 1, (range_left + range_right) / 2, range_right, 0, half, action[range_index]);
modifyRec(2 * range_index + 1, (range_left + range_right) / 2, range_right, op_left, op_right, f);
value[range_index] = acc(value[2 * range_index], value[2 * range_index + 1]);
action[range_index] = OpMonoid::identity();
}
}
T accumulateRec(const int range_index, const int range_left, const int range_right, const int op_left, const int op_right) const
{
if (op_left <= range_left and range_right <= op_right) {
return value[range_index];
} else if (range_right <= op_left or op_right <= range_left) {
return AccMonoid::identity();
} else {
return act(action[range_index], acc(accumulateRec(2 * range_index, range_left, (range_left + range_right) / 2, op_left, op_right),
accumulateRec(2 * range_index + 1, (range_left + range_right) / 2, range_right, op_left, op_right)));
}
}
int queryRec(const int range_index, const int range_left, const int range_right, const int op_left, const int op_right) const
{
if (op_left <= range_left and range_right <= op_right) {
return value[range_index];
} else if (range_right <= op_left or op_right <= range_left) {
return AccMonoid::identity();
} else {
return queryRec(2 * range_index, range_left, (range_left + range_right) / 2, op_left, op_right) + queryRec(2 * range_index + 1, (range_left + range_right) / 2, range_right, op_left, op_right);
}
}
const int data_num; // Num of valid data on leaves.
const int size;
const int half;
vector<T> value; // Tree for value(length: size)
vector<F> action; // Tree for action(length: half)
bool has_lazy;
const AccMonoid acc{};
const OpMonoid compose{};
const BaseAlgebra act{};
};
struct Min_Plus {
using T = ll;
struct AccMonoid {
T operator()(const T& a, const T& b) const { return min(a, b); }
static constexpr T identity() { return INF<T>; }
};
struct OpMonoid {
using T = ll;
T operator()(const T& f1, const T& f2) const { return f1 + f2; }
static constexpr T identity() { return 0; }
};
T operator()(const OpMonoid::T& f, const T& x) const { return f + x; }
};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<ll> T(N - 1);
vector<ll> init(N - 1);
vector<ll> tim(N - 1);
int pos = 0;
for (int i = 0; i < N - 1; i++) {
ll prev = (i == 0 ? 0LL : init[i - 1] + 3);
cin >> T[i];
if (prev > T[i]) {
init[i] = prev;
} else {
pos = i;
init[i] = T[i];
}
}
for (int i = 0; i < pos; i++) {
tim[i] = init[pos] - 3 * (pos - i) - T[i];
}
for (int i = pos + 1; i < N - 1; i++) {
tim[i] = init[pos] + 3 * (i - pos) - T[i];
}
int M;
cin >> M;
SegmentTree<Min_Plus> seg(tim);
ll prev = init[N - 2] + 3;
for (int i = 0; i < M; i++) {
int l, r;
cin >> l >> r;
l--, r--;
ll d;
cin >> d;
const ll minimum = seg.accumulate(l, r + 1);
if (minimum >= d) {
seg.modify(l, r + 1, -d);
cout << prev << endl;
} else {
const ll rest = d - minimum;
seg.modify(l, r + 1, -d);
seg.modify(0, N - 1, rest);
prev += rest;
cout << prev << endl;
}
}
return 0;
}