답안 #531545

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
531545 2022-03-01T03:36:32 Z eecs Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 7076 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 300010, SZ = 500;
int n, q, a[maxn], bel[maxn], occ[maxn], cnt[maxn];
long long ans[maxn];
vector<array<int, 3>> Q;
unordered_set<int> S;

int main() {
    scanf("%d %d", &n, &q);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]), bel[i] = (i - 1) / SZ + 1;
    }
    for (int i = 1, l, r; i <= q; i++) {
        scanf("%d %d", &l, &r);
        Q.push_back({l, r, i});
    }
    sort(Q.begin(), Q.end(), [&](array<int, 3> p, array<int, 3> q) {
        if (bel[p[0]] ^ bel[q[0]]) return bel[p[0]] < bel[q[0]];
        return bel[p[0]] & 1 ? p[1] < q[1] : p[1] > q[1];
    });
    cnt[0] = maxn, S.insert(0);
    auto ins = [&](int x, int coef) {
        if (!--cnt[occ[x]]) S.erase(occ[x]);
        occ[x] += coef;
        if (!cnt[occ[x]]++) S.insert(occ[x]);
    };
    int l = 1, r = 0;
    for (auto &p : Q) {
        while (l > p[0]) ins(a[--l], 1);
        while (r < p[1]) ins(a[++r], 1);
        while (l < p[0]) ins(a[l++], -1);
        while (r > p[1]) ins(a[r--], -1);
        vector<pair<int, int>> V;
        long long s = 0;
        int all = 0;
        for (int x : S) if (x) {
            V.emplace_back(x, cnt[x]), all += cnt[x];
            s += 1LL * x * x * cnt[x], ans[p[2]] += 1LL * x * (x + 1) / 2 * cnt[x];
        }
        sort(V.begin(), V.end());
        ans[p[2]] += (1LL * (r - l + 1) * (r - l + 1) - s) / 2;
        int pl = 1, pr = all, ls = 0, rs = 0;
        for (auto &q : V) {
            while (q.second--) {
                if (pl <= all - pr) {
                    ans[p[2]] += 1LL * pl * q.first * (2 * ls + q.first - (r - l + 1));
                    pl++, ls += q.first;
                } else {
                    ans[p[2]] += 1LL * pr * q.first * (r - l + 1 - 2 * rs - q.first);
                    pr--, rs += q.first;
                }
            }
        }
    }
    for (int i = 1; i <= q; i++) {
        printf("%lld\n", ans[i]);
    }
    return 0;
}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:11:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |     scanf("%d %d", &n, &q);
      |     ~~~~~^~~~~~~~~~~~~~~~~
diversity.cpp:13:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |         scanf("%d", &a[i]), bel[i] = (i - 1) / SZ + 1;
      |         ~~~~~^~~~~~~~~~~~~
diversity.cpp:16:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |         scanf("%d %d", &l, &r);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 16 ms 1356 KB Output is correct
5 Correct 30 ms 2464 KB Output is correct
6 Correct 45 ms 3416 KB Output is correct
7 Correct 44 ms 3876 KB Output is correct
8 Correct 45 ms 3352 KB Output is correct
9 Correct 45 ms 3388 KB Output is correct
10 Correct 49 ms 3624 KB Output is correct
11 Correct 50 ms 3552 KB Output is correct
12 Correct 46 ms 3500 KB Output is correct
13 Correct 47 ms 3456 KB Output is correct
14 Correct 47 ms 3524 KB Output is correct
15 Correct 53 ms 3524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 16 ms 1356 KB Output is correct
5 Correct 30 ms 2464 KB Output is correct
6 Correct 45 ms 3416 KB Output is correct
7 Correct 44 ms 3876 KB Output is correct
8 Correct 45 ms 3352 KB Output is correct
9 Correct 45 ms 3388 KB Output is correct
10 Correct 49 ms 3624 KB Output is correct
11 Correct 50 ms 3552 KB Output is correct
12 Correct 46 ms 3500 KB Output is correct
13 Correct 47 ms 3456 KB Output is correct
14 Correct 47 ms 3524 KB Output is correct
15 Correct 53 ms 3524 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 2 ms 312 KB Output is correct
19 Correct 16 ms 1336 KB Output is correct
20 Correct 32 ms 2500 KB Output is correct
21 Correct 49 ms 3664 KB Output is correct
22 Correct 45 ms 3524 KB Output is correct
23 Correct 53 ms 3520 KB Output is correct
24 Correct 50 ms 3528 KB Output is correct
25 Correct 56 ms 3560 KB Output is correct
26 Correct 46 ms 3556 KB Output is correct
27 Correct 46 ms 3568 KB Output is correct
28 Correct 45 ms 3668 KB Output is correct
29 Correct 51 ms 3448 KB Output is correct
30 Correct 51 ms 3528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 16 ms 1356 KB Output is correct
5 Correct 30 ms 2464 KB Output is correct
6 Correct 45 ms 3416 KB Output is correct
7 Correct 44 ms 3876 KB Output is correct
8 Correct 45 ms 3352 KB Output is correct
9 Correct 45 ms 3388 KB Output is correct
10 Correct 49 ms 3624 KB Output is correct
11 Correct 50 ms 3552 KB Output is correct
12 Correct 46 ms 3500 KB Output is correct
13 Correct 47 ms 3456 KB Output is correct
14 Correct 47 ms 3524 KB Output is correct
15 Correct 53 ms 3524 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 2 ms 312 KB Output is correct
19 Correct 16 ms 1336 KB Output is correct
20 Correct 32 ms 2500 KB Output is correct
21 Correct 49 ms 3664 KB Output is correct
22 Correct 45 ms 3524 KB Output is correct
23 Correct 53 ms 3520 KB Output is correct
24 Correct 50 ms 3528 KB Output is correct
25 Correct 56 ms 3560 KB Output is correct
26 Correct 46 ms 3556 KB Output is correct
27 Correct 46 ms 3568 KB Output is correct
28 Correct 45 ms 3668 KB Output is correct
29 Correct 51 ms 3448 KB Output is correct
30 Correct 51 ms 3528 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 0 ms 332 KB Output is correct
34 Correct 2 ms 444 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 2 ms 448 KB Output is correct
37 Correct 6 ms 824 KB Output is correct
38 Correct 5 ms 844 KB Output is correct
39 Correct 12 ms 1468 KB Output is correct
40 Correct 24 ms 2628 KB Output is correct
41 Correct 49 ms 3772 KB Output is correct
42 Correct 33 ms 3732 KB Output is correct
43 Correct 31 ms 3780 KB Output is correct
44 Correct 33 ms 3768 KB Output is correct
45 Correct 35 ms 3756 KB Output is correct
46 Correct 34 ms 3732 KB Output is correct
47 Correct 31 ms 3716 KB Output is correct
48 Correct 31 ms 3832 KB Output is correct
49 Correct 32 ms 3712 KB Output is correct
50 Correct 33 ms 3800 KB Output is correct
51 Correct 43 ms 3840 KB Output is correct
52 Correct 41 ms 3780 KB Output is correct
53 Correct 47 ms 3744 KB Output is correct
54 Correct 32 ms 3828 KB Output is correct
55 Correct 32 ms 3728 KB Output is correct
56 Correct 32 ms 3848 KB Output is correct
57 Correct 32 ms 3772 KB Output is correct
58 Correct 36 ms 3740 KB Output is correct
59 Correct 48 ms 3820 KB Output is correct
60 Correct 32 ms 3740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 16 ms 1356 KB Output is correct
15 Correct 30 ms 2464 KB Output is correct
16 Correct 45 ms 3416 KB Output is correct
17 Correct 44 ms 3876 KB Output is correct
18 Correct 45 ms 3352 KB Output is correct
19 Correct 45 ms 3388 KB Output is correct
20 Correct 49 ms 3624 KB Output is correct
21 Correct 50 ms 3552 KB Output is correct
22 Correct 46 ms 3500 KB Output is correct
23 Correct 47 ms 3456 KB Output is correct
24 Correct 47 ms 3524 KB Output is correct
25 Correct 53 ms 3524 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 2 ms 312 KB Output is correct
29 Correct 16 ms 1336 KB Output is correct
30 Correct 32 ms 2500 KB Output is correct
31 Correct 49 ms 3664 KB Output is correct
32 Correct 45 ms 3524 KB Output is correct
33 Correct 53 ms 3520 KB Output is correct
34 Correct 50 ms 3528 KB Output is correct
35 Correct 56 ms 3560 KB Output is correct
36 Correct 46 ms 3556 KB Output is correct
37 Correct 46 ms 3568 KB Output is correct
38 Correct 45 ms 3668 KB Output is correct
39 Correct 51 ms 3448 KB Output is correct
40 Correct 51 ms 3528 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 2 ms 444 KB Output is correct
45 Correct 2 ms 332 KB Output is correct
46 Correct 2 ms 448 KB Output is correct
47 Correct 6 ms 824 KB Output is correct
48 Correct 5 ms 844 KB Output is correct
49 Correct 12 ms 1468 KB Output is correct
50 Correct 24 ms 2628 KB Output is correct
51 Correct 49 ms 3772 KB Output is correct
52 Correct 33 ms 3732 KB Output is correct
53 Correct 31 ms 3780 KB Output is correct
54 Correct 33 ms 3768 KB Output is correct
55 Correct 35 ms 3756 KB Output is correct
56 Correct 34 ms 3732 KB Output is correct
57 Correct 31 ms 3716 KB Output is correct
58 Correct 31 ms 3832 KB Output is correct
59 Correct 32 ms 3712 KB Output is correct
60 Correct 33 ms 3800 KB Output is correct
61 Correct 43 ms 3840 KB Output is correct
62 Correct 41 ms 3780 KB Output is correct
63 Correct 47 ms 3744 KB Output is correct
64 Correct 32 ms 3828 KB Output is correct
65 Correct 32 ms 3728 KB Output is correct
66 Correct 32 ms 3848 KB Output is correct
67 Correct 32 ms 3772 KB Output is correct
68 Correct 36 ms 3740 KB Output is correct
69 Correct 48 ms 3820 KB Output is correct
70 Correct 32 ms 3740 KB Output is correct
71 Correct 5 ms 944 KB Output is correct
72 Correct 5 ms 844 KB Output is correct
73 Correct 5 ms 836 KB Output is correct
74 Correct 5 ms 956 KB Output is correct
75 Correct 6 ms 828 KB Output is correct
76 Correct 11 ms 1492 KB Output is correct
77 Correct 14 ms 1472 KB Output is correct
78 Correct 10 ms 1536 KB Output is correct
79 Correct 9 ms 1524 KB Output is correct
80 Correct 9 ms 1484 KB Output is correct
81 Correct 17 ms 2784 KB Output is correct
82 Correct 18 ms 2840 KB Output is correct
83 Correct 20 ms 2864 KB Output is correct
84 Correct 20 ms 2780 KB Output is correct
85 Correct 19 ms 2920 KB Output is correct
86 Correct 20 ms 2928 KB Output is correct
87 Correct 19 ms 2920 KB Output is correct
88 Correct 19 ms 2980 KB Output is correct
89 Correct 19 ms 2936 KB Output is correct
90 Correct 19 ms 3004 KB Output is correct
91 Correct 30 ms 4740 KB Output is correct
92 Correct 33 ms 4652 KB Output is correct
93 Correct 33 ms 4656 KB Output is correct
94 Correct 32 ms 4652 KB Output is correct
95 Correct 30 ms 4676 KB Output is correct
96 Correct 35 ms 5308 KB Output is correct
97 Correct 36 ms 5296 KB Output is correct
98 Correct 38 ms 5272 KB Output is correct
99 Correct 41 ms 5316 KB Output is correct
100 Correct 34 ms 5288 KB Output is correct
101 Correct 35 ms 5220 KB Output is correct
102 Correct 38 ms 5204 KB Output is correct
103 Correct 49 ms 5284 KB Output is correct
104 Correct 53 ms 5204 KB Output is correct
105 Correct 35 ms 5188 KB Output is correct
106 Correct 34 ms 5524 KB Output is correct
107 Correct 35 ms 5516 KB Output is correct
108 Correct 35 ms 5464 KB Output is correct
109 Correct 36 ms 5448 KB Output is correct
110 Correct 43 ms 5508 KB Output is correct
111 Correct 46 ms 5520 KB Output is correct
112 Correct 35 ms 5532 KB Output is correct
113 Correct 35 ms 5428 KB Output is correct
114 Correct 37 ms 5420 KB Output is correct
115 Correct 36 ms 5480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 16 ms 1356 KB Output is correct
15 Correct 30 ms 2464 KB Output is correct
16 Correct 45 ms 3416 KB Output is correct
17 Correct 44 ms 3876 KB Output is correct
18 Correct 45 ms 3352 KB Output is correct
19 Correct 45 ms 3388 KB Output is correct
20 Correct 49 ms 3624 KB Output is correct
21 Correct 50 ms 3552 KB Output is correct
22 Correct 46 ms 3500 KB Output is correct
23 Correct 47 ms 3456 KB Output is correct
24 Correct 47 ms 3524 KB Output is correct
25 Correct 53 ms 3524 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 2 ms 312 KB Output is correct
29 Correct 16 ms 1336 KB Output is correct
30 Correct 32 ms 2500 KB Output is correct
31 Correct 49 ms 3664 KB Output is correct
32 Correct 45 ms 3524 KB Output is correct
33 Correct 53 ms 3520 KB Output is correct
34 Correct 50 ms 3528 KB Output is correct
35 Correct 56 ms 3560 KB Output is correct
36 Correct 46 ms 3556 KB Output is correct
37 Correct 46 ms 3568 KB Output is correct
38 Correct 45 ms 3668 KB Output is correct
39 Correct 51 ms 3448 KB Output is correct
40 Correct 51 ms 3528 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 0 ms 332 KB Output is correct
44 Correct 2 ms 444 KB Output is correct
45 Correct 2 ms 332 KB Output is correct
46 Correct 2 ms 448 KB Output is correct
47 Correct 6 ms 824 KB Output is correct
48 Correct 5 ms 844 KB Output is correct
49 Correct 12 ms 1468 KB Output is correct
50 Correct 24 ms 2628 KB Output is correct
51 Correct 49 ms 3772 KB Output is correct
52 Correct 33 ms 3732 KB Output is correct
53 Correct 31 ms 3780 KB Output is correct
54 Correct 33 ms 3768 KB Output is correct
55 Correct 35 ms 3756 KB Output is correct
56 Correct 34 ms 3732 KB Output is correct
57 Correct 31 ms 3716 KB Output is correct
58 Correct 31 ms 3832 KB Output is correct
59 Correct 32 ms 3712 KB Output is correct
60 Correct 33 ms 3800 KB Output is correct
61 Correct 43 ms 3840 KB Output is correct
62 Correct 41 ms 3780 KB Output is correct
63 Correct 47 ms 3744 KB Output is correct
64 Correct 32 ms 3828 KB Output is correct
65 Correct 32 ms 3728 KB Output is correct
66 Correct 32 ms 3848 KB Output is correct
67 Correct 32 ms 3772 KB Output is correct
68 Correct 36 ms 3740 KB Output is correct
69 Correct 48 ms 3820 KB Output is correct
70 Correct 32 ms 3740 KB Output is correct
71 Correct 5 ms 944 KB Output is correct
72 Correct 5 ms 844 KB Output is correct
73 Correct 5 ms 836 KB Output is correct
74 Correct 5 ms 956 KB Output is correct
75 Correct 6 ms 828 KB Output is correct
76 Correct 11 ms 1492 KB Output is correct
77 Correct 14 ms 1472 KB Output is correct
78 Correct 10 ms 1536 KB Output is correct
79 Correct 9 ms 1524 KB Output is correct
80 Correct 9 ms 1484 KB Output is correct
81 Correct 17 ms 2784 KB Output is correct
82 Correct 18 ms 2840 KB Output is correct
83 Correct 20 ms 2864 KB Output is correct
84 Correct 20 ms 2780 KB Output is correct
85 Correct 19 ms 2920 KB Output is correct
86 Correct 20 ms 2928 KB Output is correct
87 Correct 19 ms 2920 KB Output is correct
88 Correct 19 ms 2980 KB Output is correct
89 Correct 19 ms 2936 KB Output is correct
90 Correct 19 ms 3004 KB Output is correct
91 Correct 30 ms 4740 KB Output is correct
92 Correct 33 ms 4652 KB Output is correct
93 Correct 33 ms 4656 KB Output is correct
94 Correct 32 ms 4652 KB Output is correct
95 Correct 30 ms 4676 KB Output is correct
96 Correct 35 ms 5308 KB Output is correct
97 Correct 36 ms 5296 KB Output is correct
98 Correct 38 ms 5272 KB Output is correct
99 Correct 41 ms 5316 KB Output is correct
100 Correct 34 ms 5288 KB Output is correct
101 Correct 35 ms 5220 KB Output is correct
102 Correct 38 ms 5204 KB Output is correct
103 Correct 49 ms 5284 KB Output is correct
104 Correct 53 ms 5204 KB Output is correct
105 Correct 35 ms 5188 KB Output is correct
106 Correct 34 ms 5524 KB Output is correct
107 Correct 35 ms 5516 KB Output is correct
108 Correct 35 ms 5464 KB Output is correct
109 Correct 36 ms 5448 KB Output is correct
110 Correct 43 ms 5508 KB Output is correct
111 Correct 46 ms 5520 KB Output is correct
112 Correct 35 ms 5532 KB Output is correct
113 Correct 35 ms 5428 KB Output is correct
114 Correct 37 ms 5420 KB Output is correct
115 Correct 36 ms 5480 KB Output is correct
116 Correct 607 ms 2404 KB Output is correct
117 Correct 497 ms 2368 KB Output is correct
118 Correct 893 ms 3048 KB Output is correct
119 Correct 541 ms 3112 KB Output is correct
120 Correct 401 ms 3092 KB Output is correct
121 Correct 726 ms 3676 KB Output is correct
122 Correct 640 ms 3772 KB Output is correct
123 Correct 1695 ms 4896 KB Output is correct
124 Correct 1308 ms 5228 KB Output is correct
125 Correct 1827 ms 5256 KB Output is correct
126 Correct 5697 ms 6020 KB Output is correct
127 Correct 5575 ms 6020 KB Output is correct
128 Correct 5600 ms 6020 KB Output is correct
129 Correct 5551 ms 6012 KB Output is correct
130 Correct 5480 ms 6004 KB Output is correct
131 Correct 5008 ms 6152 KB Output is correct
132 Correct 4914 ms 6184 KB Output is correct
133 Correct 5076 ms 6160 KB Output is correct
134 Correct 5027 ms 6164 KB Output is correct
135 Correct 4806 ms 6056 KB Output is correct
136 Correct 4580 ms 6176 KB Output is correct
137 Correct 4776 ms 6192 KB Output is correct
138 Correct 4575 ms 6176 KB Output is correct
139 Correct 4696 ms 6164 KB Output is correct
140 Correct 4606 ms 6172 KB Output is correct
141 Correct 2953 ms 6196 KB Output is correct
142 Correct 3115 ms 6192 KB Output is correct
143 Correct 3053 ms 6212 KB Output is correct
144 Correct 3022 ms 6204 KB Output is correct
145 Correct 3059 ms 6196 KB Output is correct
146 Correct 1659 ms 6472 KB Output is correct
147 Correct 1658 ms 6480 KB Output is correct
148 Correct 1692 ms 6480 KB Output is correct
149 Correct 1680 ms 6476 KB Output is correct
150 Correct 1671 ms 6472 KB Output is correct
151 Correct 2201 ms 6528 KB Output is correct
152 Correct 2173 ms 6616 KB Output is correct
153 Correct 2179 ms 6516 KB Output is correct
154 Correct 2169 ms 6516 KB Output is correct
155 Correct 2183 ms 6516 KB Output is correct
156 Execution timed out 7013 ms 7076 KB Time limit exceeded
157 Halted 0 ms 0 KB -