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 all(c) (c).begin(), (c).end()
#define rall(A) A.rbegin(),A.rend()
#define pb push_back
#define dbg(x) do {cerr << #x <<" = " << (x) << endl; } while (false)
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
//cout << setprecision(12) << fixed;
struct BIT {
vector<int> bit; // binary indexed tree
int n;
BIT(int n) {
this->n = n;
bit.assign(n, 0);
}
BIT(vector<int> a) : BIT(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
ll count_swaps(vector<int> s) {
int n = s.size() >> 1;
vi L[n+1], R[n+1];
for(int i = 0; i < 2 * n; i++) {
cin >> s[i];
if(s[i] < 0)
L[-s[i]].pb(i+1);
else
R[s[i]].pb(i+1);
}
for(int i = 1; i <= n; i++) {
reverse(all(L[i]));
reverse(all(R[i]));
}
int a[2*n + 1];
bool vis[2 * n + 1];
memset(vis, 0, sizeof(vis));
int ind = 1;
for(int i = 0; i < 2 * n; i++) {
if(vis[i]) continue;
int l, r;
l = L[abs(s[i])].back();
r = R[abs(s[i])].back();
vis[l-1] = vis[r-1] = 1;
L[abs(s[i])].pop_back();
R[abs(s[i])].pop_back();
a[l] = ind++;
a[r] = ind++;
}
ll ans = 0;
BIT ft(2 * n + 2);
for(int i = 1; i <= 2 * n; i++) {
ans += ft.sum(a[i] + 1, 2 * n + 1);
ft.add(a[i], 1);
}
return ans;
}
// int main() {
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// int n; cin >> n;
// vi s(2 * n);
//
// for(int i = 0; i < 2 * n; i++)
// cin >> s[i];
//
// cout << count_swaps(s) << "\n";
//
// return 0;
// }
//
# | 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... |