結果

問題 No.913 木の燃やし方
ユーザー raven7959raven7959
提出日時 2023-03-13 22:03:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 364 ms / 3,000 ms
コード長 15,246 bytes
コンパイル時間 5,187 ms
コンパイル使用メモリ 218,940 KB
実行使用メモリ 39,000 KB
最終ジャッジ日時 2023-10-18 11:22:47
合計ジャッジ時間 19,098 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,324 KB
testcase_01 AC 2 ms
4,324 KB
testcase_02 AC 2 ms
4,324 KB
testcase_03 AC 4 ms
4,324 KB
testcase_04 AC 5 ms
4,324 KB
testcase_05 AC 5 ms
4,324 KB
testcase_06 AC 4 ms
4,324 KB
testcase_07 AC 3 ms
4,324 KB
testcase_08 AC 339 ms
35,040 KB
testcase_09 AC 324 ms
33,984 KB
testcase_10 AC 323 ms
33,984 KB
testcase_11 AC 330 ms
34,512 KB
testcase_12 AC 349 ms
35,832 KB
testcase_13 AC 342 ms
35,040 KB
testcase_14 AC 327 ms
33,984 KB
testcase_15 AC 326 ms
33,984 KB
testcase_16 AC 327 ms
35,040 KB
testcase_17 AC 315 ms
33,984 KB
testcase_18 AC 317 ms
33,984 KB
testcase_19 AC 339 ms
35,832 KB
testcase_20 AC 336 ms
35,832 KB
testcase_21 AC 349 ms
36,624 KB
testcase_22 AC 350 ms
37,680 KB
testcase_23 AC 346 ms
39,000 KB
testcase_24 AC 327 ms
35,832 KB
testcase_25 AC 324 ms
35,832 KB
testcase_26 AC 349 ms
35,832 KB
testcase_27 AC 352 ms
35,832 KB
testcase_28 AC 364 ms
36,624 KB
testcase_29 AC 353 ms
36,624 KB
testcase_30 AC 354 ms
36,624 KB
testcase_31 AC 361 ms
37,944 KB
testcase_32 AC 353 ms
36,096 KB
testcase_33 AC 351 ms
35,832 KB
testcase_34 AC 339 ms
35,832 KB
testcase_35 AC 336 ms
35,832 KB
testcase_36 AC 330 ms
35,832 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,abm,mmx,avx,avx2")
#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define rrep(i, n) for (ll i = (int)(n) - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ll(x.size())
#define yn(joken) cout<<((joken) ? "Yes" : "No")<<"\n"
#define YN(joken) cout<<((joken) ? "YES" : "NO")<<"\n"
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int,int>>;
using vpl = vector<pair<ll,ll>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vld = vector<long double>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvs = vector<vector<string>>;
using vvc = vector<vector<char>>;
using vvd = vector<vector<double>>;
using vvld = vector<vector<long double>>;
using vvvi = vector<vector<vector<int>>>;
using vvvl = vector<vector<vector<ll>>>;
using vvvvi = vector<vector<vector<vector<int>>>>;
using vvvvl = vector<vector<vector<vector<ll>>>>;
template <class T> using priq = priority_queue<T>;
template <class T> using priqg = priority_queue<T, vector<T>, greater<T>>;
const int INF = 1e9;
const ll LINF = 2e18;
template <class T, class S> inline bool chmax(T &a, const S &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class S> inline bool chmin(T &a, const S &b) { return (a > b ? a = b, 1 : 0); }
template <typename T>
vi iota(int n) {
    vi a(n);
    return iota(a.begin(), a.end(), 0), a;
}
template <typename T> vi iota(const vector<T> &a, bool greater = false) {
    vi ret(a.size());
    iota(ret.begin(), ret.end(), 0);
    sort(ret.begin(), ret.end(), [&](int i, int j) {
        if(greater) return a[i] > a[j];
        return a[i] < a[j];
    });
    return ret;
}
template <typename S> void rearrange(const vector<S> &id) {}
template <typename S, typename T> void rearrange_exec(const vector<S> &id, vector<T> &v) {
    vector<T> w(v.size());
    rep(i, sz(id)) w[i] = v[id[i]];
    v.swap(w);
}
// 並び替える順番, 並び替えるvector
template <typename S, typename Head, typename... Tail> void rearrange(const vector<S> &id, Head &a, Tail &...tail) {
    rearrange_exec(id, a);
    rearrange(id, tail...);
}
template <typename T> vector<T> RUI(const vector<T> &v) {
    vector<T> res(v.size() + 1);
    for(int i = 0; i < v.size(); i++) res[i + 1] = res[i] + v[i];
    return res;
}
// 反時計周りに 90 度回転
template <typename T> void roth(vector<vector<T>> &v) {
    if(empty(v)) return;
    int n = v.size(), m = v[0].size();
    vector<vector<T>> res(m, vector<T>(n));
    rep(i, n) rep(j, m) res[m - 1 - j][i] = v[i][j];
    v.swap(res);
}
// 時計周りに 90 度回転
template <typename T> void rott(vector<vector<T>> &v) {
    if(empty(v)) return;
    int n = v.size(), m = v[0].size();
    vector<vector<T>> res(m, vector<T>(n));
    rep(i, n) rep(j, m) res[j][n - 1 - i] = v[i][j];
    v.swap(res);
}
bool ispow2(int i) { return i && (i & -i) == i; }
bool ispow2(ll i) { return i && (i & -i) == i; }
template <typename T, typename S> T ceil(T x, S y) { // x/y以上の最小の整数を返す
    assert(y);
    return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y));
}
 
template <typename T, typename S> T floor(T x, S y) { // x/y以下の最大の整数を返す
    assert(y);
    return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1)));
}
template <class S> vector<pair<S, int>> RunLength(const vector<S> &v) {
    vector<pair<S, int>> res;
    for(auto &e : v) {
        if(res.empty() || res.back().first != e)
            res.emplace_back(e, 1);
        else
            res.back().second++;
    }
    return res;
}
vector<pair<char, int>> RunLength(const string &v) {
    vector<pair<char, int>> res;
    for(auto &e : v) {
        if(res.empty() || res.back().first != e)
            res.emplace_back(e, 1);
        else
            res.back().second++;
    }
    return res;
}
template <class T, class F> T bin_search(T ok, T ng, const F &f) {
    while(abs(ok - ng) > 1) {
        T mid = ok + ng >> 1;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <class T, class F> T bin_search_double(T ok, T ng, const F &f, int iter = 80) {
    while(iter--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
namespace aux {
template <typename T, unsigned N, unsigned L> struct tp {
    static void output(std::ostream &os, const T &v) {
        os << std::get<N>(v) << (&os == &cerr ? ", " : " ");
        tp<T, N + 1, L>::output(os, v);
    }
};
template <typename T, unsigned N> struct tp<T, N, N> {
    static void output(std::ostream &os, const T &v) { os << std::get<N>(v); }
};
} // namespace aux
template <typename... Ts> std::ostream &operator<<(std::ostream &os, const std::tuple<Ts...> &t) {
    if(&os == &cerr) { os << '('; }
    aux::tp<std::tuple<Ts...>, 0, sizeof...(Ts) - 1>::output(os, t);
    if(&os == &cerr) { os << ')'; }
    return os;
}
template <typename T> std::ostream &operator<<(std::ostream &os, const stack<T> &_st) {
    auto st = _st;
    vector<T> res;
    while(!empty(st)) res.emplace_back(st.top()), st.pop();
    reverse(all(res));
    return os << res;
}
template <typename T> std::ostream &operator<<(std::ostream &os, const queue<T> &_qu) {
    auto qu = _qu;
    vector<T> res;
    while(!empty(qu)) res.emplace_back(qu.front()), qu.pop();
    return os << res;
}
template <typename T> std::ostream &operator<<(std::ostream &os, const deque<T> &_dq) {
    auto dq = _dq;
    vector<T> res;
    while(!empty(dq)) res.emplace_back(dq.front()), dq.pop_front();
    return os << res;
}
template <typename T, typename S, typename U> std::ostream &operator<<(std::ostream &os, const priority_queue<T, S, U> &_pq) {
    auto pq = _pq;
    vector<T> res;
    while(!empty(pq)) res.emplace_back(pq.top()), pq.pop();
    return os << res;
}
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &p) {
    if(&os == &cerr) { return os << "(" << p.first << ", " << p.second << ")"; }
    return os << p.first << " " << p.second;
}
template <class Ch, class Tr, class Container> std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, const Container &x) {
    bool f = true;
    if(&os == &cerr) os << "[";
    for(auto &y : x) {
        if(&os == &cerr)
            os << (f ? "" : ", ") << y;
        else
            os << (f ? "" : " ") << y;
        f = false;
    }
    if(&os == &cerr) os << "]";
    return os;
}

static uint32_t RandXor(){
    static uint32_t x=123456789;
    static uint32_t y=362436069;
    static uint32_t z=521288629;
    static uint32_t w=88675123;
    uint32_t t;
 
    t=x^(x<<11);
    x=y; y=z; z=w;
    return w=(w^(w>>19))^(t^(t>>8));
}

static double Rand01(){
    return (RandXor()+0.5)*(1.0/UINT_MAX);
}

template <typename T>
void rshuffle(vector<T> &V){
    random_device seed_gen;
    mt19937 engine(seed_gen());
    shuffle(V.begin(),V.end(),engine);
}

// auto seg=get_dual_segment_tree(N,h,OM0,commutative); のように宣言する
// N: 元配列の要素数
// h: 作用fと要素aに対して, f*f->f および f*a->a のはず
// OM0: 単位元
// commutative: 作用が可換かどうか, デフォルトはtrue
// update(l,r,x): 半開区間[l,r)にxを作用させる
// seg[k]でk番目の要素を取得できる

template <typename OperatorMonoid, typename H>
struct DualSegmentTree{

    DualSegmentTree(int n, const H h, const OperatorMonoid &OM0, bool commutative) : h(h), OM0(OM0), commutative(commutative){
        sz = 1;
        height = 0;
        while (sz < n) sz <<= 1, height++;
        lazy.assign(2 * sz, OM0);
    }

    void update(int a, int b, const OperatorMonoid &x){
        a+=sz;
        b+=sz-1;
        if(!commutative) thrust(a);
        if(!commutative) thrust(b);
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1){
            if (l & 1) lazy[l] = h(lazy[l], x), ++l;
            if (r & 1) --r, lazy[r] = h(lazy[r], x);
        }
    }

    vector<OperatorMonoid> get_all(){
        for(int i=1;i<sz;i++) propagate(i);
        return vector<OperatorMonoid>(lazy.begin()+sz,lazy.end());
    }

    OperatorMonoid operator[](int k){
        if(commutative){
            OperatorMonoid ret=OM0;
            k+=sz;
            while(k){
                ret=h(ret,lazy[k]);
                k>>=1;
            }
            return ret;
        }
        else{
            thrust(k += sz);
            return lazy[k];
        }
    }

private:
    int sz, height;
    vector<OperatorMonoid> lazy;
    const H h;
    const OperatorMonoid OM0;
    bool commutative;

    inline void propagate(int k){
        if (lazy[k] != OM0){
            lazy[2 * k] = h(lazy[2 * k], lazy[k]);
            lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
            lazy[k] = OM0;
        }
    }

    inline void thrust(int k){
        for (int i = height; i > 0; i--) propagate(k >> i);
    }
};

template <typename OperatorMonoid, typename H>
DualSegmentTree<OperatorMonoid, H> get_dual_segment_tree(int N, const H &h, const OperatorMonoid &OM0,bool commutative=true){
    return {N, h, OM0, commutative};
}

// DynamicLiChaoTree<ll> LCT(ll min_index,ll max_index); などする
// xの動きうる最大範囲が[min_index,max_index)
// void add_line(T a,T b): 直線 ax+b を追加
// void add_segment_line(T Llim,T Rlim,T a,T b): [Llim,Rlim]の線分 ax+b を追加
// T get(T x): min(ax+b) を返す

template <typename T>
struct DynamicLiChaoTree{
private:
    struct node {
        node *left, *right;
        static const T inf=numeric_limits<T>::max();
        T a,b,l,r;
        node():node(0,inf){}
        node(const T _a,const T _b,const T _l,const T _r):
            left(nullptr),right(nullptr),a(_a),b(_b),l(_l),r(_r) {}
        T f(const T x){return (l<=x && x<r) ? a*x+b : inf;}
        T g(const T x){return a*x+b;}
        bool same(const T _l,const T _r){return l==_l && r==_r;}
    };

    const T min_index,max_index;
    node* root;

    static void swap(node *x, node *y){
        std::swap(x->a,y->a);
        std::swap(x->b,y->b);
        std::swap(x->l,y->l);
        std::swap(x->r,y->r);
    }

    void _add_segment(node* cur,node* nw,T l,T r){
        while(true){
            const T m=(l+r)>>1;
            // nwがnwの全域でcurに負けてる
            if(cur->f(nw->l)<=nw->g(nw->l) && cur->f(nw->r-1)<=nw->g(nw->r-1)) break;
            // curがcurの全域でnwに負けてる
            if(cur->g(cur->l)>=nw->f(cur->l) && cur->g(cur->r-1)>=nw->f(cur->r-1)){
                swap(cur,nw);
                break;
            }
            if(cur->same(l,r) && nw->same(l,r)){ // curもnwも[l,r)を覆ってる
                if(nw->g(l)<cur->g(l)) swap(cur,nw);
                if(cur->g(m)<=nw->g(m)){
                    if(!cur->right){
                        cur->right=new node(nw->a,nw->b,m,r);
                        break;
                    }
                    else{
                        cur=cur->right;
                        l=m;
                        nw->l=m;
                    }
                }
                else{
                    swap(cur,nw);
                    if(!cur->left){
                        cur->left=new node(nw->a,nw->b,l,m);
                        break;
                    }
                    else{
                        cur=cur->left;
                        r=m;
                        nw->r=m;
                    }
                }
                continue;
            }
            // curが[l,r)を覆ってる状態にする
            if(nw->same(l,r)) swap(cur,nw);
            if(nw->r<=m){
                if(!cur->left){
                    cur->left=new node(*nw);
                    break;
                }
                else{
                    cur=cur->left;
                    r=m;
                }
            }
            else if(m<=nw->l){
                if(!cur->right){
                    cur->right=new node(*nw);
                    break;
                }
                else{
                    cur=cur->right;
                    l=m;
                }
            }
            else{
                node _nw(nw->a,nw->b,m,nw->r);
                nw->r=m;
                if(!cur->left){
                    cur->left=new node(*nw);
                }
                else{
                    _add_segment(cur->left,nw,l,m);
                }
                if(!cur->right){
                    cur->right=new node(_nw);
                }
                else{
                    _add_segment(cur->right,&_nw,m,r);
                }
                break;
            }
        }
    }

    T _get(node *cur,const T x,T l,T r) const {
        T ret=numeric_limits<T>::max();
        while(cur){
            ret=min(ret,cur->f(x));
            const T m=(l+r)>>1;
            if(x<m){
                cur=cur->left;
                r=m;
            }
            else{
                cur=cur->right;
                l=m;
            }
        }
        return ret;
    }

public:
    DynamicLiChaoTree(const T min_index,const T max_index):
        min_index(min_index),
        max_index(max_index),
        root(new node(0,numeric_limits<T>::max(),min_index,max_index)){}

    void add_line(const T a, const T b){
        node nw(a,b,min_index,max_index);
        _add_segment(root,&nw,min_index,max_index);
    }

    void add_segment(const T a,const T b,const T l,const T r){
        node nw(a,b,l,r);
        _add_segment(root,&nw,min_index,max_index);
    }

    T get(T x){
        return _get(root,x,min_index,max_index);
    }
};

void solve(){
    ll N;
    cin>>N;
    vl A(N);
    cin>>A;
    auto h=[&](ll l,ll r){
        return min(l,r);
    };
    auto seg=get_dual_segment_tree(N,h,LINF);
    auto calc=[&](auto &&self,ll l,ll r)->void{
        if(r-l<=0) return;
        if(r-l==1){
            seg.update(l,r,1+A[l]);
            return;
        }
        ll m=(l+r)/2;
        self(self,l,m);
        self(self,m,r);
        DynamicLiChaoTree<ll> DLC1(1,m-l+1);
        ll smy=0;
        for(ll i=m;i<r;i++){
            smy+=A[i];
            DLC1.add_line(2*(i-m+1),smy+(i-m+1)*(i-m+1));
        }
        ll smx=0;
        for(ll i=m-1;i>=l;i--){
            smx+=A[i];
            seg.update(i,m,DLC1.get(m-i)+(m-i)*(m-i)+smx);
        }
        DynamicLiChaoTree<ll> DLC2(1,r-m+1);
        smx=0;
        for(ll i=m-1;i>=l;i--){
            smx+=A[i];
            DLC2.add_line(2*(m-i),smx+(m-i)*(m-i));
        }
        smy=0;
        for(ll i=m;i<r;i++){
            smy+=A[i];
            seg.update(m,i+1,DLC2.get(i-m+1)+(i-m+1)*(i-m+1)+smy);
        }
    };
    calc(calc,0,N);
    auto ANS=seg.get_all();
    rep(i,N) cout<<ANS[i]<<"\n";
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    solve();
}
0