이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "shoes.h"
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 5;
deque<int> L[MAXN], R[MAXN];
int seg[MAXN*4], vis[MAXN];
void upd(int l, int r, int k, int a, int b) {
if (a <= l && r <= b) {
seg[k]++;
return;
}
int m = (l+r) / 2;
if (a <= m) upd(l,m,k*2,a,b);
if (m < b) upd(m+1,r,k*2+1,a,b);
}
int qry(int l, int r, int k, int a) {
if (l == r) return a+seg[k];
int m = (l+r) / 2;
return seg[k] + (a <= m ? qry(l,m,k*2,a) : qry(m+1,r,k*2+1,a));
}
ll count_swaps(vi s) {
int n = s.size() / 2;
deque<pii> act;
for (int i = 0; i < 2*n; i++) {
act.pb(mp(i,s[i]));
if (s[i] < 0)
L[-s[i]].pb(i);
else
R[s[i]].pb(i);
}
ll ret = 0;
while (act.size()) {
pii a1 = act.front(); act.pop_front();
if (vis[a1.fi])
continue;
if (a1.se < 0)
L[-a1.se].pop_front();
else
R[a1.se].pop_front();
if (a1.se < 0) {
int nx = R[-a1.se].front(); R[-a1.se].pop_front();
vis[nx] = 1;
ret += (ll)abs(qry(0,2*n-1,1,a1.fi)+1-qry(0,2*n-1,1,nx));
upd(0,2*n-1,1,a1.fi,nx);
} else {
int nx = L[a1.se].front(); L[a1.se].pop_front();
vis[nx] = 1;
ret += (ll)abs(qry(0,2*n-1,1,a1.fi)-qry(0,2*n-1,1,nx));
upd(0,2*n-1,1,a1.fi,nx);
}
}
return ret;
}
/*int main() {
int n; scanf("%d", &n);
vi s(2*n);
for (int i = 0; i < 2*n; i++)
scanf("%d", &s[i]);
printf("%I64d\n", count_swaps(s));
return 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... |