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;
using ll = long long;
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
template<typename T> bool chmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
const int N = 100010*2;
int n,a[N],vis[N];
map<int,queue<int>> p;
ll ans;
ll fw[N], fw2[N];
void update(int x, int y, int v) { //inclusive
for (int tx=x; tx < N; tx += tx&(-tx)) fw[tx] += v, fw2[tx] -= v*(x-1);
for (int ty=y+1; ty < N; ty += ty&(-ty)) fw[ty] -= v, fw2[ty] += v*y;
}
ll sum (int x) {
ll res = 0;
for (int tx=x; tx; tx -= tx&(-tx)) res += fw[tx]*x + fw2[tx];
return res;
}
ll query(int x) {
return sum(x)-sum(x-1);
}
long long count_swaps(std::vector<int> s) {
n = s.size();
for (int i = 1; i <= n; ++i) {
a[i]=s[i-1];
p[a[i]].push(i);
update(i,i,i);
}
for (int i = 1; i <= n; ++i) {
if (vis[i]) continue;
int from = p[-a[i]].front();
int rx = query(from);
int lx = query(i);
int actual = abs(a[i])*(lx%2?-1:1);
ans += rx-lx+(actual!=a[i])-1;
p[-a[i]].pop(); p[a[i]].pop();
vis[from]=1;
update(i,from,1);
}
return ans;
}
# | 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... |