This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using vi = V<int>;
using ll = long long;
using pi = pair<int, int>;
using vpi = V<pi>;
using vl = V<ll>;
using db = long double;
const int INF = 1e9 + 10;
struct Scanner {
FILE* stream;
Scanner(FILE* s) : stream(s) {}
char buf[1 << 20], * l = buf, * r = buf;
bool flush() { l = buf; r = l + fread(buf, 1, 1 << 20, stream); return l == r; }
void get(char& c) { c = l == r && flush() ? ' ' : *l++; }
friend Scanner& operator >>(Scanner& in, char& c) { return in.get(c), in; }
friend Scanner& operator >>(Scanner& in, char* s) {
for (in.get(s[0]); isspace(s[0]); in.get(s[0]));
for (int i = 0; !isspace(s[i]) || (s[i] = '\0'); i++) in.get(s[i + 1]);
return in;
}
friend Scanner& operator >>(Scanner& in, std::string& s) {
char c;
for (in.get(c); isspace(c); in.get(c));
for (s = ""; !isspace(c); in.get(c)) s += c;
return in;
}
template <class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
friend Scanner& operator >>(Scanner& in, T& x) {
char c, f = '+';
for (in.get(c); !isdigit(c); in.get(c))
if constexpr (std::is_signed_v<T>) f = c;
for (x = 0; isdigit(c); in.get(c)) x = x * (1 << 1) + x * (1 << 3) + c - '0';
if constexpr (std::is_signed_v<T>) x = f == '-' ? -x : x;
return in;
}
template <class T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
friend Scanner& operator >>(Scanner& in, T& x) {
std::string s; in >> s; x = std::stod(s);
return in;
}
template <class T, class U>
friend Scanner& operator >>(Scanner& in, std::pair<T, U>& a) {
return in >> a.first >> a.second;
}
template <class T, size_t S>
friend Scanner& operator >>(Scanner& in, std::array<T, S>& a) {
for (int i = 0, n = S; i < n; i++) in >> a[i];
return in;
}
template <class T>
friend Scanner& operator >>(Scanner& in, std::vector<T>& a) {
for (int i = 0, n = a.size(); i < n; i++) in >> a[i];
return in;
}
};
struct Printer {
FILE* stream;
Printer(FILE* s) : stream(s) {}
char buf[1 << 20], * l = buf, * r = buf + (1 << 20) - 1;
int format = 0, precision = 15;
~Printer() { flush(); }
void flush() { fwrite(buf, 1, l - buf, stream); l = buf; }
void put(const char& c) { *l++ = c; if (l == r) flush(); }
friend Printer& operator <<(Printer& out, const char& c) { return out.put(c), out; }
friend Printer& operator <<(Printer& out, const char* s) {
for (int i = 0; s[i] != '\0'; i++) out.put(s[i]);
return out;
}
friend Printer& operator <<(Printer& out, const std::string& s) {
for (int i = 0, n = s.size(); i < n; i++) out.put(s[i]);
return out;
}
template <class T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
friend Printer& operator <<(Printer& out, T x) {
static char s[40];
static int i = 0;
if (x == 0) { out.put('0'); return out; }
if constexpr (std::is_signed_v<T>) x = x < 0 ? out.put('-'), -x : x;
while (x > 0) s[++i] = x % 10 + '0', x /= 10;
while (i > 0) out.put(s[i--]);
return out;
}
template <class T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
friend Printer& operator <<(Printer& out, T x) {
std::ostringstream oss;
oss << std::fixed << std::setprecision(out.precision) << x;
return out << oss.str();
}
template <class T, class U>
friend Printer& operator <<(Printer& out, const std::pair<T, U>& a) {
return out << a.first << " \n"[out.format > 1] << a.second;
}
template <class T, size_t S>
friend Printer& operator <<(Printer& out, const std::array<T, S>& a) {
out << a[0];
for (int i = 1, n = S; i < n; i++) out << " \n"[out.format > 1] << a[i];
return out;
}
template <class T>
friend Printer& operator <<(Printer& out, const std::vector<T>& a) {
if (!a.empty()) out << a[0];
for (int i = 1, n = a.size(); i < n; i++) out << " \n"[out.format > 0] << a[i];
return out;
}
};
Scanner in(stdin);
Printer out(stdout);
struct BIT {
int N; vl A; void init(int n) { N = n; A = vl(N, 0); }
void upd(int p, int x) { for(++p;p<=N;p+=p&-p) A[p-1] += x; }
ll sum(int l, int r) { return sum(r + 1) - sum(l); }
ll sum(int r) { ll s = 0; for(;r;r-=r&-r) s += A[r-1]; return s; }
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; in >> N;
vl A(N); for(auto& x : A) in >> x;
vl X = A; sort(begin(X), end(X));
X.erase(unique(begin(X), end(X)), end(X));
for(auto& x : A) x = lower_bound(begin(X), end(X), x) - begin(X);
int M = sz(X);
BIT up, dwn; up.init(M); dwn.init(M);
ll ans = 0;
for(auto& x : A) {
ans += dwn.sum(x + 1, M - 1);
ll DWN = up.sum(0, x - 1);
dwn.upd(x, DWN);
up.upd(x, 1);
}
out << ans << nl;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |