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 int long long
#define P(x) do {if(debug) cout << x << endl;} while(false)
#define H(x) P(#x << ": " << x)
#define FR(i, a, b) for(int i = (a); i < (b); ++i)
#define F(i, n) FR(i, 0, n)
#define DR(i, a, b) for(int i = (b); i --> (a);)
#define D(i, n) DR(i, 0, n)
#define S(s) (int)(s).size()
#define ALL(x) (x).begin(), (x).end()
#define MI(x, a) (x) = min(x, a)
#define MA(x, a) (x) = max(x, a)
#define V vector
#define pb push_back
#define mp make_pair
using namespace std;
const bool debug = 0;
//const int inf = 1e18;
struct ftree{
vector<int> tree;
int n;
ftree(int _n){
tree.resize(_n+1, 0);
n = _n;
}
int query(int i){
int res = 0;
for(int j = i; j>0; j-=(j&-j)){
res+=tree[j];
}
//res+=ftree[0];µ
return res;
}
void update(int i, int change){
//int change = v-(query(i+1)+query(i));
for(int j = i+1; j<tree.size(); j+=(j&-j)){
tree[j]+=change;
}
}
};
long long count_swaps(vector<int> S){
int n = S.size()/2;
//cout<<"AAA";
vector<vector<int> > pos(2*n+5);
for(int i = 2*n-1; i>=0; i--){
H(S[i]);
H(S[i]+n);
pos[S[i]+n].push_back(i);
}
ftree ft(2*n);
vector<bool> vis(2*n, 0);
int cnt = 0;
//cout<<"blib"<<endl;
for(int i = 0; i < 2*n; i++){
if(vis[i]) continue;
int x = pos[n-S[i]].back();
pos[n-S[i]].pop_back();
pos[S[i]+n].pop_back();
H(i);
H(x);
H( x - i - ((S[i]<0)?1:0));
cnt += x - i - ((S[i]<0)?1:0);
H(cnt);
cnt -= ft.query(x) - ft.query(i+1);
H(cnt);
ft.update(x, 1);
vis[x]=true;
}
return cnt;
}
Compilation message (stderr)
shoes.cpp: In member function 'void ftree::update(int, int)':
shoes.cpp:38:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = i+1; j<tree.size(); j+=(j&-j)){
~^~~~~~~~~~~~
# | 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... |