이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 4e5 + 545 , inf = 1e9 + 12 , mod = 1e9 + 7;
#include "shoes.h"
struct segtree {
ll tree[N*3] ;
void built(int v,int l,int r) {
if(l == r) {
tree[v] = 1;
return;
}
int mid = (l + r)/2;
built(v*2 , l , mid);
built(v*2 + 1 , mid+1 , r);
tree[v] = tree[v*2] + tree[v*2 + 1];
}
void update(int v,int l,int r,int ml,int val) {
if(r < ml || ml < l || l > r) {
return;
}
if(l == r && ml == l) {
tree[v] += val;
return;
}
int mid = (l + r)/2;
update(v , l , mid , ml , val);
update(v , mid + 1 , r , ml , val);
tree[v] = tree[v*2] + tree[v*2 + 1];
}
ll get(int v,int l,int r,int ml,int mr) {
if(r < ml || mr < l) {
return 0;
}
if(ml <= l && r <= mr) {
return tree[v];
}
int mid = (l + r)/2;
return (get(v , l , mid , ml , mr) + get(v , mid + 1 , r , ml ,mr));
}
};
segtree S;
ll count_swaps(vector<int> v) {
int n = ((int)v.size())/2;
set<pair<ll , ll>> s;
v.push_back(0LL);
for(int i = 2*n;i>=1;i--){
v[i] = v[i-1];
s.insert({v[i] , i});
}
S.built(1,1,2*n);
ll res = 0;
for(int i=1;i<=n;i++) {
if(s.find({v[i] , i}) != s.end()){
auto it = s.lower_bound({-v[i] , i});
if((*it).second - 2 >= i) {
res += S.get(1,1,2*n,i + 1,(*it).second-1);
}
if(v[i] > 0) {
res++;
}
S.update(1 , 1 , 2*n , i , -1);
S.update(1 , 1 , 2*n , (*it).second, -1);
if(s.find({v[i], i}) != s.end()){
s.erase(s.find({v[i], i}));
}
if(s.find({-v[i], (*it).second}) != s.end()){
s.erase(s.find({-v[i], (*it).second}));
}
}
}
return res;
}
# | 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... |