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 all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define ff first
#define ss second
#define P 1000000007
#define in(x, a, b) (a <= x && x < b)
using namespace std;
using ll = long long;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
const ll inf = 1000000001, INF = (ll)1e18 + 1;
void upd(vi &t, int pos, int val) {
for(int i = pos + 1; i <= t.size(); i += (i&-i)) {
t[i - 1] += val;
}
}
int get(vi &t, int pos) {
int sum = 0;
for(int i = pos + 1; i > 0; i -= (i&-i)) {
sum += t[i - 1];
}
return sum;
}
ll count_swaps(vi a) {
int n = a.size();
vi t(n, 0);
for(int i = 0; i < n; i++) upd(t, i, 1);
vi nxt(n);
vvi u(n / 2 + 10, vi(2, -1));
for(int i = n - 1; i >= 0; i--) {
nxt[i] = u[abs(a[i])][(a[i] < 0 ? 0 : 1)];
u[abs(a[i])][(a[i] < 0 ? 1 : 0)] = i;
}
ll ans = 0;
for(int i = 0; i < n; i++) {
if(nxt[i] == -1 || !(get(t, i) - get(t, i - 1))) continue;
if(a[i] < 0) ans += get(t, nxt[i]) - get(t, i) - 1;
else ans += get(t, nxt[i]) - get(t, i);
upd(t, nxt[i], -1);
}
return ans;
}
//int main(){
// ios_base::sync_with_stdio(false);
// int n;
// cin >> n;
//
// vi a(n);
// for(int i = 0; i < n; i++) cin >> a[i];
//
// cout << count_swaps(a) << endl;
//
// return 0;
//}
Compilation message (stderr)
shoes.cpp: In function 'void upd(vi&, int, int)':
shoes.cpp:23:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = pos + 1; i <= t.size(); i += (i&-i)) {
~~^~~~~~~~~~~
# | 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... |