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 <vector>
#include <map>
#include <algorithm>
#define N 100005
#define pb push_back
#define x first
#define y second
#define ls p<<1
#define rs p<<1|1
#define vi std::vector<int>
#define rep(i, a, b) for(int i = a; i < b; i++)
int t[N << 2];
void build(int p, int l, int r){
if(l == r){
t[p] = 1;
return;
}
int m = (l + r) >> 1;
build(ls, l, m);
build(rs, m+1, r);
t[p] = t[ls] + t[rs];
}
void upd(int p, int l, int r, int x, int v){
t[p] += v;
if(l == r){
return;
}
int m = (l+r) >> 1;
if(x <= m)upd(ls, l, m, x, v);
else upd(rs, m+1, r, x, v);
}
int query(int p, int l, int r, int x, int y){
if(x > y)return 0;
if(l >= x && r <= y)return t[p];
int m = (l+r) >> 1, ans = 0;
if(x <= m)ans = query(ls, l, m, x, y);
if(y > m)ans += query(rs, m+1, r, x, y);
return ans;
}
std::map<int, int>b[N];
long long count_swaps(std::vector<int> s) {
int n = s.size();
build(1, 0, n-1);
rep(i, 1, n+1)b[i].clear();
vi a;
rep(i, 0, n){
if(s[i] < 0)a.pb(i);
else b[s[i]][i] = 1;
}
std:reverse(a.begin(), a.end());
long long ans = 0;
for(auto &x : a){
int i = -s[x], v;
auto it = b[i].lower_bound(x);
if(it != b[i].end()){
int nw = 0;
while(it != b[i].end()){
int k = it->x, w = it->y;
v = query(1, 0, n-1, x+1, k-1);
ans += 1LL * v * w;
upd(1, 0, n-1, k, -w);
nw += w;
auto ne = it;
it++;
b[i].erase(ne);
}
if(nw){
b[i][x] = nw - 1;
}
upd(1, 0, n-1, x, nw);
}
else{
it--;
v = query(1, 0, n-1, it->x+1, x-1);
ans += v + 1;
upd(1, 0, n-1, it->x, -1);
upd(1, 0, n-1, x, 1);
b[i].erase(it);
}
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:53:5: warning: label 'std' defined but not used [-Wunused-label]
std:reverse(a.begin(), a.end());
^~~
# | 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... |