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 ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int N = 2e5 + 1;
const string NAME = "";
const int lim = 2147483647;
//const unsigned int lim = 4294967295;
//const long long lim = 9223372036854775807;
//const unsigned long long lim = 18446744073709551615;
const int mset = 0x3f;
const double pi = acos(-1);
int n;
vector<int> q[2][N];
int perm[N];
long long tree[4 * N];
bool used[N];
void update(int s, int l, int r, int x, int y){
if(x < l || x > r)
return;
if(l == r){
tree[s] += y;
return;
}
int mid = (l + r) / 2;
update(2 * s, l, mid, x, y);
update(2 * s + 1, mid + 1, r, x, y);
tree[s] = tree[2 * s] + tree[2 * s + 1];
}
long long get(int s, int l, int r, int u, int v){
if(l > v || r < u)
return 0;
if(l >= u && r <= v)
return tree[s];
int mid = (l + r) / 2;
return get(2 * s, l, mid, u, v) + get(2 * s + 1, mid + 1, r, u, v);
}
long long count_swaps(vector<int> S){
n = S.size() / 2;
reverse(S.begin(), S.end());
S.pb(0);
reverse(S.begin(), S.end());
for(int i = 1; i <= 2 * n; ++i){
if(S[i] > 0)
q[1][S[i]].pb(i);
else q[0][-S[i]].pb(i);
}
for(int i = 1; i <= n; ++i){
reverse(q[0][i].begin(), q[0][i].end());
reverse(q[1][i].begin(), q[1][i].end());
}
int cnt = 0;
for(int i = 1; i <= 2 * n; ++i){
if(used[i])
continue;
if(S[i] > 0){
int t0 = q[0][S[i]].back();
int t1 = q[1][S[i]].back();
q[1][S[i]].pop_back();
q[0][S[i]].pop_back();
used[t0] = true;
used[t1] = true;
perm[t0] = ++cnt;
perm[t1] = ++cnt;
}
else{
int t0 = q[0][-S[i]].back();
int t1 = q[1][-S[i]].back();
q[1][-S[i]].pop_back();
q[0][-S[i]].pop_back();
used[t0] = true;
used[t1] = true;
perm[t0] = ++cnt;
perm[t1] = ++cnt;
}
}
long long ans = 0;
for(int i = 1; i <= 2 * n; ++i){
ans += get(1, 1, 2 * n, perm[i] + 1, 2 * n);
update(1, 1, 2 * n, perm[i], 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... |