이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 12, mod = 998244353, inf = 1e9 + 12 ;
long long L[maxn * 4], R[maxn * 4], sum[maxn * 4];
bitset <maxn> marked;
vector <int> locs[maxn][2];
void make_tree(int l, int r, int ind){
int mid = (l + r) / 2;
L[ind] = l;
R[ind] = r;
if(l == r)
return;
make_tree(l, mid, ind * 2);
make_tree(mid + 1, r, ind * 2 + 1);
}
void update_tree(int l, int r, int u){
if(r < L[u] || R[u] < l)
return;
if(l <= L[u] && R[u] <= r){
sum[u] = 1;
return;
}
update_tree(l, r, u * 2);
update_tree(l, r, u * 2 + 1);
sum[u] = (sum[u * 2] + sum[u * 2 + 1]);
}
int get_sum(int l, int r, int u){
if(r < L[u] || R[u] < l)
return 0;
if(l <= L[u] && R[u] <= r){
return sum[u];
}
return get_sum(l, r, u * 2) + get_sum(l, r, u * 2 + 1);
}
long long count_swaps(vector <int> s){
int n = s.size();
make_tree(0, n, 1);
for(int i = 0; i < n; i++)
locs[abs(s[i])][(s[i] < 0)].push_back(i);
n = n / 2;
for(int i = 1; i <= n; i++){
reverse(locs[i][0].begin(), locs[i][0].end());
reverse(locs[i][1].begin(), locs[i][1].end());
}
long long nat = 0, k = 0;
// cout << locs[1][0][0] << ' ' << locs[1][1][0] << endl;
for(int i = 0; i < n * 2; i++){
if(marked[i])
continue;
int now = abs(s[i]);
if(locs[now][0].back() > locs[now][1].back())
nat -= 1;
nat += abs(locs[now][0].back() - locs[now][1].back());
int mn = min(locs[now][0].back(), locs[now][1].back()), mx = max(locs[now][0].back(), locs[now][1].back());
nat -= get_sum(mn, mx, 1);
update_tree(mx, mx, 1);
locs[now][0].pop_back();
locs[now][1].pop_back();
// cout << nat << endl;
marked[mx] = 1;
marked[mn] = 1;
}
return nat;
}
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:51:24: warning: unused variable 'k' [-Wunused-variable]
51 | long long nat = 0, k = 0;
| ^
# | 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... |