This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
const int maxn=400001,p=200000,N=(1<<(int)log2(maxn)+1);
int Tree[N*2+1],arr[maxn+1],l,r;
vector <int> v[maxn];
bool left(int i){
if(i-p>=0) return 0;
else return 1;
}
int solve(int l1,int r1,int i){
if(l1>r||r1<l) return 0;
if(l1>=l&&r1<=r) return Tree[i];
return solve(l1,(l1+r1)/2,i*2)+solve(((l1+r1)/2)+1,r1,i*2+1);
}
void update(int i){
arr[i]=0;
i+=N;
Tree[i]=0;
i/=2;
while(i!=0){
Tree[i]=Tree[i*2]+Tree[i*2+1];
i/=2;
}
return;
}
long long count_swaps(vector<int> s){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n=s.size(),ans=0;
for(int i=0;i<n;i++){
arr[i]=s[i];
Tree[i+N]=1;
arr[i]+=p;
}
for(int i=n-1;i>=0;i--){
v[arr[i]].push_back(i);
}
for(int i=N-1;i>=1;i--) Tree[i]=Tree[i*2]+Tree[i*2+1];
for(int i=0;i<n;i++){
if(arr[i]==0) continue;
l=i+1;
int inv=((arr[i]-p)*-1)+p;
v[arr[i]].pop_back();
if(left(arr[i])==1){
r=v[inv].back();
v[inv].pop_back();
ans+=solve(0,N-1,1)-1;
}
else{
r=v[inv].back();
v[inv].pop_back();
ans+=solve(0,N-1,1);
}
update(l-1);
update(r);
}
return ans;
}
// signed main()
// {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
// int n;
// cin>>n;
// vector <int> s(n);
// for(int i=0;i<n;i++) cin>>s[i];
// cout<<count_swaps(s);
// }
Compilation message (stderr)
shoes.cpp:11:53: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
11 | const int maxn=400001,p=200000,N=(1<<(int)log2(maxn)+1);
| ~~~~~~~~~~~~~~~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |