Submission #613661

# Submission time Handle Problem Language Result Execution time Memory
613661 2022-07-30T09:01:02 Z yuto1115 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 3908 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#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 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 1488 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 1492 KB Output is correct
3 Correct 4 ms 1492 KB Output is correct
4 Correct 14 ms 1876 KB Output is correct
5 Correct 29 ms 2260 KB Output is correct
6 Correct 54 ms 2660 KB Output is correct
7 Correct 47 ms 2652 KB Output is correct
8 Correct 43 ms 2656 KB Output is correct
9 Correct 42 ms 2644 KB Output is correct
10 Correct 42 ms 2644 KB Output is correct
11 Correct 43 ms 2644 KB Output is correct
12 Correct 44 ms 2656 KB Output is correct
13 Correct 43 ms 2656 KB Output is correct
14 Correct 45 ms 2656 KB Output is correct
15 Correct 43 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 4 ms 1492 KB Output is correct
4 Correct 14 ms 1876 KB Output is correct
5 Correct 29 ms 2260 KB Output is correct
6 Correct 54 ms 2660 KB Output is correct
7 Correct 47 ms 2652 KB Output is correct
8 Correct 43 ms 2656 KB Output is correct
9 Correct 42 ms 2644 KB Output is correct
10 Correct 42 ms 2644 KB Output is correct
11 Correct 43 ms 2644 KB Output is correct
12 Correct 44 ms 2656 KB Output is correct
13 Correct 43 ms 2656 KB Output is correct
14 Correct 45 ms 2656 KB Output is correct
15 Correct 43 ms 2644 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 2 ms 1492 KB Output is correct
19 Correct 16 ms 1876 KB Output is correct
20 Correct 32 ms 2272 KB Output is correct
21 Correct 53 ms 2772 KB Output is correct
22 Correct 54 ms 2656 KB Output is correct
23 Correct 48 ms 2656 KB Output is correct
24 Correct 51 ms 2656 KB Output is correct
25 Correct 53 ms 2660 KB Output is correct
26 Correct 50 ms 2656 KB Output is correct
27 Correct 56 ms 2660 KB Output is correct
28 Correct 53 ms 2660 KB Output is correct
29 Correct 59 ms 2644 KB Output is correct
30 Correct 51 ms 2656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 4 ms 1492 KB Output is correct
4 Correct 14 ms 1876 KB Output is correct
5 Correct 29 ms 2260 KB Output is correct
6 Correct 54 ms 2660 KB Output is correct
7 Correct 47 ms 2652 KB Output is correct
8 Correct 43 ms 2656 KB Output is correct
9 Correct 42 ms 2644 KB Output is correct
10 Correct 42 ms 2644 KB Output is correct
11 Correct 43 ms 2644 KB Output is correct
12 Correct 44 ms 2656 KB Output is correct
13 Correct 43 ms 2656 KB Output is correct
14 Correct 45 ms 2656 KB Output is correct
15 Correct 43 ms 2644 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 2 ms 1492 KB Output is correct
19 Correct 16 ms 1876 KB Output is correct
20 Correct 32 ms 2272 KB Output is correct
21 Correct 53 ms 2772 KB Output is correct
22 Correct 54 ms 2656 KB Output is correct
23 Correct 48 ms 2656 KB Output is correct
24 Correct 51 ms 2656 KB Output is correct
25 Correct 53 ms 2660 KB Output is correct
26 Correct 50 ms 2656 KB Output is correct
27 Correct 56 ms 2660 KB Output is correct
28 Correct 53 ms 2660 KB Output is correct
29 Correct 59 ms 2644 KB Output is correct
30 Correct 51 ms 2656 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 2 ms 1492 KB Output is correct
35 Correct 2 ms 1492 KB Output is correct
36 Correct 2 ms 1492 KB Output is correct
37 Correct 9 ms 1680 KB Output is correct
38 Correct 7 ms 1620 KB Output is correct
39 Correct 16 ms 1900 KB Output is correct
40 Correct 44 ms 2260 KB Output is correct
41 Correct 59 ms 2672 KB Output is correct
42 Correct 59 ms 2672 KB Output is correct
43 Correct 61 ms 2668 KB Output is correct
44 Correct 61 ms 2660 KB Output is correct
45 Correct 69 ms 2680 KB Output is correct
46 Correct 60 ms 2684 KB Output is correct
47 Correct 60 ms 2684 KB Output is correct
48 Correct 68 ms 2672 KB Output is correct
49 Correct 58 ms 2684 KB Output is correct
50 Correct 59 ms 2692 KB Output is correct
51 Correct 59 ms 2656 KB Output is correct
52 Correct 64 ms 2684 KB Output is correct
53 Correct 61 ms 2688 KB Output is correct
54 Correct 69 ms 2684 KB Output is correct
55 Correct 58 ms 2672 KB Output is correct
56 Correct 57 ms 2696 KB Output is correct
57 Correct 57 ms 2660 KB Output is correct
58 Correct 59 ms 2676 KB Output is correct
59 Correct 61 ms 2644 KB Output is correct
60 Correct 62 ms 2676 KB Output is correct
# Verdict Execution time Memory 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 1488 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 4 ms 1492 KB Output is correct
14 Correct 14 ms 1876 KB Output is correct
15 Correct 29 ms 2260 KB Output is correct
16 Correct 54 ms 2660 KB Output is correct
17 Correct 47 ms 2652 KB Output is correct
18 Correct 43 ms 2656 KB Output is correct
19 Correct 42 ms 2644 KB Output is correct
20 Correct 42 ms 2644 KB Output is correct
21 Correct 43 ms 2644 KB Output is correct
22 Correct 44 ms 2656 KB Output is correct
23 Correct 43 ms 2656 KB Output is correct
24 Correct 45 ms 2656 KB Output is correct
25 Correct 43 ms 2644 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 2 ms 1492 KB Output is correct
29 Correct 16 ms 1876 KB Output is correct
30 Correct 32 ms 2272 KB Output is correct
31 Correct 53 ms 2772 KB Output is correct
32 Correct 54 ms 2656 KB Output is correct
33 Correct 48 ms 2656 KB Output is correct
34 Correct 51 ms 2656 KB Output is correct
35 Correct 53 ms 2660 KB Output is correct
36 Correct 50 ms 2656 KB Output is correct
37 Correct 56 ms 2660 KB Output is correct
38 Correct 53 ms 2660 KB Output is correct
39 Correct 59 ms 2644 KB Output is correct
40 Correct 51 ms 2656 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 2 ms 1492 KB Output is correct
45 Correct 2 ms 1492 KB Output is correct
46 Correct 2 ms 1492 KB Output is correct
47 Correct 9 ms 1680 KB Output is correct
48 Correct 7 ms 1620 KB Output is correct
49 Correct 16 ms 1900 KB Output is correct
50 Correct 44 ms 2260 KB Output is correct
51 Correct 59 ms 2672 KB Output is correct
52 Correct 59 ms 2672 KB Output is correct
53 Correct 61 ms 2668 KB Output is correct
54 Correct 61 ms 2660 KB Output is correct
55 Correct 69 ms 2680 KB Output is correct
56 Correct 60 ms 2684 KB Output is correct
57 Correct 60 ms 2684 KB Output is correct
58 Correct 68 ms 2672 KB Output is correct
59 Correct 58 ms 2684 KB Output is correct
60 Correct 59 ms 2692 KB Output is correct
61 Correct 59 ms 2656 KB Output is correct
62 Correct 64 ms 2684 KB Output is correct
63 Correct 61 ms 2688 KB Output is correct
64 Correct 69 ms 2684 KB Output is correct
65 Correct 58 ms 2672 KB Output is correct
66 Correct 57 ms 2696 KB Output is correct
67 Correct 57 ms 2660 KB Output is correct
68 Correct 59 ms 2676 KB Output is correct
69 Correct 61 ms 2644 KB Output is correct
70 Correct 62 ms 2676 KB Output is correct
71 Correct 6 ms 1620 KB Output is correct
72 Correct 6 ms 1620 KB Output is correct
73 Correct 7 ms 1620 KB Output is correct
74 Correct 7 ms 1620 KB Output is correct
75 Correct 8 ms 1620 KB Output is correct
76 Correct 16 ms 1876 KB Output is correct
77 Correct 14 ms 1872 KB Output is correct
78 Correct 11 ms 1876 KB Output is correct
79 Correct 11 ms 1972 KB Output is correct
80 Correct 13 ms 1876 KB Output is correct
81 Correct 30 ms 2260 KB Output is correct
82 Correct 25 ms 2268 KB Output is correct
83 Correct 22 ms 2260 KB Output is correct
84 Correct 28 ms 2264 KB Output is correct
85 Correct 23 ms 2300 KB Output is correct
86 Correct 26 ms 2260 KB Output is correct
87 Correct 22 ms 2272 KB Output is correct
88 Correct 21 ms 2292 KB Output is correct
89 Correct 23 ms 2268 KB Output is correct
90 Correct 23 ms 2260 KB Output is correct
91 Correct 32 ms 2660 KB Output is correct
92 Correct 36 ms 2636 KB Output is correct
93 Correct 35 ms 2636 KB Output is correct
94 Correct 28 ms 2648 KB Output is correct
95 Correct 35 ms 2636 KB Output is correct
96 Correct 28 ms 2636 KB Output is correct
97 Correct 29 ms 2664 KB Output is correct
98 Correct 28 ms 2652 KB Output is correct
99 Correct 29 ms 2640 KB Output is correct
100 Correct 31 ms 2660 KB Output is correct
101 Correct 28 ms 2644 KB Output is correct
102 Correct 30 ms 2660 KB Output is correct
103 Correct 33 ms 2656 KB Output is correct
104 Correct 30 ms 2648 KB Output is correct
105 Correct 29 ms 2636 KB Output is correct
106 Correct 26 ms 2680 KB Output is correct
107 Correct 27 ms 2636 KB Output is correct
108 Correct 27 ms 2652 KB Output is correct
109 Correct 38 ms 2632 KB Output is correct
110 Correct 27 ms 2660 KB Output is correct
111 Correct 30 ms 2656 KB Output is correct
112 Correct 30 ms 2644 KB Output is correct
113 Correct 26 ms 2660 KB Output is correct
114 Correct 27 ms 2656 KB Output is correct
115 Correct 27 ms 2644 KB Output is correct
# Verdict Execution time Memory 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 1488 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 4 ms 1492 KB Output is correct
14 Correct 14 ms 1876 KB Output is correct
15 Correct 29 ms 2260 KB Output is correct
16 Correct 54 ms 2660 KB Output is correct
17 Correct 47 ms 2652 KB Output is correct
18 Correct 43 ms 2656 KB Output is correct
19 Correct 42 ms 2644 KB Output is correct
20 Correct 42 ms 2644 KB Output is correct
21 Correct 43 ms 2644 KB Output is correct
22 Correct 44 ms 2656 KB Output is correct
23 Correct 43 ms 2656 KB Output is correct
24 Correct 45 ms 2656 KB Output is correct
25 Correct 43 ms 2644 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 2 ms 1492 KB Output is correct
29 Correct 16 ms 1876 KB Output is correct
30 Correct 32 ms 2272 KB Output is correct
31 Correct 53 ms 2772 KB Output is correct
32 Correct 54 ms 2656 KB Output is correct
33 Correct 48 ms 2656 KB Output is correct
34 Correct 51 ms 2656 KB Output is correct
35 Correct 53 ms 2660 KB Output is correct
36 Correct 50 ms 2656 KB Output is correct
37 Correct 56 ms 2660 KB Output is correct
38 Correct 53 ms 2660 KB Output is correct
39 Correct 59 ms 2644 KB Output is correct
40 Correct 51 ms 2656 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 2 ms 1492 KB Output is correct
45 Correct 2 ms 1492 KB Output is correct
46 Correct 2 ms 1492 KB Output is correct
47 Correct 9 ms 1680 KB Output is correct
48 Correct 7 ms 1620 KB Output is correct
49 Correct 16 ms 1900 KB Output is correct
50 Correct 44 ms 2260 KB Output is correct
51 Correct 59 ms 2672 KB Output is correct
52 Correct 59 ms 2672 KB Output is correct
53 Correct 61 ms 2668 KB Output is correct
54 Correct 61 ms 2660 KB Output is correct
55 Correct 69 ms 2680 KB Output is correct
56 Correct 60 ms 2684 KB Output is correct
57 Correct 60 ms 2684 KB Output is correct
58 Correct 68 ms 2672 KB Output is correct
59 Correct 58 ms 2684 KB Output is correct
60 Correct 59 ms 2692 KB Output is correct
61 Correct 59 ms 2656 KB Output is correct
62 Correct 64 ms 2684 KB Output is correct
63 Correct 61 ms 2688 KB Output is correct
64 Correct 69 ms 2684 KB Output is correct
65 Correct 58 ms 2672 KB Output is correct
66 Correct 57 ms 2696 KB Output is correct
67 Correct 57 ms 2660 KB Output is correct
68 Correct 59 ms 2676 KB Output is correct
69 Correct 61 ms 2644 KB Output is correct
70 Correct 62 ms 2676 KB Output is correct
71 Correct 6 ms 1620 KB Output is correct
72 Correct 6 ms 1620 KB Output is correct
73 Correct 7 ms 1620 KB Output is correct
74 Correct 7 ms 1620 KB Output is correct
75 Correct 8 ms 1620 KB Output is correct
76 Correct 16 ms 1876 KB Output is correct
77 Correct 14 ms 1872 KB Output is correct
78 Correct 11 ms 1876 KB Output is correct
79 Correct 11 ms 1972 KB Output is correct
80 Correct 13 ms 1876 KB Output is correct
81 Correct 30 ms 2260 KB Output is correct
82 Correct 25 ms 2268 KB Output is correct
83 Correct 22 ms 2260 KB Output is correct
84 Correct 28 ms 2264 KB Output is correct
85 Correct 23 ms 2300 KB Output is correct
86 Correct 26 ms 2260 KB Output is correct
87 Correct 22 ms 2272 KB Output is correct
88 Correct 21 ms 2292 KB Output is correct
89 Correct 23 ms 2268 KB Output is correct
90 Correct 23 ms 2260 KB Output is correct
91 Correct 32 ms 2660 KB Output is correct
92 Correct 36 ms 2636 KB Output is correct
93 Correct 35 ms 2636 KB Output is correct
94 Correct 28 ms 2648 KB Output is correct
95 Correct 35 ms 2636 KB Output is correct
96 Correct 28 ms 2636 KB Output is correct
97 Correct 29 ms 2664 KB Output is correct
98 Correct 28 ms 2652 KB Output is correct
99 Correct 29 ms 2640 KB Output is correct
100 Correct 31 ms 2660 KB Output is correct
101 Correct 28 ms 2644 KB Output is correct
102 Correct 30 ms 2660 KB Output is correct
103 Correct 33 ms 2656 KB Output is correct
104 Correct 30 ms 2648 KB Output is correct
105 Correct 29 ms 2636 KB Output is correct
106 Correct 26 ms 2680 KB Output is correct
107 Correct 27 ms 2636 KB Output is correct
108 Correct 27 ms 2652 KB Output is correct
109 Correct 38 ms 2632 KB Output is correct
110 Correct 27 ms 2660 KB Output is correct
111 Correct 30 ms 2656 KB Output is correct
112 Correct 30 ms 2644 KB Output is correct
113 Correct 26 ms 2660 KB Output is correct
114 Correct 27 ms 2656 KB Output is correct
115 Correct 27 ms 2644 KB Output is correct
116 Correct 2776 ms 2892 KB Output is correct
117 Correct 2354 ms 2892 KB Output is correct
118 Correct 3721 ms 3176 KB Output is correct
119 Correct 2313 ms 3184 KB Output is correct
120 Correct 1570 ms 3192 KB Output is correct
121 Correct 2714 ms 3384 KB Output is correct
122 Correct 1939 ms 3384 KB Output is correct
123 Correct 6313 ms 3908 KB Output is correct
124 Correct 3060 ms 3904 KB Output is correct
125 Correct 2155 ms 3900 KB Output is correct
126 Execution timed out 7092 ms 3652 KB Time limit exceeded
127 Halted 0 ms 0 KB -