이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll mod=1e9+7;
const ll maxn=2e5+5;
#define ok ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fi first
#define se second
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define endl '\n'
int n, fen[maxn], sm, pos, cnt;
queue<int> qul[maxn], qur[maxn];
bool use[maxn];
void add(int bit) {
for(; bit<=n; bit+=bit&(-bit)) fen[bit]++;
}
int get(int bit) {
int res=0;
for(; bit>0; bit-=bit&(-bit)) res+=fen[bit];
return res;
}
ll count_swaps(vector<int> v) {
n=(int)v.size();
ll ans=0;
for(int i=0; i<n; i++) {
if(v[i]<0) qul[-1*v[i]].push(i+1);
else qur[v[i]].push(i+1);
}
for(int i=0; i<n; i++) {
if(use[i+1]) continue;
if(v[i]<0) {
sm=-1*v[i];
pos=qur[sm].front();
cnt=get(pos)-get(i+1);
qur[sm].pop();
qul[sm].pop();
int range=pos-(i+1)-1-cnt;
ans+=range;
add(pos);
use[pos]=1;
} else {
pos=qul[v[i]].front();
cnt=get(pos)-get(i+1);
qur[v[i]].pop();
qul[v[i]].pop();
int range=pos-(i+1)-cnt;
ans+=range;
add(pos);
use[pos]=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... |