답안 #520480

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520480 2022-01-30T07:04:08 Z KoD Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 12236 KB
#include <bits/stdc++.h>

using std::vector;
using std::array;
using std::pair;
using std::tuple;

using i64 = std::int64_t;

constexpr int B = 600;
constexpr int MAX = 300000;

constexpr i64 sum_k(const int n) {
    return (i64)n * (n + 1) / 2;
}
 
constexpr i64 sum_k2(const int n) {
    return (i64)n * (n + 1) * (2 * n + 1) / 6;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N, Q;
    std::cin >> N >> Q;
    vector<int> A(N);
    for (auto& x : A) {
        std::cin >> x;
        x -= 1;
    }
    vector<vector<int>> query((N + B - 1) / B);
    vector<int> L(Q), R(Q);
    for (int i = 0; i < Q; ++i) {
        std::cin >> L[i] >> R[i];
        L[i] -= 1;
        query[L[i] / B].push_back(i);
    }
    vector<i64> ans(Q);
    vector<int> count(MAX), block(N + 1), nonzero;
    const auto change = [&](const int x, const int c) {
        block[count[x]] -= 1;
        count[x] += c;
        if (count[x] != 0 and block[count[x]] == 0) {
            nonzero.push_back(count[x]);
        }
        block[count[x]] += 1;
    };
    for (auto& qidx : query) {
        if (qidx.empty()) {
            continue;
        }
        std::sort(qidx.begin(), qidx.end(), [&](const int i, const int j) {
            return R[i] < R[j]; 
        });
        int l = L[qidx[0]], r = L[qidx[0]];
        for (const int q : qidx) {
            while (l < L[q]) {
                change(A[l++], -1);
            }
            while (l > L[q]) {
                change(A[--l], 1);
            }
            while (r < R[q]) {
                change(A[r++], 1);
            }
            std::sort(nonzero.begin(), nonzero.end());
            nonzero.erase(std::unique(nonzero.begin(), nonzero.end()), nonzero.end());
            const int len = R[q] - L[q];
            ans[q] = sum_k(len);
            const auto add_to = [&](const i64 s, const int a, const int n) {
                ans[q] += (n * s + sum_k(n) * a) * len;
                ans[q] -= n * s * s + 2 * s * sum_k(n) * a + sum_k2(n) * a * a;
            };
            int left = 0, right = 0;
            vector<int> next;
            for (const int a : nonzero) {
                const int b = block[a];
                std::cerr << a << ' ' << b << std::endl;
                if (b == 0) {
                    continue;
                }
                next.push_back(a);
                if (left > right) {
                    std::swap(left, right);
                }
                const int need = std::min(b, (right - left + a - 1) / a);
                const int to_left = need + (b - need) / 2;
                const int to_right = b - to_left;
                add_to(left, a, to_left);
                add_to(right, a, to_right);
                left += a * to_left;
                right += a * to_right;
            }
            ans[q] -= (i64)left * (len - left);
            nonzero = std::move(next);
        }
    }
    for (const i64 x : ans) {
        std::cout << x << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 1 ms 1484 KB Output is correct
3 Correct 1 ms 1484 KB Output is correct
4 Correct 1 ms 1464 KB Output is correct
5 Correct 1 ms 1484 KB Output is correct
6 Correct 1 ms 1484 KB Output is correct
7 Correct 1 ms 1464 KB Output is correct
8 Correct 1 ms 1484 KB Output is correct
9 Correct 1 ms 1484 KB Output is correct
10 Correct 1 ms 1484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 3 ms 1484 KB Output is correct
3 Correct 18 ms 1648 KB Output is correct
4 Correct 140 ms 3164 KB Output is correct
5 Correct 362 ms 4964 KB Output is correct
6 Correct 322 ms 6572 KB Output is correct
7 Correct 827 ms 7072 KB Output is correct
8 Correct 310 ms 6596 KB Output is correct
9 Correct 312 ms 6588 KB Output is correct
10 Correct 491 ms 6592 KB Output is correct
11 Correct 379 ms 6596 KB Output is correct
12 Correct 292 ms 6592 KB Output is correct
13 Correct 303 ms 6596 KB Output is correct
14 Correct 407 ms 6584 KB Output is correct
15 Correct 375 ms 6592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 3 ms 1484 KB Output is correct
3 Correct 18 ms 1648 KB Output is correct
4 Correct 140 ms 3164 KB Output is correct
5 Correct 362 ms 4964 KB Output is correct
6 Correct 322 ms 6572 KB Output is correct
7 Correct 827 ms 7072 KB Output is correct
8 Correct 310 ms 6596 KB Output is correct
9 Correct 312 ms 6588 KB Output is correct
10 Correct 491 ms 6592 KB Output is correct
11 Correct 379 ms 6596 KB Output is correct
12 Correct 292 ms 6592 KB Output is correct
13 Correct 303 ms 6596 KB Output is correct
14 Correct 407 ms 6584 KB Output is correct
15 Correct 375 ms 6592 KB Output is correct
16 Correct 1 ms 1484 KB Output is correct
17 Correct 2 ms 1484 KB Output is correct
18 Correct 10 ms 1612 KB Output is correct
19 Correct 104 ms 3244 KB Output is correct
20 Correct 195 ms 4668 KB Output is correct
21 Correct 320 ms 6600 KB Output is correct
22 Correct 218 ms 6660 KB Output is correct
23 Correct 281 ms 6608 KB Output is correct
24 Correct 217 ms 6596 KB Output is correct
25 Correct 242 ms 6660 KB Output is correct
26 Correct 243 ms 6632 KB Output is correct
27 Correct 272 ms 6588 KB Output is correct
28 Correct 404 ms 6588 KB Output is correct
29 Correct 254 ms 6636 KB Output is correct
30 Correct 246 ms 6592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 3 ms 1484 KB Output is correct
3 Correct 18 ms 1648 KB Output is correct
4 Correct 140 ms 3164 KB Output is correct
5 Correct 362 ms 4964 KB Output is correct
6 Correct 322 ms 6572 KB Output is correct
7 Correct 827 ms 7072 KB Output is correct
8 Correct 310 ms 6596 KB Output is correct
9 Correct 312 ms 6588 KB Output is correct
10 Correct 491 ms 6592 KB Output is correct
11 Correct 379 ms 6596 KB Output is correct
12 Correct 292 ms 6592 KB Output is correct
13 Correct 303 ms 6596 KB Output is correct
14 Correct 407 ms 6584 KB Output is correct
15 Correct 375 ms 6592 KB Output is correct
16 Correct 1 ms 1484 KB Output is correct
17 Correct 2 ms 1484 KB Output is correct
18 Correct 10 ms 1612 KB Output is correct
19 Correct 104 ms 3244 KB Output is correct
20 Correct 195 ms 4668 KB Output is correct
21 Correct 320 ms 6600 KB Output is correct
22 Correct 218 ms 6660 KB Output is correct
23 Correct 281 ms 6608 KB Output is correct
24 Correct 217 ms 6596 KB Output is correct
25 Correct 242 ms 6660 KB Output is correct
26 Correct 243 ms 6632 KB Output is correct
27 Correct 272 ms 6588 KB Output is correct
28 Correct 404 ms 6588 KB Output is correct
29 Correct 254 ms 6636 KB Output is correct
30 Correct 246 ms 6592 KB Output is correct
31 Correct 2 ms 1460 KB Output is correct
32 Correct 2 ms 1464 KB Output is correct
33 Correct 1 ms 1444 KB Output is correct
34 Correct 4 ms 1580 KB Output is correct
35 Correct 5 ms 1652 KB Output is correct
36 Correct 3 ms 1612 KB Output is correct
37 Correct 10 ms 2096 KB Output is correct
38 Correct 8 ms 2096 KB Output is correct
39 Correct 14 ms 2752 KB Output is correct
40 Correct 28 ms 4100 KB Output is correct
41 Correct 48 ms 5224 KB Output is correct
42 Correct 51 ms 5100 KB Output is correct
43 Correct 61 ms 5092 KB Output is correct
44 Correct 50 ms 5064 KB Output is correct
45 Correct 56 ms 5056 KB Output is correct
46 Correct 46 ms 5068 KB Output is correct
47 Correct 43 ms 5064 KB Output is correct
48 Correct 44 ms 5060 KB Output is correct
49 Correct 40 ms 5064 KB Output is correct
50 Correct 43 ms 5064 KB Output is correct
51 Correct 63 ms 5116 KB Output is correct
52 Correct 52 ms 5004 KB Output is correct
53 Correct 42 ms 5052 KB Output is correct
54 Correct 48 ms 5064 KB Output is correct
55 Correct 45 ms 5064 KB Output is correct
56 Correct 40 ms 5064 KB Output is correct
57 Correct 40 ms 5016 KB Output is correct
58 Correct 51 ms 5028 KB Output is correct
59 Correct 42 ms 5064 KB Output is correct
60 Correct 40 ms 5056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 1 ms 1484 KB Output is correct
3 Correct 1 ms 1484 KB Output is correct
4 Correct 1 ms 1464 KB Output is correct
5 Correct 1 ms 1484 KB Output is correct
6 Correct 1 ms 1484 KB Output is correct
7 Correct 1 ms 1464 KB Output is correct
8 Correct 1 ms 1484 KB Output is correct
9 Correct 1 ms 1484 KB Output is correct
10 Correct 1 ms 1484 KB Output is correct
11 Correct 1 ms 1484 KB Output is correct
12 Correct 3 ms 1484 KB Output is correct
13 Correct 18 ms 1648 KB Output is correct
14 Correct 140 ms 3164 KB Output is correct
15 Correct 362 ms 4964 KB Output is correct
16 Correct 322 ms 6572 KB Output is correct
17 Correct 827 ms 7072 KB Output is correct
18 Correct 310 ms 6596 KB Output is correct
19 Correct 312 ms 6588 KB Output is correct
20 Correct 491 ms 6592 KB Output is correct
21 Correct 379 ms 6596 KB Output is correct
22 Correct 292 ms 6592 KB Output is correct
23 Correct 303 ms 6596 KB Output is correct
24 Correct 407 ms 6584 KB Output is correct
25 Correct 375 ms 6592 KB Output is correct
26 Correct 1 ms 1484 KB Output is correct
27 Correct 2 ms 1484 KB Output is correct
28 Correct 10 ms 1612 KB Output is correct
29 Correct 104 ms 3244 KB Output is correct
30 Correct 195 ms 4668 KB Output is correct
31 Correct 320 ms 6600 KB Output is correct
32 Correct 218 ms 6660 KB Output is correct
33 Correct 281 ms 6608 KB Output is correct
34 Correct 217 ms 6596 KB Output is correct
35 Correct 242 ms 6660 KB Output is correct
36 Correct 243 ms 6632 KB Output is correct
37 Correct 272 ms 6588 KB Output is correct
38 Correct 404 ms 6588 KB Output is correct
39 Correct 254 ms 6636 KB Output is correct
40 Correct 246 ms 6592 KB Output is correct
41 Correct 2 ms 1460 KB Output is correct
42 Correct 2 ms 1464 KB Output is correct
43 Correct 1 ms 1444 KB Output is correct
44 Correct 4 ms 1580 KB Output is correct
45 Correct 5 ms 1652 KB Output is correct
46 Correct 3 ms 1612 KB Output is correct
47 Correct 10 ms 2096 KB Output is correct
48 Correct 8 ms 2096 KB Output is correct
49 Correct 14 ms 2752 KB Output is correct
50 Correct 28 ms 4100 KB Output is correct
51 Correct 48 ms 5224 KB Output is correct
52 Correct 51 ms 5100 KB Output is correct
53 Correct 61 ms 5092 KB Output is correct
54 Correct 50 ms 5064 KB Output is correct
55 Correct 56 ms 5056 KB Output is correct
56 Correct 46 ms 5068 KB Output is correct
57 Correct 43 ms 5064 KB Output is correct
58 Correct 44 ms 5060 KB Output is correct
59 Correct 40 ms 5064 KB Output is correct
60 Correct 43 ms 5064 KB Output is correct
61 Correct 63 ms 5116 KB Output is correct
62 Correct 52 ms 5004 KB Output is correct
63 Correct 42 ms 5052 KB Output is correct
64 Correct 48 ms 5064 KB Output is correct
65 Correct 45 ms 5064 KB Output is correct
66 Correct 40 ms 5064 KB Output is correct
67 Correct 40 ms 5016 KB Output is correct
68 Correct 51 ms 5028 KB Output is correct
69 Correct 42 ms 5064 KB Output is correct
70 Correct 40 ms 5056 KB Output is correct
71 Correct 7 ms 2036 KB Output is correct
72 Correct 6 ms 2132 KB Output is correct
73 Correct 6 ms 2012 KB Output is correct
74 Correct 6 ms 2120 KB Output is correct
75 Correct 5 ms 2012 KB Output is correct
76 Correct 10 ms 2748 KB Output is correct
77 Correct 9 ms 2752 KB Output is correct
78 Correct 10 ms 2760 KB Output is correct
79 Correct 13 ms 2756 KB Output is correct
80 Correct 11 ms 2756 KB Output is correct
81 Correct 17 ms 3652 KB Output is correct
82 Correct 17 ms 3696 KB Output is correct
83 Correct 19 ms 3640 KB Output is correct
84 Correct 19 ms 3704 KB Output is correct
85 Correct 17 ms 3648 KB Output is correct
86 Correct 20 ms 3640 KB Output is correct
87 Correct 21 ms 3628 KB Output is correct
88 Correct 19 ms 3644 KB Output is correct
89 Correct 18 ms 3648 KB Output is correct
90 Correct 20 ms 3644 KB Output is correct
91 Correct 28 ms 4468 KB Output is correct
92 Correct 29 ms 4440 KB Output is correct
93 Correct 30 ms 4456 KB Output is correct
94 Correct 34 ms 4500 KB Output is correct
95 Correct 37 ms 4392 KB Output is correct
96 Correct 35 ms 4468 KB Output is correct
97 Correct 33 ms 4416 KB Output is correct
98 Correct 34 ms 4412 KB Output is correct
99 Correct 33 ms 4372 KB Output is correct
100 Correct 36 ms 4544 KB Output is correct
101 Correct 34 ms 4420 KB Output is correct
102 Correct 31 ms 4404 KB Output is correct
103 Correct 37 ms 4440 KB Output is correct
104 Correct 31 ms 4548 KB Output is correct
105 Correct 33 ms 4472 KB Output is correct
106 Correct 38 ms 4372 KB Output is correct
107 Correct 38 ms 4496 KB Output is correct
108 Correct 32 ms 4380 KB Output is correct
109 Correct 36 ms 4364 KB Output is correct
110 Correct 32 ms 4456 KB Output is correct
111 Correct 33 ms 4376 KB Output is correct
112 Correct 33 ms 4604 KB Output is correct
113 Correct 32 ms 4432 KB Output is correct
114 Correct 31 ms 4452 KB Output is correct
115 Correct 44 ms 4404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1484 KB Output is correct
2 Correct 1 ms 1484 KB Output is correct
3 Correct 1 ms 1484 KB Output is correct
4 Correct 1 ms 1464 KB Output is correct
5 Correct 1 ms 1484 KB Output is correct
6 Correct 1 ms 1484 KB Output is correct
7 Correct 1 ms 1464 KB Output is correct
8 Correct 1 ms 1484 KB Output is correct
9 Correct 1 ms 1484 KB Output is correct
10 Correct 1 ms 1484 KB Output is correct
11 Correct 1 ms 1484 KB Output is correct
12 Correct 3 ms 1484 KB Output is correct
13 Correct 18 ms 1648 KB Output is correct
14 Correct 140 ms 3164 KB Output is correct
15 Correct 362 ms 4964 KB Output is correct
16 Correct 322 ms 6572 KB Output is correct
17 Correct 827 ms 7072 KB Output is correct
18 Correct 310 ms 6596 KB Output is correct
19 Correct 312 ms 6588 KB Output is correct
20 Correct 491 ms 6592 KB Output is correct
21 Correct 379 ms 6596 KB Output is correct
22 Correct 292 ms 6592 KB Output is correct
23 Correct 303 ms 6596 KB Output is correct
24 Correct 407 ms 6584 KB Output is correct
25 Correct 375 ms 6592 KB Output is correct
26 Correct 1 ms 1484 KB Output is correct
27 Correct 2 ms 1484 KB Output is correct
28 Correct 10 ms 1612 KB Output is correct
29 Correct 104 ms 3244 KB Output is correct
30 Correct 195 ms 4668 KB Output is correct
31 Correct 320 ms 6600 KB Output is correct
32 Correct 218 ms 6660 KB Output is correct
33 Correct 281 ms 6608 KB Output is correct
34 Correct 217 ms 6596 KB Output is correct
35 Correct 242 ms 6660 KB Output is correct
36 Correct 243 ms 6632 KB Output is correct
37 Correct 272 ms 6588 KB Output is correct
38 Correct 404 ms 6588 KB Output is correct
39 Correct 254 ms 6636 KB Output is correct
40 Correct 246 ms 6592 KB Output is correct
41 Correct 2 ms 1460 KB Output is correct
42 Correct 2 ms 1464 KB Output is correct
43 Correct 1 ms 1444 KB Output is correct
44 Correct 4 ms 1580 KB Output is correct
45 Correct 5 ms 1652 KB Output is correct
46 Correct 3 ms 1612 KB Output is correct
47 Correct 10 ms 2096 KB Output is correct
48 Correct 8 ms 2096 KB Output is correct
49 Correct 14 ms 2752 KB Output is correct
50 Correct 28 ms 4100 KB Output is correct
51 Correct 48 ms 5224 KB Output is correct
52 Correct 51 ms 5100 KB Output is correct
53 Correct 61 ms 5092 KB Output is correct
54 Correct 50 ms 5064 KB Output is correct
55 Correct 56 ms 5056 KB Output is correct
56 Correct 46 ms 5068 KB Output is correct
57 Correct 43 ms 5064 KB Output is correct
58 Correct 44 ms 5060 KB Output is correct
59 Correct 40 ms 5064 KB Output is correct
60 Correct 43 ms 5064 KB Output is correct
61 Correct 63 ms 5116 KB Output is correct
62 Correct 52 ms 5004 KB Output is correct
63 Correct 42 ms 5052 KB Output is correct
64 Correct 48 ms 5064 KB Output is correct
65 Correct 45 ms 5064 KB Output is correct
66 Correct 40 ms 5064 KB Output is correct
67 Correct 40 ms 5016 KB Output is correct
68 Correct 51 ms 5028 KB Output is correct
69 Correct 42 ms 5064 KB Output is correct
70 Correct 40 ms 5056 KB Output is correct
71 Correct 7 ms 2036 KB Output is correct
72 Correct 6 ms 2132 KB Output is correct
73 Correct 6 ms 2012 KB Output is correct
74 Correct 6 ms 2120 KB Output is correct
75 Correct 5 ms 2012 KB Output is correct
76 Correct 10 ms 2748 KB Output is correct
77 Correct 9 ms 2752 KB Output is correct
78 Correct 10 ms 2760 KB Output is correct
79 Correct 13 ms 2756 KB Output is correct
80 Correct 11 ms 2756 KB Output is correct
81 Correct 17 ms 3652 KB Output is correct
82 Correct 17 ms 3696 KB Output is correct
83 Correct 19 ms 3640 KB Output is correct
84 Correct 19 ms 3704 KB Output is correct
85 Correct 17 ms 3648 KB Output is correct
86 Correct 20 ms 3640 KB Output is correct
87 Correct 21 ms 3628 KB Output is correct
88 Correct 19 ms 3644 KB Output is correct
89 Correct 18 ms 3648 KB Output is correct
90 Correct 20 ms 3644 KB Output is correct
91 Correct 28 ms 4468 KB Output is correct
92 Correct 29 ms 4440 KB Output is correct
93 Correct 30 ms 4456 KB Output is correct
94 Correct 34 ms 4500 KB Output is correct
95 Correct 37 ms 4392 KB Output is correct
96 Correct 35 ms 4468 KB Output is correct
97 Correct 33 ms 4416 KB Output is correct
98 Correct 34 ms 4412 KB Output is correct
99 Correct 33 ms 4372 KB Output is correct
100 Correct 36 ms 4544 KB Output is correct
101 Correct 34 ms 4420 KB Output is correct
102 Correct 31 ms 4404 KB Output is correct
103 Correct 37 ms 4440 KB Output is correct
104 Correct 31 ms 4548 KB Output is correct
105 Correct 33 ms 4472 KB Output is correct
106 Correct 38 ms 4372 KB Output is correct
107 Correct 38 ms 4496 KB Output is correct
108 Correct 32 ms 4380 KB Output is correct
109 Correct 36 ms 4364 KB Output is correct
110 Correct 32 ms 4456 KB Output is correct
111 Correct 33 ms 4376 KB Output is correct
112 Correct 33 ms 4604 KB Output is correct
113 Correct 32 ms 4432 KB Output is correct
114 Correct 31 ms 4452 KB Output is correct
115 Correct 44 ms 4404 KB Output is correct
116 Execution timed out 7027 ms 12236 KB Time limit exceeded
117 Halted 0 ms 0 KB -