This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <bits/stdc++.h>
#include "shoes.h"
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define mod 1000000007
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
struct BIT{
int bit[maxn];
void modify(int ind, int val) {
ind++;
for (;ind < maxn;ind += ind & (-ind)) bit[ind] += val;
}
int query(int ind) {
ind++;
int ret = 0;
for (;ind > 0;ind -= ind & (-ind)) ret += bit[ind];
return ret;
}
}bit;
vector<int> lef[maxn], rig[maxn];
long long count_swaps(std::vector<int> s) {
int n = s.size();
vector<bool> p(n, 1);
for (int i = 0;i < n;i++) bit.modify(i, 1);
for (int i = 0;i < n;i++) {
if (s[i] < 0) lef[-s[i]].push_back(i);
else rig[s[i]].push_back(i);
}
for (int i = 1;i <= n;i++) {
reverse(lef[i].begin(), lef[i].end());
reverse(rig[i].begin(), rig[i].end());
}
ll ret = 0;
for (int i = 0;i < n;i++) {
if (!p[i]) continue;
if (s[i] < 0) {
int x = rig[-s[i]].back();
lef[-s[i]].pop_back();
rig[-s[i]].pop_back();
bit.modify(i, -1);
bit.modify(x, -1);
p[i] = p[x] = 0;
ret += bit.query(x);
} else {
int x = lef[s[i]].back();
lef[s[i]].pop_back();
rig[s[i]].pop_back();
bit.modify(i, -1);
bit.modify(x, -1);
p[i] = p[x] = 0;
ret += bit.query(x) + 1;
}
}
return ret;
}
/*
*/
# | 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... |