結果
問題 |
No.1526 Sum of Mex 2
|
ユーザー |
|
提出日時 | 2024-05-26 16:23:35 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 222 ms / 3,000 ms |
コード長 | 1,492 bytes |
コンパイル時間 | 6,549 ms |
コンパイル使用メモリ | 315,816 KB |
実行使用メモリ | 15,872 KB |
最終ジャッジ日時 | 2024-12-20 20:21:59 |
合計ジャッジ時間 | 12,791 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 32 |
ソースコード
#include<bits/stdc++.h> using namespace std; #include<atcoder/all> using namespace atcoder; int op(int a,int b){return min(a,b);} int e(){return 1e9;} int tar; bool f(int x){ return x>tar; } const int MAX_N=200009; int main(){ int n;cin>>n; vector<int> a(n);for(auto&e:a)cin>>e,e--; set<pair<int,int>> st; for(int i=0;i<MAX_N;i++)st.insert({i,-1}); long ans=0,sum=0; segtree<int,op,e> seg(vector<int>(MAX_N,-1)); vector<bool> update(MAX_N); for(int i=0;i<n;i++){ seg.set(a[i],i); update[a[i]]=1; int idx; if(a[i]==0){ idx=0; }else{ auto p=st.lower_bound({a[i],-1}); if(p->first!=a[i]){ ans+=sum; continue; } idx=(--p)->first; update[idx]=1; } while(update[idx]){ update[idx]=0; tar=seg.get(idx); int next_idx=seg.max_right(idx+1,f); //delete idx<=x<next_idx while(1){ auto p=st.lower_bound({idx,-1}); if(p->first>=next_idx)break; if(p!=st.begin()){ auto lp=p,rp=p;lp--;rp++; sum+=long(lp->second-p->second)*(rp->first-p->first); }else{ auto rp=p;rp++; sum-=long(p->second-rp->second)*rp->first; } st.erase(p); } //insert idx auto p=st.lower_bound({idx,-1}); if(p!=st.begin()){ auto lp=p;lp--; sum-=long(lp->second-seg.get(idx))*(p->first-idx); }else{ sum+=long(seg.get(idx)-p->second)*p->first; } st.insert({idx,seg.get(idx)}); if(!update[next_idx])continue; idx=next_idx; } ans+=sum; } ans+=long(n)*(n+1)/2; cout<<ans<<endl; }