QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#122732#6627. Line Townlmeowdn0 1ms22076kbC++144.0kb2023-07-10 23:07:132023-07-10 23:07:14

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-10 23:07:14]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:22076kb
  • [2023-07-10 23:07:13]
  • 提交

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],g[N][2],h[N][2],f[N<<1][2],ls[N<<1],rs[N<<1],s[N<<1],tag[N<<1],pre[N],suf[N],tot;

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) g[i][0]=g[i][1]=h[i][0]=h[i][1]=0;
  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]++;
    rep(j,0,1) {
      if(j) w=-w;
      if(w>0) {
        if(g[p][j]) h[p][j]=inf; g[p][j]=0;
      } else {
        if(g[p][j]) h[p][j]++; g[p][j]^=1;
      }
      f[p][j]=(g[p][j]?inf:h[p][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 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) {
  int ans=0,cnt=0; init(); build(1,1,m);
  per(i,n,1) {
    //cout<<i<<endl;
    if(!a[i]) suf[i]=suf[i+1], ++cnt;
    else {
      ans+=cnt; ans+=qrys(1,1,m,1,abs(a[i])-1);
      if(cnt&1) a[i]=-a[i];
      mdf(1,1,m,abs(a[i]),0,a[i]);
      flip(1,1,m,1,abs(a[i])-1);
      suf[i]=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]+suf[i]);
  //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: 3
Accepted
time: 0ms
memory: 22008kb

input:

10
1 1 1 1 1 -1 -1 -1 1 -1

output:

-1

result:

ok 1 number(s): "-1"

Test #2:

score: -3
Wrong Answer
time: 1ms
memory: 22076kb

input:

10
1 1 1 1 1 1 -1 1 1 -1

output:

4

result:

wrong answer 1st numbers differ - expected: '3', found: '4'

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: 1ms
memory: 19968kb

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: 1ms
memory: 19984kb

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%