답안 #613639

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
613639 2022-07-30T08:45:31 Z yuto1115 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 4308 KB
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep2(i, s, n) for(ll i = ll(s); i < ll(n); ++i)
#define rrep(i, n) for(ll i = ll(n) - 1; i >= 0; --i)
#define rrep2(i, n, t) for(ll i = ll(n) - 1; i >= ll(t); --i)
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define SZ(a) int(a.size())
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vs = vector<string>;
const int inf = 1001001001;
const ll linf = 1001001001001001001;

template<class T>
bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

const int sz = 300000;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vi a(n);
    for (int &i: a) {
        cin >> i;
        --i;
    }
    vi l(q), r(q);
    rep(i, q) {
        cin >> l[i] >> r[i];
        --l[i];
    }
    const int B = 1000;
    vi ord(q);
    iota(all(ord), 0);
    sort(all(ord), [&](int i, int j) {
        if (r[i] / B != r[j] / B) return r[i] / B < r[j] / B;
        return l[i] < l[j];
    });
    vi cnt(sz);
    map<int, int> mp;
    mp[0] = sz;
    auto add = [&](int x) {
        if (--mp[cnt[x]] == 0) mp.erase(cnt[x]);
        ++mp[++cnt[x]];
    };
    auto del = [&](int x) {
        if (--mp[cnt[x]] == 0) mp.erase(cnt[x]);
        ++mp[--cnt[x]];
    };
    auto calc = [&](int s, int a, int k, int n) -> ll {
        assert(a > 0 and k > 0 and s + a * k <= n / 2);
        ll res = 0;
        res -= (ll) k * (k + 1) * (2 * k + 1) / 6 * a * a;
        res += (ll) k * (k + 1) / 2 * a * (n - 2 * s);
        res += (ll) k * s * (n - s);
        return res;
    };
    auto solve = [&mp, &calc](int n) -> ll {
        assert(SZ(mp) <= 800);
        --prev(mp.end())->second;
        vi v(2);
        ll res = 0;
        for (auto[f, s]: mp) {
            if (!f or !s) continue;
//            cerr << f << ' ' << s << endl;
            if (v[0] > v[1]) swap(v[0], v[1]);
            res += calc(v[0], f, (s + 1) / 2, n);
            if (s >= 2) res += calc(v[1], f, s / 2, n);
            v[0] += (s + 1) / 2 * f;
            v[1] += s / 2 * f;
        }
        ++prev(mp.end())->second;
        rep(i, 2) assert(v[i] <= n / 2);
        res += (ll) n * (n + 1) / 2;
        return res;
    };
    vl ans(q);
    int nl = 0, nr = 0;
    for (int i: ord) {
//        cerr << i << endl;
        while (nl > l[i]) add(a[--nl]);
        while (nr < r[i]) add(a[nr++]);
        while (nl < l[i]) del(a[nl++]);
        while (nr > r[i]) del(a[--nr]);
        ans[i] = solve(r[i] - l[i]);
    }
    rep(i, q) cout << ans[i] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1480 KB Output is correct
5 Correct 1 ms 1476 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 3 ms 1492 KB Output is correct
4 Correct 19 ms 2000 KB Output is correct
5 Correct 38 ms 2388 KB Output is correct
6 Correct 47 ms 2784 KB Output is correct
7 Correct 37 ms 2784 KB Output is correct
8 Correct 43 ms 2764 KB Output is correct
9 Correct 44 ms 2784 KB Output is correct
10 Correct 55 ms 2764 KB Output is correct
11 Correct 42 ms 2788 KB Output is correct
12 Correct 43 ms 2772 KB Output is correct
13 Correct 43 ms 2768 KB Output is correct
14 Correct 45 ms 2892 KB Output is correct
15 Correct 42 ms 2788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 3 ms 1492 KB Output is correct
4 Correct 19 ms 2000 KB Output is correct
5 Correct 38 ms 2388 KB Output is correct
6 Correct 47 ms 2784 KB Output is correct
7 Correct 37 ms 2784 KB Output is correct
8 Correct 43 ms 2764 KB Output is correct
9 Correct 44 ms 2784 KB Output is correct
10 Correct 55 ms 2764 KB Output is correct
11 Correct 42 ms 2788 KB Output is correct
12 Correct 43 ms 2772 KB Output is correct
13 Correct 43 ms 2768 KB Output is correct
14 Correct 45 ms 2892 KB Output is correct
15 Correct 42 ms 2788 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 3 ms 1536 KB Output is correct
19 Correct 18 ms 2000 KB Output is correct
20 Correct 39 ms 2400 KB Output is correct
21 Correct 48 ms 2772 KB Output is correct
22 Correct 50 ms 2776 KB Output is correct
23 Correct 50 ms 2796 KB Output is correct
24 Correct 52 ms 2772 KB Output is correct
25 Correct 62 ms 2772 KB Output is correct
26 Correct 53 ms 2780 KB Output is correct
27 Correct 51 ms 2784 KB Output is correct
28 Correct 51 ms 2788 KB Output is correct
29 Correct 50 ms 2784 KB Output is correct
30 Correct 51 ms 2792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 3 ms 1492 KB Output is correct
4 Correct 19 ms 2000 KB Output is correct
5 Correct 38 ms 2388 KB Output is correct
6 Correct 47 ms 2784 KB Output is correct
7 Correct 37 ms 2784 KB Output is correct
8 Correct 43 ms 2764 KB Output is correct
9 Correct 44 ms 2784 KB Output is correct
10 Correct 55 ms 2764 KB Output is correct
11 Correct 42 ms 2788 KB Output is correct
12 Correct 43 ms 2772 KB Output is correct
13 Correct 43 ms 2768 KB Output is correct
14 Correct 45 ms 2892 KB Output is correct
15 Correct 42 ms 2788 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 3 ms 1536 KB Output is correct
19 Correct 18 ms 2000 KB Output is correct
20 Correct 39 ms 2400 KB Output is correct
21 Correct 48 ms 2772 KB Output is correct
22 Correct 50 ms 2776 KB Output is correct
23 Correct 50 ms 2796 KB Output is correct
24 Correct 52 ms 2772 KB Output is correct
25 Correct 62 ms 2772 KB Output is correct
26 Correct 53 ms 2780 KB Output is correct
27 Correct 51 ms 2784 KB Output is correct
28 Correct 51 ms 2788 KB Output is correct
29 Correct 50 ms 2784 KB Output is correct
30 Correct 51 ms 2792 KB Output is correct
31 Correct 1 ms 1492 KB Output is correct
32 Correct 1 ms 1492 KB Output is correct
33 Correct 2 ms 1492 KB Output is correct
34 Correct 3 ms 1492 KB Output is correct
35 Correct 3 ms 1492 KB Output is correct
36 Correct 2 ms 1492 KB Output is correct
37 Correct 9 ms 1736 KB Output is correct
38 Correct 7 ms 1748 KB Output is correct
39 Correct 15 ms 2000 KB Output is correct
40 Correct 37 ms 2420 KB Output is correct
41 Correct 58 ms 2776 KB Output is correct
42 Correct 63 ms 2804 KB Output is correct
43 Correct 57 ms 2932 KB Output is correct
44 Correct 57 ms 2808 KB Output is correct
45 Correct 57 ms 2788 KB Output is correct
46 Correct 61 ms 2808 KB Output is correct
47 Correct 60 ms 2816 KB Output is correct
48 Correct 57 ms 2760 KB Output is correct
49 Correct 62 ms 2792 KB Output is correct
50 Correct 60 ms 2776 KB Output is correct
51 Correct 58 ms 2800 KB Output is correct
52 Correct 57 ms 2772 KB Output is correct
53 Correct 61 ms 2808 KB Output is correct
54 Correct 59 ms 2776 KB Output is correct
55 Correct 61 ms 2812 KB Output is correct
56 Correct 58 ms 2812 KB Output is correct
57 Correct 57 ms 2776 KB Output is correct
58 Correct 60 ms 2760 KB Output is correct
59 Correct 59 ms 2800 KB Output is correct
60 Correct 58 ms 2940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1480 KB Output is correct
5 Correct 1 ms 1476 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1492 KB Output is correct
13 Correct 3 ms 1492 KB Output is correct
14 Correct 19 ms 2000 KB Output is correct
15 Correct 38 ms 2388 KB Output is correct
16 Correct 47 ms 2784 KB Output is correct
17 Correct 37 ms 2784 KB Output is correct
18 Correct 43 ms 2764 KB Output is correct
19 Correct 44 ms 2784 KB Output is correct
20 Correct 55 ms 2764 KB Output is correct
21 Correct 42 ms 2788 KB Output is correct
22 Correct 43 ms 2772 KB Output is correct
23 Correct 43 ms 2768 KB Output is correct
24 Correct 45 ms 2892 KB Output is correct
25 Correct 42 ms 2788 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 3 ms 1536 KB Output is correct
29 Correct 18 ms 2000 KB Output is correct
30 Correct 39 ms 2400 KB Output is correct
31 Correct 48 ms 2772 KB Output is correct
32 Correct 50 ms 2776 KB Output is correct
33 Correct 50 ms 2796 KB Output is correct
34 Correct 52 ms 2772 KB Output is correct
35 Correct 62 ms 2772 KB Output is correct
36 Correct 53 ms 2780 KB Output is correct
37 Correct 51 ms 2784 KB Output is correct
38 Correct 51 ms 2788 KB Output is correct
39 Correct 50 ms 2784 KB Output is correct
40 Correct 51 ms 2792 KB Output is correct
41 Correct 1 ms 1492 KB Output is correct
42 Correct 1 ms 1492 KB Output is correct
43 Correct 2 ms 1492 KB Output is correct
44 Correct 3 ms 1492 KB Output is correct
45 Correct 3 ms 1492 KB Output is correct
46 Correct 2 ms 1492 KB Output is correct
47 Correct 9 ms 1736 KB Output is correct
48 Correct 7 ms 1748 KB Output is correct
49 Correct 15 ms 2000 KB Output is correct
50 Correct 37 ms 2420 KB Output is correct
51 Correct 58 ms 2776 KB Output is correct
52 Correct 63 ms 2804 KB Output is correct
53 Correct 57 ms 2932 KB Output is correct
54 Correct 57 ms 2808 KB Output is correct
55 Correct 57 ms 2788 KB Output is correct
56 Correct 61 ms 2808 KB Output is correct
57 Correct 60 ms 2816 KB Output is correct
58 Correct 57 ms 2760 KB Output is correct
59 Correct 62 ms 2792 KB Output is correct
60 Correct 60 ms 2776 KB Output is correct
61 Correct 58 ms 2800 KB Output is correct
62 Correct 57 ms 2772 KB Output is correct
63 Correct 61 ms 2808 KB Output is correct
64 Correct 59 ms 2776 KB Output is correct
65 Correct 61 ms 2812 KB Output is correct
66 Correct 58 ms 2812 KB Output is correct
67 Correct 57 ms 2776 KB Output is correct
68 Correct 60 ms 2760 KB Output is correct
69 Correct 59 ms 2800 KB Output is correct
70 Correct 58 ms 2940 KB Output is correct
71 Correct 7 ms 1748 KB Output is correct
72 Correct 8 ms 1792 KB Output is correct
73 Correct 7 ms 1748 KB Output is correct
74 Correct 7 ms 1792 KB Output is correct
75 Correct 7 ms 1808 KB Output is correct
76 Correct 13 ms 2040 KB Output is correct
77 Correct 11 ms 2004 KB Output is correct
78 Correct 12 ms 2004 KB Output is correct
79 Correct 12 ms 1988 KB Output is correct
80 Correct 12 ms 2024 KB Output is correct
81 Correct 23 ms 2396 KB Output is correct
82 Correct 23 ms 2396 KB Output is correct
83 Correct 28 ms 2388 KB Output is correct
84 Correct 24 ms 2496 KB Output is correct
85 Correct 22 ms 2388 KB Output is correct
86 Correct 24 ms 2400 KB Output is correct
87 Correct 23 ms 2408 KB Output is correct
88 Correct 23 ms 2396 KB Output is correct
89 Correct 22 ms 2376 KB Output is correct
90 Correct 22 ms 2388 KB Output is correct
91 Correct 29 ms 2764 KB Output is correct
92 Correct 29 ms 2772 KB Output is correct
93 Correct 28 ms 2760 KB Output is correct
94 Correct 35 ms 2764 KB Output is correct
95 Correct 34 ms 2764 KB Output is correct
96 Correct 28 ms 2764 KB Output is correct
97 Correct 29 ms 2776 KB Output is correct
98 Correct 29 ms 2780 KB Output is correct
99 Correct 30 ms 2780 KB Output is correct
100 Correct 29 ms 2780 KB Output is correct
101 Correct 28 ms 2780 KB Output is correct
102 Correct 29 ms 2776 KB Output is correct
103 Correct 32 ms 2780 KB Output is correct
104 Correct 29 ms 2784 KB Output is correct
105 Correct 30 ms 2804 KB Output is correct
106 Correct 29 ms 2780 KB Output is correct
107 Correct 27 ms 2764 KB Output is correct
108 Correct 31 ms 2764 KB Output is correct
109 Correct 28 ms 2756 KB Output is correct
110 Correct 29 ms 2768 KB Output is correct
111 Correct 27 ms 2772 KB Output is correct
112 Correct 34 ms 2764 KB Output is correct
113 Correct 27 ms 2780 KB Output is correct
114 Correct 28 ms 2780 KB Output is correct
115 Correct 28 ms 2760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1480 KB Output is correct
5 Correct 1 ms 1476 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1492 KB Output is correct
13 Correct 3 ms 1492 KB Output is correct
14 Correct 19 ms 2000 KB Output is correct
15 Correct 38 ms 2388 KB Output is correct
16 Correct 47 ms 2784 KB Output is correct
17 Correct 37 ms 2784 KB Output is correct
18 Correct 43 ms 2764 KB Output is correct
19 Correct 44 ms 2784 KB Output is correct
20 Correct 55 ms 2764 KB Output is correct
21 Correct 42 ms 2788 KB Output is correct
22 Correct 43 ms 2772 KB Output is correct
23 Correct 43 ms 2768 KB Output is correct
24 Correct 45 ms 2892 KB Output is correct
25 Correct 42 ms 2788 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 3 ms 1536 KB Output is correct
29 Correct 18 ms 2000 KB Output is correct
30 Correct 39 ms 2400 KB Output is correct
31 Correct 48 ms 2772 KB Output is correct
32 Correct 50 ms 2776 KB Output is correct
33 Correct 50 ms 2796 KB Output is correct
34 Correct 52 ms 2772 KB Output is correct
35 Correct 62 ms 2772 KB Output is correct
36 Correct 53 ms 2780 KB Output is correct
37 Correct 51 ms 2784 KB Output is correct
38 Correct 51 ms 2788 KB Output is correct
39 Correct 50 ms 2784 KB Output is correct
40 Correct 51 ms 2792 KB Output is correct
41 Correct 1 ms 1492 KB Output is correct
42 Correct 1 ms 1492 KB Output is correct
43 Correct 2 ms 1492 KB Output is correct
44 Correct 3 ms 1492 KB Output is correct
45 Correct 3 ms 1492 KB Output is correct
46 Correct 2 ms 1492 KB Output is correct
47 Correct 9 ms 1736 KB Output is correct
48 Correct 7 ms 1748 KB Output is correct
49 Correct 15 ms 2000 KB Output is correct
50 Correct 37 ms 2420 KB Output is correct
51 Correct 58 ms 2776 KB Output is correct
52 Correct 63 ms 2804 KB Output is correct
53 Correct 57 ms 2932 KB Output is correct
54 Correct 57 ms 2808 KB Output is correct
55 Correct 57 ms 2788 KB Output is correct
56 Correct 61 ms 2808 KB Output is correct
57 Correct 60 ms 2816 KB Output is correct
58 Correct 57 ms 2760 KB Output is correct
59 Correct 62 ms 2792 KB Output is correct
60 Correct 60 ms 2776 KB Output is correct
61 Correct 58 ms 2800 KB Output is correct
62 Correct 57 ms 2772 KB Output is correct
63 Correct 61 ms 2808 KB Output is correct
64 Correct 59 ms 2776 KB Output is correct
65 Correct 61 ms 2812 KB Output is correct
66 Correct 58 ms 2812 KB Output is correct
67 Correct 57 ms 2776 KB Output is correct
68 Correct 60 ms 2760 KB Output is correct
69 Correct 59 ms 2800 KB Output is correct
70 Correct 58 ms 2940 KB Output is correct
71 Correct 7 ms 1748 KB Output is correct
72 Correct 8 ms 1792 KB Output is correct
73 Correct 7 ms 1748 KB Output is correct
74 Correct 7 ms 1792 KB Output is correct
75 Correct 7 ms 1808 KB Output is correct
76 Correct 13 ms 2040 KB Output is correct
77 Correct 11 ms 2004 KB Output is correct
78 Correct 12 ms 2004 KB Output is correct
79 Correct 12 ms 1988 KB Output is correct
80 Correct 12 ms 2024 KB Output is correct
81 Correct 23 ms 2396 KB Output is correct
82 Correct 23 ms 2396 KB Output is correct
83 Correct 28 ms 2388 KB Output is correct
84 Correct 24 ms 2496 KB Output is correct
85 Correct 22 ms 2388 KB Output is correct
86 Correct 24 ms 2400 KB Output is correct
87 Correct 23 ms 2408 KB Output is correct
88 Correct 23 ms 2396 KB Output is correct
89 Correct 22 ms 2376 KB Output is correct
90 Correct 22 ms 2388 KB Output is correct
91 Correct 29 ms 2764 KB Output is correct
92 Correct 29 ms 2772 KB Output is correct
93 Correct 28 ms 2760 KB Output is correct
94 Correct 35 ms 2764 KB Output is correct
95 Correct 34 ms 2764 KB Output is correct
96 Correct 28 ms 2764 KB Output is correct
97 Correct 29 ms 2776 KB Output is correct
98 Correct 29 ms 2780 KB Output is correct
99 Correct 30 ms 2780 KB Output is correct
100 Correct 29 ms 2780 KB Output is correct
101 Correct 28 ms 2780 KB Output is correct
102 Correct 29 ms 2776 KB Output is correct
103 Correct 32 ms 2780 KB Output is correct
104 Correct 29 ms 2784 KB Output is correct
105 Correct 30 ms 2804 KB Output is correct
106 Correct 29 ms 2780 KB Output is correct
107 Correct 27 ms 2764 KB Output is correct
108 Correct 31 ms 2764 KB Output is correct
109 Correct 28 ms 2756 KB Output is correct
110 Correct 29 ms 2768 KB Output is correct
111 Correct 27 ms 2772 KB Output is correct
112 Correct 34 ms 2764 KB Output is correct
113 Correct 27 ms 2780 KB Output is correct
114 Correct 28 ms 2780 KB Output is correct
115 Correct 28 ms 2760 KB Output is correct
116 Correct 2112 ms 3264 KB Output is correct
117 Correct 1831 ms 3148 KB Output is correct
118 Correct 2970 ms 3444 KB Output is correct
119 Correct 1802 ms 3472 KB Output is correct
120 Correct 1234 ms 3564 KB Output is correct
121 Correct 2394 ms 3640 KB Output is correct
122 Correct 1744 ms 3644 KB Output is correct
123 Correct 6097 ms 4308 KB Output is correct
124 Correct 2924 ms 4160 KB Output is correct
125 Correct 2191 ms 4160 KB Output is correct
126 Execution timed out 7043 ms 3908 KB Time limit exceeded
127 Halted 0 ms 0 KB -