답안 #613633

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
613633 2022-07-30T08:39:30 Z yuto1115 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 5620 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 = min(n, 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';
}
# 결과 실행 시간 메모리 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 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 2 ms 1492 KB Output is correct
4 Correct 13 ms 1876 KB Output is correct
5 Correct 29 ms 2272 KB Output is correct
6 Correct 45 ms 2656 KB Output is correct
7 Correct 48 ms 2652 KB Output is correct
8 Correct 43 ms 2644 KB Output is correct
9 Correct 43 ms 2656 KB Output is correct
10 Correct 41 ms 2660 KB Output is correct
11 Correct 43 ms 2664 KB Output is correct
12 Correct 42 ms 2656 KB Output is correct
13 Correct 43 ms 2660 KB Output is correct
14 Correct 42 ms 2632 KB Output is correct
15 Correct 45 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 2 ms 1492 KB Output is correct
4 Correct 13 ms 1876 KB Output is correct
5 Correct 29 ms 2272 KB Output is correct
6 Correct 45 ms 2656 KB Output is correct
7 Correct 48 ms 2652 KB Output is correct
8 Correct 43 ms 2644 KB Output is correct
9 Correct 43 ms 2656 KB Output is correct
10 Correct 41 ms 2660 KB Output is correct
11 Correct 43 ms 2664 KB Output is correct
12 Correct 42 ms 2656 KB Output is correct
13 Correct 43 ms 2660 KB Output is correct
14 Correct 42 ms 2632 KB Output is correct
15 Correct 45 ms 2644 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 2 ms 1580 KB Output is correct
18 Correct 2 ms 1492 KB Output is correct
19 Correct 16 ms 1876 KB Output is correct
20 Correct 41 ms 2264 KB Output is correct
21 Correct 48 ms 2644 KB Output is correct
22 Correct 49 ms 2660 KB Output is correct
23 Correct 48 ms 2644 KB Output is correct
24 Correct 50 ms 2644 KB Output is correct
25 Correct 55 ms 2656 KB Output is correct
26 Correct 50 ms 2764 KB Output is correct
27 Correct 49 ms 2652 KB Output is correct
28 Correct 47 ms 2644 KB Output is correct
29 Correct 50 ms 2660 KB Output is correct
30 Correct 50 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 2 ms 1492 KB Output is correct
4 Correct 13 ms 1876 KB Output is correct
5 Correct 29 ms 2272 KB Output is correct
6 Correct 45 ms 2656 KB Output is correct
7 Correct 48 ms 2652 KB Output is correct
8 Correct 43 ms 2644 KB Output is correct
9 Correct 43 ms 2656 KB Output is correct
10 Correct 41 ms 2660 KB Output is correct
11 Correct 43 ms 2664 KB Output is correct
12 Correct 42 ms 2656 KB Output is correct
13 Correct 43 ms 2660 KB Output is correct
14 Correct 42 ms 2632 KB Output is correct
15 Correct 45 ms 2644 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 2 ms 1580 KB Output is correct
18 Correct 2 ms 1492 KB Output is correct
19 Correct 16 ms 1876 KB Output is correct
20 Correct 41 ms 2264 KB Output is correct
21 Correct 48 ms 2644 KB Output is correct
22 Correct 49 ms 2660 KB Output is correct
23 Correct 48 ms 2644 KB Output is correct
24 Correct 50 ms 2644 KB Output is correct
25 Correct 55 ms 2656 KB Output is correct
26 Correct 50 ms 2764 KB Output is correct
27 Correct 49 ms 2652 KB Output is correct
28 Correct 47 ms 2644 KB Output is correct
29 Correct 50 ms 2660 KB Output is correct
30 Correct 50 ms 2644 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 13 ms 1620 KB Output is correct
38 Correct 7 ms 1620 KB Output is correct
39 Correct 16 ms 1876 KB Output is correct
40 Correct 35 ms 2268 KB Output is correct
41 Correct 56 ms 2636 KB Output is correct
42 Correct 58 ms 2660 KB Output is correct
43 Correct 57 ms 2680 KB Output is correct
44 Correct 57 ms 2680 KB Output is correct
45 Correct 59 ms 2820 KB Output is correct
46 Correct 56 ms 2676 KB Output is correct
47 Correct 57 ms 2660 KB Output is correct
48 Correct 57 ms 2684 KB Output is correct
49 Correct 56 ms 2656 KB Output is correct
50 Correct 56 ms 2644 KB Output is correct
51 Correct 57 ms 2680 KB Output is correct
52 Correct 59 ms 2692 KB Output is correct
53 Correct 57 ms 2684 KB Output is correct
54 Correct 58 ms 2644 KB Output is correct
55 Correct 55 ms 2652 KB Output is correct
56 Correct 59 ms 2664 KB Output is correct
57 Correct 54 ms 2644 KB Output is correct
58 Correct 56 ms 2676 KB Output is correct
59 Correct 58 ms 2656 KB Output is correct
60 Correct 59 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 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 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 2 ms 1492 KB Output is correct
14 Correct 13 ms 1876 KB Output is correct
15 Correct 29 ms 2272 KB Output is correct
16 Correct 45 ms 2656 KB Output is correct
17 Correct 48 ms 2652 KB Output is correct
18 Correct 43 ms 2644 KB Output is correct
19 Correct 43 ms 2656 KB Output is correct
20 Correct 41 ms 2660 KB Output is correct
21 Correct 43 ms 2664 KB Output is correct
22 Correct 42 ms 2656 KB Output is correct
23 Correct 43 ms 2660 KB Output is correct
24 Correct 42 ms 2632 KB Output is correct
25 Correct 45 ms 2644 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 2 ms 1580 KB Output is correct
28 Correct 2 ms 1492 KB Output is correct
29 Correct 16 ms 1876 KB Output is correct
30 Correct 41 ms 2264 KB Output is correct
31 Correct 48 ms 2644 KB Output is correct
32 Correct 49 ms 2660 KB Output is correct
33 Correct 48 ms 2644 KB Output is correct
34 Correct 50 ms 2644 KB Output is correct
35 Correct 55 ms 2656 KB Output is correct
36 Correct 50 ms 2764 KB Output is correct
37 Correct 49 ms 2652 KB Output is correct
38 Correct 47 ms 2644 KB Output is correct
39 Correct 50 ms 2660 KB Output is correct
40 Correct 50 ms 2644 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 13 ms 1620 KB Output is correct
48 Correct 7 ms 1620 KB Output is correct
49 Correct 16 ms 1876 KB Output is correct
50 Correct 35 ms 2268 KB Output is correct
51 Correct 56 ms 2636 KB Output is correct
52 Correct 58 ms 2660 KB Output is correct
53 Correct 57 ms 2680 KB Output is correct
54 Correct 57 ms 2680 KB Output is correct
55 Correct 59 ms 2820 KB Output is correct
56 Correct 56 ms 2676 KB Output is correct
57 Correct 57 ms 2660 KB Output is correct
58 Correct 57 ms 2684 KB Output is correct
59 Correct 56 ms 2656 KB Output is correct
60 Correct 56 ms 2644 KB Output is correct
61 Correct 57 ms 2680 KB Output is correct
62 Correct 59 ms 2692 KB Output is correct
63 Correct 57 ms 2684 KB Output is correct
64 Correct 58 ms 2644 KB Output is correct
65 Correct 55 ms 2652 KB Output is correct
66 Correct 59 ms 2664 KB Output is correct
67 Correct 54 ms 2644 KB Output is correct
68 Correct 56 ms 2676 KB Output is correct
69 Correct 58 ms 2656 KB Output is correct
70 Correct 59 ms 2660 KB Output is correct
71 Correct 7 ms 1620 KB Output is correct
72 Correct 6 ms 1620 KB Output is correct
73 Correct 6 ms 1620 KB Output is correct
74 Correct 8 ms 1620 KB Output is correct
75 Correct 7 ms 1620 KB Output is correct
76 Correct 12 ms 1876 KB Output is correct
77 Correct 11 ms 1876 KB Output is correct
78 Correct 11 ms 1880 KB Output is correct
79 Correct 12 ms 1884 KB Output is correct
80 Correct 12 ms 1876 KB Output is correct
81 Correct 21 ms 2260 KB Output is correct
82 Correct 21 ms 2276 KB Output is correct
83 Correct 22 ms 2272 KB Output is correct
84 Correct 23 ms 2260 KB Output is correct
85 Correct 22 ms 2268 KB Output is correct
86 Correct 25 ms 2392 KB Output is correct
87 Correct 22 ms 2268 KB Output is correct
88 Correct 22 ms 2260 KB Output is correct
89 Correct 21 ms 2272 KB Output is correct
90 Correct 23 ms 2264 KB Output is correct
91 Correct 29 ms 2656 KB Output is correct
92 Correct 27 ms 2664 KB Output is correct
93 Correct 28 ms 2676 KB Output is correct
94 Correct 28 ms 2656 KB Output is correct
95 Correct 27 ms 2632 KB Output is correct
96 Correct 27 ms 2644 KB Output is correct
97 Correct 30 ms 2660 KB Output is correct
98 Correct 30 ms 2652 KB Output is correct
99 Correct 28 ms 2592 KB Output is correct
100 Correct 28 ms 2656 KB Output is correct
101 Correct 29 ms 2652 KB Output is correct
102 Correct 28 ms 2636 KB Output is correct
103 Correct 28 ms 2648 KB Output is correct
104 Correct 28 ms 2652 KB Output is correct
105 Correct 28 ms 2648 KB Output is correct
106 Correct 26 ms 2664 KB Output is correct
107 Correct 27 ms 2656 KB Output is correct
108 Correct 26 ms 2644 KB Output is correct
109 Correct 31 ms 2644 KB Output is correct
110 Correct 27 ms 2656 KB Output is correct
111 Correct 26 ms 2644 KB Output is correct
112 Correct 26 ms 2656 KB Output is correct
113 Correct 31 ms 2632 KB Output is correct
114 Correct 28 ms 2664 KB Output is correct
115 Correct 27 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 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 2 ms 1492 KB Output is correct
14 Correct 13 ms 1876 KB Output is correct
15 Correct 29 ms 2272 KB Output is correct
16 Correct 45 ms 2656 KB Output is correct
17 Correct 48 ms 2652 KB Output is correct
18 Correct 43 ms 2644 KB Output is correct
19 Correct 43 ms 2656 KB Output is correct
20 Correct 41 ms 2660 KB Output is correct
21 Correct 43 ms 2664 KB Output is correct
22 Correct 42 ms 2656 KB Output is correct
23 Correct 43 ms 2660 KB Output is correct
24 Correct 42 ms 2632 KB Output is correct
25 Correct 45 ms 2644 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 2 ms 1580 KB Output is correct
28 Correct 2 ms 1492 KB Output is correct
29 Correct 16 ms 1876 KB Output is correct
30 Correct 41 ms 2264 KB Output is correct
31 Correct 48 ms 2644 KB Output is correct
32 Correct 49 ms 2660 KB Output is correct
33 Correct 48 ms 2644 KB Output is correct
34 Correct 50 ms 2644 KB Output is correct
35 Correct 55 ms 2656 KB Output is correct
36 Correct 50 ms 2764 KB Output is correct
37 Correct 49 ms 2652 KB Output is correct
38 Correct 47 ms 2644 KB Output is correct
39 Correct 50 ms 2660 KB Output is correct
40 Correct 50 ms 2644 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 13 ms 1620 KB Output is correct
48 Correct 7 ms 1620 KB Output is correct
49 Correct 16 ms 1876 KB Output is correct
50 Correct 35 ms 2268 KB Output is correct
51 Correct 56 ms 2636 KB Output is correct
52 Correct 58 ms 2660 KB Output is correct
53 Correct 57 ms 2680 KB Output is correct
54 Correct 57 ms 2680 KB Output is correct
55 Correct 59 ms 2820 KB Output is correct
56 Correct 56 ms 2676 KB Output is correct
57 Correct 57 ms 2660 KB Output is correct
58 Correct 57 ms 2684 KB Output is correct
59 Correct 56 ms 2656 KB Output is correct
60 Correct 56 ms 2644 KB Output is correct
61 Correct 57 ms 2680 KB Output is correct
62 Correct 59 ms 2692 KB Output is correct
63 Correct 57 ms 2684 KB Output is correct
64 Correct 58 ms 2644 KB Output is correct
65 Correct 55 ms 2652 KB Output is correct
66 Correct 59 ms 2664 KB Output is correct
67 Correct 54 ms 2644 KB Output is correct
68 Correct 56 ms 2676 KB Output is correct
69 Correct 58 ms 2656 KB Output is correct
70 Correct 59 ms 2660 KB Output is correct
71 Correct 7 ms 1620 KB Output is correct
72 Correct 6 ms 1620 KB Output is correct
73 Correct 6 ms 1620 KB Output is correct
74 Correct 8 ms 1620 KB Output is correct
75 Correct 7 ms 1620 KB Output is correct
76 Correct 12 ms 1876 KB Output is correct
77 Correct 11 ms 1876 KB Output is correct
78 Correct 11 ms 1880 KB Output is correct
79 Correct 12 ms 1884 KB Output is correct
80 Correct 12 ms 1876 KB Output is correct
81 Correct 21 ms 2260 KB Output is correct
82 Correct 21 ms 2276 KB Output is correct
83 Correct 22 ms 2272 KB Output is correct
84 Correct 23 ms 2260 KB Output is correct
85 Correct 22 ms 2268 KB Output is correct
86 Correct 25 ms 2392 KB Output is correct
87 Correct 22 ms 2268 KB Output is correct
88 Correct 22 ms 2260 KB Output is correct
89 Correct 21 ms 2272 KB Output is correct
90 Correct 23 ms 2264 KB Output is correct
91 Correct 29 ms 2656 KB Output is correct
92 Correct 27 ms 2664 KB Output is correct
93 Correct 28 ms 2676 KB Output is correct
94 Correct 28 ms 2656 KB Output is correct
95 Correct 27 ms 2632 KB Output is correct
96 Correct 27 ms 2644 KB Output is correct
97 Correct 30 ms 2660 KB Output is correct
98 Correct 30 ms 2652 KB Output is correct
99 Correct 28 ms 2592 KB Output is correct
100 Correct 28 ms 2656 KB Output is correct
101 Correct 29 ms 2652 KB Output is correct
102 Correct 28 ms 2636 KB Output is correct
103 Correct 28 ms 2648 KB Output is correct
104 Correct 28 ms 2652 KB Output is correct
105 Correct 28 ms 2648 KB Output is correct
106 Correct 26 ms 2664 KB Output is correct
107 Correct 27 ms 2656 KB Output is correct
108 Correct 26 ms 2644 KB Output is correct
109 Correct 31 ms 2644 KB Output is correct
110 Correct 27 ms 2656 KB Output is correct
111 Correct 26 ms 2644 KB Output is correct
112 Correct 26 ms 2656 KB Output is correct
113 Correct 31 ms 2632 KB Output is correct
114 Correct 28 ms 2664 KB Output is correct
115 Correct 27 ms 2644 KB Output is correct
116 Correct 2553 ms 3296 KB Output is correct
117 Correct 2198 ms 3416 KB Output is correct
118 Correct 3590 ms 3892 KB Output is correct
119 Correct 2142 ms 3944 KB Output is correct
120 Correct 1519 ms 3940 KB Output is correct
121 Correct 2588 ms 4352 KB Output is correct
122 Correct 1842 ms 4396 KB Output is correct
123 Correct 5748 ms 5180 KB Output is correct
124 Correct 2729 ms 5472 KB Output is correct
125 Correct 2029 ms 5620 KB Output is correct
126 Execution timed out 7005 ms 5384 KB Time limit exceeded
127 Halted 0 ms 0 KB -