This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#define NDEBUG
#include <bits/stdc++.h>
#include <bits/extc++.h>
#include "shoes.h"
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define pob pop_back()
#define pof pop_front()
#define F first
#define S second
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
#define pii pair<int, int>
#define pll pair<ll, ll>
#define tiii tuple<int, int, int>
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) ((a) / (b) + !!((a) % (b)))
//#define TEST
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
using namespace __gnu_pbds;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream &operator<<(ostream &o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
vector<ll> BIT;
int lowbit(int x){
return x & -x;
}
void modify(int x, int v){
for(; x < BIT.size(); x += lowbit(x)) BIT[x] += v;
}
ll query(int x){
if(x == 0) return 0;
ll ans = 0;
for(; x > 0; x -= lowbit(x)) ans += BIT[x];
return ans;
}
ll query(int l, int r){
if(r < l) return 0;
return query(r) - query(l - 1);
}
ll count_swaps(vector<int> s){
int n = s.size();
s.insert(s.begin(), 0);
BIT.resize(n + 1);
for(int i = 1; i <= n; i++) modify(i, 1);
ll ans = 0;
unordered_map<ll, queue<int>> pos;
for(int i = 1; i <= n; i++){
pos[s[i]].push(i);
}
for(int i = 1; i <= n; i++){
if(pos[s[i]].empty() || pos[s[i]].front() != i) continue;
int t = pos[-s[i]].front();
pos[s[i]].pop();
pos[-s[i]].pop();
ans += query(i + 1, t - 1);
if(s[i] > 0) ans++;
modify(i, -1);
modify(t, -1);
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'void modify(int, int)':
shoes.cpp:57:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; x < BIT.size(); x += lowbit(x)) BIT[x] += v;
~~^~~~~~~~~~~~
# | 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... |