답안 #830146

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
830146 2023-08-18T20:02:08 Z NK_ Mountains (NOI20_mountains) C++17
100 / 100
107 ms 10740 KB
// 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 57 ms 10616 KB Output is correct
3 Correct 60 ms 10664 KB Output is correct
4 Correct 60 ms 10668 KB Output is correct
5 Correct 60 ms 10676 KB Output is correct
6 Correct 60 ms 10660 KB Output is correct
7 Correct 61 ms 10664 KB Output is correct
8 Correct 59 ms 10664 KB Output is correct
9 Correct 57 ms 8488 KB Output is correct
10 Correct 57 ms 8396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 5588 KB Output is correct
2 Correct 12 ms 5588 KB Output is correct
3 Correct 12 ms 5588 KB Output is correct
4 Correct 12 ms 5588 KB Output is correct
5 Correct 12 ms 5588 KB Output is correct
6 Correct 12 ms 5588 KB Output is correct
7 Correct 12 ms 5588 KB Output is correct
8 Correct 9 ms 5588 KB Output is correct
9 Correct 9 ms 5552 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 5588 KB Output is correct
2 Correct 12 ms 5588 KB Output is correct
3 Correct 12 ms 5588 KB Output is correct
4 Correct 12 ms 5588 KB Output is correct
5 Correct 12 ms 5588 KB Output is correct
6 Correct 12 ms 5588 KB Output is correct
7 Correct 12 ms 5588 KB Output is correct
8 Correct 9 ms 5588 KB Output is correct
9 Correct 9 ms 5552 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 32 ms 5844 KB Output is correct
12 Correct 32 ms 5868 KB Output is correct
13 Correct 33 ms 5868 KB Output is correct
14 Correct 31 ms 5844 KB Output is correct
15 Correct 32 ms 5844 KB Output is correct
16 Correct 31 ms 5844 KB Output is correct
17 Correct 32 ms 5844 KB Output is correct
18 Correct 13 ms 5844 KB Output is correct
19 Correct 12 ms 5972 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 724 KB Output is correct
15 Correct 2 ms 724 KB Output is correct
16 Correct 3 ms 724 KB Output is correct
17 Correct 2 ms 724 KB Output is correct
18 Correct 2 ms 668 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 5588 KB Output is correct
2 Correct 12 ms 5588 KB Output is correct
3 Correct 12 ms 5588 KB Output is correct
4 Correct 12 ms 5588 KB Output is correct
5 Correct 12 ms 5588 KB Output is correct
6 Correct 12 ms 5588 KB Output is correct
7 Correct 12 ms 5588 KB Output is correct
8 Correct 9 ms 5588 KB Output is correct
9 Correct 9 ms 5552 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 32 ms 5844 KB Output is correct
12 Correct 32 ms 5868 KB Output is correct
13 Correct 33 ms 5868 KB Output is correct
14 Correct 31 ms 5844 KB Output is correct
15 Correct 32 ms 5844 KB Output is correct
16 Correct 31 ms 5844 KB Output is correct
17 Correct 32 ms 5844 KB Output is correct
18 Correct 13 ms 5844 KB Output is correct
19 Correct 12 ms 5972 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 88 ms 7448 KB Output is correct
22 Correct 84 ms 7500 KB Output is correct
23 Correct 85 ms 7528 KB Output is correct
24 Correct 83 ms 7460 KB Output is correct
25 Correct 84 ms 7504 KB Output is correct
26 Correct 86 ms 7444 KB Output is correct
27 Correct 80 ms 7464 KB Output is correct
28 Correct 41 ms 7476 KB Output is correct
29 Correct 44 ms 7500 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 57 ms 10616 KB Output is correct
3 Correct 60 ms 10664 KB Output is correct
4 Correct 60 ms 10668 KB Output is correct
5 Correct 60 ms 10676 KB Output is correct
6 Correct 60 ms 10660 KB Output is correct
7 Correct 61 ms 10664 KB Output is correct
8 Correct 59 ms 10664 KB Output is correct
9 Correct 57 ms 8488 KB Output is correct
10 Correct 57 ms 8396 KB Output is correct
11 Correct 12 ms 5588 KB Output is correct
12 Correct 12 ms 5588 KB Output is correct
13 Correct 12 ms 5588 KB Output is correct
14 Correct 12 ms 5588 KB Output is correct
15 Correct 12 ms 5588 KB Output is correct
16 Correct 12 ms 5588 KB Output is correct
17 Correct 12 ms 5588 KB Output is correct
18 Correct 9 ms 5588 KB Output is correct
19 Correct 9 ms 5552 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 32 ms 5844 KB Output is correct
22 Correct 32 ms 5868 KB Output is correct
23 Correct 33 ms 5868 KB Output is correct
24 Correct 31 ms 5844 KB Output is correct
25 Correct 32 ms 5844 KB Output is correct
26 Correct 31 ms 5844 KB Output is correct
27 Correct 32 ms 5844 KB Output is correct
28 Correct 13 ms 5844 KB Output is correct
29 Correct 12 ms 5972 KB Output is correct
30 Correct 0 ms 212 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 0 ms 212 KB Output is correct
41 Correct 2 ms 724 KB Output is correct
42 Correct 2 ms 724 KB Output is correct
43 Correct 2 ms 724 KB Output is correct
44 Correct 2 ms 724 KB Output is correct
45 Correct 2 ms 724 KB Output is correct
46 Correct 3 ms 724 KB Output is correct
47 Correct 2 ms 724 KB Output is correct
48 Correct 2 ms 668 KB Output is correct
49 Correct 2 ms 468 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 88 ms 7448 KB Output is correct
52 Correct 84 ms 7500 KB Output is correct
53 Correct 85 ms 7528 KB Output is correct
54 Correct 83 ms 7460 KB Output is correct
55 Correct 84 ms 7504 KB Output is correct
56 Correct 86 ms 7444 KB Output is correct
57 Correct 80 ms 7464 KB Output is correct
58 Correct 41 ms 7476 KB Output is correct
59 Correct 44 ms 7500 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 105 ms 10740 KB Output is correct
62 Correct 102 ms 10644 KB Output is correct
63 Correct 107 ms 10704 KB Output is correct
64 Correct 104 ms 10652 KB Output is correct
65 Correct 104 ms 10668 KB Output is correct
66 Correct 106 ms 10700 KB Output is correct
67 Correct 103 ms 10700 KB Output is correct
68 Correct 100 ms 8476 KB Output is correct
69 Correct 89 ms 7956 KB Output is correct
70 Correct 0 ms 212 KB Output is correct