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>
#define PB push_back
#define ST first
#define ND second
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 100*1000+10;
int T[(1<<19)],R;
vi lft[nax],rght[nax];
int nxt[2*nax];
void update(int a,int x) {
a+=R;
T[a] += x;
while(a>1) {
a/=2;
T[a] = T[2*a] + T[2*a+1];
}
}
int query(int a,int b) {
a+=R; b+=R;
if(a > b) return 0;
int w = T[a] + (a!=b?T[b]:0);
while(a/2!=b/2) {
if(a%2==0) w+=T[a+1];
if(b%2==1) w+=T[b-1];
a/=2; b/=2;
}
return w;
}
ll count_swaps(vector<int> t) {
int n = t.size();
R = 1;
ll ans = 0;
while(R < n) R*=2;
for(int i = 0; i < n; ++i) {
if(t[i] < 0) {
lft[-t[i]].PB(i);
} else {
rght[t[i]].PB(i);
}
}
for(int i = 1; i <= n/2; ++i) {
for(int j = 0; j < (int)lft[i].size(); ++j) {
ans += (lft[i][j] > rght[i][j]);
nxt[min(lft[i][j],rght[i][j])] = max(lft[i][j],rght[i][j]);
}
}
for(int i = 0; i < n; ++i) {
if(nxt[i] == 0) continue;
ans += nxt[i] - i - 1 - query(i+1,nxt[i]-1);
update(nxt[i],1);
}
return ans;
}
//~ int main() {
//~ 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... |