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;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<ld, ld>;
#define mp make_pair
#define ff first
#define ss second
#define ar array
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<ld>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;
#define sz(x) (int)((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for(int i = (b) - 1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define trav(a, x) for(auto& a : x)
template<class T> bool ckmin(T& a, const T& b) { return (b < a ? a = b, 1 : 0); }
template<class T> bool ckmax(T& a, const T& b) { return (b > a ? a = b, 1 : 0); }
const int maxn = 2e5 + 3;
int st[2][maxn];
void add(bool f, int ind, int delta) {
++ind;
while(ind < maxn) {
st[f][ind] += delta;
ind += ind & -ind;
}
}
int val(bool f, int ind) {
++ind;
int ans = 0;
while(ind > 0) {
ans += st[f][ind];
ind -= ind & -ind;
}
return ans;
}
ll count_swaps(vi S) {
int N = sz(S) >> 1;
{
map<int, int> c;
trav(u, S) c[abs(u)];
int tim3 = 1; trav(u, c) u.ss = tim3++;
trav(u, S) u = c[abs(u)] * (u < 0 ? -1 : 1);
}
vi rs;
vi ls;
F0R(i, 2 * N) {
if(S[i] > 0) rs.pb(i);
else ls.pb(i);
}
add(0, 0, ls[0]);
add(1, 0, rs[0]);
FOR(i, 1, sz(ls)) {
add(0, i, ls[i] - ls[i - 1]);
}
FOR(i, 1, sz(rs)) {
add(1, i, rs[i] - rs[i - 1]);
}
ll ans = 0;
R0F(i, 2 * N) {
if(i & 1) {
int ind = val(1, sz(rs) - 1);
rs.pop_back();
if(i == ind) continue;
ans += i - ind;
int tl = 0, tr = sz(ls) - 1;
while(tl < tr) {
int tm = (tl + tr) / 2;
if(val(0, tm) < ind) {
tl = tm + 1;
} else {
tr = tm;
}
}
add(0, tl, -1);
} else {
int ind = val(0, sz(ls) - 1);
ls.pop_back();
if(i == ind) continue;
ans += i - ind;
int tl = 0, tr = sz(rs) - 1;
while(tl < tr) {
int tm = (tl + tr) / 2;
if(val(1, tm) < ind) {
tl = tm + 1;
} else {
tr = tm;
}
}
add(1, tl, -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... |