結果
| 問題 |
No.1786 Maximum Suffix Median (Online)
|
| コンテスト | |
| ユーザー |
hotman78
|
| 提出日時 | 2021-12-15 23:17:15 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 12,660 bytes |
| コンパイル時間 | 18,583 ms |
| コンパイル使用メモリ | 308,252 KB |
| 最終ジャッジ日時 | 2025-01-26 23:22:06 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 WA * 5 TLE * 19 |
ソースコード
#line 2 "cpplib/util/template.hpp"
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
struct __INIT__{__INIT__(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}__INIT__;
typedef long long lint;
#define INF (1LL<<60)
#define IINF (1<<30)
#define EPS (1e-10)
#define endl ('\n')
typedef vector<lint> vec;
typedef vector<vector<lint>> mat;
typedef vector<vector<vector<lint>>> mat3;
typedef vector<string> svec;
typedef vector<vector<string>> smat;
template<typename T>using V=vector<T>;
template<typename T>using VV=V<V<T>>;
template<typename T>inline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<<i;f=1;}cout<<endl;}
template<typename T>inline void output2(T t){for(auto i:t)output(i);}
template<typename T>inline void debug(T t){bool f=0;for(auto i:t){cerr<<(f?" ":"")<<i;f=1;}cerr<<endl;}
template<typename T>inline void debug2(T t){for(auto i:t)debug(i);}
#define loop(n) for(long long _=0;_<(long long)(n);++_)
#define _overload4(_1,_2,_3,_4,name,...) name
#define __rep(i,a) repi(i,0,a,1)
#define _rep(i,a,b) repi(i,a,b,1)
#define repi(i,a,b,c) for(long long i=(long long)(a);i<(long long)(b);i+=c)
#define rep(...) _overload4(__VA_ARGS__,repi,_rep,__rep)(__VA_ARGS__)
#define _overload3_rev(_1,_2,_3,name,...) name
#define _rep_rev(i,a) repi_rev(i,0,a)
#define repi_rev(i,a,b) for(long long i=(long long)(b)-1;i>=(long long)(a);--i)
#define rrep(...) _overload3_rev(__VA_ARGS__,repi_rev,_rep_rev)(__VA_ARGS__)
// #define rep(i,...) for(auto i:range(__VA_ARGS__))
// #define rrep(i,...) for(auto i:reversed(range(__VA_ARGS__)))
// #define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i)
// #define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i)
// #define irep(i) for(lint i=0;;++i)
// inline vector<long long> range(long long n){if(n<=0)return vector<long long>();vector<long long>v(n);iota(v.begin(),v.end(),0LL);return v;}
// inline vector<long long> range(long long a,long long b){if(b<=a)return vector<long long>();vector<long long>v(b-a);iota(v.begin(),v.end(),a);return v;}
// inline vector<long long> range(long long a,long long b,long long c){if((b-a+c-1)/c<=0)return vector<long long>();vector<long long>v((b-a+c-1)/c);for(int i=0;i<(int)v.size();++i)v[i]=i?v[i-1]+c:a;return v;}
// template<typename T>inline T reversed(T v){reverse(v.begin(),v.end());return v;}
#define all(n) begin(n),end(n)
template<typename T,typename E>bool chmin(T& s,const E& t){bool res=s>t;s=min<T>(s,t);return res;}
template<typename T,typename E>bool chmax(T& s,const E& t){bool res=s<t;s=max<T>(s,t);return res;}
const string ds="DRUL";
const vector<lint> dx={1,0,-1,0,1,1,-1,-1};
const vector<lint> dy={0,1,0,-1,1,-1,1,-1};
#define SUM(v) accumulate(all(v),0LL)
#if __cplusplus>=201703L
template<typename T,typename ...Args>auto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector<T>(arg,x);else return vector(arg,make_vector<T>(x,args...));}
#endif
#define extrep(v,...) for(auto v:__MAKE_MAT__({__VA_ARGS__}))
#define bit(n,a) ((n>>a)&1)
vector<vector<long long>> __MAKE_MAT__(vector<long long> v){if(v.empty())return vector<vector<long long>>(1,vector<long long>());long long n=v.back();v.pop_back();vector<vector<long long>> ret;vector<vector<long long>> tmp=__MAKE_MAT__(v);for(auto e:tmp)for(long long i=0;i<n;++i){ret.push_back(e);ret.back().push_back(i);}return ret;}
using graph=vector<vector<int>>;
template<typename T>using graph_w=vector<vector<pair<int,T>>>;
template<typename T,typename E>ostream& operator<<(ostream& out,pair<T,E>v){out<<"("<<v.first<<","<<v.second<<")";return out;}
#if __cplusplus>=201703L
constexpr inline long long powll(long long a,long long b){long long res=1;while(b--)res*=a;return res;}
#endif
template<typename T,typename E>pair<T,E>& operator+=(pair<T,E>&s,const pair<T,E>&t){s.first+=t.first;s.second+=t.second;return s;}
template<typename T,typename E>pair<T,E>& operator-=(pair<T,E>&s,const pair<T,E>&t){s.first-=t.first;s.second-=t.second;return s;}
template<typename T,typename E>pair<T,E> operator+(const pair<T,E>&s,const pair<T,E>&t){auto res=s;return res+=t;}
template<typename T,typename E>pair<T,E> operator-(const pair<T,E>&s,const pair<T,E>&t){auto res=s;return res-=t;}
// 128*1024*1024
#define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
#define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);
#line 4 "cpplib/BBST/splay_tree/splay_tree_base.hpp"
template<typename Node>
struct splay_tree_base{
using np=Node*;
np root=0;
inline int size(np t){return t?t->sz:0;}
int size(){return size(root);}
np rot(np t,bool b){
if(t)t->push(),t->update();
np s=t->ch[1-b];
assert(s);
np u=s->ch[b];
t->ch[1-b]=u;
s->ch[b]=t;
t->push(),t->update();s->push(),s->update();
return s;
}
void erase(int idx){
auto [p,q]=split(root,idx);
auto [s,t]=split(q,1);
delete s;
root=merge(p,t);
}
void insert(int idx,np val){
if(val)val->push(),val->update();
if(size(root)==idx){
root=merge(root,val);
}else{
auto [p,q]=split(root,idx);
root=merge(merge(p,val),q);
}
}
np merge(np s,np t){
if(s)s->push(),s->update();
if(t)t->push(),t->update();
if(!s||!t)return s?s:t;
s=splay(s,size(s)-1);
s->ch[1]=t;
return s->update();
}
std::pair<np,np> split(np t,int idx){
if(t)t->push(),t->update();
if(size(t)==idx)return std::make_pair(t,nullptr);
t=splay(t,idx);
np s=t->ch[0];
t->ch[0]=nullptr;
return std::make_pair(s,t->update());
}
np splay(np t,int idx){
if(idx==size(t->ch[0]))return t;
bool b1=size(t->ch[0])<idx;
if(b1)idx-=size(t->ch[0])+1;
if(idx==size(t->ch[b1]->ch[0]))return rot(t,1-b1);
bool b2=size(t->ch[b1]->ch[0])<idx;
if(b2)idx-=size(t->ch[b1]->ch[0])+1;
t->ch[b1]->ch[b2]=splay(t->ch[b1]->ch[b2],idx);
if(b1==b2)t=rot(t,1-b2);
else t->ch[b1]=rot(t->ch[b1],1-b2);
return rot(t,1-b1);
}
std::vector<np> output(){
std::vector<np>res;
auto dfs=[&](auto dfs,np t)->void{
if(!t)return;
dfs(dfs,t->ch[0]);
res.push_back(t);
dfs(dfs,t->ch[1]);
};
dfs(dfs,root);
return res;
}
np get_root(){return root;}
np get(int i){root=splay(root,i);return root;}
template<typename C>
int lower_bound(C less){
int res=__lower_bound(root,less);
if(res<size())root=splay(root,res);
return res;
}
template<typename C>
int __lower_bound(np t,C less){
if(!t)return 0;
t->push();t->update();
bool b=less(t);
if(b)return size(t->ch[0])+1+__lower_bound(t->ch[1],less);
else return __lower_bound(t->ch[0],less);
}
};
#line 2 "cpplib/BBST/splay_tree/splay_tree_map_lazy.hpp"
template<typename T,typename E,typename M,typename F,typename G,typename H>
struct splay_tree_map_lazy_node{
using np=splay_tree_map_lazy_node*;
np ch[2]={0,0};
int sz=1;
T key;E val;M lazy;E sum;F op;G composition;H mapping;
splay_tree_map_lazy_node(T key,E val,M lazy,F op,G composition,H mapping)
:key(key),val(val),lazy(lazy),sum(val),op(op),composition(composition),mapping(mapping){}
int size(np t){return t?t->sz:0;}
np push(){
if(lazy==E())return this;
val=mapping(val,lazy);
if(ch[0])ch[0]->lazy=composition(ch[0]->lazy,lazy);
if(ch[1])ch[1]->lazy=composition(ch[1]->lazy,lazy);
lazy=M();
return this;
}
np update(){
sz=size(ch[0])+1+size(ch[1]);
sum=val;
if(ch[0]){
ch[0]->push();
sum=op(ch[0]->sum,sum);
}
if(ch[1]){
ch[1]->push();
sum=op(sum,ch[1]->sum);
}
return this;
}
};
template<typename T,typename E,typename M,typename F,typename G,typename H>
struct splay_tree_map_lazy:splay_tree_base<splay_tree_map_lazy_node<T,E,M,F,G,H>>{
using node=splay_tree_map_lazy_node<T,E,M,F,G,H>;
using super=splay_tree_base<node>;
using np=node*;
F op;G g;H h;
splay_tree_map_lazy(F op=F(),G g=G(),H h=H()):op(op),g(g),h(h){}
inline int size(){return super::size(super::root);}
int get_idx(T key){return super::lower_bound([&](np t){return t->key<key;});}
inline void insert(T key,E val){
return super::insert(get_idx(key),new node(key,val,M(),op,g,h));
}
inline E back(){return (*this)[size()-1];}
inline E front(){return (*this)[0];}
bool contains(T key){
int idx=get_idx(key);
return idx<size()&&super::get(idx)->key==key;
}
E operator[](T key){
if(!contains(key)){
insert(key,E());
}
return super::get(get_idx(key))->val;
}
E fold_idx(int l,int r){
auto [p,q]=super::split(super::root,l);
auto [s,t]=super::split(q,r-l);
T res=s?s->sum:E();
super::root=super::merge(p,super::merge(s,t));
return res;
}
E fold(T l,T r){
int ll=get_idx(l);
int rr=get_idx(r);
auto [p,q]=super::split(super::root,ll);
auto [s,t]=super::split(q,rr-ll);
E res=s?s->sum:E();
super::root=super::merge(p,super::merge(s,t));
return res;
}
void propagate_idx(int l,int r,M val){
auto [p,q]=super::split(super::root,l);
auto [s,t]=super::split(q,r-l);
if(s){
s->lazy=g(s->lazy,val);
s->push();
}
super::root=super::merge(p,super::merge(s,t));
}
void propagate(T l,T r,M val){
int ll=get_idx(l);
int rr=get_idx(r);
auto [p,q]=super::split(super::root,ll);
auto [s,t]=super::split(q,rr-ll);
if(s){
s->lazy=g(s->lazy,val);
s->push();
}
super::root=super::merge(p,super::merge(s,t));
}
void fix(T key,E val){
if(!contains(key)){
insert(key,E());
}
auto t=super::get(get_idx(key));
t->val=val;
t->sum=val;
}
template<typename OP>
void update(T key,OP g=OP()){
int idx=super::lower_bound([&](np t){return t->key<key;});
np t=super::get(idx);
g(t->val);
t->update();
}
};
#line 3 "cpplib/math/binary_search.hpp"
/**
* @brief 二分探索
*/
template<typename T=long long,typename F>
T bs(T mn,T mx,F func){
mn--;
mx++;
while(mx-mn>1){
T mid=(mn+mx)/2;
if(!func(mid))mx=mid;
else mn=mid;
}
return mn;
}
#line 4 "main.cpp"
int main(){
lint n;
cin>>n;
// auto f=[&](auto s,auto t){
// return max(s,t);
// };
// auto g=[&](auto s,auto t){
// return s+t;
// };
// auto h=[&](auto s,auto t,auto l,auto r){
// auto p=s.unwrap_or(-IINF);
// if(p!=-IINF)return p+t;
// else return p;
// };
auto f=[&](auto s,auto t){
return max(s,t);
};
auto g=[&](auto s,auto t){
return s+t;
};
auto h=[&](auto s,auto t){
return s+t;
};
//seg[i]:= 値 i に対する最大カウント >=0 なら最終値になり得る(=0でしかなりえないが、こうしてOK)
// i を追加したら [0,i) に +1,それ以外に -1
//seg2[i]:= 値 i を入れた時カウントいくつから始まるか
// i を追加したら [0,i) に -1,それ以外に +1
lint pre=0;
splay_tree_map_lazy<int,int,int,decltype(f),decltype(g),decltype(h)>seg(f,g,h),seg2(f,g,h);
int mx=-1;
rep(i,n){
int x;
cin>>x;
x^=pre;
auto p=(mx>=x?seg.fold(x,1<<30):-IINF);
auto q=(mx>=x?seg2.fold(x,1<<30):-IINF);
chmax(mx,x);
seg.propagate(0,x,1);
seg.propagate(x,1LL<<30,-1);
seg.fix(x,max({0,p,q}));
seg2.propagate(0,x,1);
seg2.propagate(x,1LL<<30,-1);
seg2.fix(x,max(q,0)+1);
pre=bs(0LL,1LL<<30,[&](lint x)->bool{
return (mx>=x?seg.fold(x,1<<30):-IINF)>=0;
});
cout<<pre<<endl;
}
}
hotman78