結果
| 問題 |
No.3185 Three Abs
|
| コンテスト | |
| ユーザー |
Today03
|
| 提出日時 | 2025-06-20 22:17:59 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 7,540 bytes |
| コンパイル時間 | 3,442 ms |
| コンパイル使用メモリ | 292,392 KB |
| 実行使用メモリ | 36,200 KB |
| 最終ジャッジ日時 | 2025-06-20 22:18:16 |
| 合計ジャッジ時間 | 16,670 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | WA * 30 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(),(x).end()
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)
#define PER(i, n) for(ll i=(ll)(n)-1; i>=0; i--)
template<typename T> int LB(const vector<T>& v, T x) { return lower_bound(ALL(v),x)-v.begin(); }
template<typename T> int UQ(T& v) { sort(ALL(v)); v.erase(unique(ALL(v)),v.end()); return v.size(); }
template<typename T> bool chmax(T &a, T b) { return a<b ? a=b, true : false; }
template<typename T> bool chmin(T &a, T b) { return a>b ? a=b, true : false; }
template<typename T> using rpriority_queue = priority_queue<T,vector<T>,greater<T>>;
using ll=long long; const int INF=1e9+10; const ll INFL=4e18;
using ld=long double; using lll=__int128_t; using ull=unsigned long long;
using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;
using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;
#ifdef LOCAL
#include "./debug.hpp"
#else
#define debug(...)
#define print_line
#endif
/// @brief セグメント木
template<typename Monoid>
struct SegTree {
using Type=typename Monoid::Type;
SegTree()=default;
/// @brief 要素数 n のセグ木を構築する
SegTree(int n) {
this->n=n;
dat.assign(n<<1,Monoid::id());
cand.reserve(100),cand_l.reserve(100),cand_r.reserve(100);
}
/// @brief 配列 v からセグ木を構築する
/// @note O(N)
SegTree(const vector<Type>& v) {
this->n=v.size();
dat.assign(n<<1,Monoid::id());
for(int i=0; i<n; i++) dat[i+n]=v[i];
for(int i=n-1; i>0; i--) dat[i]=Monoid::op(dat[i<<1],dat[i<<1|1]);
cand.reserve(100),cand_l.reserve(100),cand_r.reserve(100);
}
/// @brief i 番目の要素を x に変更する
/// @note O(log(N))
void set(int i, Type x) {
i+=n;
dat[i]=x;
while(i>>=1) dat[i]=Monoid::op(dat[i<<1],dat[i<<1|1]);
}
/// @brief 区間 [l, r) のモノイド積を返す
/// @note O(log(N))
Type fold(int l, int r) {
Type retl=Monoid::id(),retr=Monoid::id();
l+=n,r+=n;
while(l<r) {
if(l&1) retl=Monoid::op(retl,dat[l++]);
if(r&1) retr=Monoid::op(dat[--r],retr);
l>>=1,r>>=1;
}
return Monoid::op(retl,retr);
}
/// @brief 区間 [l, x) のモノイド積が f を満たすような最大の x >= l を返す
/// @attention `f(Monoid::id())=true` が成り立つ必要がある
/// @note O(log(N))
template<typename F>
int find_right(int l, F f) {
assert(f(Monoid::id()));
if(l==n) return n;
l+=n;
int r=n+n;
cand_l.clear(),cand_r.clear();
while(l<r) {
if(l&1) cand_l.push_back(l++);
if(r&1) cand_r.push_back(--r);
l>>=1,r>>=1;
}
cand=cand_l;
reverse(cand_r.begin(),cand_r.end());
cand.insert(cand.end(),cand_r.begin(),cand_r.end());
Type val=Monoid::id();
for(int i:cand) {
if(f(Monoid::op(val,dat[i]))) {
val=Monoid::op(val,dat[i]);
} else {
while(i<n) {
i<<=1;
if(f(Monoid::op(val,dat[i]))) {
val=Monoid::op(val,dat[i]);
i|=1;
}
}
return i-n;
}
}
return n;
}
/// @brief 区間 [x, r) のモノイド積が f を満たすような最小の x <= r を返す
/// @attention `f(Monoid::id())=true` が成り立つ必要がある
/// @note O(log(N))
template<typename F>
int find_left(int r,F f) {
assert(f(Monoid::id()));
if(r==0) return 0;
r+=n;
int l=n;
cand_l.clear(),cand_r.clear();
while(l<r) {
if(l&1) cand_l.push_back(l++);
if(r&1) cand_r.push_back(--r);
l>>=1,r>>=1;
}
cand=cand_r;
reverse(cand_l.begin(),cand_l.end());
cand.insert(cand.end(),cand_l.begin(),cand_l.end());
Type val=Monoid::id();
for(int i:cand) {
if(f(Monoid::op(dat[i],val))) {
val=Monoid::op(dat[i],val);
} else {
while(i<n) {
i=(i<<1)|1;
if(f(Monoid::op(dat[i],val))) {
val=Monoid::op(dat[i],val);
i^=1;
}
}
return i-n+1;
}
}
return 0;
}
/// @brief i 番目の要素を返す
/// @note O(1)
Type operator[](int i) { return dat[i+n]; }
/// @brief 配列のサイズを返す
int size() { return n; }
private:
int n;
vector<Type> dat;
vector<int> cand,cand_l,cand_r;
};
/// @brief モノイド
namespace Monoid {
/// @brief Minモノイド
/// @tparam max_value 単位元
template<typename T, T max_value=INF>
struct Min {
using Type=T;
static Type id() { return max_value; }
static Type op(const Type& a, const Type& b) { return min(a,b); }
};
/// @brief Maxモノイド
/// @tparam min_value 単位元
template<typename T, T min_value=-INF>
struct Max {
using Type=T;
static Type id() { return min_value; }
static Type op(const Type& a, const Type& b) { return max(a,b); }
};
/// @brief 和
template<typename T>
struct Sum {
using Type=T;
static Type id() { return 0; }
static Type op(const Type& a, const Type& b) { return a+b; }
};
/// @brief (和,区間の長さ)
template<typename T>
struct SumPair {
using Type=pair<T,int>;
static Type id() { return make_pair(T(0),0); }
static Type op(const Type& a, const Type& b) { return {a.first+b.first,a.second+b.second}; }
};
}
/// @brief 区間クエリ
namespace RangeQuery {
/// @brief 1点変更 / 区間 min
template<typename T, T max_value=INF>
struct Min { using Type=struct SegTree<Monoid::Min<T,max_value>>; };
/// @brief 1点変更 / 区間 max
template<typename T, T min_value=-INF>
struct Max { using Type=struct SegTree<Monoid::Max<T,min_value>>; };
/// @brief 1点変更 / 区間和
template<typename T>
struct Sum { using Type=struct SegTree<Monoid::Sum<T>>; };
}
//----------------------------------------------------------
void solve() {
ll N; cin>>N;
VL A(N); REP(i,N) cin>>A[i];
VL P(N+1);
REP(i,N) P[i+1]=P[i]+A[i];
auto Q=P; UQ(Q);
debug(P,Q);
using segt=RangeQuery::Max<ll,-100>::Type;
vector<segt> small(4,segt(N+1));
vector<segt> large(4,segt(N+1));
small[0].set(LB(Q,0ll),0); large[0].set(LB(Q,0ll),0);
for(ll i=1; i<=N; i++) {
ll idx=LB(Q,P[i]);
for(int t=1; t<=3; t++) {
ll res=-INFL;
chmax(res,small[t-1].fold(0,idx+1)+P[i]);
chmax(res,large[t-1].fold(idx,N+1)-P[i]);
debug(i,t,res);
if(i==N && t==3) {
cout<<res<<'\n';
return;
}
small[t].set(idx,max(small[t][idx],res-P[i]));
large[t].set(idx,max(large[t][idx],res+P[i]));
}
{
small[0].set(idx,max(-P[i],small[0][idx]));
large[0].set(idx,max(P[i],large[0][idx]));
}
debug(i);
debug(small);
debug(large);
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
//cout<<fixed<<setprecision(15);
int T=1;
cin>>T;
while(T--) solve();
}
Today03