이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+7;
ll tree[N*4], lz[N*4];
void push(int v, int l, int r){
if(l > r) return;
if(lz[v]){
tree[v] += ll(r-l+1)*lz[v];
if(l != r){
lz[v<<1] += lz[v];
lz[(v<<1)+1] += lz[v];
}
lz[v] = 0;
}
}
ll get(int v, int l, int r, int ql, int qr){
push(v, l, r);
if(l > qr || r < ql) return 0;
if(ql <= l && r <= qr) return tree[v];
int mid = (l+r)>>1;
return get(v<<1, l, mid, ql, qr)+get((v<<1)+1, mid+1, r, ql, qr);
}
void update(int v, int l, int r, int ql, int qr){
push(v, l, r);
if(l > qr || r < ql) return;
if(ql <= l && r <= qr){
tree[v] += ll(r-l+1);
if(l != r){
lz[v<<1]++;
lz[(v<<1)+1]++;
}
return;
}
int mid = (l+r)>>1;
update(v<<1, l, mid, ql, qr);
update((v<<1)+1, mid+1, r, ql, qr);
tree[v] = tree[v<<1]+tree[(v<<1)+1];
}
/*
ll count_swaps(vector <int> s);
int main() {
int n;
assert(1 == scanf("%d", &n));
vector<int> S(2 * n);
for (int i = 0; i < 2 * n; i++)
assert(1 == scanf("%d", &S[i]));
fclose(stdin);
long long result = count_swaps(S);
printf("%lld\n", result);
fclose(stdout);
return 0;
}
*/
ll count_swaps(vector<int> s){
int n = s.size(), i;
vector <pair <int, int>> order[N];
for(i = 0; i < n; i++){
order[abs(s[i])].emplace_back(make_pair(s[i], i+1));
}
ll ans = 0;
vector <pair<int, int>> qu;
for(i = 1; i <= n/2; i++){
sort(order[abs(i)].begin(), order[abs(i)].end());
for(int j = 0; j < (int)order[i].size()/2; j++){
int l = order[i][j].second;
int r = order[i][j+(int)order[i].size()/2].second;
if(l > r){
swap(l, r);
}
else{
ans--;
}
qu.push_back(make_pair(l, r));
}
}
sort(qu.begin(), qu.end());
for(auto to : qu){
int l = to.first, r = to.second;
ans += (r+get(1, 1, n, r, r))-(l+get(1, 1, n, l, l));
if(l+1 != r){
update(1, 1, n, l+1, r-1);
}
}
return ans;
}
# | 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... |