結果

問題 No.1297 銅像
ユーザー chocoruskchocorusk
提出日時 2020-11-29 22:16:36
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 295 ms / 2,000 ms
コード長 4,059 bytes
コンパイル時間 1,336 ms
コンパイル使用メモリ 136,240 KB
実行使用メモリ 26,936 KB
最終ジャッジ日時 2023-10-11 02:38:53
合計ジャッジ時間 7,729 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 9 ms
4,352 KB
testcase_07 AC 9 ms
4,540 KB
testcase_08 AC 9 ms
4,352 KB
testcase_09 AC 282 ms
25,848 KB
testcase_10 AC 253 ms
26,792 KB
testcase_11 AC 173 ms
10,600 KB
testcase_12 AC 183 ms
10,708 KB
testcase_13 AC 251 ms
24,168 KB
testcase_14 AC 148 ms
10,560 KB
testcase_15 AC 159 ms
10,572 KB
testcase_16 AC 165 ms
10,692 KB
testcase_17 AC 145 ms
10,552 KB
testcase_18 AC 154 ms
11,264 KB
testcase_19 AC 295 ms
26,936 KB
testcase_20 AC 121 ms
4,968 KB
testcase_21 AC 122 ms
4,916 KB
testcase_22 AC 149 ms
10,540 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:132:15: 警告: ‘dp1’ may be used uninitialized [-Wmaybe-uninitialized]
  132 |     cout<<(ll)dp1<<endl;
      |               ^~~
main.cpp:123:9: 備考: ‘dp1’ はここで定義されています
  123 |     lll dp1, dp2;
      |         ^~~

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
using lll=__int128_t;
// http://satanic0258.hatenablog.com/entry/2016/08/16/181331

using CHT_TYPE = lll;
class ConvexHullTrickDynamic {
private:
	// 直線 **************************************************************
	struct Line {
		CHT_TYPE a, b; // y = ax + b
		mutable std::function<const Line*()> getSuc; // 次の直線へのポインタ (ソートで用いる)
 
		bool operator<(const Line& rhs) const {
			// 取得クエリでは次の直線との差分でソート
			if (rhs.b == IS_QUERY) {
				const Line* suc = getSuc();
				if (suc == nullptr) return false;
				const CHT_TYPE& x = rhs.a;
				return (suc->a - a)*x + suc->b - b > 0;
			}
			if (b == IS_QUERY) {
				const Line* suc = rhs.getSuc();
				if (suc == nullptr) return true;
				const CHT_TYPE& x = a;
				return (suc->a - rhs.a)*x + suc->b - rhs.b < 0;
			}
 
			// 通常の直線どうしは傾きソート
			return a < rhs.a;
		}
	};
 
	// 直線集合 **********************************************************
	class LinesSet : public std::multiset<Line> {
	private:
		// true -> 最小値クエリ, false -> 最大値クエリ
		bool flagMin;
 
	public:
		// コンストラクタ ( 第一引数falseで最大値クエリ,デフォルトで最小値クエリ )
		LinesSet(bool flagMin = true) : flagMin(flagMin) {};
 
		// 直線lが不必要であるかどうか
		inline bool isBad(iterator l) {
			const auto&& nel = std::next(l);
			if (l == begin()) { // lが傾き最小のとき
				if (nel == end()) return false; // lしかないなら必要
				return l->a == nel->a && l->b <= nel->b;
			}
			else {
				const auto&& prl = std::prev(l);
				if (nel == end()) return l->a == prl->a && l->b <= prl->b;
				return (prl->b - l->b) * (nel->a - l->a) >= (nel->b - l->b) * (prl->a - l->a);
			}
		}
 
		// 直線y=ax+bを追加する
		inline void add(CHT_TYPE a, CHT_TYPE b) {
			if (flagMin) a = -a, b = -b;
			auto&& it = insert({a, b});
			it->getSuc = [=] { return (std::next(it) == end() ? nullptr : &*std::next(it)); };
			if (isBad(it)) { erase(it); return; }
			while (std::next(it) != end() && isBad(std::next(it))) erase(std::next(it));
			while (it != begin() && isBad(std::prev(it))) erase(std::prev(it));
		}
 
		// 直線群の中でxの時に最小(最大)となる値を返す
		inline CHT_TYPE get(CHT_TYPE x) {
			auto&& l = *lower_bound(Line{x, IS_QUERY});
			if (flagMin) return -l.a * x - l.b;
			else return l.a * x + l.b;
		}
	};
 
	static const CHT_TYPE IS_QUERY = std::numeric_limits<CHT_TYPE>::lowest();
	LinesSet linesSet;
 
public:
	// コンストラクタ ( 第一引数falseで最大値クエリ,デフォルトで最小値クエリ )
	ConvexHullTrickDynamic(bool flagMin = true) : linesSet(flagMin) {}
	// 直線y=ax+bを追加する
	inline void add(CHT_TYPE a, CHT_TYPE b) { linesSet.add(a, b); }
	// あるxのときの直線集合での最小値を求める
	inline CHT_TYPE get(CHT_TYPE x) { return linesSet.get(x); }
};
int main()
{
    int n; cin>>n;
    ll c; cin>>c;
    ll a[100010], b[100010];
    for(int i=0; i<n; i++){
        cin>>a[i]>>b[i];
    }
    ConvexHullTrickDynamic cht1, cht2;
    cht1.add(0, 0);
    lll dp1, dp2;
    for(int i=0; i<n; i++){
        dp2=cht1.get(-2*a[i]-(2*i+1)*c)+c*i*(i+1)+2*a[i]*(i+1)+2*b[i];
        dp2/=2;
        cht2.add(-2*(i+1)*c+2*a[i], -(i+1)*c-2*(i+1)*a[i]+2*dp2+c*(i+1)*(i+1));
        dp1=cht2.get(i+1)+c*(i+1)*(i+2);
        dp1/=2;
        cht1.add(i+1, c*(i+1)*(i+1)+2*dp1);
    }
    cout<<(ll)dp1<<endl;
    return 0;
}
0