Submission #3288469
Source Code Expand
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const int N = 455555;
const int INF = int(1e9);
int a[N];
/*
struct node
{
int v,lazy;
};
node st[N*4];
void build(int id, int l, int r)
{
st[id].lazy=0;st[id].v=INF;
if(r-l>=2)
{
int mid=(l+r)>>1;
build(id*2,l,mid); build(id*2+1,mid,r);
}
}
void push(int id, int l, int r)
{
if(st[id].lazy!=0)
{
st[id].v+=st[id].lazy;
if(r-l>=2)
{
st[id*2].lazy+=st[id].lazy; st[id*2+1].lazy+=st[id].lazy;
}
st[id].lazy=0;
}
}
void update(int id, int l, int r, int pos, int v)
{
push(id,l,r);
if(pos>=r||pos<l) return ;
if(r-l<2)
{
st[id].v=v; return ;
}
int mid=(l+r)>>1;
update(id*2,l,mid,pos,v); update(id*2+1,mid,r,pos,v);
st[id].v=min(st[id*2].v,st[id*2+1].v);
}
void updaterange(int id, int l, int r, int ql, int qr, int v)
{
push(id,l,r);
if(ql>=r||l>=qr) return ;
if(ql<=l&&r<=qr)
{
st[id].lazy+=v; push(id,l,r); return ;
}
int mid=(l+r)>>1;
updaterange(id*2,l,mid,ql,qr,v); updaterange(id*2+1,mid,r,ql,qr,v);
st[id].v=min(st[id*2].v,st[id*2+1].v);
}
ii query(int id, int l, int r, int ql, int qr)
{
push(id,l,r);
if(ql>=r||l>=qr) return mp(INF, -1);
if(ql<=l&&r<=qr) return mp(st[id].v, l);
int mid=(l+r)>>1;
return min(query(id*2,l,mid,ql,qr),query(id*2+1,mid,r,ql,qr));
}
*/
struct node
{
int prior,siz;
ii val;//value stored in the array
ii sum;//whatever info you want to maintain in segtree for each node
int lazy;//whatever lazy update you want to do
struct node *l,*r;
};
node buffer[555555];
typedef node* pnode;
int sz(pnode t)
{
return t?t->siz:0;
}
void upd_sz(pnode t)
{
if(t)t->siz=sz(t->l)+1+sz(t->r);
}
void lazy(pnode t)
{
if(!t || !t->lazy)return;
t->val.fi+=t->lazy;//operation of lazy
t->sum.fi+=t->lazy;
if(t->l)t->l->lazy+=t->lazy;//propagate lazy
if(t->r)t->r->lazy+=t->lazy;
t->lazy=0;
}
void reset(pnode t)
{
if(t)t->sum = t->val;//no need to reset lazy coz when we call this lazy would itself be propagated
}
void combine(pnode& t,pnode l,pnode r){//combining two ranges of segtree
if(!l || !r)return void(t = l?l:r);
t->sum = min(l->sum, r->sum);
}
void operation(pnode t){//operation of segtree
if(!t)return;
reset(t);//reset the value of current node assuming it now represents a single element of the array
lazy(t->l);lazy(t->r);//imp:propagate lazy before combining t->l,t->r;
combine(t,t->l,t);
combine(t,t,t->r);
}
void split(pnode t,pnode &l,pnode &r,int pos,int add=0){
//if(pos<0&&add==0){l=NULL; r=t; return ;}
if(!t)return void(l=r=NULL);
lazy(t);
int curr_pos = add + sz(t->l);
if(curr_pos<=pos)//element at pos goes to left subtree(l)
split(t->r,t->r,r,pos,curr_pos+1),l=t;
else
split(t->l,l,t->l,pos,add),r=t;
upd_sz(t);
operation(t);
}
void merge(pnode &t,pnode l,pnode r){ //l->leftarray,r->rightarray,t->resulting array
lazy(l);lazy(r);
if(!l || !r) t = l?l:r;
else if(l->prior>r->prior)merge(l->r,l->r,r),t=l;
else merge(r->l,l,r->l),t=r;
upd_sz(t);
operation(t);
}
pnode curnode = &buffer[0];
pnode init(ii val){
pnode ret = curnode++;
ret->prior=rand();ret->siz=1;
ret->val=val;
ret->sum=val;ret->lazy=0;
return ret;
}
ii range_query(pnode t,int l,int r){//[l,r]
if(l>r) return mp(INF,-1);
pnode L,mid,R;
split(t,L,mid,l-1);
split(mid,t,R,r-l);//note: r-l!!
ii ans = t->sum;
merge(mid,L,t);
merge(t,mid,R);
return ans;
}
void range_update(pnode t,int l,int r,ll val){//[l,r]
if(l>r) return ;
pnode L,mid,R;
split(t,L,mid,l-1);
split(mid,t,R,r-l);//note: r-l!!
t->lazy+=val; //lazy_update
merge(mid,L,t);
merge(t,mid,R);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
pbds T; int timer = 0;
ll ans = 0;
pnode treap = NULL;
for(int i=0;i<n;i++)
{
int aft = T.order_of_key(mp(a[i]+1,-1)) - 1;
int mn = i-1-aft;
if(mn==0)
{
T.insert(mp(a[i],++timer));
pnode tmp = init(mp(a[i]-i,a[i]));
merge(treap, treap, tmp);
continue;
}
int id = aft+1;
aft=-1;
ii X = range_query(treap, id+1, i-1);
if(X.fi+i-1-a[i]<mn)
{
mn=X.fi+i-1-a[i];
aft=X.se;
}
ans+=mn;
//cerr<<"MN : "<<mn<<" AFT : "<<aft<<endl;
if(aft>=0)
{
int pos = T.order_of_key(mp(aft+1,-1));
range_update(treap,pos,i-1,-1);
T.insert(mp(aft,++timer));
pnode tmp = init(mp(aft-pos,aft));
pnode L,R;
split(treap,L,R,pos-1);
merge(L,L,tmp);
merge(treap,L,R);
}
else
{
aft=a[i];
int pos = T.order_of_key(mp(aft+1,-1));
range_update(treap,pos,i-1,-1);
T.insert(mp(aft,++timer));
pnode tmp = init(mp(aft-pos,aft));
pnode L,R;
split(treap,L,R,pos-1);
merge(L,L,tmp);
merge(treap,L,R);
}
//cerr<<"MN "<<mn<<' '<<"AFT "<<aft<<'\n';
}
cout<<ans<<'\n';
}
Submission Info
Submission Time |
|
Task |
B - Increment and Swap |
User |
zscoder |
Language |
C++14 (GCC 5.4.1) |
Score |
1500 |
Code Size |
5299 Byte |
Status |
AC |
Exec Time |
1265 ms |
Memory |
39552 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1500 / 1500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
example0.txt, example1.txt |
All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, 059.txt, 060.txt, 061.txt, 062.txt, 063.txt, example0.txt, example1.txt |
Case Name |
Status |
Exec Time |
Memory |
000.txt |
AC |
956 ms |
39552 KB |
001.txt |
AC |
1005 ms |
39552 KB |
002.txt |
AC |
976 ms |
39552 KB |
003.txt |
AC |
1004 ms |
39552 KB |
004.txt |
AC |
1023 ms |
39552 KB |
005.txt |
AC |
1082 ms |
39552 KB |
006.txt |
AC |
1173 ms |
39552 KB |
007.txt |
AC |
1140 ms |
39552 KB |
008.txt |
AC |
1182 ms |
39552 KB |
009.txt |
AC |
1202 ms |
39552 KB |
010.txt |
AC |
1254 ms |
39552 KB |
011.txt |
AC |
1230 ms |
39552 KB |
012.txt |
AC |
1236 ms |
39552 KB |
013.txt |
AC |
1185 ms |
39552 KB |
014.txt |
AC |
1206 ms |
39552 KB |
015.txt |
AC |
1265 ms |
39552 KB |
016.txt |
AC |
1182 ms |
39552 KB |
017.txt |
AC |
1230 ms |
39552 KB |
018.txt |
AC |
1183 ms |
39552 KB |
019.txt |
AC |
1265 ms |
39552 KB |
020.txt |
AC |
1212 ms |
39552 KB |
021.txt |
AC |
80 ms |
28288 KB |
022.txt |
AC |
961 ms |
38912 KB |
023.txt |
AC |
420 ms |
32896 KB |
024.txt |
AC |
155 ms |
29312 KB |
025.txt |
AC |
600 ms |
34432 KB |
026.txt |
AC |
102 ms |
28544 KB |
027.txt |
AC |
831 ms |
36352 KB |
028.txt |
AC |
373 ms |
31872 KB |
029.txt |
AC |
378 ms |
31616 KB |
030.txt |
AC |
370 ms |
31744 KB |
031.txt |
AC |
886 ms |
36352 KB |
032.txt |
AC |
525 ms |
33024 KB |
033.txt |
AC |
514 ms |
33024 KB |
034.txt |
AC |
948 ms |
36992 KB |
035.txt |
AC |
140 ms |
28928 KB |
036.txt |
AC |
1080 ms |
38144 KB |
037.txt |
AC |
752 ms |
35584 KB |
038.txt |
AC |
187 ms |
29440 KB |
039.txt |
AC |
562 ms |
33536 KB |
040.txt |
AC |
485 ms |
32896 KB |
041.txt |
AC |
1053 ms |
38016 KB |
042.txt |
AC |
358 ms |
39552 KB |
043.txt |
AC |
682 ms |
39552 KB |
044.txt |
AC |
1225 ms |
39552 KB |
045.txt |
AC |
8 ms |
26880 KB |
046.txt |
AC |
551 ms |
39552 KB |
047.txt |
AC |
1123 ms |
39552 KB |
048.txt |
AC |
447 ms |
39552 KB |
049.txt |
AC |
490 ms |
39552 KB |
050.txt |
AC |
516 ms |
39552 KB |
051.txt |
AC |
546 ms |
39552 KB |
052.txt |
AC |
702 ms |
39552 KB |
053.txt |
AC |
859 ms |
39552 KB |
054.txt |
AC |
1209 ms |
39552 KB |
055.txt |
AC |
627 ms |
39552 KB |
056.txt |
AC |
662 ms |
39552 KB |
057.txt |
AC |
743 ms |
39552 KB |
058.txt |
AC |
757 ms |
39552 KB |
059.txt |
AC |
890 ms |
39552 KB |
060.txt |
AC |
1033 ms |
39552 KB |
061.txt |
AC |
1196 ms |
39552 KB |
062.txt |
AC |
370 ms |
39552 KB |
063.txt |
AC |
690 ms |
39552 KB |
example0.txt |
AC |
8 ms |
26880 KB |
example1.txt |
AC |
8 ms |
26880 KB |