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 <vector>
#include <cstdlib>
#include <cstdio>
#define SZ (1<<18)
using namespace std;
int N, st[1<<19], claimed[1<<18];
vector<int> pos[1<<19];
void seg_set(int i, int v) {
for (st[i += SZ] = v; i > 1; i >>= 1) {
st[i>>1] = st[i] + st[i^1];
}
}
int seg_sum(int l, int r) {
int res = 0;
for (l += SZ, r += SZ; l < r; l >>= 1, r >>= 1) {
if (l&1) res += st[l++];
if (r&1) res += st[--r];
}
return res;
}
long long count_swaps(vector<int> s) {
long long ans = 0;
N = (int)s.size() / 2;
for (int i = s.size() - 1; i >= 0; i--) {
pos[s[i] + SZ].push_back(i);
seg_set(i, 1);
}
for (int i = 0; i < s.size(); i++) {
if (!claimed[i]) {
pos[s[i]+SZ].pop_back();
if (s[i] < 0) {
int otherpos = pos[-s[i]+SZ].back(); pos[-s[i]+SZ].pop_back();
ans = (ans + seg_sum(i+1, otherpos));
seg_set(otherpos, 0);
claimed[otherpos] = 1;
} else {
int otherpos = pos[-s[i]+SZ].back(); pos[-s[i]+SZ].pop_back();
ans = (ans + seg_sum(i, otherpos));
seg_set(otherpos, 0);
claimed[otherpos] = 1;
}
}
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:32:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < s.size(); 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... |