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>
using namespace std;
/*
John Watson
https://codeforces.com/profile/quangminh98
Mua Code nhu mua Florentino !!
*/
#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
struct FenwickTree
{
int n;
vector<int> bits;
FenwickTree(int n) : n(n) { bits.resize(n + 5, 0); }
void update(int pos, int val)
{
for (pos; pos <= n; pos += (pos & (-pos)))
bits[pos] += val;
}
int query(int pos)
{
int ans = 0;
for (pos; pos > 0; pos -= (pos & (-pos)))
ans += bits[pos];
return ans;
}
int query(int u, int v) { return (query(v) - query(u - 1)); }
};
ll count_swaps(vector<int> s)
{
int n = s.size();
unordered_map<int, vector<int>> pos, neg;
for (int i = n - 1; i >= 0; i--)
{
if (s[i] < 0)
neg[s[i]].push_back(i);
else
pos[s[i]].push_back(i);
}
FenwickTree bit(n);
for (int i = 0; i < n; i++)
bit.update(i + 1, 1);
int res = 0;
vector<int> mark(n + 5, 1);
for (int i = 0; i < n; i++)
{
if (mark[i] == 0) continue;
mark[i] = 0;
if (s[i] < 0)
{
neg[s[i]].pop_back();
int j = pos[-s[i]].back();
pos[-s[i]].pop_back();
mark[j] = 0;
res += bit.query(i + 2, j + 1) - 1;
bit.update(i + 1, -1);
bit.update(j + 1, -1);
} else
{
pos[s[i]].pop_back();
int j = neg[-s[i]].back();
neg[-s[i]].pop_back();
mark[j] = 0;
res += bit.query(i + 1, j + 1) - 1;
bit.update(i + 1, -1);
bit.update(j + 1, -1);
}
}
cout << res << '\n';
}
Compilation message (stderr)
shoes.cpp: In member function 'void FenwickTree::update(int, int)':
shoes.cpp:23:8: warning: statement has no effect [-Wunused-value]
23 | for (pos; pos <= n; pos += (pos & (-pos)))
| ^~~
shoes.cpp: In member function 'int FenwickTree::query(int)':
shoes.cpp:30:8: warning: statement has no effect [-Wunused-value]
30 | for (pos; pos > 0; pos -= (pos & (-pos)))
| ^~~
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:83:1: warning: no return statement in function returning non-void [-Wreturn-type]
83 | }
| ^
# | 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... |