結果

問題 No.913 木の燃やし方
ユーザー akusyounin2412
提出日時 2020-02-23 17:59:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 710 ms / 3,000 ms
コード長 4,719 bytes
コンパイル時間 1,383 ms
コンパイル使用メモリ 133,336 KB
実行使用メモリ 18,864 KB
最終ジャッジ日時 2024-10-10 06:45:31
合計ジャッジ時間 21,257 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

# include <iostream>
# include <algorithm>
#include <array>
# include <cassert>
#include <cctype>
#include <climits>
#include <numeric>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <tuple>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <bitset>
# include <complex>
# include <chrono>
# include <random>
# include <limits.h>
# include <unordered_map>
# include <unordered_set>
# include <deque>
# include <cstdio>
# include <cstring>
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#include <cstdint>
#include <cfenv>
#include<fstream>
//#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
long long MOD = 1000000000 + 7;// 998244353;// ;
constexpr long long INF = numeric_limits<LL>::max() / 2;
const double PI = acos(-1);
#define fir first
#define sec second
#define thi third
#define debug(x) cerr<<#x<<": "<<x<<'\n'
typedef pair<LL, LL> Pll;
typedef pair<LL, pair<LL, LL>> Ppll;
typedef pair<LL, pair<LL, bitset<100001>>> Pbll;
typedef pair<LL, pair<LL, vector<LL>>> Pvll;
typedef pair<LL, LL> Vec2;
struct Tll { LL first, second, third; };
struct Fll { LL first, second, third, fourd; };
typedef pair<LL, Tll> Ptll;
#define rep(i,rept) for(LL i=0;i<rept;i++)
#define Rrep(i,mf) for(LL i=mf-1;i>=0;i--)
void YN(bool f) {
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void yn(bool f) {
if (f)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
struct Edge { LL to, cost; };
struct edge { LL from, to, cost; };
vector<vector<LL>>g, rev;
vector<edge>edges;
vector<LL>v;
map<LL, LL>ma;
set<LL>st;
LL h, w, n, m, k, t, s, p, q, last, cnt, sum[210000], ans[210000], a[510000], b[510000], dp[210000];
string str, ss;
bool f;
char c;
template<typename T>
class ConvexHullTrick {
private:
// ()
std::vector<std::pair<T, T>> lines;
// ()x調
bool isMonotonicX;
// /
std::function<bool(T l, T r)> comp;
public:
// ( 調flag = true )
ConvexHullTrick(bool flagX = false, std::function<bool(T l, T r)> compFunc = [](T l, T r) {return l >= r; })
:isMonotonicX(flagX), comp(compFunc) {
//lines.emplace_back(0, 0);//
};
void clear() {
lines.clear();
}
// l1, l2, l3l2
bool check(std::pair<T, T> l1, std::pair<T, T> l2, std::pair<T, T> l3) {
if (l1 < l3) std::swap(l1, l3);
return (l3.second - l2.second) * (l2.first - l1.first) >= (l2.second - l1.second) * (l3.first - l2.first);
//return comp((l3.second - l2.second) * (l2.first - l1.first), (l2.second - l1.second) * (l3.first - l2.first));
}
// y=ax+b
void add(T a, T b) {
std::pair<T, T> line(a, b);
while (lines.size() >= 2 && check(*(lines.end() - 2), lines.back(), line))
lines.pop_back();
lines.emplace_back(line);
}
// if_i(x)x
T f(int i, T x) {
return lines[i].first * x + lines[i].second;
}
// if_i(x)x
T f(std::pair<T, T> line, T x) {
return line.first * x + line.second;
}
// x()
T get(T x) {
// ()x調
if (isMonotonicX) {
static int head = 0;
while (lines.size() - head >= 2 && comp(f(head, x), f(head + 1, x)))
++head;
return f(head, x);
}
else {
int low = -1, high = lines.size() - 1;
while (high - low > 1) {
int mid = (high + low) / 2;
(comp(f(mid, x), f(mid + 1, x)) ? low : high) = mid;
}
return f(high, x);
}
}
};
//[L,R)
LL chmin[210000];
void dfs(LL L,LL R) {
if (R-L<=2) {
return;
}
ConvexHullTrick<LL> cht;
LL mid = (L + R) / 2;
for (LL i =R; i > mid; i--) {
cht.add(-2 * i, i * i + sum[i]);
}
chmin[L] = INF;
for (LL i = L; i <= mid;i++) {
chmin[i] = min(chmin[i], cht.get(i) + i * i - sum[i]);
chmin[i + 1] = chmin[i];
ans[i] = min(ans[i], chmin[i]);
}
cht.clear();
for (LL i = L; i <=mid; i++) {
cht.add(-2 * i, i * i - sum[i]);
}
chmin[R - 1] = INF;
for (LL i = R; i > mid; i--) {
chmin[i - 1] = min(cht.get(i) + i * i + sum[i], chmin[i - 1]);
chmin[i - 2] = chmin[i - 1];
ans[i - 1] = min(ans[i - 1], chmin[i - 1]);
}
dfs(L, (L + R) / 2);
dfs((L + R) / 2, R);
}
int main() {
cin >> n;
rep(i, n) {
cin >> a[i];
sum[i + 1] = sum[i] + a[i];
ans[i] = a[i] + 1;
}
dfs(0,n);
rep(i, n)
cout << ans[i] << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0