結果

問題 No.2404 Vertical Throw Up
コンテスト
ユーザー kwm_t
提出日時 2026-04-19 18:58:30
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 4,392 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,577 ms
コンパイル使用メモリ 347,228 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-19 18:58:37
合計ジャッジ時間 5,082 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
#ifndef KWM_T_DATA_STRUCTURE_DYNAMIC_LICHAO_TREE_HPP
#define KWM_T_DATA_STRUCTURE_DYNAMIC_LICHAO_TREE_HPP

#include <algorithm>
#include <cassert>

namespace kwm_t::data_structure {

/**
 * @brief Dynamic Li Chao Tree
 *
 * 典型用途:
 *   - 直線集合の区間・点更新 + 最小値クエリ
 *   - 動的DP最適化
 *
 * 計算量:
 *   - add line: O(log X)
 *   - add segment: O(log^2 X)
 *   - query: O(log X)
 *
 * @tparam T 値型
 * @tparam x_low 定義域左端
 * @tparam x_high 定義域右端
 * @tparam id 初期値(単位元)
 *
 * verified
 *  https://yukicoder.me/submissions/1160623
 */
template <typename T, T x_low, T x_high, T id>
struct DynamicLiChaoTree {

	struct Line {
		T a, b;
		Line(T a, T b) : a(a), b(b) {}
		inline T get(T x) const { return a * x + b; }
	};

	struct Node {
		Line x;
		Node *l, *r;
		Node(const Line &x) : x(x), l(nullptr), r(nullptr) {}
	};

	Node *root;

	DynamicLiChaoTree() : root(nullptr) {}

private:
	Node* add_line(Node *t, Line &x,
		const T &l, const T &r,
		const T &x_l, const T &x_r) {
		if (!t) return new Node(x);

		T t_l = t->x.get(l), t_r = t->x.get(r);

		if (t_l <= x_l && t_r <= x_r) return t;
		if (t_l >= x_l && t_r >= x_r) {
			t->x = x;
			return t;
		}

		T m = (l + r) / 2;
		if (m == r) --m;

		T t_m = t->x.get(m), x_m = x.get(m);

		if (t_m > x_m) {
			std::swap(t->x, x);
			if (x_l >= t_l)
				t->l = add_line(t->l, x, l, m, t_l, t_m);
			else
				t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r);
		}
		else {
			if (t_l >= x_l)
				t->l = add_line(t->l, x, l, m, x_l, x_m);
			else
				t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r);
		}

		return t;
	}

	Node* add_segment(Node *t, Line &x,
		const T &a, const T &b,
		const T &l, const T &r,
		const T &x_l, const T &x_r) {
		if (r < a || b < l) return t;

		if (a <= l && r <= b) {
			Line y{ x };
			return add_line(t, y, l, r, x_l, x_r);
		}

		if (t) {
			T t_l = t->x.get(l), t_r = t->x.get(r);
			if (t_l <= x_l && t_r <= x_r) return t;
		}
		else {
			t = new Node(Line(0, id));
		}

		T m = (l + r) / 2;
		if (m == r) --m;

		T x_m = x.get(m);

		t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m);
		t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r);

		return t;
	}

	T query(const Node *t, const T &l, const T &r, const T &x) const {
		if (!t) return id;

		if (l == r) return t->x.get(x);

		T m = (l + r) / 2;
		if (m == r) --m;

		if (x <= m)
			return std::min(t->x.get(x),
				query(t->l, l, m, x));
		else
			return std::min(t->x.get(x),
				query(t->r, m + 1, r, x));
	}

public:
	void add_line(const T &a, const T &b) {
		Line x(a, b);
		root = add_line(root, x,
			x_low, x_high,
			x.get(x_low), x.get(x_high));
	}

	void add_segment(const T &l, const T &r, const T &a, const T &b) {
		Line x(a, b);
		root = add_segment(root, x,
			l, r - 1,
			x_low, x_high,
			x.get(x_low), x.get(x_high));
	}

	T query(const T &x) const {
		return query(root, x_low, x_high, x);
	}
};

} // namespace kwm_t::data_structure

#endif // KWM_T_DATA_STRUCTURE_DYNAMIC_LICHAO_TREE_HPP
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	long long a; cin >> a;
	kwm_t::data_structure::DynamicLiChaoTree<long long, 0, 100000008, LINF>dp;
	int q; cin >> q;
	while (q--) {
		int t; cin >> t;
		if (1 == t) {
			long long s, t; cin >> s >> t;
			// ax+b
			long long p = -a * (s + t);
			long long q = a * s *t;
			dp.add_segment(s, t, p, q);
		}
		else {
			long long t; cin >> t;
			auto val = dp.query(t) * -1;
			val += -a * t  * t;
			cout << max(0LL, val) << endl;
		}
	}
	return 0;
}
0