이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "shoes.h"
using namespace std;
#define ll long long
#define endl '\n'
const int N = 2e5 + 5;
vector<ll> node[100005];
ll a[200005];
queue<ll> q[200005];
ll st[N * 4], lazy[N * 4];
void down(ll k){
st[k << 1] += lazy[k];
st[k << 1 | 1] += lazy[k];
lazy[k << 1] += lazy[k];
lazy[k << 1 | 1] += lazy[k];
lazy[k] = 0;
}
void upd(ll k, ll l, ll r, ll x, ll y){
if (l != r && lazy[k]) down(k);
if (x > r || y < l) return;
if (x <= l && y >= r){
st[k]++;
lazy[k] = 1;
return;
}
ll mid = (l + r) >> 1;
upd(k << 1, l, mid, x, y);
upd(k << 1 | 1, mid + 1, r, x, y);
}
ll get(ll k, ll l, ll r, ll x){
if (x > r || x < l) return 0;
if (l != r && lazy[k]) down(k);
if (x <= l && x >= r) return st[k];
ll mid = (l + r) >> 1;
return max(get(k << 1, l, mid, x), get(k << 1 | 1, mid + 1, r, x));
}
ll count_swaps(vector<int> s){
ll ans = 0;
int n = s.size();
for (int i = 0; i < n; i++){
if (q[n / 2 - s[i]].size()){
ll id = q[n / 2 - s[i]].front();
q[n / 2 - s[i]].pop();
ll up = id + get(1, 1, n, id + 1);
if (s[i] > 0){
ans = ans + i - up - 1;
} else {
ans = ans + i - up;
}
upd(1, 1, n, id + 1, i + 1);
} else {
q[s[i] + n / 2].push(i);
}
}
return ans;
}
/*
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("VietCT.INP", "r")){
freopen("VietCT.INP", "r", stdin);
freopen("VietCT.OUT", "w", stdout);
}
ll n;
cin >> n;
vector<int> mem;
for (int i = 1; i <= (2 * n); i++){
ll u;
cin >> u;
mem.push_back(u);
}
cout << count_swaps(mem);
}
*/
/*
2
-2 -2 2 2
*/
# | 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... |