이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include "shoes.h"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<bool> vb;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
template <typename T, typename U> std::istream&operator>>(std::istream&i, pair<T,U>&p) {i >> p.x >> p.y; return i;}
template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template <typename T, typename U> std::ostream&operator<<(std::ostream&o, const pair<T,U>&p) {o << p.x << ' ' << p.y; return o;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {if(t.empty())o<<'\n';for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
#define deb(x) cout << '>' << #x << ':' << x << endl;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define END '\n'
#define inf 9e18
#define ff first
#define ss second
#define pb push_back
struct SegTree {
int n = 1;
vi st;
SegTree (int sz) {
while(n < sz) n <<= 1;
st.resize(n << 1, 0);
}
void add(int pos, int x, int lx, int rx) {
if (rx - lx == 1) {
st[x] = 1;
return;
}
int mid = (lx + rx) >> 1, a = (x << 1) | 1, b = (x << 1) + 2;
if (pos < mid) add(pos, a, lx, mid);
else add(pos, b, mid, rx);
st[x] = st[a] + st[b];
}
void add(int pos) {
add(pos, 0, 0, n);
}
int query(int l, int r, int x, int lx, int rx) {
if (rx <= l || lx >= r) return 0;
if (lx >= l && rx <= r) return st[x];
int mid = (lx + rx) >> 1, a = (x << 1) | 1, b = (x << 1) + 2;
return query(l, r, a, lx, mid) + query(l, r, b, mid, rx);
}
int query(int l, int r) {
return query(l, r, 0, 0, n);
}
};
long long count_swaps(std::vector<int> s) {
int n = s.size();
vector<set<int>> A(n + 1), B(n + 1);
vi done(n, 0);
SegTree st(n);
for (int i = 0; i < n; ++i) {
if (s[i] > 0) A[s[i]].insert(i);
else B[-s[i]].insert(i);
}
int idx = 0;
ll ans = 0;
for (int i = 0; i < n; ++i) {
int nxt;
if (done[i]) {
continue;
}
if (s[i] > 0) {
A[s[i]].erase(i);
nxt = *B[s[i]].begin();
B[s[i]].erase(B[s[i]].begin());
} else {
B[-s[i]].erase(i);
nxt = *A[-s[i]].begin();
A[-s[i]].erase(A[-s[i]].begin());
}
// cout << i << " " << s[i] << " " << idx << " " << nxt << " " << get_off(done, n, nxt) << END;
ans += (nxt + st.query(nxt, n)) - idx - 1;
done[nxt] = 1;
st.add(nxt);
if (s[i] > 0) ++ans;
idx += 2;
// cout << ans << END;
}
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... |