結果
| 問題 |
No.913 木の燃やし方
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2019-10-18 22:55:10 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,089 bytes |
| コンパイル時間 | 2,926 ms |
| コンパイル使用メモリ | 204,460 KB |
| 最終ジャッジ日時 | 2025-01-07 23:31:10 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 WA * 6 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
namespace MonotoneMinima{
template<typename T,typename F>
void induce(Int l,Int r,Int a,Int b,vector<Int> &dp,F dist){
if(l==r) return;
Int m=(l+r)>>1;
Int &idx=(dp[m]=a);
T res=dist(idx,m);
for(Int i=a;i<b;i++){
T tmp=dist(i,m);
if(tmp<res) res=tmp,idx=i;
}
induce<T>(l,m,a,idx+1,dp,dist);
induce<T>(m+1,r,idx,b,dp,dist);
}
template<typename T,typename F>
vector<Int> args(Int n,F dist){
vector<Int> dp(n,-1);
induce<T>(0,n,0,n,dp,dist);
return dp;
}
}
template <typename E>
struct SegmentTree{
using H = function<E(E,E)>;
Int n,height;
H h;
E ei;
vector<E> laz;
SegmentTree(H h,E ei):h(h),ei(ei){}
void init(Int n_){
n=1;height=0;
while(n<n_) n<<=1,height++;
laz.assign(2*n,ei);
}
inline void eval(Int k){
if(laz[k]==ei) return;
laz[(k<<1)|0]=h(laz[(k<<1)|0],laz[k]);
laz[(k<<1)|1]=h(laz[(k<<1)|1],laz[k]);
laz[k]=ei;
}
inline void thrust(Int k){
for(Int i=height;i;i--) eval(k>>i);
}
void update(Int a,Int b,E x){
thrust(a+=n);
thrust(b+=n-1);
for(Int l=a,r=b+1;l<r;l>>=1,r>>=1){
if(l&1) laz[l]=h(laz[l],x),l++;
if(r&1) --r,laz[r]=h(laz[r],x);
}
}
E get_val(Int a){
thrust(a+=n);
return laz[a];
}
void set_val(Int a,E x){
thrust(a+=n);
laz[a]=x;
}
};
struct FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
// AtCoder
const i64 CYCLES_PER_SEC = 2200000000;
struct Timer {
i64 start;
Timer(){reset();}
void reset(){start=getCycle();}
inline double get(){return (double)(getCycle()-start)/CYCLES_PER_SEC;}
inline i64 getCycle(){
u32 low,high;
__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
return ((i64)low)|((i64)high<<32);
}
};
Timer timer;
//INSERT ABOVE HERE
signed main(){
Int n;
cin>>n;
vector<Int> as(n);
for(Int i=0;i<n;i++) cin>>as[i];
const Int INF = 1e18;
auto g=[&](Int a,Int b){return min(a,b);};
SegmentTree<Int> seg(g,INF);
seg.init(n);
for(Int uku=0;uku<2;uku++){
vector<Int> sm(n+1,0);
for(Int i=0;i<n;i++) sm[i+1]=sm[i]+as[i];
auto dist=
[&](Int i,Int j)->Int{
if(i<j) return INF;
return (i-j+1)*(i-j+1)+(sm[i+1]-sm[j]);
};
auto bs=MonotoneMinima::args<Int>(n,dist);
auto latte=[&](Int k){return uku?n-k:k;};
for(Int i=0;i<n;i++){
Int l=min(latte(i),latte(bs[i]+1));
Int r=max(latte(i),latte(bs[i]+1));
seg.update(l,r,dist(bs[i],i));
}
reverse(as.begin(),as.end());
}
{
vector<Int> ps;
for(Int i=0;i<n;i++)
if(as[i]<0) ps.emplace_back(i);
if(ps.empty()) ps.emplace_back(0);
Int m=ps.size();
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<Int> ud(0,m-1);
vector<Int> sm(n+1,0);
for(Int i=0;i<n;i++) sm[i+1]=sm[i]+as[i];
auto dist=
[&](Int i,Int j)->Int{
if(i<j) return INF;
return (i-j+1)*(i-j+1)+(sm[i+1]-sm[j]);
};
if(m<10000){
for(Int i=0;i<m;i++)
for(Int j=0;j<i;j++)
seg.update(ps[j],ps[i]+1,dist(ps[i],ps[j]));
}else{
for(Int l=1;l<10;l++)
for(Int i=0;i+l<m;i++)
seg.update(ps[i],ps[i+l]+1,dist(ps[i+l],ps[i]));
for(Int l=1;l<=min((Int)ps.size(),(Int)1000);l++)
for(Int i=0;i<l;i++)
seg.update(ps[i],ps[ps.size()-l+i]+1,dist(ps[ps.size()-l+i],ps[i]));
if(0){
while(timer.get()<2.9){
for(Int uku=0;uku<100;uku++){
Int l=ud(mt),r=ud(mt);
if(l>r) swap(l,r);
seg.update(ps[l],ps[r]+1,dist(ps[r],ps[l]));
}
}
}
}
}
for(Int i=0;i<n;i++)
cout<<seg.get_val(i)<<"\n";
cout<<flush;
return 0;
}
beet