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"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int N = 5e5 + 10;
ll fen[N];
void modif(int u){
for(int i = u + 1;i < N; i += (i & (-i))){
++fen[i];
}
}
ll query(int l, int r){
ll ret = 0;
for(int i = r + 1;i > 0; i -= i &(-i)){
ret += fen[i];
}
for(int i = l;i > 0;i -= i & (-i)){
ret -= fen[i];
}
return ret;
}
ll count_swaps(vector<int> s){
int n = sz(s) / 2, m = sz(s);
for(int i = 0;i < N; ++i)fen[i] = 0;
vector<bool> done(m + 5, 0);
int cnt = 1;
queue<int> q[N];
vector<int> a(m, -1);
for(int i = 0;i < m; ++i){
q[s[i] + n].push(i);
}
for(int i = 0;i < m; ++i){
if(done[i])continue;
if(s[i] > 0){
a[i] = cnt + 1;
done[i] = true;
a[q[n - s[i]].front()] = cnt;
done[q[n - s[i]].front()] = true;
q[s[i] + n].pop();
q[n - s[i]].pop();
cnt += 2;
}else{
a[i] = cnt;
done[i] = true;
a[q[n - s[i]].front()] = cnt + 1;
done[q[n - s[i]].front()] = true;
q[s[i] + n].pop();
q[n - s[i]].pop();
cnt += 2;
}
}
ll ans = 0;
for(int i = 0;i < m; ++i){
ans += query(a[i] + 1, m + 5);
modif(a[i]);
}
return ans;
}
/*
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for(int i = 0;i < n; ++i){
cin >> a[i];
}
cout << count_swaps(a) << "\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
}
*/
# | 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... |