이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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(std::vector<int> s) {
int n = (int)s.size();
upd(1, 1, n, 1, n, 1);
vector<int> g[n][2];
vector<pair<pair<int, 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].back();
a.push_back({{y, i}, (s[y] > s[i])});
g[x][1 - t].pop_back();
}
else{
g[x][t].push_back(i);
}
}
sort(a.rbegin(), a.rend(), [](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y){
int dist1 = x.first.second - x.first.first;
int dist2 = y.first.second - y.first.first;
if(dist1 == dist2){
return (x.first.first < y.first.first);
}
return (dist1 < dist2);
});
long long res = 0;
while(!a.empty()){
pair<pair<int, int>, int> cur = a.back();
a.pop_back();
int l = cur.first.first;
int r = cur.first.second;
if(l > r) swap(l, r);
int ans = get(1, 1, n, l + 1, r + 1);
upd(1, 1, n, l + 2, r, -1);
res += ans - 2;
res += cur.second;
}
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... |