This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
string name{""};
time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
duration<float, std::milli> dur;
Timer() = default;
Timer(string nm): name(nm) {}
~Timer() {
end = high_resolution_clock::now(); dur= end - start;
cout << "@" << name << "> " << dur.count() << " ms" << '\n';
}
};
template <typename T = int> inline T in()
{
static T x;
std::cin >> x;
return x;
}
std::string yn(bool b)
{
if(b) return "YES\n";
else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
for(const auto& i : wek)out << i << ' ';
return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
out << '{'<<par.first<<", "<<par.second<<"}";
return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
constexpr int Opol = 1<<19;
int tre[2*Opol];
void add(int g, int c){
g += Opol;
while(g){
tre[g] += c;
g /= 2;
}
}
int sum(int a, int b){
a += Opol-1;
b += Opol+1;
int s(0);
while(a+1 != b){
if(a%2 == 0)s += tre[a+1];
if(b%2 == 1)s += tre[b-1];
a /= 2; b /= 2;
}
return s;
}
constexpr int maxn = 1e5 + 3;
vector <int> pol[maxn], por[maxn];
ll count_swaps(vector <int> s)
{
int n(sz(s)/2);
int g(0);
for(auto i: s){
++g;
if(i > 0)++g;
if(i < 0)pol[-i].pb(g);
else por[i].pb(g);
add(g, +1);
}
vector <pair <int, int>> r, l;
rep(i, 1, n+1){
sort(all(pol[i]));
sort(all(por[i]));
rep(j, 0, sz(pol[i])){
int a(pol[i][j]), b(por[i][j]-1);
if(a < b)r.pb(a, b);
if(a > b)l.pb(a, b);
}
}
sort(all(r), [](auto a, auto b){return a.first > b.first;});
sort(all(l), [](auto a, auto b){return a.first < b.first;});
ll o(0);
for(auto [a, b]: r){
// rep(i, 1, 3*n+1)cerr << sum(i, i) << ' ';
// cerr << '\n';
// cerr << a << " -> " << b << '\n';
o += sum(a+1, b-1);
// cerr <<'#'<< o << '\n';
add(a, -1);
add(b, +1);
}
for(auto [a, b]: l){
// rep(i, 1, 3*n+1)cerr << sum(i, i) << ' ';
// cerr << '\n';
// cerr << a << " -> " << b << '\n';
o += sum(b+1, a-1);
// cerr <<'#'<< o << '\n';
add(a, -1);
add(b, +1);
}
// rep(i, 1, 3*n+1)cerr << sum(i, i) << ' ';
// cerr << '\n';
return o;
}
# | 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... |