QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#122847 | #6627. Line Town | lmeowdn | 0 | 227ms | 1367716kb | C++14 | 5.2kb | 2023-07-11 12:40:06 | 2023-07-11 12:40:07 |
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],gpre[N],gsuf[N],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[l][0],g[l][1]), swap(h[l][0],h[l][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[l][0],g[l][1]), swap(h[l][0],h[l][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 *g) {
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, g[i]=g[i+1],++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];
g[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,gsuf);
reverse(a+1,a+n+1);
rep(i,1,n) a[i]=-a[i];
work(a,pre,gpre);
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) if(gpre[i-1]*gsuf[i]>0) chmin(ans,pre[i-1][1]+suf[i][1]+1);
//rep(i,2,n) cout<<suf[i][1]<<" "; puts("");
//rep(i,1,n-1) cout<<pre[i][1]<<" "; puts("");
if(ans==inf) ans=-1;
printf("%lld\n",ans);
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 3
Accepted
time: 189ms
memory: 1366348kb
input:
10 1 1 1 1 1 -1 -1 -1 1 -1
output:
-1
result:
ok 1 number(s): "-1"
Test #2:
score: 0
Accepted
time: 189ms
memory: 1367716kb
input:
10 1 1 1 1 1 1 -1 1 1 -1
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: 0
Accepted
time: 211ms
memory: 1367608kb
input:
1 -1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 227ms
memory: 1366832kb
input:
2000 1 -1 -1 -1 -1 -1 1 -1 1 1 1 -1 -1 -1 1 1 -1 1 1 -1 -1 1 -1 -1 1 -1 1 -1 1 1 -1 1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 -1 1 1 -1 1 -1 1 1 -1 -1 -1 1 1 1 -1 1 1 -1 -1 1 -1 1 1 1 1 -1 -1 -1 1 1 -1 -1 1 1 1 -1 -1 -1 -1 1 -1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 -1 -1 ...
output:
15146
result:
ok 1 number(s): "15146"
Test #5:
score: 0
Accepted
time: 217ms
memory: 1367584kb
input:
2000 -1 -1 1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 -1 -1 1 -1 -1 1 -1 1 1 -1 1 1 1 -1 1 1 1 1 1 -1 -1 -1 -1 1 1 -1 -1 -1 1 -1 -1 1 1 1 1 -1 -1 1 -1 -1 1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 1 -1 1 1 1 1 -1 1 1 1 1 -1 -1 -1 -1 1 1 1 1 1 1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 1 -1 1 1 -1 1 1 1 -1 1 1 -1 -1 -1 ...
output:
24933
result:
ok 1 number(s): "24933"
Test #6:
score: 0
Accepted
time: 219ms
memory: 1367504kb
input:
2000 1 1 -1 -1 -1 -1 1 1 -1 1 -1 1 1 1 -1 -1 -1 1 -1 -1 1 1 -1 -1 1 1 -1 1 1 -1 1 1 -1 1 1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 1 -1 1 1 1 -1 -1 -1 -1 1 -1 1 -1 1 1 -1 1 1 1 1 -1 1 1 1 1 -1 -1 1 1 1 -1 1 1 1 1 1 -1 -1 -1 1 1 -1 1 1 -1 -1 1 -1 1 -1 -1 1 1 1 -1 -1 1 1 -1 1 -1 -1 -1 1 -1 -1 -1 1 -1 1 -1 -1 1 -...
output:
18090
result:
ok 1 number(s): "18090"
Test #7:
score: 0
Accepted
time: 218ms
memory: 1367048kb
input:
2000 -1 -1 -1 -1 -1 1 1 -1 -1 -1 1 1 -1 -1 1 -1 1 1 1 1 -1 -1 1 1 1 1 -1 -1 -1 -1 1 1 1 1 1 -1 1 -1 -1 -1 -1 -1 -1 1 1 -1 -1 -1 -1 -1 1 1 -1 1 -1 -1 1 1 -1 1 1 -1 -1 -1 -1 1 -1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 1 1 -1 -1 -1 1 -1 -1 1 1 1 -1 1 1 -1 -1 -1 1 -1 -1 1 -1 1 1 1 -1 1 -1 -1 1 1 1...
output:
-1
result:
ok 1 number(s): "-1"
Test #8:
score: 0
Accepted
time: 180ms
memory: 1367276kb
input:
2000 -1 1 -1 1 1 -1 -1 1 1 -1 1 1 -1 1 -1 -1 1 -1 1 1 1 1 -1 -1 -1 1 -1 1 -1 -1 1 1 -1 1 -1 -1 1 -1 1 -1 1 1 1 1 1 -1 -1 -1 1 -1 -1 -1 -1 1 1 1 1 1 1 1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 1 -1 -1 -1 1 -1 1 1 1 -1 1 1 1 1 -1 -1 -1 1 -1 1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 1 1 -1 1 -1 1 -1 -1 -1 1 -1 -1 -1 ...
output:
9973
result:
ok 1 number(s): "9973"
Test #9:
score: 0
Accepted
time: 215ms
memory: 1366496kb
input:
2000 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 ...
output:
499500
result:
ok 1 number(s): "499500"
Test #10:
score: 0
Accepted
time: 216ms
memory: 1367028kb
input:
2000 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ...
output:
499500
result:
ok 1 number(s): "499500"
Test #11:
score: 0
Accepted
time: 204ms
memory: 1366380kb
input:
1999 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ...
output:
499500
result:
ok 1 number(s): "499500"
Test #12:
score: 0
Accepted
time: 201ms
memory: 1366824kb
input:
1997 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ...
output:
498501
result:
ok 1 number(s): "498501"
Test #13:
score: -3
Wrong Answer
time: 196ms
memory: 1366112kb
input:
2000 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 1 -1 ...
output:
498502
result:
wrong answer 1st numbers differ - expected: '-1', found: '498502'
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: 221ms
memory: 1364000kb
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: 197ms
memory: 1365460kb
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%