This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shoes.h"
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define sz() size()
#define fr first
#define sc second
#define pi pair<int,int>
#define pii pair<pair<int,int>,int>
#define mp make_pair
//#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
using namespace std;
const int mod=1e9+7;
const int modp=1999999973;
const int modulo=998244353;
long long ans,aib[200005],n;
vector<long long>arr(200005);
queue<long long>pos[200005];
queue<long long>neg[200005];
void update(long long pos,long long val){
while(pos<=n){
aib[pos]+=val;
pos+=(pos&-pos);
}
}
long long sum(long long pos){
long long rs=0;
while(pos>=1){
rs+=aib[pos];
pos-=(pos&-pos);
}
return rs;
}
long long count_swaps(std::vector<int> s) {
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
srand(chrono::steady_clock::now().time_since_epoch().count());
n=s.size();
long long cnt=-1;
for(int i=0;i<s.size();i++){
if(s[i]<0){
if(pos[-s[i]].size()==0){
cnt+=2;
arr[i+1]=cnt;
neg[-s[i]].push(cnt+1);
}
else{
arr[i+1]=pos[-s[i]].front();
pos[-s[i]].pop();
}
}
else{
if(neg[s[i]].size()==0){
cnt+=2;
arr[i+1]=cnt+1;
pos[s[i]].push(cnt);
}
else{
arr[i+1]=neg[s[i]].front();
neg[s[i]].pop();
}
}
}
for(int i=n;i>=1;i--){
ans+=sum(arr[i]-1LL);
update(arr[i],1LL);
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:50:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<s.size();i++){
~^~~~~~~~~
# | 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... |