이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "shoes.h"
#else
#include "grader.cpp"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 2e5 + 5;
int n;
ll ans;
deque<int> p[SIZE];
int a[SIZE], to[SIZE];
int bit[SIZE];
void upd(int pos, int x) {
for (; pos <= 2 * n; pos += pos & -pos) bit[pos] += x;
}
int que(int pos) {
int re = 0;
for (; pos; pos -= pos & -pos) re += bit[pos];
return re;
}
int que(int l, int r) {
return que(r) - que(l - 1);
}
ll count_swaps(vector<int> s) {
n = s.size() / 2;
ans = 0;
fill(bit + 1, bit + 2 * n + 1, 0);
FOR (i, 1, 2 * n) {
a[i] = s[i - 1];
p[i].clear();
to[i] = 0;
}
FOR (i, 1, 2 * n) {
int x = a[i], y = abs(x);
if (p[y].size() == 0 || a[p[y][0]] != -x) p[y].pb(i);
else {
to[p[y][0]] = i;
p[y].pop_front();
}
upd(i, 1);
}
FOR (i, 1, 2 * n) if (to[i]) {
ans += que(i + 1, to[i] - 1);
if (a[i] > 0) ans++;
upd(i, -1);
upd(to[i], -1);
}
return ans;
}
/*
in1
2
2 1 -1 -2
out1
4
in2
3
-2 2 2 -2 -2 2
out2
1
*/
# | 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... |