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 <bits/stdc++.h>
using namespace std;
// #define int long long
#define f0(i, n) for(int i = 0; i < n; i++)
#define f1(i, n) for(int i = 1; i <= n; i++)
bool compare(pair<long long, long long> p1, pair<long long, long long> p2) {
long long ind1 = p1.first + p1.second;
long long ind2 = p2.first + p2.second;
if(p1.first > p1.second) ind1++;
if(p2.first > p2.second) ind2++;
return ind1 < ind2;
}
class segment_tree {
private:
long long size, size_n;
vector<long long> tree;
public:
segment_tree(long long n, vector<long long> arr) {
long long base = 1;
while(base < 2*n) base *= 2;
size = base;
tree.resize(size, 0);
base /= 2;
size_n = base;
f0(i, n) tree[i + base] = arr[i];
for(long long i = base - 1; i >= 0; i--) {
tree[i] = tree[2*i] + tree[2*i + 1];
}
}
void update(long long ind, long long val) {
ind += size_n;
long long delta = val - tree[ind];
while(ind > 0) {
tree[ind] += delta;
ind /= 2;
}
}
long long query(long long l, long long r) {
l += size_n;
r += size_n;
long long res = 0;
while( l <= r) {
if(l % 2 == 1) {
res += tree[l];
l++;
}
if(r % 2 == 0) {
res += tree[r];
r--;
}
l /= 2;
r /= 2;
}
return res;
}
};
long long count_swaps(std::vector<int> s) {
long long n = s.size()/2;
vector<pair<long long, long long>> pp;
vector<long long> v[n+1][2];
f0(i, 2*n) {
if(s[i] < 0) {
v[-s[i]][0].push_back(i);
} else {
v[s[i]][1].push_back(i);
}
}
f1(i, n) {
f0(j, v[i][0].size()) {
pp.push_back({v[i][0][j], v[i][1][j]});
}
}
sort(pp.begin(), pp.end(), compare);
long long ans[2*n];
long long ans2[2*n];
f0(i, n) {
ans[2*i] = pp[i].first;
ans[2*i + 1] = pp[i].second;
ans2[pp[i].first] = 2*i;
ans2[pp[i].second] = 2*i + 1;
// ans += abs(pp[i].first - 2*i);
// ans += abs(pp[i].second - 2*i - 1);
// cout << pp[i].first << " " << 2*i << " " << pp[i].second << " " << 2*i + 1 << endl;
}
segment_tree st(2*n, vector<long long>(2*n, 0));
long long ret = 0;
f0(i, 2*n) {
ret += ans2[i];
ret -= st.query(0, ans2[i]);
st.update(ans2[i], 1);
}
return ret;
// return ans;
return 1;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:8:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define f0(i, n) for(int i = 0; i < n; i++)
......
80 | f0(j, v[i][0].size()) {
| ~~~~~~~~~~~~~~~~~
shoes.cpp:80:3: note: in expansion of macro 'f0'
80 | f0(j, v[i][0].size()) {
| ^~
shoes.cpp:87:12: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
87 | long long ans[2*n];
| ^~~
# | 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... |