Submission #730412

# Submission time Handle Problem Language Result Execution time Memory
730412 2023-04-25T20:22:39 Z MilosMilutinovic Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 9980 KB
/**
 *    author:  wxhtzdy
 *    created: 25.04.2023 21:06:35
**/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> l(q), r(q);
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
    --l[i]; --r[i];
  }
  const int BLOCK = 320;
  vector<int> order(q);
  iota(order.begin(), order.end(), 0);
  sort(order.begin(), order.end(), [&](int i, int j) {
    if (l[i] / BLOCK != l[j] / BLOCK) {
      return l[i] < l[j];
    } else {
      return r[i] < r[j];
    }
  });
  const int MAX = 3.01e5;
  vector<int> cnt(MAX);
  vector<int> f(MAX);
  vector<int> b;
  vector<bool> was(MAX);
  function<void(int)> Add = [&](int x) {   
    f[cnt[x]]--;
    cnt[x]++;
    f[cnt[x]]++;
    if (f[cnt[x]] > 0) {
      b.push_back(cnt[x]);
    }
  };
  function<void(int)> Remove = [&](int x) {
    f[cnt[x]]--;
    cnt[x]--;
    f[cnt[x]]++;
    if (cnt[x] > 0) {
      b.push_back(cnt[x]);
    }
  };
  auto Solve = [&](int p, int len, int c, int n) {
    long long res = 0;
    for (int i = 0; i < c; i++) {
      res += p * 1LL * (p - 1) / 2;
      p += len;
      res += (n - p + 1) * 1LL * (n - p + 2) / 2;
    }
    return res;
  };
  int L = 0, R = -1;
  vector<long long> ans(q);
  for (int i : order) {
    while (L < l[i]) {
      Remove(a[L++]);
    }
    while (L > l[i]) {
      Add(a[--L]);
    }
    while (R < r[i]) {
      Add(a[++R]);
    }
    while (R > r[i]) {
      Remove(a[R--]);
    }
    vector<int> new_b;
    for (int x : b) {
      if (x > 0 && !was[x] && f[x] > 0) {
        new_b.push_back(x); 
        was[x] = true;
      }
    }
    for (int x : b) {
      if (x > 0 && f[x] > 0) {
        was[x] = false;  
      }
    }
    swap(new_b, b);
    sort(b.begin(), b.end());
    int s = 0;
    for (int x : b) {
      s += f[x];
    }
    ans[i] = s * 1LL * (r[i] - l[i] + 1) * 1LL * (r[i] - l[i] + 2) / 2;
    int curl = l[i], curr = r[i];
    bool fl = false;
    for (int j = 0; j < (int) b.size(); j++) {
      int x = f[b[j]] / 2;
      int y = f[b[j]] / 2;
      if (f[b[j]] & 1) {
        if (!fl) {
          x += 1;            
        } else { 
          y += 1;
        }
        fl = !fl;
      }
      ans[i] -= Solve(curl - l[i] + 1, b[j], x, r[i] - l[i] + 1);
      ans[i] -= Solve(r[i] - curr + 1, b[j], y, r[i] - l[i] + 1);
      curl += x * b[j];
      curr -= y * b[j];
    }
  } 
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 12 ms 3664 KB Output is correct
5 Correct 16 ms 4568 KB Output is correct
6 Correct 22 ms 6088 KB Output is correct
7 Correct 22 ms 5972 KB Output is correct
8 Correct 23 ms 5972 KB Output is correct
9 Correct 22 ms 5972 KB Output is correct
10 Correct 23 ms 5960 KB Output is correct
11 Correct 23 ms 5976 KB Output is correct
12 Correct 23 ms 5976 KB Output is correct
13 Correct 22 ms 5968 KB Output is correct
14 Correct 28 ms 6028 KB Output is correct
15 Correct 22 ms 5952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 12 ms 3664 KB Output is correct
5 Correct 16 ms 4568 KB Output is correct
6 Correct 22 ms 6088 KB Output is correct
7 Correct 22 ms 5972 KB Output is correct
8 Correct 23 ms 5972 KB Output is correct
9 Correct 22 ms 5972 KB Output is correct
10 Correct 23 ms 5960 KB Output is correct
11 Correct 23 ms 5976 KB Output is correct
12 Correct 23 ms 5976 KB Output is correct
13 Correct 22 ms 5968 KB Output is correct
14 Correct 28 ms 6028 KB Output is correct
15 Correct 22 ms 5952 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 9 ms 3660 KB Output is correct
20 Correct 16 ms 4568 KB Output is correct
21 Correct 25 ms 5964 KB Output is correct
22 Correct 26 ms 6060 KB Output is correct
23 Correct 23 ms 6088 KB Output is correct
24 Correct 24 ms 6088 KB Output is correct
25 Correct 24 ms 5976 KB Output is correct
26 Correct 25 ms 5972 KB Output is correct
27 Correct 25 ms 5976 KB Output is correct
28 Correct 24 ms 6008 KB Output is correct
29 Correct 24 ms 6080 KB Output is correct
30 Correct 24 ms 5984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 12 ms 3664 KB Output is correct
5 Correct 16 ms 4568 KB Output is correct
6 Correct 22 ms 6088 KB Output is correct
7 Correct 22 ms 5972 KB Output is correct
8 Correct 23 ms 5972 KB Output is correct
9 Correct 22 ms 5972 KB Output is correct
10 Correct 23 ms 5960 KB Output is correct
11 Correct 23 ms 5976 KB Output is correct
12 Correct 23 ms 5976 KB Output is correct
13 Correct 22 ms 5968 KB Output is correct
14 Correct 28 ms 6028 KB Output is correct
15 Correct 22 ms 5952 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 9 ms 3660 KB Output is correct
20 Correct 16 ms 4568 KB Output is correct
21 Correct 25 ms 5964 KB Output is correct
22 Correct 26 ms 6060 KB Output is correct
23 Correct 23 ms 6088 KB Output is correct
24 Correct 24 ms 6088 KB Output is correct
25 Correct 24 ms 5976 KB Output is correct
26 Correct 25 ms 5972 KB Output is correct
27 Correct 25 ms 5976 KB Output is correct
28 Correct 24 ms 6008 KB Output is correct
29 Correct 24 ms 6080 KB Output is correct
30 Correct 24 ms 5984 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 3 ms 2772 KB Output is correct
35 Correct 2 ms 2772 KB Output is correct
36 Correct 2 ms 2772 KB Output is correct
37 Correct 6 ms 3240 KB Output is correct
38 Correct 6 ms 3284 KB Output is correct
39 Correct 10 ms 3664 KB Output is correct
40 Correct 19 ms 4568 KB Output is correct
41 Correct 27 ms 6104 KB Output is correct
42 Correct 28 ms 6088 KB Output is correct
43 Correct 28 ms 5976 KB Output is correct
44 Correct 29 ms 5956 KB Output is correct
45 Correct 26 ms 6088 KB Output is correct
46 Correct 29 ms 6008 KB Output is correct
47 Correct 27 ms 5968 KB Output is correct
48 Correct 37 ms 6080 KB Output is correct
49 Correct 28 ms 6088 KB Output is correct
50 Correct 28 ms 5976 KB Output is correct
51 Correct 28 ms 6088 KB Output is correct
52 Correct 27 ms 5968 KB Output is correct
53 Correct 28 ms 5972 KB Output is correct
54 Correct 27 ms 5964 KB Output is correct
55 Correct 30 ms 5952 KB Output is correct
56 Correct 29 ms 5964 KB Output is correct
57 Correct 29 ms 5968 KB Output is correct
58 Correct 27 ms 5972 KB Output is correct
59 Correct 30 ms 5968 KB Output is correct
60 Correct 27 ms 5952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 12 ms 3664 KB Output is correct
15 Correct 16 ms 4568 KB Output is correct
16 Correct 22 ms 6088 KB Output is correct
17 Correct 22 ms 5972 KB Output is correct
18 Correct 23 ms 5972 KB Output is correct
19 Correct 22 ms 5972 KB Output is correct
20 Correct 23 ms 5960 KB Output is correct
21 Correct 23 ms 5976 KB Output is correct
22 Correct 23 ms 5976 KB Output is correct
23 Correct 22 ms 5968 KB Output is correct
24 Correct 28 ms 6028 KB Output is correct
25 Correct 22 ms 5952 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 9 ms 3660 KB Output is correct
30 Correct 16 ms 4568 KB Output is correct
31 Correct 25 ms 5964 KB Output is correct
32 Correct 26 ms 6060 KB Output is correct
33 Correct 23 ms 6088 KB Output is correct
34 Correct 24 ms 6088 KB Output is correct
35 Correct 24 ms 5976 KB Output is correct
36 Correct 25 ms 5972 KB Output is correct
37 Correct 25 ms 5976 KB Output is correct
38 Correct 24 ms 6008 KB Output is correct
39 Correct 24 ms 6080 KB Output is correct
40 Correct 24 ms 5984 KB Output is correct
41 Correct 1 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 3 ms 2772 KB Output is correct
45 Correct 2 ms 2772 KB Output is correct
46 Correct 2 ms 2772 KB Output is correct
47 Correct 6 ms 3240 KB Output is correct
48 Correct 6 ms 3284 KB Output is correct
49 Correct 10 ms 3664 KB Output is correct
50 Correct 19 ms 4568 KB Output is correct
51 Correct 27 ms 6104 KB Output is correct
52 Correct 28 ms 6088 KB Output is correct
53 Correct 28 ms 5976 KB Output is correct
54 Correct 29 ms 5956 KB Output is correct
55 Correct 26 ms 6088 KB Output is correct
56 Correct 29 ms 6008 KB Output is correct
57 Correct 27 ms 5968 KB Output is correct
58 Correct 37 ms 6080 KB Output is correct
59 Correct 28 ms 6088 KB Output is correct
60 Correct 28 ms 5976 KB Output is correct
61 Correct 28 ms 6088 KB Output is correct
62 Correct 27 ms 5968 KB Output is correct
63 Correct 28 ms 5972 KB Output is correct
64 Correct 27 ms 5964 KB Output is correct
65 Correct 30 ms 5952 KB Output is correct
66 Correct 29 ms 5964 KB Output is correct
67 Correct 29 ms 5968 KB Output is correct
68 Correct 27 ms 5972 KB Output is correct
69 Correct 30 ms 5968 KB Output is correct
70 Correct 27 ms 5952 KB Output is correct
71 Correct 8 ms 3284 KB Output is correct
72 Correct 6 ms 3284 KB Output is correct
73 Correct 6 ms 3284 KB Output is correct
74 Correct 6 ms 3284 KB Output is correct
75 Correct 7 ms 3216 KB Output is correct
76 Correct 11 ms 3668 KB Output is correct
77 Correct 10 ms 3664 KB Output is correct
78 Correct 12 ms 3668 KB Output is correct
79 Correct 12 ms 3664 KB Output is correct
80 Correct 10 ms 3664 KB Output is correct
81 Correct 18 ms 4572 KB Output is correct
82 Correct 19 ms 4560 KB Output is correct
83 Correct 19 ms 4604 KB Output is correct
84 Correct 19 ms 4684 KB Output is correct
85 Correct 19 ms 4556 KB Output is correct
86 Correct 20 ms 4556 KB Output is correct
87 Correct 20 ms 4564 KB Output is correct
88 Correct 20 ms 4620 KB Output is correct
89 Correct 20 ms 4564 KB Output is correct
90 Correct 21 ms 4684 KB Output is correct
91 Correct 31 ms 5980 KB Output is correct
92 Correct 31 ms 5964 KB Output is correct
93 Correct 31 ms 5976 KB Output is correct
94 Correct 31 ms 5960 KB Output is correct
95 Correct 29 ms 5976 KB Output is correct
96 Correct 34 ms 5980 KB Output is correct
97 Correct 36 ms 5964 KB Output is correct
98 Correct 34 ms 5968 KB Output is correct
99 Correct 37 ms 5996 KB Output is correct
100 Correct 37 ms 6020 KB Output is correct
101 Correct 35 ms 5968 KB Output is correct
102 Correct 34 ms 5972 KB Output is correct
103 Correct 33 ms 5964 KB Output is correct
104 Correct 34 ms 6064 KB Output is correct
105 Correct 34 ms 6048 KB Output is correct
106 Correct 36 ms 5968 KB Output is correct
107 Correct 34 ms 5960 KB Output is correct
108 Correct 35 ms 5972 KB Output is correct
109 Correct 39 ms 6056 KB Output is correct
110 Correct 35 ms 6080 KB Output is correct
111 Correct 34 ms 5968 KB Output is correct
112 Correct 34 ms 6000 KB Output is correct
113 Correct 35 ms 5976 KB Output is correct
114 Correct 37 ms 5988 KB Output is correct
115 Correct 34 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 12 ms 3664 KB Output is correct
15 Correct 16 ms 4568 KB Output is correct
16 Correct 22 ms 6088 KB Output is correct
17 Correct 22 ms 5972 KB Output is correct
18 Correct 23 ms 5972 KB Output is correct
19 Correct 22 ms 5972 KB Output is correct
20 Correct 23 ms 5960 KB Output is correct
21 Correct 23 ms 5976 KB Output is correct
22 Correct 23 ms 5976 KB Output is correct
23 Correct 22 ms 5968 KB Output is correct
24 Correct 28 ms 6028 KB Output is correct
25 Correct 22 ms 5952 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 9 ms 3660 KB Output is correct
30 Correct 16 ms 4568 KB Output is correct
31 Correct 25 ms 5964 KB Output is correct
32 Correct 26 ms 6060 KB Output is correct
33 Correct 23 ms 6088 KB Output is correct
34 Correct 24 ms 6088 KB Output is correct
35 Correct 24 ms 5976 KB Output is correct
36 Correct 25 ms 5972 KB Output is correct
37 Correct 25 ms 5976 KB Output is correct
38 Correct 24 ms 6008 KB Output is correct
39 Correct 24 ms 6080 KB Output is correct
40 Correct 24 ms 5984 KB Output is correct
41 Correct 1 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 3 ms 2772 KB Output is correct
45 Correct 2 ms 2772 KB Output is correct
46 Correct 2 ms 2772 KB Output is correct
47 Correct 6 ms 3240 KB Output is correct
48 Correct 6 ms 3284 KB Output is correct
49 Correct 10 ms 3664 KB Output is correct
50 Correct 19 ms 4568 KB Output is correct
51 Correct 27 ms 6104 KB Output is correct
52 Correct 28 ms 6088 KB Output is correct
53 Correct 28 ms 5976 KB Output is correct
54 Correct 29 ms 5956 KB Output is correct
55 Correct 26 ms 6088 KB Output is correct
56 Correct 29 ms 6008 KB Output is correct
57 Correct 27 ms 5968 KB Output is correct
58 Correct 37 ms 6080 KB Output is correct
59 Correct 28 ms 6088 KB Output is correct
60 Correct 28 ms 5976 KB Output is correct
61 Correct 28 ms 6088 KB Output is correct
62 Correct 27 ms 5968 KB Output is correct
63 Correct 28 ms 5972 KB Output is correct
64 Correct 27 ms 5964 KB Output is correct
65 Correct 30 ms 5952 KB Output is correct
66 Correct 29 ms 5964 KB Output is correct
67 Correct 29 ms 5968 KB Output is correct
68 Correct 27 ms 5972 KB Output is correct
69 Correct 30 ms 5968 KB Output is correct
70 Correct 27 ms 5952 KB Output is correct
71 Correct 8 ms 3284 KB Output is correct
72 Correct 6 ms 3284 KB Output is correct
73 Correct 6 ms 3284 KB Output is correct
74 Correct 6 ms 3284 KB Output is correct
75 Correct 7 ms 3216 KB Output is correct
76 Correct 11 ms 3668 KB Output is correct
77 Correct 10 ms 3664 KB Output is correct
78 Correct 12 ms 3668 KB Output is correct
79 Correct 12 ms 3664 KB Output is correct
80 Correct 10 ms 3664 KB Output is correct
81 Correct 18 ms 4572 KB Output is correct
82 Correct 19 ms 4560 KB Output is correct
83 Correct 19 ms 4604 KB Output is correct
84 Correct 19 ms 4684 KB Output is correct
85 Correct 19 ms 4556 KB Output is correct
86 Correct 20 ms 4556 KB Output is correct
87 Correct 20 ms 4564 KB Output is correct
88 Correct 20 ms 4620 KB Output is correct
89 Correct 20 ms 4564 KB Output is correct
90 Correct 21 ms 4684 KB Output is correct
91 Correct 31 ms 5980 KB Output is correct
92 Correct 31 ms 5964 KB Output is correct
93 Correct 31 ms 5976 KB Output is correct
94 Correct 31 ms 5960 KB Output is correct
95 Correct 29 ms 5976 KB Output is correct
96 Correct 34 ms 5980 KB Output is correct
97 Correct 36 ms 5964 KB Output is correct
98 Correct 34 ms 5968 KB Output is correct
99 Correct 37 ms 5996 KB Output is correct
100 Correct 37 ms 6020 KB Output is correct
101 Correct 35 ms 5968 KB Output is correct
102 Correct 34 ms 5972 KB Output is correct
103 Correct 33 ms 5964 KB Output is correct
104 Correct 34 ms 6064 KB Output is correct
105 Correct 34 ms 6048 KB Output is correct
106 Correct 36 ms 5968 KB Output is correct
107 Correct 34 ms 5960 KB Output is correct
108 Correct 35 ms 5972 KB Output is correct
109 Correct 39 ms 6056 KB Output is correct
110 Correct 35 ms 6080 KB Output is correct
111 Correct 34 ms 5968 KB Output is correct
112 Correct 34 ms 6000 KB Output is correct
113 Correct 35 ms 5976 KB Output is correct
114 Correct 37 ms 5988 KB Output is correct
115 Correct 34 ms 5980 KB Output is correct
116 Correct 266 ms 4224 KB Output is correct
117 Correct 293 ms 4724 KB Output is correct
118 Correct 539 ms 5412 KB Output is correct
119 Correct 581 ms 5404 KB Output is correct
120 Correct 541 ms 5416 KB Output is correct
121 Correct 1152 ms 6076 KB Output is correct
122 Correct 1165 ms 6304 KB Output is correct
123 Correct 3381 ms 7680 KB Output is correct
124 Correct 3430 ms 7820 KB Output is correct
125 Correct 3925 ms 7884 KB Output is correct
126 Correct 6556 ms 9904 KB Output is correct
127 Correct 6424 ms 9936 KB Output is correct
128 Correct 6515 ms 9980 KB Output is correct
129 Correct 6456 ms 9904 KB Output is correct
130 Correct 6575 ms 9912 KB Output is correct
131 Correct 6862 ms 9836 KB Output is correct
132 Execution timed out 7015 ms 9836 KB Time limit exceeded
133 Halted 0 ms 0 KB -