답안 #613641

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
613641 2022-07-30T08:46:46 Z yuto1115 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 3912 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 = 1500;
    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 1492 KB Output is correct
5 Correct 1 ms 1492 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 1404 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 2 ms 1492 KB Output is correct
4 Correct 15 ms 1876 KB Output is correct
5 Correct 27 ms 2272 KB Output is correct
6 Correct 50 ms 2604 KB Output is correct
7 Correct 38 ms 2660 KB Output is correct
8 Correct 45 ms 2772 KB Output is correct
9 Correct 42 ms 2656 KB Output is correct
10 Correct 41 ms 2644 KB Output is correct
11 Correct 42 ms 2660 KB Output is correct
12 Correct 44 ms 2660 KB Output is correct
13 Correct 43 ms 2644 KB Output is correct
14 Correct 41 ms 2780 KB Output is correct
15 Correct 44 ms 2660 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 2 ms 1492 KB Output is correct
4 Correct 15 ms 1876 KB Output is correct
5 Correct 27 ms 2272 KB Output is correct
6 Correct 50 ms 2604 KB Output is correct
7 Correct 38 ms 2660 KB Output is correct
8 Correct 45 ms 2772 KB Output is correct
9 Correct 42 ms 2656 KB Output is correct
10 Correct 41 ms 2644 KB Output is correct
11 Correct 42 ms 2660 KB Output is correct
12 Correct 44 ms 2660 KB Output is correct
13 Correct 43 ms 2644 KB Output is correct
14 Correct 41 ms 2780 KB Output is correct
15 Correct 44 ms 2660 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 2 ms 1492 KB Output is correct
18 Correct 3 ms 1492 KB Output is correct
19 Correct 18 ms 1876 KB Output is correct
20 Correct 33 ms 2272 KB Output is correct
21 Correct 48 ms 2644 KB Output is correct
22 Correct 49 ms 2648 KB Output is correct
23 Correct 50 ms 2644 KB Output is correct
24 Correct 50 ms 2660 KB Output is correct
25 Correct 51 ms 2644 KB Output is correct
26 Correct 54 ms 2656 KB Output is correct
27 Correct 51 ms 2644 KB Output is correct
28 Correct 47 ms 2620 KB Output is correct
29 Correct 59 ms 2664 KB Output is correct
30 Correct 52 ms 2660 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 2 ms 1492 KB Output is correct
4 Correct 15 ms 1876 KB Output is correct
5 Correct 27 ms 2272 KB Output is correct
6 Correct 50 ms 2604 KB Output is correct
7 Correct 38 ms 2660 KB Output is correct
8 Correct 45 ms 2772 KB Output is correct
9 Correct 42 ms 2656 KB Output is correct
10 Correct 41 ms 2644 KB Output is correct
11 Correct 42 ms 2660 KB Output is correct
12 Correct 44 ms 2660 KB Output is correct
13 Correct 43 ms 2644 KB Output is correct
14 Correct 41 ms 2780 KB Output is correct
15 Correct 44 ms 2660 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 2 ms 1492 KB Output is correct
18 Correct 3 ms 1492 KB Output is correct
19 Correct 18 ms 1876 KB Output is correct
20 Correct 33 ms 2272 KB Output is correct
21 Correct 48 ms 2644 KB Output is correct
22 Correct 49 ms 2648 KB Output is correct
23 Correct 50 ms 2644 KB Output is correct
24 Correct 50 ms 2660 KB Output is correct
25 Correct 51 ms 2644 KB Output is correct
26 Correct 54 ms 2656 KB Output is correct
27 Correct 51 ms 2644 KB Output is correct
28 Correct 47 ms 2620 KB Output is correct
29 Correct 59 ms 2664 KB Output is correct
30 Correct 52 ms 2660 KB Output is correct
31 Correct 1 ms 1492 KB Output is correct
32 Correct 1 ms 1492 KB Output is correct
33 Correct 1 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 11 ms 1664 KB Output is correct
38 Correct 8 ms 1620 KB Output is correct
39 Correct 16 ms 1904 KB Output is correct
40 Correct 35 ms 2264 KB Output is correct
41 Correct 56 ms 2656 KB Output is correct
42 Correct 61 ms 2656 KB Output is correct
43 Correct 66 ms 2680 KB Output is correct
44 Correct 56 ms 2644 KB Output is correct
45 Correct 68 ms 2684 KB Output is correct
46 Correct 60 ms 2688 KB Output is correct
47 Correct 58 ms 2644 KB Output is correct
48 Correct 57 ms 2644 KB Output is correct
49 Correct 56 ms 2652 KB Output is correct
50 Correct 65 ms 2684 KB Output is correct
51 Correct 61 ms 2656 KB Output is correct
52 Correct 59 ms 2692 KB Output is correct
53 Correct 57 ms 2656 KB Output is correct
54 Correct 59 ms 2648 KB Output is correct
55 Correct 58 ms 2680 KB Output is correct
56 Correct 69 ms 2680 KB Output is correct
57 Correct 60 ms 2660 KB Output is correct
58 Correct 57 ms 2692 KB Output is correct
59 Correct 66 ms 2648 KB Output is correct
60 Correct 56 ms 2644 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 1492 KB Output is correct
5 Correct 1 ms 1492 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 1404 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1492 KB Output is correct
13 Correct 2 ms 1492 KB Output is correct
14 Correct 15 ms 1876 KB Output is correct
15 Correct 27 ms 2272 KB Output is correct
16 Correct 50 ms 2604 KB Output is correct
17 Correct 38 ms 2660 KB Output is correct
18 Correct 45 ms 2772 KB Output is correct
19 Correct 42 ms 2656 KB Output is correct
20 Correct 41 ms 2644 KB Output is correct
21 Correct 42 ms 2660 KB Output is correct
22 Correct 44 ms 2660 KB Output is correct
23 Correct 43 ms 2644 KB Output is correct
24 Correct 41 ms 2780 KB Output is correct
25 Correct 44 ms 2660 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 2 ms 1492 KB Output is correct
28 Correct 3 ms 1492 KB Output is correct
29 Correct 18 ms 1876 KB Output is correct
30 Correct 33 ms 2272 KB Output is correct
31 Correct 48 ms 2644 KB Output is correct
32 Correct 49 ms 2648 KB Output is correct
33 Correct 50 ms 2644 KB Output is correct
34 Correct 50 ms 2660 KB Output is correct
35 Correct 51 ms 2644 KB Output is correct
36 Correct 54 ms 2656 KB Output is correct
37 Correct 51 ms 2644 KB Output is correct
38 Correct 47 ms 2620 KB Output is correct
39 Correct 59 ms 2664 KB Output is correct
40 Correct 52 ms 2660 KB Output is correct
41 Correct 1 ms 1492 KB Output is correct
42 Correct 1 ms 1492 KB Output is correct
43 Correct 1 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 11 ms 1664 KB Output is correct
48 Correct 8 ms 1620 KB Output is correct
49 Correct 16 ms 1904 KB Output is correct
50 Correct 35 ms 2264 KB Output is correct
51 Correct 56 ms 2656 KB Output is correct
52 Correct 61 ms 2656 KB Output is correct
53 Correct 66 ms 2680 KB Output is correct
54 Correct 56 ms 2644 KB Output is correct
55 Correct 68 ms 2684 KB Output is correct
56 Correct 60 ms 2688 KB Output is correct
57 Correct 58 ms 2644 KB Output is correct
58 Correct 57 ms 2644 KB Output is correct
59 Correct 56 ms 2652 KB Output is correct
60 Correct 65 ms 2684 KB Output is correct
61 Correct 61 ms 2656 KB Output is correct
62 Correct 59 ms 2692 KB Output is correct
63 Correct 57 ms 2656 KB Output is correct
64 Correct 59 ms 2648 KB Output is correct
65 Correct 58 ms 2680 KB Output is correct
66 Correct 69 ms 2680 KB Output is correct
67 Correct 60 ms 2660 KB Output is correct
68 Correct 57 ms 2692 KB Output is correct
69 Correct 66 ms 2648 KB Output is correct
70 Correct 56 ms 2644 KB Output is correct
71 Correct 7 ms 1620 KB Output is correct
72 Correct 6 ms 1620 KB Output is correct
73 Correct 9 ms 1708 KB Output is correct
74 Correct 8 ms 1620 KB Output is correct
75 Correct 8 ms 1620 KB Output is correct
76 Correct 13 ms 1876 KB Output is correct
77 Correct 11 ms 1876 KB Output is correct
78 Correct 12 ms 1876 KB Output is correct
79 Correct 12 ms 1876 KB Output is correct
80 Correct 11 ms 1876 KB Output is correct
81 Correct 23 ms 2276 KB Output is correct
82 Correct 22 ms 2272 KB Output is correct
83 Correct 22 ms 2264 KB Output is correct
84 Correct 21 ms 2260 KB Output is correct
85 Correct 22 ms 2260 KB Output is correct
86 Correct 22 ms 2272 KB Output is correct
87 Correct 22 ms 2272 KB Output is correct
88 Correct 22 ms 2280 KB Output is correct
89 Correct 22 ms 2272 KB Output is correct
90 Correct 28 ms 2268 KB Output is correct
91 Correct 27 ms 2660 KB Output is correct
92 Correct 29 ms 2660 KB Output is correct
93 Correct 29 ms 2660 KB Output is correct
94 Correct 27 ms 2668 KB Output is correct
95 Correct 28 ms 2660 KB Output is correct
96 Correct 28 ms 2648 KB Output is correct
97 Correct 37 ms 2644 KB Output is correct
98 Correct 39 ms 2652 KB Output is correct
99 Correct 29 ms 2648 KB Output is correct
100 Correct 34 ms 2632 KB Output is correct
101 Correct 28 ms 2660 KB Output is correct
102 Correct 30 ms 2656 KB Output is correct
103 Correct 29 ms 2656 KB Output is correct
104 Correct 28 ms 2656 KB Output is correct
105 Correct 31 ms 2660 KB Output is correct
106 Correct 36 ms 2644 KB Output is correct
107 Correct 27 ms 2656 KB Output is correct
108 Correct 28 ms 2660 KB Output is correct
109 Correct 29 ms 2664 KB Output is correct
110 Correct 29 ms 2644 KB Output is correct
111 Correct 27 ms 2644 KB Output is correct
112 Correct 26 ms 2644 KB Output is correct
113 Correct 27 ms 2644 KB Output is correct
114 Correct 27 ms 2652 KB Output is correct
115 Correct 27 ms 2656 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 1492 KB Output is correct
5 Correct 1 ms 1492 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 1404 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1492 KB Output is correct
13 Correct 2 ms 1492 KB Output is correct
14 Correct 15 ms 1876 KB Output is correct
15 Correct 27 ms 2272 KB Output is correct
16 Correct 50 ms 2604 KB Output is correct
17 Correct 38 ms 2660 KB Output is correct
18 Correct 45 ms 2772 KB Output is correct
19 Correct 42 ms 2656 KB Output is correct
20 Correct 41 ms 2644 KB Output is correct
21 Correct 42 ms 2660 KB Output is correct
22 Correct 44 ms 2660 KB Output is correct
23 Correct 43 ms 2644 KB Output is correct
24 Correct 41 ms 2780 KB Output is correct
25 Correct 44 ms 2660 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 2 ms 1492 KB Output is correct
28 Correct 3 ms 1492 KB Output is correct
29 Correct 18 ms 1876 KB Output is correct
30 Correct 33 ms 2272 KB Output is correct
31 Correct 48 ms 2644 KB Output is correct
32 Correct 49 ms 2648 KB Output is correct
33 Correct 50 ms 2644 KB Output is correct
34 Correct 50 ms 2660 KB Output is correct
35 Correct 51 ms 2644 KB Output is correct
36 Correct 54 ms 2656 KB Output is correct
37 Correct 51 ms 2644 KB Output is correct
38 Correct 47 ms 2620 KB Output is correct
39 Correct 59 ms 2664 KB Output is correct
40 Correct 52 ms 2660 KB Output is correct
41 Correct 1 ms 1492 KB Output is correct
42 Correct 1 ms 1492 KB Output is correct
43 Correct 1 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 11 ms 1664 KB Output is correct
48 Correct 8 ms 1620 KB Output is correct
49 Correct 16 ms 1904 KB Output is correct
50 Correct 35 ms 2264 KB Output is correct
51 Correct 56 ms 2656 KB Output is correct
52 Correct 61 ms 2656 KB Output is correct
53 Correct 66 ms 2680 KB Output is correct
54 Correct 56 ms 2644 KB Output is correct
55 Correct 68 ms 2684 KB Output is correct
56 Correct 60 ms 2688 KB Output is correct
57 Correct 58 ms 2644 KB Output is correct
58 Correct 57 ms 2644 KB Output is correct
59 Correct 56 ms 2652 KB Output is correct
60 Correct 65 ms 2684 KB Output is correct
61 Correct 61 ms 2656 KB Output is correct
62 Correct 59 ms 2692 KB Output is correct
63 Correct 57 ms 2656 KB Output is correct
64 Correct 59 ms 2648 KB Output is correct
65 Correct 58 ms 2680 KB Output is correct
66 Correct 69 ms 2680 KB Output is correct
67 Correct 60 ms 2660 KB Output is correct
68 Correct 57 ms 2692 KB Output is correct
69 Correct 66 ms 2648 KB Output is correct
70 Correct 56 ms 2644 KB Output is correct
71 Correct 7 ms 1620 KB Output is correct
72 Correct 6 ms 1620 KB Output is correct
73 Correct 9 ms 1708 KB Output is correct
74 Correct 8 ms 1620 KB Output is correct
75 Correct 8 ms 1620 KB Output is correct
76 Correct 13 ms 1876 KB Output is correct
77 Correct 11 ms 1876 KB Output is correct
78 Correct 12 ms 1876 KB Output is correct
79 Correct 12 ms 1876 KB Output is correct
80 Correct 11 ms 1876 KB Output is correct
81 Correct 23 ms 2276 KB Output is correct
82 Correct 22 ms 2272 KB Output is correct
83 Correct 22 ms 2264 KB Output is correct
84 Correct 21 ms 2260 KB Output is correct
85 Correct 22 ms 2260 KB Output is correct
86 Correct 22 ms 2272 KB Output is correct
87 Correct 22 ms 2272 KB Output is correct
88 Correct 22 ms 2280 KB Output is correct
89 Correct 22 ms 2272 KB Output is correct
90 Correct 28 ms 2268 KB Output is correct
91 Correct 27 ms 2660 KB Output is correct
92 Correct 29 ms 2660 KB Output is correct
93 Correct 29 ms 2660 KB Output is correct
94 Correct 27 ms 2668 KB Output is correct
95 Correct 28 ms 2660 KB Output is correct
96 Correct 28 ms 2648 KB Output is correct
97 Correct 37 ms 2644 KB Output is correct
98 Correct 39 ms 2652 KB Output is correct
99 Correct 29 ms 2648 KB Output is correct
100 Correct 34 ms 2632 KB Output is correct
101 Correct 28 ms 2660 KB Output is correct
102 Correct 30 ms 2656 KB Output is correct
103 Correct 29 ms 2656 KB Output is correct
104 Correct 28 ms 2656 KB Output is correct
105 Correct 31 ms 2660 KB Output is correct
106 Correct 36 ms 2644 KB Output is correct
107 Correct 27 ms 2656 KB Output is correct
108 Correct 28 ms 2660 KB Output is correct
109 Correct 29 ms 2664 KB Output is correct
110 Correct 29 ms 2644 KB Output is correct
111 Correct 27 ms 2644 KB Output is correct
112 Correct 26 ms 2644 KB Output is correct
113 Correct 27 ms 2644 KB Output is correct
114 Correct 27 ms 2652 KB Output is correct
115 Correct 27 ms 2656 KB Output is correct
116 Correct 2913 ms 2892 KB Output is correct
117 Correct 2740 ms 2900 KB Output is correct
118 Correct 4344 ms 3184 KB Output is correct
119 Correct 2609 ms 3184 KB Output is correct
120 Correct 1806 ms 3180 KB Output is correct
121 Correct 2997 ms 3388 KB Output is correct
122 Correct 2105 ms 3380 KB Output is correct
123 Correct 6047 ms 3900 KB Output is correct
124 Correct 2797 ms 3908 KB Output is correct
125 Correct 2152 ms 3912 KB Output is correct
126 Execution timed out 7060 ms 3652 KB Time limit exceeded
127 Halted 0 ms 0 KB -