This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const double PI = 4 * atan(1);
#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define vi vector<int>
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define vpi vector<pair<int, int>>
#define vpd vector<pair<double, double>>
#define pd pair<double, double>
#define f0r(i,a) for(int i=0;i<a;i++)
#define f1r(i,a,b) for(int i=a;i<b;i++)
#define trav(a, x) for (auto& a : x)
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();
}
const int MAX = 2e5 + 5;
template<class T, int SZ> struct Seg { // SZ should be power of 2
T ID = 0; // comb(ID,b) must equal b
T comb(T a, T b) { return a+b; } // easily change this to min or max
T seg[2*SZ];
Seg() { memset(seg,0,sizeof seg); }
void upd(int p, T value) { // set value at position p
seg[p += SZ] = value;
for (p /= 2; p; p /= 2) seg[p] = comb(seg[2*p],seg[2*p+1]);
}
T query(int l, int r) { // sum on interval [l, r]
r ++; T lres = ID, rres = ID; // make sure non-commutative operations work
for (l += SZ, r += SZ; l < r; l /= 2, r /= 2) {
if (l&1) lres = comb(lres,seg[l++]);
if (r&1) rres = comb(seg[--r],rres);
}
return comb(lres,rres);
}
};
Seg<ll, (1<<18)> seg;
vi loc[2][MAX];
int vis[MAX];
int other[MAX];
unordered_map<int, int> um;
ll count_swaps(vi s){
int sz = sz(s);
int n = sz/2;
ll ans = 0;
set<int> vals;
for(int x: s) vals.insert(abs(x));
int cnt = 1;
//int mx = 0;
for(int x: vals){
um[x] = cnt;
cnt++;
}
int mx = cnt; ///1 greater than the maximum
f0r(i, sz){
if(s[i]>0){
loc[1][um[s[i]]].eb(i);
}
else{
loc[0][um[-s[i]]].eb(i);
}
}
// vi a;
// a.resize(sz);
int tot = 0;
f0r(i, mx){
f0r(j, sz(loc[0][i])){
if(loc[0][i][j] >loc[1][i][j]){
ans++;
}
// a[loc[0][i][j]] = tot;
//a[loc[1][i][j]] = tot;
other[loc[0][i][j]] = loc[1][i][j];
other[loc[1][i][j]] = loc[0][i][j];
tot++;
}
}
// cout << a << endl;
f0r(i, sz){
if(other[i]<i) continue;
ans += other[i] - i - 1;
ans -= seg.query(i, other[i]);
seg.upd(other[i], 1);
}
return ans;
}
/*
int main(){
fast_io();
int n;
cin >> n;
vi a;
f0r(i,n){
int ai;
cin >> ai;
a.eb(ai);
}
cout << count_swaps(a) << endl;
return 0;
}*/
Compilation message (stderr)
shoes.cpp:1:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(linker, "/stack:200000000")
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:86:9: warning: unused variable 'n' [-Wunused-variable]
int n = sz/2;
^
shoes.cpp: In function 'void io(std::__cxx11::string)':
shoes.cpp:52:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FIN, "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~
shoes.cpp:53:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(FOUT, "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |