이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define ll long long
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 200000;
int aib[1 + nmax];
void update(int x, int lim, int val){
while(x <= lim){
aib[x] += val;
x += (x ^ (x & (x - 1)));
}
}
int query(int x){
int result = 0;
while(0 < x){
result += aib[x];
x = (x & (x - 1));
}
return result;
}
map<int,vector<int>> frec;
ll count_swaps(vector<int> v){
int n = v.size();
for(int i = 1;i <= n; i++)
update(i, n, 1);
for(int i = n; 1 <= i; i--)
frec[v[i - 1]].push_back(i);
ll result = 0;
for(int i = 1;i <= n; i++){
if(query(i) - query(i - 1) == 1){
int val = v[i - 1], pos = frec[-val].back();
frec[val].pop_back();
frec[-val].pop_back();
update(i, n, -1);
update(pos, n, -1);
if(val < 0)
result += query(pos) - query(i);
else
result += query(pos) - query(i) + 1;
}
}
return result;
}
# | 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... |