結果
| 問題 | No.2036 Max Middle |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-11-06 19:47:45 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 27 ms / 2,000 ms |
| + 679µs | |
| コード長 | 4,926 bytes |
| 記録 | |
| コンパイル時間 | 3,337 ms |
| コンパイル使用メモリ | 363,328 KB |
| 実行使用メモリ | 14,276 KB |
| 最終ジャッジ日時 | 2026-11-06 19:48:51 |
| 合計ジャッジ時間 | 5,904 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 17 |
ソースコード
/*
#include<kyopro/master>
void self::main(){
LL(N);
VL(N,a);
vec<ll>s(N-1);
fo(i,N-1){
if(a[i]>a[i+1])s[i]=0;
if(a[i]<a[i+1])s[i]=1;
if(a[i]==a[i+1])s[i]=2;
}
s.emplace_front(2);
s.eb(2);
auto v=s.pos_groups(3)[2];
ll ans=0;
fo(i,v.size()-1){
ans+=s.slice(v[i]+1,v[i+1]).inversion();
}
pp(ans);
}
*/
#include<bits/stdc++.h>
using namespace std;
namespace self{void main();void sub();void pp(auto&&...a);void ppe(auto&&...a);}
int main(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);self::main();}
#define eb emplace_back
#define RD(T,...) T __VA_ARGS__;lin(__VA_ARGS__)
#define LL(...) RD(ll,__VA_ARGS__)
#define RDVL(T,n,...) vec<T>__VA_ARGS__;fe(refs(__VA_ARGS__),e)e.get().resizes(n);lin(__VA_ARGS__)
#define VL(n,...) RDVL(ll,n,__VA_ARGS__)
#define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a)
#define ef(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):ranges::reverse_view(a))
namespace self{
using ll=long long;
constexpr auto refs(auto&...a){return array{ref(a)...};}
template<class T>constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
void lin(auto&...a){(cin>>...>>a);}
template<class A,class B=A>struct pair{
A a;B b;
pair()=default;
pair(A a,B b):a(a),b(b){}
auto operator<=>(const pair&)const=default;
};
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
}
namespace self{
template<class T>class FenwickTree{
public:
ll n_;
vector<T>data_;
FenwickTree(ll n):n_(n),data_(n){}
void add(ll i,T x){
assert(0<=i&&i<n_);
i++;
while(i<=n_){
data_[i-1]+=x;
i+=i&-i;
}
}
T sum(ll l,ll r)const{
assert(0<=l&&l<=r&&r<=n_);
T res=0;
while(r>0)res+=data_[r-1],r-=r&-r;
while(l>0)res-=data_[l-1],l-=l&-l;
return res;
}
};
}
namespace self{
template<class V>concept vectorial=is_base_of_v<vector<typename remove_cvref_t<V>::value_type>,remove_cvref_t<V>>;
template<class V>istream&operator>>(istream&i,vector<V>&v){fe(v,e)i>>e;return i;}
template<class V>constexpr int depth=0;
template<vectorial V>constexpr int depth<V> =depth<typename V::value_type>+1;
template<class T>struct core_t_helper{using type=T;};
template<vectorial V>struct core_t_helper<V>{using type=typename core_t_helper<typename V::value_type>::type;};
template<class T>using core_t=core_t_helper<T>::type;
template<class V>struct vec;
template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;};
template<class T>struct hvec_helper<0,T>{using type=T;};
template<int D,class T>using hvec=hvec_helper<D,T>::type;
template<class V>struct vec:vector<V>{
static constexpr int D=depth<V>+1;
using C=core_t<V>;
using vector<V>::vector;
vec(const auto&...a)requires(sizeof...(a)>=3){resizes(a...);}
void resizes(const auto&...a){if constexpr(sizeof...(a)==D)*this=make(a...,C{});else*this=make(a...);}
static auto make(ll n,const auto&...a){
if constexpr(sizeof...(a)==1)return vec<C>(n,array{a...}[0]);
else return vec<decltype(make(a...))>(n,make(a...));
}
ll size()const{return vector<V>::size();}
auto&emplace_front(auto&&...a){this->emplace(this->begin(),std::forward<decltype(a)>(a)...);return*this;}
auto&emplace_back(auto&&...a){vector<V>::emplace_back(std::forward<decltype(a)>(a)...);return*this;}
vec slice(ll l,ll r)const{return l<r?vec(this->begin()+l,this->begin()+r):vec{};}
template<class T=C>auto fold(auto&&f)const{
pair<T,bool>res{};
fe(*this,e){
if constexpr(!vectorial<V>){
if(res.b)f(res.a,e);
else res={e,1};
}else { }
}
return res;
}
auto min()const{return fold([](auto&a,auto b){if(b<a)a=b;}).a;}
template<class F=less<>>vec stable_sort(F f={})const{vec v=*this;ranges::stable_sort(v,f);return v;}
template<class F=less<>>auto sort_from(F f={})const{
vec<ll>o(size());
std::iota(o.begin(),o.end(),0);
return o.stable_sort([&](ll i,ll j){return f((*this)[i],(*this)[j]);});
}
auto pos_groups(C M)const{
assert(min()>=0);
vec<vec<ll>>res(M,0,0);
fo(i,size())res[(*this)[i]].eb(i);
return res;
}
ll inversion()const{
ll res=0;
FenwickTree<ll>s(size());
ef(sort_from(),i){
res+=s.sum(0,i);
s.add(i,1);
}
return res;
}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
}
void self::pp(auto&&...a){const char*c="";((cout<<c<<a,c=" "),...);cout<<'\n';}
void self::main(){
LL(N);
VL(N,a);
vec<ll>s(N-1);
fo(i,N-1){
if(a[i]>a[i+1])s[i]=0;
if(a[i]<a[i+1])s[i]=1;
if(a[i]==a[i+1])s[i]=2;
}
s.emplace_front(2);
s.eb(2);
auto v=s.pos_groups(3)[2];
ll ans=0;
fo(i,v.size()-1){
ans+=s.slice(v[i]+1,v[i+1]).inversion();
}
pp(ans);
}