이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
class FenwickTree{
public:
int fensz;
vector<int> fen;
FenwickTree(int n){
fensz = n + 10;
fen.resize(fensz);
}
void add(int i, int k){
++i;
for(; i < fensz; i += (i & -i)) fen[i] += k;
}
int get(int i){
++i;
int ans = 0;
for(; i; i -= (i & -i)) ans += fen[i];
return ans;
}
};
long long count_swaps(vector<int> a){
int N = SZ(a);
const int MAXS = 2e5+100;
queue<int> ql[MAXS], qr[MAXS];
rep(i, 0, N){
if(a[i] < 0) ql[-a[i]].push(i);
else qr[a[i]].push(i);
}
FenwickTree fen(N);
vector<bool> used(N, false);
ll ans = 0;
rep(i, 0, N){
if(used[i]) continue;
used[i] = true;
if(a[i] < 0){
a[i] *= -1;
ql[a[i]].pop();
assert(!qr[a[i]].empty());
int p = qr[a[i]].front();
qr[a[i]].pop();
ans += ((p + fen.get(p)) - (i + fen.get(i))) - 1;
used[p] = true;
fen.add(p + 1, -1);
} else{
qr[a[i]].pop();
assert(!ql[a[i]].empty());
int p = ql[a[i]].front();
ql[a[i]].pop();
ans += ((p + fen.get(p)) - (i + fen.get(i)));
used[p] = true;
fen.add(p + 1, -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... |