#include <bits/stdc++.h>
using namespace std;
int i,j,n,t,ans,k,b[200001],fix[200001],c[200001],seg[200001];
pair <int,int> a[200001];
map <int,int> m;
vector <int> v[200001];
void upd(int h,int l,int r,int val,int ind) {
if (ind<l || ind>r) return;
if (l==ind && r==ind) {seg[h]=1;return;}
upd(h*2,l,(l+r)/2,val,ind);
upd(h*2+1,(l+r)/2+1,r,val,ind);
seg[h]=seg[h*2]+seg[h*2+1];
}
int get(int h,int l,int r,int L,int R) {
if (l>R || r<L) return 0;
if (l>=L && r<=R) return seg[h];
return get(h*2,l,(l+r)/2,L,R)+get(h*2+1,(l+r)/2+1,r,L,R);
}
int64_t count_swaps(vector<int> p){
n=p.size()/2;
for (i=1;i<=2*n;i++) {
b[i]=p[i-1];
c[i]=b[i];
if (b[i]>0) {fix[b[i]]++;v[b[i]].push_back(i);}
}
for (i=1;i<=2*n;i++) {
m[b[i]]=0;
if (b[i]<0) {v[abs(b[i])].push_back(i);}
}
for (i=1;i<=2*n;i++) {
if (b[i]>0) m[b[i]]=0;
else
m[b[i]]+=1;
}
t=1;
for (i=1;i<=2*n;i++) {
if (b[i]==0) continue;
if (b[i]>0) {
a[t].first=v[b[i]][m[-1*b[i]]];
a[t].second=i;
m[-1*b[i]]++;
m[b[i]]++;
b[a[t].first]=0;
b[a[t].second]=0;
t++;
}
else
{
a[t].first=i;
a[t].second=v[-1*b[i]][m[-1*b[i]]];
m[-1*b[i]]++;
m[b[i]]++;
b[a[t].first]=0;
b[a[t].second]=0;
t++;
}
}
for (i=1;i<=n;i++) {
if (c[min(a[i].first,a[i].second)]>0) {ans=ans+max(a[i].first,a[i].second)-min(a[i].first,a[i].second);}
else
ans=ans+max(a[i].second,a[i].first)-min(a[i].second,a[i].first)-1;
}
for (i=1;i<=n;i++) {
ans=ans-get(1,1,2*n+1,min(a[i].first,a[i].second),max(a[i].first,a[i].second));
upd(1,1,2*n+1,1,a[i].first);
upd(1,1,2*n+1,1,a[i].second);
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5112 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5112 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5112 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5112 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5112 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5112 KB |
secret mismatch |
2 |
Halted |
0 ms |
0 KB |
- |