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 "shoes.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
using namespace std;
#define ll long long
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<stack<int>>
struct segmentTree {
int n;
vi L;
void set_n(int a) {
n = a;
L.resize(4*n, 0);
}
void update(int p, int l, int r, int x, int v) {
if (l > x || r < x) return;
if (l == r) {
L[p] = v;
return;
}
int m = (l+r)/2;
update(p*2, l, m, x, v);
update(p*2+1, m+1, r, x, v);
L[p] = L[p*2] + L[p*2+1];
}
int query(int p, int l, int r, int x, int y) {
//cerr << p << " " << l << " " << r << " " << x << " " << y << endl;
if (l > y || r < x) return 0;
if (x <= l && r <= y) {
return L[p];
}
int m = (l+r)/2;
return query(p*2, l, m, x, y) + query(p*2+1, m+1, r, x, y);
}
};
ll count_swaps(vector<int> S) {
//cerr << endl;
int n = S.size();
vs L(n*2+5);
for (int i = n-1; i > -1; i--) L[S[i]+n].push(i);
segmentTree T;
T.set_n(n+5);
ll t = 0;
int p = 0;
while (p < n) {
//cerr << p << " " << t << endl;
L[S[p]+n].pop();
if (T.query(1, 0, n, p, p)) {
//cerr << "oink" << endl;
p++; continue;
}
//cerr << "oink" << endl;
int a = L[S[p]*(-1)+n].top();
t += a-p-T.query(1, 0, n, p, a);
if (S[p] < 0) t--;
T.update(1, 0, n, a, 1);
p++;
}
return t;
}
# | 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... |