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>
#include "shoes.h"
using namespace std;
const int N = 1e5 + 7;
int t[4 * N];
int lz[4 * N];
void push(int v, int tl, int tr){
if(lz[v] == 0) return;
int tm = (tl + tr) / 2;
t[v * 2] += (tm - tl + 1) * lz[v];
t[v * 2 + 1] += (tr - tm) * lz[v];
lz[v * 2] += lz[v];
lz[v * 2 + 1] += lz[v];
lz[v] = 0;
}
void upd(int v, int tl, int tr, int l, int r, int x){
if(l > r) return;
if(tl == l && tr == r){
t[v] += (tr - tl + 1) * x;
lz[v] += x;
return;
}
push(v, tl, tr);
int tm = (tl + tr) / 2;
upd(v * 2, tl, tm, l, min(tm, r), x);
upd(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r, x);
t[v] = t[v * 2] + t[v * 2 + 1];
}
int get(int v, int tl, int tr, int l, int r){
if(l > r) return 0;
if(tl == l && tr == r){
return t[v];
}
push(v, tl, tr);
int tm = (tl + tr) / 2;
return (get(v * 2, tl, tm, l, min(tm, r)) +
get(v * 2 + 1, tm + 1, tr, max(tm + 1, l), r));
}
long long count_swaps(vector<int> s) {
int n = (int)s.size();
vector<int> g[n][2];
vector<pair<int, int>> a;
for(int i = 0; i < (int)s.size(); i ++){
int t = (s[i] > 0);
int x = abs(s[i]);
if(!g[x][1 - t].empty()){
auto it = g[x][1 - t].begin();
a.push_back({*it, i});
int dist = i - *it - 1 + (s[*it] > s[i]);
upd(1, 1, n, *it + 1, *it + 1, dist);
upd(1, 1, n, i + 1, i + 1, dist);
g[x][1 - t].erase(it);
}
else{
g[x][t].push_back(i);
}
}
sort(a.rbegin(), a.rend(), [](pair<int, int> x, pair<int, int> y){
int dist1 = x.second - x.first;
int dist2 = y.second - y.first;
if(dist1 == dist2){
return (x.first < y.first);
}
return (dist1 < dist2);
});
long long res = 0;
while(!a.empty()){
pair<int, int> cur = a.back();
a.pop_back();
int l = cur.first + 1;
int r = cur.second + 1;
res += max(0, min(get(1, 1, n, l, l), get(1, 1, n, r, r)));
upd(1, 1, n, l, r, -1);
}
return res;
}
# | 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... |