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 REP(v, i, j) for (long long v = i; v < j; v++)
#define FORI(v) for (auto i : v)
#define FORJ(v) for (auto j : v)
#define OUT(v, a) FORI(v) cout << i << a;
#define OUTS(v, a, b) cout << v.size() << a; OUT(v, b)
#define in(a, n) REP(i, 0, n) cin >> a[i];
#define SORT(v) sort(begin(v), end(v))
#define REV(v) reverse(begin(v), end(v))
#define pb push_back
#define fi first
#define se second
#define detachIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef pair<long long, long long> pii;
typedef pair<pii, long long> piii;
typedef pair<pii, pii> piiii;
const long long MAXN = 200100;
long long tree[MAXN * 4];
void update(long long l, long long r, long long idx, long long pos, long long val)
{
if (l == r)
{
tree[idx] += val;
return;
}
long long m = (l + r) / 2;
if (pos <= m)
update(l, m, idx * 2, pos, val);
else
update(m + 1, r, idx * 2 + 1, pos, val);
tree[idx] = tree[idx * 2] + tree[idx * 2 + 1];
}
long long get(long long l, long long r, long long idx, long long start, long long end)
{
if (r < start || l > end)
return 0;
if (start <= l && r <= end)
return tree[idx];
long long m = (l + r) / 2;
long long l1 = get(l, m, idx * 2, start, end);
long long l2 = get(m + 1, r, idx * 2 + 1, start, end);
return l1 + l2;
}
queue<long long> mp[300000];
long long count_swaps(vector<int> v)
{
memset(tree, 0, sizeof tree);
long long ans = 0;
REP(i, 0, v.size())
{
update(0, v.size() - 1, 1, i, 1);
if (mp[v[i] + 110000].size())
{
auto &q = mp[v[i] + 110000];
auto x = q.front();
ans += i - x;
if ((x - i) != 1)
ans -= get(0, v.size() - 1, 1, x + 1, i - 1);
if (v[i] > 0)
ans--;
update(0, v.size() - 1, 1, i, -1);
update(0, v.size() - 1, 1, x, -1);
q.pop();
}
else
mp[-v[i] + 110000].push(i);
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:2:46: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
2 | #define REP(v, i, j) for (long long v = i; v < j; v++)
......
66 | REP(i, 0, v.size())
| ~~~~~~~~~~~~~~
shoes.cpp:66:5: note: in expansion of macro 'REP'
66 | REP(i, 0, v.size())
| ^~~
# | 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... |