Submission #1089981

# Submission time Handle Problem Language Result Execution time Memory
1089981 2024-09-17T13:28:25 Z juicy Diversity (CEOI21_diversity) C++17
100 / 100
993 ms 6092 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 3e5 + 5, B = 550;

int n, q;
int a[N], cnt[N], fre[N];
long long res[N];
vector<int> hev;

void add(int x, int y) {
  --fre[cnt[x]];
  ++fre[cnt[x] += y];
}

long long S1(int i) {
  return (long long) i * (i + 1) / 2;
}

long long S2(int i) {
  return (long long) i * (i + 1) * (2 * i + 1) / 6; 
}

long long calc(int i, int a, int b, int n) {
  return (long long) a * i * (n - i) + S1(a - 1) * b * (n - 2 * i) - S2(a - 1) * b * b;
}

long long qry(int l) {
  long long res = S1(l);
  vector<array<int, 2>> cnd;
  for (int i = 1; i < B; ++i) {
    if (fre[i]) {
      cnd.push_back({i, fre[i]});
    }
  }
  for (int i : hev) {
    if (cnt[i] >= B) {
      cnd.push_back({cnt[i], 1});
    }
  }
  sort(cnd.begin(), cnd.end());
  int x = 0, y = l, m = 0;
  for (auto [a, b] : cnd) {
    int lt, rt;
    if (m & 1) {
      lt = (b + 1) / 2;
      rt = b / 2;
    } else {
      lt = b / 2;
      rt = (b + 1) / 2;
    }
    if (lt) {
      res += calc(x, lt, a, l);
      x += lt * a;
    }
    if (rt) {
      y -= rt * a;
      res += calc(y, rt, a, l);
    }
    m += b;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    add(a[i], 1);
  }
  for (int i = 1; i < N; ++i) {
    if (cnt[i] >= B) {
      hev.push_back(i);
    }
  }
  vector<array<int, 3>> que;
  for (int i = 1; i <= q; ++i) {
    int l, r; cin >> l >> r;
    que.push_back({l, r, i});
  }
  sort(que.begin(), que.end(), [&](const auto &a, const auto &b) {
    return a[0] / B == b[0] / B ? a[1] < b[1] : a[0] < b[0];
  });
  int L = 1, R = n;
  for (auto [l, r, id] : que) {
    while (R < r) {
      add(a[++R], 1);
    } 
    while (L > l) {
      add(a[--L], 1);
    }
    while (R > r) {
      add(a[R--], -1);
    }
    while (L < l) {
      add(a[L++], -1);
    }
    res[id] = qry(r - l + 1);
  }
  for (int i = 1; i <= q; ++i) {
    cout << res[i] << "\n";
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 4 ms 1116 KB Output is correct
5 Correct 8 ms 1756 KB Output is correct
6 Correct 13 ms 2396 KB Output is correct
7 Correct 12 ms 2652 KB Output is correct
8 Correct 12 ms 2396 KB Output is correct
9 Correct 13 ms 2392 KB Output is correct
10 Correct 12 ms 2652 KB Output is correct
11 Correct 12 ms 2504 KB Output is correct
12 Correct 12 ms 2396 KB Output is correct
13 Correct 12 ms 2396 KB Output is correct
14 Correct 12 ms 2396 KB Output is correct
15 Correct 12 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 4 ms 1116 KB Output is correct
5 Correct 8 ms 1756 KB Output is correct
6 Correct 13 ms 2396 KB Output is correct
7 Correct 12 ms 2652 KB Output is correct
8 Correct 12 ms 2396 KB Output is correct
9 Correct 13 ms 2392 KB Output is correct
10 Correct 12 ms 2652 KB Output is correct
11 Correct 12 ms 2504 KB Output is correct
12 Correct 12 ms 2396 KB Output is correct
13 Correct 12 ms 2396 KB Output is correct
14 Correct 12 ms 2396 KB Output is correct
15 Correct 12 ms 2396 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 5 ms 1116 KB Output is correct
20 Correct 9 ms 1676 KB Output is correct
21 Correct 13 ms 2648 KB Output is correct
22 Correct 15 ms 2392 KB Output is correct
23 Correct 13 ms 2492 KB Output is correct
24 Correct 13 ms 2524 KB Output is correct
25 Correct 14 ms 2512 KB Output is correct
26 Correct 14 ms 2392 KB Output is correct
27 Correct 14 ms 2396 KB Output is correct
28 Correct 14 ms 2528 KB Output is correct
29 Correct 14 ms 2496 KB Output is correct
30 Correct 14 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 4 ms 1116 KB Output is correct
5 Correct 8 ms 1756 KB Output is correct
6 Correct 13 ms 2396 KB Output is correct
7 Correct 12 ms 2652 KB Output is correct
8 Correct 12 ms 2396 KB Output is correct
9 Correct 13 ms 2392 KB Output is correct
10 Correct 12 ms 2652 KB Output is correct
11 Correct 12 ms 2504 KB Output is correct
12 Correct 12 ms 2396 KB Output is correct
13 Correct 12 ms 2396 KB Output is correct
14 Correct 12 ms 2396 KB Output is correct
15 Correct 12 ms 2396 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 5 ms 1116 KB Output is correct
20 Correct 9 ms 1676 KB Output is correct
21 Correct 13 ms 2648 KB Output is correct
22 Correct 15 ms 2392 KB Output is correct
23 Correct 13 ms 2492 KB Output is correct
24 Correct 13 ms 2524 KB Output is correct
25 Correct 14 ms 2512 KB Output is correct
26 Correct 14 ms 2392 KB Output is correct
27 Correct 14 ms 2396 KB Output is correct
28 Correct 14 ms 2528 KB Output is correct
29 Correct 14 ms 2496 KB Output is correct
30 Correct 14 ms 2396 KB Output is correct
31 Correct 1 ms 352 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 484 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 3 ms 860 KB Output is correct
38 Correct 3 ms 676 KB Output is correct
39 Correct 5 ms 1220 KB Output is correct
40 Correct 9 ms 1884 KB Output is correct
41 Correct 14 ms 2592 KB Output is correct
42 Correct 14 ms 2780 KB Output is correct
43 Correct 13 ms 2652 KB Output is correct
44 Correct 14 ms 2648 KB Output is correct
45 Correct 14 ms 2784 KB Output is correct
46 Correct 14 ms 2596 KB Output is correct
47 Correct 14 ms 2648 KB Output is correct
48 Correct 14 ms 2652 KB Output is correct
49 Correct 13 ms 2652 KB Output is correct
50 Correct 16 ms 2652 KB Output is correct
51 Correct 14 ms 2652 KB Output is correct
52 Correct 14 ms 2652 KB Output is correct
53 Correct 14 ms 2592 KB Output is correct
54 Correct 13 ms 2764 KB Output is correct
55 Correct 15 ms 2652 KB Output is correct
56 Correct 14 ms 2772 KB Output is correct
57 Correct 14 ms 2780 KB Output is correct
58 Correct 14 ms 2652 KB Output is correct
59 Correct 15 ms 2600 KB Output is correct
60 Correct 14 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 4 ms 1116 KB Output is correct
15 Correct 8 ms 1756 KB Output is correct
16 Correct 13 ms 2396 KB Output is correct
17 Correct 12 ms 2652 KB Output is correct
18 Correct 12 ms 2396 KB Output is correct
19 Correct 13 ms 2392 KB Output is correct
20 Correct 12 ms 2652 KB Output is correct
21 Correct 12 ms 2504 KB Output is correct
22 Correct 12 ms 2396 KB Output is correct
23 Correct 12 ms 2396 KB Output is correct
24 Correct 12 ms 2396 KB Output is correct
25 Correct 12 ms 2396 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 5 ms 1116 KB Output is correct
30 Correct 9 ms 1676 KB Output is correct
31 Correct 13 ms 2648 KB Output is correct
32 Correct 15 ms 2392 KB Output is correct
33 Correct 13 ms 2492 KB Output is correct
34 Correct 13 ms 2524 KB Output is correct
35 Correct 14 ms 2512 KB Output is correct
36 Correct 14 ms 2392 KB Output is correct
37 Correct 14 ms 2396 KB Output is correct
38 Correct 14 ms 2528 KB Output is correct
39 Correct 14 ms 2496 KB Output is correct
40 Correct 14 ms 2396 KB Output is correct
41 Correct 1 ms 352 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 484 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 3 ms 860 KB Output is correct
48 Correct 3 ms 676 KB Output is correct
49 Correct 5 ms 1220 KB Output is correct
50 Correct 9 ms 1884 KB Output is correct
51 Correct 14 ms 2592 KB Output is correct
52 Correct 14 ms 2780 KB Output is correct
53 Correct 13 ms 2652 KB Output is correct
54 Correct 14 ms 2648 KB Output is correct
55 Correct 14 ms 2784 KB Output is correct
56 Correct 14 ms 2596 KB Output is correct
57 Correct 14 ms 2648 KB Output is correct
58 Correct 14 ms 2652 KB Output is correct
59 Correct 13 ms 2652 KB Output is correct
60 Correct 16 ms 2652 KB Output is correct
61 Correct 14 ms 2652 KB Output is correct
62 Correct 14 ms 2652 KB Output is correct
63 Correct 14 ms 2592 KB Output is correct
64 Correct 13 ms 2764 KB Output is correct
65 Correct 15 ms 2652 KB Output is correct
66 Correct 14 ms 2772 KB Output is correct
67 Correct 14 ms 2780 KB Output is correct
68 Correct 14 ms 2652 KB Output is correct
69 Correct 15 ms 2600 KB Output is correct
70 Correct 14 ms 2652 KB Output is correct
71 Correct 4 ms 860 KB Output is correct
72 Correct 4 ms 860 KB Output is correct
73 Correct 3 ms 860 KB Output is correct
74 Correct 4 ms 860 KB Output is correct
75 Correct 3 ms 860 KB Output is correct
76 Correct 6 ms 1116 KB Output is correct
77 Correct 6 ms 1260 KB Output is correct
78 Correct 7 ms 1116 KB Output is correct
79 Correct 6 ms 1112 KB Output is correct
80 Correct 6 ms 1116 KB Output is correct
81 Correct 10 ms 2080 KB Output is correct
82 Correct 12 ms 2136 KB Output is correct
83 Correct 11 ms 2152 KB Output is correct
84 Correct 10 ms 2140 KB Output is correct
85 Correct 11 ms 2160 KB Output is correct
86 Correct 12 ms 2396 KB Output is correct
87 Correct 13 ms 2396 KB Output is correct
88 Correct 12 ms 2396 KB Output is correct
89 Correct 12 ms 2396 KB Output is correct
90 Correct 12 ms 2216 KB Output is correct
91 Correct 19 ms 3640 KB Output is correct
92 Correct 20 ms 3680 KB Output is correct
93 Correct 18 ms 3772 KB Output is correct
94 Correct 20 ms 3548 KB Output is correct
95 Correct 19 ms 3684 KB Output is correct
96 Correct 21 ms 4184 KB Output is correct
97 Correct 21 ms 4184 KB Output is correct
98 Correct 23 ms 4192 KB Output is correct
99 Correct 23 ms 4192 KB Output is correct
100 Correct 23 ms 4184 KB Output is correct
101 Correct 22 ms 4172 KB Output is correct
102 Correct 23 ms 4188 KB Output is correct
103 Correct 21 ms 4180 KB Output is correct
104 Correct 23 ms 4184 KB Output is correct
105 Correct 26 ms 4176 KB Output is correct
106 Correct 23 ms 4436 KB Output is correct
107 Correct 22 ms 4444 KB Output is correct
108 Correct 28 ms 4516 KB Output is correct
109 Correct 22 ms 4340 KB Output is correct
110 Correct 23 ms 4324 KB Output is correct
111 Correct 22 ms 4432 KB Output is correct
112 Correct 23 ms 4440 KB Output is correct
113 Correct 22 ms 4304 KB Output is correct
114 Correct 22 ms 4444 KB Output is correct
115 Correct 23 ms 4528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 352 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 4 ms 1116 KB Output is correct
15 Correct 8 ms 1756 KB Output is correct
16 Correct 13 ms 2396 KB Output is correct
17 Correct 12 ms 2652 KB Output is correct
18 Correct 12 ms 2396 KB Output is correct
19 Correct 13 ms 2392 KB Output is correct
20 Correct 12 ms 2652 KB Output is correct
21 Correct 12 ms 2504 KB Output is correct
22 Correct 12 ms 2396 KB Output is correct
23 Correct 12 ms 2396 KB Output is correct
24 Correct 12 ms 2396 KB Output is correct
25 Correct 12 ms 2396 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 5 ms 1116 KB Output is correct
30 Correct 9 ms 1676 KB Output is correct
31 Correct 13 ms 2648 KB Output is correct
32 Correct 15 ms 2392 KB Output is correct
33 Correct 13 ms 2492 KB Output is correct
34 Correct 13 ms 2524 KB Output is correct
35 Correct 14 ms 2512 KB Output is correct
36 Correct 14 ms 2392 KB Output is correct
37 Correct 14 ms 2396 KB Output is correct
38 Correct 14 ms 2528 KB Output is correct
39 Correct 14 ms 2496 KB Output is correct
40 Correct 14 ms 2396 KB Output is correct
41 Correct 1 ms 352 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 484 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 3 ms 860 KB Output is correct
48 Correct 3 ms 676 KB Output is correct
49 Correct 5 ms 1220 KB Output is correct
50 Correct 9 ms 1884 KB Output is correct
51 Correct 14 ms 2592 KB Output is correct
52 Correct 14 ms 2780 KB Output is correct
53 Correct 13 ms 2652 KB Output is correct
54 Correct 14 ms 2648 KB Output is correct
55 Correct 14 ms 2784 KB Output is correct
56 Correct 14 ms 2596 KB Output is correct
57 Correct 14 ms 2648 KB Output is correct
58 Correct 14 ms 2652 KB Output is correct
59 Correct 13 ms 2652 KB Output is correct
60 Correct 16 ms 2652 KB Output is correct
61 Correct 14 ms 2652 KB Output is correct
62 Correct 14 ms 2652 KB Output is correct
63 Correct 14 ms 2592 KB Output is correct
64 Correct 13 ms 2764 KB Output is correct
65 Correct 15 ms 2652 KB Output is correct
66 Correct 14 ms 2772 KB Output is correct
67 Correct 14 ms 2780 KB Output is correct
68 Correct 14 ms 2652 KB Output is correct
69 Correct 15 ms 2600 KB Output is correct
70 Correct 14 ms 2652 KB Output is correct
71 Correct 4 ms 860 KB Output is correct
72 Correct 4 ms 860 KB Output is correct
73 Correct 3 ms 860 KB Output is correct
74 Correct 4 ms 860 KB Output is correct
75 Correct 3 ms 860 KB Output is correct
76 Correct 6 ms 1116 KB Output is correct
77 Correct 6 ms 1260 KB Output is correct
78 Correct 7 ms 1116 KB Output is correct
79 Correct 6 ms 1112 KB Output is correct
80 Correct 6 ms 1116 KB Output is correct
81 Correct 10 ms 2080 KB Output is correct
82 Correct 12 ms 2136 KB Output is correct
83 Correct 11 ms 2152 KB Output is correct
84 Correct 10 ms 2140 KB Output is correct
85 Correct 11 ms 2160 KB Output is correct
86 Correct 12 ms 2396 KB Output is correct
87 Correct 13 ms 2396 KB Output is correct
88 Correct 12 ms 2396 KB Output is correct
89 Correct 12 ms 2396 KB Output is correct
90 Correct 12 ms 2216 KB Output is correct
91 Correct 19 ms 3640 KB Output is correct
92 Correct 20 ms 3680 KB Output is correct
93 Correct 18 ms 3772 KB Output is correct
94 Correct 20 ms 3548 KB Output is correct
95 Correct 19 ms 3684 KB Output is correct
96 Correct 21 ms 4184 KB Output is correct
97 Correct 21 ms 4184 KB Output is correct
98 Correct 23 ms 4192 KB Output is correct
99 Correct 23 ms 4192 KB Output is correct
100 Correct 23 ms 4184 KB Output is correct
101 Correct 22 ms 4172 KB Output is correct
102 Correct 23 ms 4188 KB Output is correct
103 Correct 21 ms 4180 KB Output is correct
104 Correct 23 ms 4184 KB Output is correct
105 Correct 26 ms 4176 KB Output is correct
106 Correct 23 ms 4436 KB Output is correct
107 Correct 22 ms 4444 KB Output is correct
108 Correct 28 ms 4516 KB Output is correct
109 Correct 22 ms 4340 KB Output is correct
110 Correct 23 ms 4324 KB Output is correct
111 Correct 22 ms 4432 KB Output is correct
112 Correct 23 ms 4440 KB Output is correct
113 Correct 22 ms 4304 KB Output is correct
114 Correct 22 ms 4444 KB Output is correct
115 Correct 23 ms 4528 KB Output is correct
116 Correct 114 ms 2392 KB Output is correct
117 Correct 118 ms 2388 KB Output is correct
118 Correct 183 ms 3016 KB Output is correct
119 Correct 176 ms 3044 KB Output is correct
120 Correct 141 ms 3056 KB Output is correct
121 Correct 237 ms 3532 KB Output is correct
122 Correct 177 ms 3528 KB Output is correct
123 Correct 428 ms 4300 KB Output is correct
124 Correct 248 ms 4464 KB Output is correct
125 Correct 212 ms 4548 KB Output is correct
126 Correct 864 ms 5064 KB Output is correct
127 Correct 768 ms 5008 KB Output is correct
128 Correct 799 ms 5012 KB Output is correct
129 Correct 891 ms 5064 KB Output is correct
130 Correct 873 ms 5064 KB Output is correct
131 Correct 756 ms 4968 KB Output is correct
132 Correct 695 ms 5064 KB Output is correct
133 Correct 771 ms 5032 KB Output is correct
134 Correct 778 ms 5060 KB Output is correct
135 Correct 744 ms 5068 KB Output is correct
136 Correct 803 ms 4960 KB Output is correct
137 Correct 806 ms 5060 KB Output is correct
138 Correct 852 ms 5060 KB Output is correct
139 Correct 727 ms 5036 KB Output is correct
140 Correct 752 ms 5040 KB Output is correct
141 Correct 667 ms 5068 KB Output is correct
142 Correct 654 ms 5068 KB Output is correct
143 Correct 680 ms 5064 KB Output is correct
144 Correct 668 ms 5068 KB Output is correct
145 Correct 666 ms 5320 KB Output is correct
146 Correct 417 ms 5324 KB Output is correct
147 Correct 424 ms 5484 KB Output is correct
148 Correct 429 ms 5576 KB Output is correct
149 Correct 406 ms 5372 KB Output is correct
150 Correct 418 ms 5316 KB Output is correct
151 Correct 364 ms 5576 KB Output is correct
152 Correct 369 ms 5584 KB Output is correct
153 Correct 375 ms 5472 KB Output is correct
154 Correct 375 ms 5576 KB Output is correct
155 Correct 369 ms 5572 KB Output is correct
156 Correct 927 ms 6092 KB Output is correct
157 Correct 920 ms 6092 KB Output is correct
158 Correct 830 ms 6088 KB Output is correct
159 Correct 898 ms 5896 KB Output is correct
160 Correct 898 ms 5904 KB Output is correct
161 Correct 761 ms 6040 KB Output is correct
162 Correct 759 ms 6088 KB Output is correct
163 Correct 736 ms 5944 KB Output is correct
164 Correct 762 ms 6088 KB Output is correct
165 Correct 751 ms 5936 KB Output is correct
166 Correct 980 ms 5576 KB Output is correct
167 Correct 949 ms 5544 KB Output is correct
168 Correct 942 ms 5584 KB Output is correct
169 Correct 951 ms 5584 KB Output is correct
170 Correct 945 ms 5584 KB Output is correct
171 Correct 992 ms 5584 KB Output is correct
172 Correct 987 ms 5548 KB Output is correct
173 Correct 993 ms 5580 KB Output is correct
174 Correct 983 ms 5584 KB Output is correct
175 Correct 988 ms 5684 KB Output is correct