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 <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define modul 998244353
#define PI 3.14159265359
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,bit[200005],ans[200005],rs=0;
queue<ll>a[200005];
queue<ll>b[200005];
ll query(ll pos){
ll sum=0;
for(;pos>=1;pos-=pos&(-pos)){
sum=sum+bit[pos];
}
return sum;
}
void update(ll pos,ll val){
for(;pos<=n;pos+=pos&(-pos)){
bit[pos]+=val;
}
}
ll count_swaps(std::vector<int>s){
n=s.size();
ll cnt=-1;
for(ll i=0;i<n;i++)
{
if(s[i]<0){
if(b[-s[i]].size()==0){
cnt=cnt+2;
ans[i+1]=cnt;
a[-s[i]].push(cnt+1);
}else {
ans[i+1]=b[-s[i]].front();
b[-s[i]].pop();
}
}else {
if(a[s[i]].size()==0){
cnt+=2;
ans[i+1]=cnt+1;
b[s[i]].push(cnt);
}else {
ans[i+1]=a[s[i]].front();
a[s[i]].pop();
}
}
}
for(ll i=n;i>=1;i--)
{
rs=rs+query(ans[i]-1);
update(ans[i],1LL);
}
return rs;
}
/*
int32_t main(){
CODE_START;
cout<<count_swaps({2,1,-1,-2});
}
*/
# | 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... |