Submission #613659

# Submission time Handle Problem Language Result Execution time Memory
613659 2022-07-30T09:00:03 Z yuto1115 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 5188 KB
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#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 = 1338;
    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';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1468 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 1472 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1488 KB Output is correct
3 Correct 2 ms 1480 KB Output is correct
4 Correct 19 ms 2004 KB Output is correct
5 Correct 27 ms 2640 KB Output is correct
6 Correct 47 ms 3284 KB Output is correct
7 Correct 49 ms 3148 KB Output is correct
8 Correct 44 ms 3284 KB Output is correct
9 Correct 43 ms 3280 KB Output is correct
10 Correct 45 ms 3324 KB Output is correct
11 Correct 50 ms 3284 KB Output is correct
12 Correct 46 ms 3324 KB Output is correct
13 Correct 45 ms 3280 KB Output is correct
14 Correct 41 ms 3280 KB Output is correct
15 Correct 44 ms 3292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1488 KB Output is correct
3 Correct 2 ms 1480 KB Output is correct
4 Correct 19 ms 2004 KB Output is correct
5 Correct 27 ms 2640 KB Output is correct
6 Correct 47 ms 3284 KB Output is correct
7 Correct 49 ms 3148 KB Output is correct
8 Correct 44 ms 3284 KB Output is correct
9 Correct 43 ms 3280 KB Output is correct
10 Correct 45 ms 3324 KB Output is correct
11 Correct 50 ms 3284 KB Output is correct
12 Correct 46 ms 3324 KB Output is correct
13 Correct 45 ms 3280 KB Output is correct
14 Correct 41 ms 3280 KB Output is correct
15 Correct 44 ms 3292 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1468 KB Output is correct
18 Correct 3 ms 1480 KB Output is correct
19 Correct 20 ms 2132 KB Output is correct
20 Correct 37 ms 2772 KB Output is correct
21 Correct 57 ms 3528 KB Output is correct
22 Correct 51 ms 3396 KB Output is correct
23 Correct 54 ms 3456 KB Output is correct
24 Correct 49 ms 3400 KB Output is correct
25 Correct 50 ms 3404 KB Output is correct
26 Correct 53 ms 3412 KB Output is correct
27 Correct 54 ms 3536 KB Output is correct
28 Correct 55 ms 3396 KB Output is correct
29 Correct 53 ms 3404 KB Output is correct
30 Correct 54 ms 3408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1488 KB Output is correct
3 Correct 2 ms 1480 KB Output is correct
4 Correct 19 ms 2004 KB Output is correct
5 Correct 27 ms 2640 KB Output is correct
6 Correct 47 ms 3284 KB Output is correct
7 Correct 49 ms 3148 KB Output is correct
8 Correct 44 ms 3284 KB Output is correct
9 Correct 43 ms 3280 KB Output is correct
10 Correct 45 ms 3324 KB Output is correct
11 Correct 50 ms 3284 KB Output is correct
12 Correct 46 ms 3324 KB Output is correct
13 Correct 45 ms 3280 KB Output is correct
14 Correct 41 ms 3280 KB Output is correct
15 Correct 44 ms 3292 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1468 KB Output is correct
18 Correct 3 ms 1480 KB Output is correct
19 Correct 20 ms 2132 KB Output is correct
20 Correct 37 ms 2772 KB Output is correct
21 Correct 57 ms 3528 KB Output is correct
22 Correct 51 ms 3396 KB Output is correct
23 Correct 54 ms 3456 KB Output is correct
24 Correct 49 ms 3400 KB Output is correct
25 Correct 50 ms 3404 KB Output is correct
26 Correct 53 ms 3412 KB Output is correct
27 Correct 54 ms 3536 KB Output is correct
28 Correct 55 ms 3396 KB Output is correct
29 Correct 53 ms 3404 KB Output is correct
30 Correct 54 ms 3408 KB Output is correct
31 Correct 1 ms 1472 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 2 ms 1492 KB Output is correct
36 Correct 2 ms 1484 KB Output is correct
37 Correct 12 ms 1876 KB Output is correct
38 Correct 8 ms 1876 KB Output is correct
39 Correct 22 ms 2284 KB Output is correct
40 Correct 40 ms 3020 KB Output is correct
41 Correct 60 ms 3820 KB Output is correct
42 Correct 62 ms 3836 KB Output is correct
43 Correct 60 ms 3824 KB Output is correct
44 Correct 62 ms 3788 KB Output is correct
45 Correct 72 ms 3784 KB Output is correct
46 Correct 60 ms 3792 KB Output is correct
47 Correct 57 ms 3796 KB Output is correct
48 Correct 58 ms 3796 KB Output is correct
49 Correct 62 ms 3784 KB Output is correct
50 Correct 62 ms 3912 KB Output is correct
51 Correct 63 ms 3788 KB Output is correct
52 Correct 57 ms 3800 KB Output is correct
53 Correct 58 ms 3788 KB Output is correct
54 Correct 57 ms 3776 KB Output is correct
55 Correct 58 ms 3832 KB Output is correct
56 Correct 59 ms 3828 KB Output is correct
57 Correct 60 ms 3812 KB Output is correct
58 Correct 57 ms 3784 KB Output is correct
59 Correct 61 ms 3792 KB Output is correct
60 Correct 61 ms 3820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1468 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 1472 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 1488 KB Output is correct
13 Correct 2 ms 1480 KB Output is correct
14 Correct 19 ms 2004 KB Output is correct
15 Correct 27 ms 2640 KB Output is correct
16 Correct 47 ms 3284 KB Output is correct
17 Correct 49 ms 3148 KB Output is correct
18 Correct 44 ms 3284 KB Output is correct
19 Correct 43 ms 3280 KB Output is correct
20 Correct 45 ms 3324 KB Output is correct
21 Correct 50 ms 3284 KB Output is correct
22 Correct 46 ms 3324 KB Output is correct
23 Correct 45 ms 3280 KB Output is correct
24 Correct 41 ms 3280 KB Output is correct
25 Correct 44 ms 3292 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1468 KB Output is correct
28 Correct 3 ms 1480 KB Output is correct
29 Correct 20 ms 2132 KB Output is correct
30 Correct 37 ms 2772 KB Output is correct
31 Correct 57 ms 3528 KB Output is correct
32 Correct 51 ms 3396 KB Output is correct
33 Correct 54 ms 3456 KB Output is correct
34 Correct 49 ms 3400 KB Output is correct
35 Correct 50 ms 3404 KB Output is correct
36 Correct 53 ms 3412 KB Output is correct
37 Correct 54 ms 3536 KB Output is correct
38 Correct 55 ms 3396 KB Output is correct
39 Correct 53 ms 3404 KB Output is correct
40 Correct 54 ms 3408 KB Output is correct
41 Correct 1 ms 1472 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 2 ms 1492 KB Output is correct
46 Correct 2 ms 1484 KB Output is correct
47 Correct 12 ms 1876 KB Output is correct
48 Correct 8 ms 1876 KB Output is correct
49 Correct 22 ms 2284 KB Output is correct
50 Correct 40 ms 3020 KB Output is correct
51 Correct 60 ms 3820 KB Output is correct
52 Correct 62 ms 3836 KB Output is correct
53 Correct 60 ms 3824 KB Output is correct
54 Correct 62 ms 3788 KB Output is correct
55 Correct 72 ms 3784 KB Output is correct
56 Correct 60 ms 3792 KB Output is correct
57 Correct 57 ms 3796 KB Output is correct
58 Correct 58 ms 3796 KB Output is correct
59 Correct 62 ms 3784 KB Output is correct
60 Correct 62 ms 3912 KB Output is correct
61 Correct 63 ms 3788 KB Output is correct
62 Correct 57 ms 3800 KB Output is correct
63 Correct 58 ms 3788 KB Output is correct
64 Correct 57 ms 3776 KB Output is correct
65 Correct 58 ms 3832 KB Output is correct
66 Correct 59 ms 3828 KB Output is correct
67 Correct 60 ms 3812 KB Output is correct
68 Correct 57 ms 3784 KB Output is correct
69 Correct 61 ms 3792 KB Output is correct
70 Correct 61 ms 3820 KB Output is correct
71 Correct 8 ms 1876 KB Output is correct
72 Correct 6 ms 1876 KB Output is correct
73 Correct 9 ms 1876 KB Output is correct
74 Correct 7 ms 1864 KB Output is correct
75 Correct 11 ms 1876 KB Output is correct
76 Correct 16 ms 2356 KB Output is correct
77 Correct 12 ms 2344 KB Output is correct
78 Correct 12 ms 2260 KB Output is correct
79 Correct 12 ms 2260 KB Output is correct
80 Correct 12 ms 2240 KB Output is correct
81 Correct 23 ms 3212 KB Output is correct
82 Correct 23 ms 3156 KB Output is correct
83 Correct 23 ms 3140 KB Output is correct
84 Correct 24 ms 3144 KB Output is correct
85 Correct 28 ms 3148 KB Output is correct
86 Correct 28 ms 3272 KB Output is correct
87 Correct 24 ms 3280 KB Output is correct
88 Correct 24 ms 3340 KB Output is correct
89 Correct 24 ms 3272 KB Output is correct
90 Correct 22 ms 3272 KB Output is correct
91 Correct 29 ms 3792 KB Output is correct
92 Correct 31 ms 3816 KB Output is correct
93 Correct 36 ms 4040 KB Output is correct
94 Correct 42 ms 3900 KB Output is correct
95 Correct 32 ms 3772 KB Output is correct
96 Correct 32 ms 3696 KB Output is correct
97 Correct 33 ms 3788 KB Output is correct
98 Correct 31 ms 3804 KB Output is correct
99 Correct 32 ms 3808 KB Output is correct
100 Correct 29 ms 3788 KB Output is correct
101 Correct 28 ms 3784 KB Output is correct
102 Correct 40 ms 3916 KB Output is correct
103 Correct 37 ms 3792 KB Output is correct
104 Correct 34 ms 3792 KB Output is correct
105 Correct 36 ms 3788 KB Output is correct
106 Correct 30 ms 3772 KB Output is correct
107 Correct 29 ms 3788 KB Output is correct
108 Correct 28 ms 3752 KB Output is correct
109 Correct 30 ms 3788 KB Output is correct
110 Correct 33 ms 3796 KB Output is correct
111 Correct 30 ms 3960 KB Output is correct
112 Correct 30 ms 3776 KB Output is correct
113 Correct 30 ms 3788 KB Output is correct
114 Correct 38 ms 3788 KB Output is correct
115 Correct 29 ms 3788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1468 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 1472 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 1488 KB Output is correct
13 Correct 2 ms 1480 KB Output is correct
14 Correct 19 ms 2004 KB Output is correct
15 Correct 27 ms 2640 KB Output is correct
16 Correct 47 ms 3284 KB Output is correct
17 Correct 49 ms 3148 KB Output is correct
18 Correct 44 ms 3284 KB Output is correct
19 Correct 43 ms 3280 KB Output is correct
20 Correct 45 ms 3324 KB Output is correct
21 Correct 50 ms 3284 KB Output is correct
22 Correct 46 ms 3324 KB Output is correct
23 Correct 45 ms 3280 KB Output is correct
24 Correct 41 ms 3280 KB Output is correct
25 Correct 44 ms 3292 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1468 KB Output is correct
28 Correct 3 ms 1480 KB Output is correct
29 Correct 20 ms 2132 KB Output is correct
30 Correct 37 ms 2772 KB Output is correct
31 Correct 57 ms 3528 KB Output is correct
32 Correct 51 ms 3396 KB Output is correct
33 Correct 54 ms 3456 KB Output is correct
34 Correct 49 ms 3400 KB Output is correct
35 Correct 50 ms 3404 KB Output is correct
36 Correct 53 ms 3412 KB Output is correct
37 Correct 54 ms 3536 KB Output is correct
38 Correct 55 ms 3396 KB Output is correct
39 Correct 53 ms 3404 KB Output is correct
40 Correct 54 ms 3408 KB Output is correct
41 Correct 1 ms 1472 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 2 ms 1492 KB Output is correct
46 Correct 2 ms 1484 KB Output is correct
47 Correct 12 ms 1876 KB Output is correct
48 Correct 8 ms 1876 KB Output is correct
49 Correct 22 ms 2284 KB Output is correct
50 Correct 40 ms 3020 KB Output is correct
51 Correct 60 ms 3820 KB Output is correct
52 Correct 62 ms 3836 KB Output is correct
53 Correct 60 ms 3824 KB Output is correct
54 Correct 62 ms 3788 KB Output is correct
55 Correct 72 ms 3784 KB Output is correct
56 Correct 60 ms 3792 KB Output is correct
57 Correct 57 ms 3796 KB Output is correct
58 Correct 58 ms 3796 KB Output is correct
59 Correct 62 ms 3784 KB Output is correct
60 Correct 62 ms 3912 KB Output is correct
61 Correct 63 ms 3788 KB Output is correct
62 Correct 57 ms 3800 KB Output is correct
63 Correct 58 ms 3788 KB Output is correct
64 Correct 57 ms 3776 KB Output is correct
65 Correct 58 ms 3832 KB Output is correct
66 Correct 59 ms 3828 KB Output is correct
67 Correct 60 ms 3812 KB Output is correct
68 Correct 57 ms 3784 KB Output is correct
69 Correct 61 ms 3792 KB Output is correct
70 Correct 61 ms 3820 KB Output is correct
71 Correct 8 ms 1876 KB Output is correct
72 Correct 6 ms 1876 KB Output is correct
73 Correct 9 ms 1876 KB Output is correct
74 Correct 7 ms 1864 KB Output is correct
75 Correct 11 ms 1876 KB Output is correct
76 Correct 16 ms 2356 KB Output is correct
77 Correct 12 ms 2344 KB Output is correct
78 Correct 12 ms 2260 KB Output is correct
79 Correct 12 ms 2260 KB Output is correct
80 Correct 12 ms 2240 KB Output is correct
81 Correct 23 ms 3212 KB Output is correct
82 Correct 23 ms 3156 KB Output is correct
83 Correct 23 ms 3140 KB Output is correct
84 Correct 24 ms 3144 KB Output is correct
85 Correct 28 ms 3148 KB Output is correct
86 Correct 28 ms 3272 KB Output is correct
87 Correct 24 ms 3280 KB Output is correct
88 Correct 24 ms 3340 KB Output is correct
89 Correct 24 ms 3272 KB Output is correct
90 Correct 22 ms 3272 KB Output is correct
91 Correct 29 ms 3792 KB Output is correct
92 Correct 31 ms 3816 KB Output is correct
93 Correct 36 ms 4040 KB Output is correct
94 Correct 42 ms 3900 KB Output is correct
95 Correct 32 ms 3772 KB Output is correct
96 Correct 32 ms 3696 KB Output is correct
97 Correct 33 ms 3788 KB Output is correct
98 Correct 31 ms 3804 KB Output is correct
99 Correct 32 ms 3808 KB Output is correct
100 Correct 29 ms 3788 KB Output is correct
101 Correct 28 ms 3784 KB Output is correct
102 Correct 40 ms 3916 KB Output is correct
103 Correct 37 ms 3792 KB Output is correct
104 Correct 34 ms 3792 KB Output is correct
105 Correct 36 ms 3788 KB Output is correct
106 Correct 30 ms 3772 KB Output is correct
107 Correct 29 ms 3788 KB Output is correct
108 Correct 28 ms 3752 KB Output is correct
109 Correct 30 ms 3788 KB Output is correct
110 Correct 33 ms 3796 KB Output is correct
111 Correct 30 ms 3960 KB Output is correct
112 Correct 30 ms 3776 KB Output is correct
113 Correct 30 ms 3788 KB Output is correct
114 Correct 38 ms 3788 KB Output is correct
115 Correct 29 ms 3788 KB Output is correct
116 Correct 2563 ms 3400 KB Output is correct
117 Correct 2235 ms 3404 KB Output is correct
118 Correct 3698 ms 3896 KB Output is correct
119 Correct 2259 ms 4060 KB Output is correct
120 Correct 1520 ms 3944 KB Output is correct
121 Correct 2718 ms 4348 KB Output is correct
122 Correct 1881 ms 4396 KB Output is correct
123 Correct 5840 ms 5188 KB Output is correct
124 Correct 3031 ms 5188 KB Output is correct
125 Correct 2285 ms 5184 KB Output is correct
126 Execution timed out 7010 ms 4928 KB Time limit exceeded
127 Halted 0 ms 0 KB -