답안 #652592

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
652592 2022-10-23T11:54:01 Z rembocoder Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 10692 KB
#include <bits/stdc++.h>

#define int int64_t

using namespace std;

const int max_a = 300'000;
const int K = 540;

int32_t main() {
    ios_base::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        a[i]--;
    }
    vector<vector<pair<int, pair<int, int>>>> gr((n + K - 1) / K);
    for (int i = 0; i < q; i++) {
        int l, r;
        cin >> l >> r;
        l--;
        gr[l / K].push_back({r, {l, i}});
    }
    vector<int> ans(q);
    for (int i = 0; i < gr.size(); i++) {
        sort(gr[i].begin(), gr[i].end());
        int l = i * K, r = i * K;
        vector<int> fr(max_a);
        vector<int> fr_fr(max_a);
        fr_fr[0] = max_a;
        set<pair<int, int>> s;
        s.insert({0, max_a});

        auto add_a = [&](int x) {
            s.erase({fr[x], fr_fr[fr[x]]});
            fr_fr[fr[x]]--;
            if (fr_fr[fr[x]]) {
                s.insert({fr[x], fr_fr[fr[x]]});
            }
            fr[x]++;
            s.erase({fr[x], fr_fr[fr[x]]});
            fr_fr[fr[x]]++;
            s.insert({fr[x], fr_fr[fr[x]]});
        };

        auto del_a = [&](int x) {
            s.erase({fr[x], fr_fr[fr[x]]});
            fr_fr[fr[x]]--;
            if (fr_fr[fr[x]]) {
                s.insert({fr[x], fr_fr[fr[x]]});
            }
            fr[x]--;
            s.erase({fr[x], fr_fr[fr[x]]});
            fr_fr[fr[x]]++;
            s.insert({fr[x], fr_fr[fr[x]]});
        };

        auto handle_blocks = [&](int L, int k, int l, int n, int& res) {
            res += (k * (n * n + L) - k * l * l - k * (k - 1) * l * L - k * (k - 1) * (2 * k - 1) / 6 * L * L
                    - (n - l) * (n - l) * k + k * (k + 1) * (n - l) * L - k * (k + 1) * (2 * k + 1) / 6 * L * L) / 2;
        };

        for (auto seg: gr[i]) {
            int q_l = seg.second.first, q_r = seg.first, q_i = seg.second.second;
            while (r < q_r) {
                add_a(a[r]);
                r++;
            }
            while (l > q_l) {
                l--;
                add_a(a[l]);
            }
            while (l < q_l) {
                del_a(a[l]);
                l++;
            }
            int res = 0;
            bool last = false; // false = left, true = right
            int total_l = 0, total_r = 0;
            //cerr << endl;
            for (auto tmp: s) {
                int L = tmp.first, total_k = tmp.second;
                if (L == 0) {
                    continue;
                }
                int l_k = total_k / 2 + (total_k % 2 == 1 && last);
                int r_k = total_k / 2 + (total_k % 2 == 1 && !last);
                //cerr << l_k << ' ' << L << endl;
                //cerr << r_k << ' ' << L << endl;
                handle_blocks(L, l_k, total_l, r - l, res);
                handle_blocks(L, r_k, total_r, r - l, res);
                if (total_k % 2 == 1) {
                    last = !last;
                }
                total_l += l_k * L;
                total_r += r_k * L;
            }
            ans[q_i] = res;
        }
    }
    for (int i = 0; i < q; i++) {
        cout << ans[i] << '\n';
    }
}

Compilation message

diversity.cpp: In function 'int32_t main()':
diversity.cpp:27:23: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<std::vector<std::pair<long int, std::pair<long int, long int> > > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |     for (int i = 0; i < gr.size(); i++) {
      |                     ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4980 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4924 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4996 KB Output is correct
3 Correct 30 ms 5244 KB Output is correct
4 Correct 286 ms 6344 KB Output is correct
5 Correct 560 ms 7240 KB Output is correct
6 Correct 916 ms 8428 KB Output is correct
7 Correct 847 ms 8408 KB Output is correct
8 Correct 932 ms 8524 KB Output is correct
9 Correct 905 ms 8224 KB Output is correct
10 Correct 899 ms 8232 KB Output is correct
11 Correct 863 ms 8336 KB Output is correct
12 Correct 829 ms 8312 KB Output is correct
13 Correct 870 ms 8288 KB Output is correct
14 Correct 842 ms 8544 KB Output is correct
15 Correct 826 ms 8276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4996 KB Output is correct
3 Correct 30 ms 5244 KB Output is correct
4 Correct 286 ms 6344 KB Output is correct
5 Correct 560 ms 7240 KB Output is correct
6 Correct 916 ms 8428 KB Output is correct
7 Correct 847 ms 8408 KB Output is correct
8 Correct 932 ms 8524 KB Output is correct
9 Correct 905 ms 8224 KB Output is correct
10 Correct 899 ms 8232 KB Output is correct
11 Correct 863 ms 8336 KB Output is correct
12 Correct 829 ms 8312 KB Output is correct
13 Correct 870 ms 8288 KB Output is correct
14 Correct 842 ms 8544 KB Output is correct
15 Correct 826 ms 8276 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 5 ms 4996 KB Output is correct
18 Correct 32 ms 5204 KB Output is correct
19 Correct 316 ms 6380 KB Output is correct
20 Correct 562 ms 7460 KB Output is correct
21 Correct 877 ms 8516 KB Output is correct
22 Correct 914 ms 8332 KB Output is correct
23 Correct 876 ms 8504 KB Output is correct
24 Correct 831 ms 8488 KB Output is correct
25 Correct 868 ms 8520 KB Output is correct
26 Correct 871 ms 8464 KB Output is correct
27 Correct 871 ms 8440 KB Output is correct
28 Correct 884 ms 8640 KB Output is correct
29 Correct 887 ms 8508 KB Output is correct
30 Correct 930 ms 8496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4996 KB Output is correct
3 Correct 30 ms 5244 KB Output is correct
4 Correct 286 ms 6344 KB Output is correct
5 Correct 560 ms 7240 KB Output is correct
6 Correct 916 ms 8428 KB Output is correct
7 Correct 847 ms 8408 KB Output is correct
8 Correct 932 ms 8524 KB Output is correct
9 Correct 905 ms 8224 KB Output is correct
10 Correct 899 ms 8232 KB Output is correct
11 Correct 863 ms 8336 KB Output is correct
12 Correct 829 ms 8312 KB Output is correct
13 Correct 870 ms 8288 KB Output is correct
14 Correct 842 ms 8544 KB Output is correct
15 Correct 826 ms 8276 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 5 ms 4996 KB Output is correct
18 Correct 32 ms 5204 KB Output is correct
19 Correct 316 ms 6380 KB Output is correct
20 Correct 562 ms 7460 KB Output is correct
21 Correct 877 ms 8516 KB Output is correct
22 Correct 914 ms 8332 KB Output is correct
23 Correct 876 ms 8504 KB Output is correct
24 Correct 831 ms 8488 KB Output is correct
25 Correct 868 ms 8520 KB Output is correct
26 Correct 871 ms 8464 KB Output is correct
27 Correct 871 ms 8440 KB Output is correct
28 Correct 884 ms 8640 KB Output is correct
29 Correct 887 ms 8508 KB Output is correct
30 Correct 930 ms 8496 KB Output is correct
31 Correct 4 ms 5024 KB Output is correct
32 Correct 5 ms 4976 KB Output is correct
33 Correct 5 ms 4976 KB Output is correct
34 Correct 35 ms 5264 KB Output is correct
35 Correct 33 ms 5208 KB Output is correct
36 Correct 35 ms 5308 KB Output is correct
37 Correct 155 ms 5780 KB Output is correct
38 Correct 163 ms 5760 KB Output is correct
39 Correct 328 ms 6396 KB Output is correct
40 Correct 631 ms 7640 KB Output is correct
41 Correct 966 ms 8892 KB Output is correct
42 Correct 919 ms 8800 KB Output is correct
43 Correct 959 ms 9028 KB Output is correct
44 Correct 962 ms 8824 KB Output is correct
45 Correct 921 ms 8836 KB Output is correct
46 Correct 915 ms 9072 KB Output is correct
47 Correct 925 ms 8788 KB Output is correct
48 Correct 944 ms 8872 KB Output is correct
49 Correct 925 ms 8944 KB Output is correct
50 Correct 921 ms 9024 KB Output is correct
51 Correct 1030 ms 8828 KB Output is correct
52 Correct 992 ms 8992 KB Output is correct
53 Correct 978 ms 8772 KB Output is correct
54 Correct 902 ms 8840 KB Output is correct
55 Correct 922 ms 8892 KB Output is correct
56 Correct 967 ms 8804 KB Output is correct
57 Correct 924 ms 8896 KB Output is correct
58 Correct 930 ms 9028 KB Output is correct
59 Correct 885 ms 8816 KB Output is correct
60 Correct 952 ms 9032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4980 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4924 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 4 ms 4996 KB Output is correct
13 Correct 30 ms 5244 KB Output is correct
14 Correct 286 ms 6344 KB Output is correct
15 Correct 560 ms 7240 KB Output is correct
16 Correct 916 ms 8428 KB Output is correct
17 Correct 847 ms 8408 KB Output is correct
18 Correct 932 ms 8524 KB Output is correct
19 Correct 905 ms 8224 KB Output is correct
20 Correct 899 ms 8232 KB Output is correct
21 Correct 863 ms 8336 KB Output is correct
22 Correct 829 ms 8312 KB Output is correct
23 Correct 870 ms 8288 KB Output is correct
24 Correct 842 ms 8544 KB Output is correct
25 Correct 826 ms 8276 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 5 ms 4996 KB Output is correct
28 Correct 32 ms 5204 KB Output is correct
29 Correct 316 ms 6380 KB Output is correct
30 Correct 562 ms 7460 KB Output is correct
31 Correct 877 ms 8516 KB Output is correct
32 Correct 914 ms 8332 KB Output is correct
33 Correct 876 ms 8504 KB Output is correct
34 Correct 831 ms 8488 KB Output is correct
35 Correct 868 ms 8520 KB Output is correct
36 Correct 871 ms 8464 KB Output is correct
37 Correct 871 ms 8440 KB Output is correct
38 Correct 884 ms 8640 KB Output is correct
39 Correct 887 ms 8508 KB Output is correct
40 Correct 930 ms 8496 KB Output is correct
41 Correct 4 ms 5024 KB Output is correct
42 Correct 5 ms 4976 KB Output is correct
43 Correct 5 ms 4976 KB Output is correct
44 Correct 35 ms 5264 KB Output is correct
45 Correct 33 ms 5208 KB Output is correct
46 Correct 35 ms 5308 KB Output is correct
47 Correct 155 ms 5780 KB Output is correct
48 Correct 163 ms 5760 KB Output is correct
49 Correct 328 ms 6396 KB Output is correct
50 Correct 631 ms 7640 KB Output is correct
51 Correct 966 ms 8892 KB Output is correct
52 Correct 919 ms 8800 KB Output is correct
53 Correct 959 ms 9028 KB Output is correct
54 Correct 962 ms 8824 KB Output is correct
55 Correct 921 ms 8836 KB Output is correct
56 Correct 915 ms 9072 KB Output is correct
57 Correct 925 ms 8788 KB Output is correct
58 Correct 944 ms 8872 KB Output is correct
59 Correct 925 ms 8944 KB Output is correct
60 Correct 921 ms 9024 KB Output is correct
61 Correct 1030 ms 8828 KB Output is correct
62 Correct 992 ms 8992 KB Output is correct
63 Correct 978 ms 8772 KB Output is correct
64 Correct 902 ms 8840 KB Output is correct
65 Correct 922 ms 8892 KB Output is correct
66 Correct 967 ms 8804 KB Output is correct
67 Correct 924 ms 8896 KB Output is correct
68 Correct 930 ms 9028 KB Output is correct
69 Correct 885 ms 8816 KB Output is correct
70 Correct 952 ms 9032 KB Output is correct
71 Correct 154 ms 5896 KB Output is correct
72 Correct 148 ms 5928 KB Output is correct
73 Correct 156 ms 5800 KB Output is correct
74 Correct 158 ms 5856 KB Output is correct
75 Correct 167 ms 5800 KB Output is correct
76 Correct 300 ms 6528 KB Output is correct
77 Correct 295 ms 6456 KB Output is correct
78 Correct 303 ms 6584 KB Output is correct
79 Correct 309 ms 6452 KB Output is correct
80 Correct 307 ms 6556 KB Output is correct
81 Correct 614 ms 7736 KB Output is correct
82 Correct 587 ms 7792 KB Output is correct
83 Correct 599 ms 7788 KB Output is correct
84 Correct 625 ms 8068 KB Output is correct
85 Correct 586 ms 7848 KB Output is correct
86 Correct 605 ms 7896 KB Output is correct
87 Correct 672 ms 7832 KB Output is correct
88 Correct 622 ms 7892 KB Output is correct
89 Correct 578 ms 7872 KB Output is correct
90 Correct 608 ms 7908 KB Output is correct
91 Correct 863 ms 9424 KB Output is correct
92 Correct 923 ms 9356 KB Output is correct
93 Correct 866 ms 9440 KB Output is correct
94 Correct 823 ms 9364 KB Output is correct
95 Correct 832 ms 9324 KB Output is correct
96 Correct 850 ms 9516 KB Output is correct
97 Correct 864 ms 9528 KB Output is correct
98 Correct 869 ms 9744 KB Output is correct
99 Correct 835 ms 9620 KB Output is correct
100 Correct 861 ms 9468 KB Output is correct
101 Correct 877 ms 9632 KB Output is correct
102 Correct 807 ms 9584 KB Output is correct
103 Correct 906 ms 9680 KB Output is correct
104 Correct 913 ms 9536 KB Output is correct
105 Correct 857 ms 9512 KB Output is correct
106 Correct 835 ms 9608 KB Output is correct
107 Correct 817 ms 9508 KB Output is correct
108 Correct 858 ms 9628 KB Output is correct
109 Correct 870 ms 9564 KB Output is correct
110 Correct 829 ms 9828 KB Output is correct
111 Correct 837 ms 9724 KB Output is correct
112 Correct 864 ms 9592 KB Output is correct
113 Correct 830 ms 9772 KB Output is correct
114 Correct 889 ms 9744 KB Output is correct
115 Correct 875 ms 9660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4980 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4924 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 2 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 4 ms 4996 KB Output is correct
13 Correct 30 ms 5244 KB Output is correct
14 Correct 286 ms 6344 KB Output is correct
15 Correct 560 ms 7240 KB Output is correct
16 Correct 916 ms 8428 KB Output is correct
17 Correct 847 ms 8408 KB Output is correct
18 Correct 932 ms 8524 KB Output is correct
19 Correct 905 ms 8224 KB Output is correct
20 Correct 899 ms 8232 KB Output is correct
21 Correct 863 ms 8336 KB Output is correct
22 Correct 829 ms 8312 KB Output is correct
23 Correct 870 ms 8288 KB Output is correct
24 Correct 842 ms 8544 KB Output is correct
25 Correct 826 ms 8276 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 5 ms 4996 KB Output is correct
28 Correct 32 ms 5204 KB Output is correct
29 Correct 316 ms 6380 KB Output is correct
30 Correct 562 ms 7460 KB Output is correct
31 Correct 877 ms 8516 KB Output is correct
32 Correct 914 ms 8332 KB Output is correct
33 Correct 876 ms 8504 KB Output is correct
34 Correct 831 ms 8488 KB Output is correct
35 Correct 868 ms 8520 KB Output is correct
36 Correct 871 ms 8464 KB Output is correct
37 Correct 871 ms 8440 KB Output is correct
38 Correct 884 ms 8640 KB Output is correct
39 Correct 887 ms 8508 KB Output is correct
40 Correct 930 ms 8496 KB Output is correct
41 Correct 4 ms 5024 KB Output is correct
42 Correct 5 ms 4976 KB Output is correct
43 Correct 5 ms 4976 KB Output is correct
44 Correct 35 ms 5264 KB Output is correct
45 Correct 33 ms 5208 KB Output is correct
46 Correct 35 ms 5308 KB Output is correct
47 Correct 155 ms 5780 KB Output is correct
48 Correct 163 ms 5760 KB Output is correct
49 Correct 328 ms 6396 KB Output is correct
50 Correct 631 ms 7640 KB Output is correct
51 Correct 966 ms 8892 KB Output is correct
52 Correct 919 ms 8800 KB Output is correct
53 Correct 959 ms 9028 KB Output is correct
54 Correct 962 ms 8824 KB Output is correct
55 Correct 921 ms 8836 KB Output is correct
56 Correct 915 ms 9072 KB Output is correct
57 Correct 925 ms 8788 KB Output is correct
58 Correct 944 ms 8872 KB Output is correct
59 Correct 925 ms 8944 KB Output is correct
60 Correct 921 ms 9024 KB Output is correct
61 Correct 1030 ms 8828 KB Output is correct
62 Correct 992 ms 8992 KB Output is correct
63 Correct 978 ms 8772 KB Output is correct
64 Correct 902 ms 8840 KB Output is correct
65 Correct 922 ms 8892 KB Output is correct
66 Correct 967 ms 8804 KB Output is correct
67 Correct 924 ms 8896 KB Output is correct
68 Correct 930 ms 9028 KB Output is correct
69 Correct 885 ms 8816 KB Output is correct
70 Correct 952 ms 9032 KB Output is correct
71 Correct 154 ms 5896 KB Output is correct
72 Correct 148 ms 5928 KB Output is correct
73 Correct 156 ms 5800 KB Output is correct
74 Correct 158 ms 5856 KB Output is correct
75 Correct 167 ms 5800 KB Output is correct
76 Correct 300 ms 6528 KB Output is correct
77 Correct 295 ms 6456 KB Output is correct
78 Correct 303 ms 6584 KB Output is correct
79 Correct 309 ms 6452 KB Output is correct
80 Correct 307 ms 6556 KB Output is correct
81 Correct 614 ms 7736 KB Output is correct
82 Correct 587 ms 7792 KB Output is correct
83 Correct 599 ms 7788 KB Output is correct
84 Correct 625 ms 8068 KB Output is correct
85 Correct 586 ms 7848 KB Output is correct
86 Correct 605 ms 7896 KB Output is correct
87 Correct 672 ms 7832 KB Output is correct
88 Correct 622 ms 7892 KB Output is correct
89 Correct 578 ms 7872 KB Output is correct
90 Correct 608 ms 7908 KB Output is correct
91 Correct 863 ms 9424 KB Output is correct
92 Correct 923 ms 9356 KB Output is correct
93 Correct 866 ms 9440 KB Output is correct
94 Correct 823 ms 9364 KB Output is correct
95 Correct 832 ms 9324 KB Output is correct
96 Correct 850 ms 9516 KB Output is correct
97 Correct 864 ms 9528 KB Output is correct
98 Correct 869 ms 9744 KB Output is correct
99 Correct 835 ms 9620 KB Output is correct
100 Correct 861 ms 9468 KB Output is correct
101 Correct 877 ms 9632 KB Output is correct
102 Correct 807 ms 9584 KB Output is correct
103 Correct 906 ms 9680 KB Output is correct
104 Correct 913 ms 9536 KB Output is correct
105 Correct 857 ms 9512 KB Output is correct
106 Correct 835 ms 9608 KB Output is correct
107 Correct 817 ms 9508 KB Output is correct
108 Correct 858 ms 9628 KB Output is correct
109 Correct 870 ms 9564 KB Output is correct
110 Correct 829 ms 9828 KB Output is correct
111 Correct 837 ms 9724 KB Output is correct
112 Correct 864 ms 9592 KB Output is correct
113 Correct 830 ms 9772 KB Output is correct
114 Correct 889 ms 9744 KB Output is correct
115 Correct 875 ms 9660 KB Output is correct
116 Correct 1757 ms 7800 KB Output is correct
117 Correct 2166 ms 7888 KB Output is correct
118 Correct 2739 ms 8652 KB Output is correct
119 Correct 3552 ms 8420 KB Output is correct
120 Correct 3304 ms 8600 KB Output is correct
121 Correct 5938 ms 9120 KB Output is correct
122 Correct 5532 ms 9100 KB Output is correct
123 Execution timed out 7026 ms 10692 KB Time limit exceeded
124 Halted 0 ms 0 KB -