이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "shoes.h"
using namespace std;
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
const long long inf = 1e18 + 7;
struct SEGT{
vector < int > t;
int tree_size;
const int identity_element = 0;
int merge(int x , int y){
return x + y;
}
void init(int x){
tree_size = x+3;
t.assign(2*tree_size , identity_element);
}
void modify(int p, int value) { // set value at position p
for (t[p += tree_size] = value; p > 1; p >>= 1){
t[p>>1] = merge(t[p] , t[p^1]);
}
}
int query(int l, int r) { // sum on interval [l, r]
int res = identity_element;
for (r+=1 , l += tree_size, r += tree_size; l < r; l >>= 1, r >>= 1) {
if (l&1) res = merge(res , t[l++]);
if (r&1) res = merge(res , t[--r]);
}
return res;
}
};
long long count_swaps(vector<int> s) {
int n = sz(s) / 2;
long long ans = 0;
SEGT segt;
segt.init(2*n+5);
map < int , queue < int > > mpa;
int vis[2*n];
memset(vis , 0 , sizeof(vis));
for(int i = 0;i<2*n;i++){
mpa[s[i]].push(i);
segt.modify(i , 1);
}
for(int i = 0;i<2*n;i++){
if(vis[i])continue;
int nxt = mpa[-s[i]].front();
// cout << i << " : " << nxt << endl;
mpa[s[i]].pop();
mpa[-s[i]].pop();
vis[i] = vis[nxt] = 1;
segt.modify(i , 0);
segt.modify(nxt , 0);
ans += segt.query(0 , nxt);
if(s[i] > 0)ans++;
}
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... |