QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#122832 | #6627. Line Town | lmeowdn | 0 | 225ms | 1362048kb | C++14 | 5.2kb | 2023-07-11 12:16:44 | 2023-07-11 12:16:47 |
Judging History
answer
#include<bits/stdc++.h>
#define fi first
#define se second
#define eb emplace_back
#define mp make_pair
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 i128;
template<typename T,typename U>
T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);}
template<typename T,typename U>
T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);}
template<class T,class S>
bool chmax(T &a,const S b) {return (a<b?a=b,1:0);}
template<class T,class S>
bool chmin(T &a,const S b) {return (a>b?a=b,1:0);}
int popcnt(int x) {return __builtin_popcount(x);}
int popcnt(ll x) {return __builtin_popcountll(x);}
int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));}
int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));}
int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));}
int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));}
#define int long long
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;
typedef tuple<int,int,int> tiii;
int read() {
int x=0,w=1; char c=getchar();
while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();}
while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();}
return x*w;
}
const int N=5e5+5,inf=0x3f3f3f3f3f3f3f3f;
int n,m,a[N],b[N],f[N<<1][2],h[N][2],ls[N<<1],rs[N<<1],s[N<<1],tag[N<<1],pre[N][2],suf[N][2],tot;
stack<int> g[N][2][2];
void init() {
rep(i,1,tot) ls[i]=0, rs[i]=0, f[i][0]=f[i][1]=s[i]=0;
rep(i,1,m) h[i][0]=h[i][1]=0;
rep(i,1,m) rep(j,0,1) rep(k,0,1) for(;g[i][j][k].size();g[i][j][k].pop());
tot=1;
}
void build(int p,int l,int r) {
if(l==r) return; int mid=l+r>>1;
build(ls[p]=++tot,l,mid), build(rs[p]=++tot,mid+1,r);
}
void flip(int p) {swap(f[p][0],f[p][1]), tag[p]^=1;}
void psd(int p) {if(tag[p]) flip(ls[p]), flip(rs[p]), tag[p]=0;}
void flip(int p,int l,int r,int x,int y) {
//if(p==1) cout<<"FLIP "<<x<<" "<<y<<endl;
if(x>y) return;
if(l==x&&r==y) {flip(p); return;} int mid=l+r>>1; psd(p);
if(y<=mid) flip(ls[p],l,mid,x,y);
else if(x>mid) flip(rs[p],mid+1,r,x,y);
else flip(ls[p],l,mid,x,mid), flip(rs[p],mid+1,r,mid+1,y);
f[p][0]=min(inf,f[ls[p]][0]+f[rs[p]][0]);
f[p][1]=min(inf,f[ls[p]][1]+f[rs[p]][1]);
}
void mdf(int p,int l,int r,int x,int y,int w) {
if(l==r) {
if(tag[p]) swap(g[p][0],g[p][1]), swap(h[p][0],h[p][1]), tag[p]=0;
if(y&1) w=-w; s[p]++;
//cout<<"ADD "<<w<<endl;
rep(j,0,1) {
if(j) w=-w; int k=s[p]&1;
if(w<0) {
if(g[l][j][k^1].size()) {
int x=g[l][j][k^1].top(); g[l][j][k^1].pop();
h[l][j]+=s[p]-x;
} else g[l][j][k].push(s[p]);
}
if(g[l][j][0].size()+g[l][j][1].size()) f[p][j]=inf;
else f[p][j]=h[l][j];
} return;
} int mid=l+r>>1; psd(p);
if(x<=mid) mdf(ls[p],l,mid,x,y,w);
else mdf(rs[p],mid+1,r,x,y+s[ls[p]],w);
s[p]=s[ls[p]]+s[rs[p]];
f[p][0]=min(inf,f[ls[p]][0]+f[rs[p]][0]);
f[p][1]=min(inf,f[ls[p]][1]+f[rs[p]][1]);
}
int assum(int p,int l,int r,int x,int y,int w) {
if(l==r) {
if(tag[p]) swap(g[p][0],g[p][1]), swap(h[p][0],h[p][1]), tag[p]=0;
if(y&1) w=-w; int k=(s[p]+1)&1;
int s1=g[l][0][k^1].size(), s0=g[l][0][k].size(), hh=h[l][0];
if(w<0) {
if(s1) hh+=s[p]+1-g[l][0][k^1].top(), s1--;
else s0++;
}
if(s0+s1) return inf; else return hh;
} int mid=l+r>>1; psd(p);
if(x<=mid) return min(inf,assum(ls[p],l,mid,x,y,w)+f[rs[p]][0]);
else return min(inf,assum(rs[p],mid+1,r,x,y+s[ls[p]],w)+f[ls[p]][0]);
}
int qryf() {return f[1][0];}
int qrys(int p,int l,int r,int x,int y) {
if(x>y) return 0;
if(l==x&&r==y) return s[p]; int mid=l+r>>1;
if(y<=mid) return qrys(ls[p],l,mid,x,y);
else if(x>mid) return qrys(rs[p],mid+1,r,x,y);
else return qrys(ls[p],l,mid,x,mid)+qrys(rs[p],mid+1,r,mid+1,y);
}
void work(int *a,int suf[N][2]) {
int ans=0,cnt=0; init(); build(1,1,m);
//rep(i,1,n) cout<<a[i]<<" "; puts("");
suf[n+1][1]=suf[0][1]=inf;
per(i,n,1) {
//cout<<i<<endl;
if(!a[i]) suf[i][0]=suf[i+1][0], suf[i][1]=suf[i+1][1]+2, ++cnt;
else {
ans+=cnt; ans+=qrys(1,1,m,1,abs(a[i])-1);
//cout<<" "<<cnt<<" "<<a[i]<<endl;
if(cnt&1) a[i]=-a[i];
//cout<<i<<" "<<a[i]<<endl;
suf[i][1]=ans+assum(1,1,m,abs(a[i]),0,-a[i]);
//cout<<" "<<suf[i][1]<<endl;
mdf(1,1,m,abs(a[i]),0,a[i]);
flip(1,1,m,1,abs(a[i])-1);
suf[i][0]=ans+qryf();
if(cnt&1) a[i]=-a[i];
}
}
}
signed main() {
n=read();
rep(i,1,n) a[i]=read();
rep(i,1,n) if(a[i]) b[++m]=abs(a[i]);
sort(b+1,b+m+1); m=unique(b+1,b+m+1)-b-1;
rep(i,1,n) {
if(a[i]>0) a[i]=lower_bound(b+1,b+m+1,a[i])-b;
else if(a[i]<0) a[i]=-(lower_bound(b+1,b+m+1,-a[i])-b);
}
work(a,suf);
reverse(a+1,a+n+1);
rep(i,1,n) a[i]=-a[i];
work(a,pre);
reverse(pre+1,pre+n+1);
int ans=inf;
rep(i,1,n+1) chmin(ans,pre[i-1][0]+suf[i][0]);
//cout<<ans<<endl;
rep(i,2,n) chmin(ans,pre[i-1][1]+suf[i][1]+1);
//rep(i,1,n) cout<<suf[i]<<" "; puts("");
//rep(i,1,n) cout<<pre[i]<<" "; puts("");
if(ans==inf) ans=-1;
printf("%lld\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 178ms
memory: 1362048kb
input:
10 1 1 1 1 1 -1 -1 -1 1 -1
output:
7
result:
wrong answer 1st numbers differ - expected: '-1', found: '7'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Wrong Answer
Test #60:
score: 4
Accepted
time: 199ms
memory: 1359656kb
input:
10 3 10 5 -9 7 2 -6 1 8 0
output:
-1
result:
ok 1 number(s): "-1"
Test #61:
score: -4
Wrong Answer
time: 225ms
memory: 1361624kb
input:
10 -9 0 -1 7 5 10 6 3 2 -8
output:
-1
result:
wrong answer 1st numbers differ - expected: '13', found: '-1'
Subtask #6:
score: 0
Skipped
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%