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 = 2e5 + 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();
priority_queue<int> g[(n / 2) + 1][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()){
int y = -g[x][1 - t].top();
g[x][1 - t].pop();
a.push_back({y, i});
int dist = i - y - 1 + (s[y] > s[i]);
upd(1, 1, n, y + 1, y + 1, dist);
upd(1, 1, n, i + 1, i + 1, dist);
}
else{
g[x][t].push(-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... |