#include <bits/stdc++.h>
using namespace std;
const int MAX_N=1e5+9;
int a[8*MAX_N];
void build(int l,int r, int p){
if(l==r){
a[p]=l;
return;
}
build(l,(l+r)/2,p*2);
build((l+r)/2+1,r,p*2+1);
a[p]=0;
return;
}
int find(int value,int l, int r, int p){
if(l==r){
if(l==value){
return a[p];
}else{
return 0;
}
}
a[p*2]+=a[p];
a[p*2+1]+=a[p];
a[p]=0;
if(l>value||value>r){
return 0;
}
return find(value,(r+l)/2+1,r,p*2+1)+find(value,l,(r+l)/2,p*2);
}
void update(int x,int y,int l,int r,int p){
if(l==r){
if( r<=y && r>=x )
a[p]++;
return;
}
a[p*2]+=a[p];
a[p*2+1]+=a[p];
a[p]=0;
if(r<x||l>y)
return;
if(r<=y&&l>=x){
a[p*2]++;
a[p*2+1]++;
return;
}
update(x,y,l,(l+r)/2,p*2);
update(x,y,(l+r)/2+1,r,p*2+1);
return;
}
long long count_swaps(std::vector<int> S){
int n=S.size()/2;
long long int ans=0;
build(0,n*2,1);
vector<vector<int> > h(n*2);
for(int i=0;i<n*2;i++){
if(h[S[i]*(-1)+n].size()==0){
h[(S[i]+n)].push_back(i+1);
}else{
int x=find(h[S[i]*(-1)+n][0],0,2*n,1);
int y=find(i,1,2*n,1);
if(S[i]<0)
ans++;
ans+=y-x-1;
update(h[S[i]*(-1)+n][0],i,0,2*n,1);
h[S[i]*(-1)+n].erase(h[S[i]*(-1)+n].begin());
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
384 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
384 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
384 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
384 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
384 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
384 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |