This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define fi first
#define se second
#define inf (INT_MAX/2-1)
#define infl (1LL<<60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a),end(a)
#define y0 y5656
#define y1 y7878
#define y2 y9090
#define aaa system("pause");
#define dbg(x) cerr<<(#x)<<": "<<(x)<<'\n',aaa
#define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<n;_++)cerr<<x[_]<<' ';cerr<<'\n',aaa
#define dbgs(x) cerr<<(#x)<<"[stl]: ";for(auto _:x)cerr<<_<<' ';cerr<<'\n',aaa
#define dbgp(x) cerr<<(#x)<<": "<<x.fi<<' '<<x.se<<'\n',aaa
#define maxn 100000
#include "shoes.h"
using namespace std;
struct AIB {
AIB () {}
int yy[maxn+5];
int lsb (int x) {
return x&(-x);
}
void upd (int n, int x, int amt) {
x++; ///index 0 -> 1
for (; x <= n; x += lsb(x)) yy[x] += amt;
}
int qry (int x) {
int ans = 0;
x++; ///index 0 -> 1
for (; x > 0; x -= lsb(x)) ans += yy[x];
return ans;
}
int qryrg (int l, int r) {
return qry(r) - qry(l-1);
}
};
AIB aib;
stack<int> stkP[maxn+5], stkM[maxn+5];
ll count_swaps (vi v) {
int n = sz(v);
int i, j, z;
ll ans = 0;
for (i = n-1; i >= 0; i--)
if (v[i] > 0) stkP[v[i]].push(i);
else stkM[-v[i]].push(i);
for (i = 0; i < n; i++) {
if (aib.qryrg(i, i) == 1) continue; ///deja mutat
if (v[i] < 0) {
while (!stkP[-v[i]].empty() && stkP[-v[i]].top() < i) stkP[-v[i]].pop();
if (!stkP[-v[i]].empty()) {
z = stkP[-v[i]].top();
ans += z-i-1 - aib.qryrg(i+1, z); ///in aib retii ce ai sters deja
aib.upd(n, z, 1);
stkP[-v[i]].pop();
}
} else {
while (!stkM[v[i]].empty() && stkM[v[i]].top() < i) stkM[v[i]].pop();
if (!stkM[v[i]].empty()) {
z = stkM[v[i]].top();
ans += z-i - aib.qryrg(i, z);
aib.upd(n, z, 1);
stkM[v[i]].pop();
}
}
}
return ans;
}
//int main () {
// int n; cin >> n;
// int i, j, z;
// vi v(n*2);
// for (i = 0; i < 2*n; i++) cin >> v[i];
// cout << count_swaps(v);
// return 0;
//}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:55:10: warning: unused variable 'j' [-Wunused-variable]
55 | int i, j, z;
| ^
# | 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... |