Submission #730433

# Submission time Handle Problem Language Result Execution time Memory
730433 2023-04-25T21:29:49 Z MilosMilutinovic Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 9888 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;
    // p ^ 2 - p + (p + len) ^ 2 - (p - len) + ... + (p + (c - 1) * len) ^ 2 - (p + (c - 1) * len)
    // - p - (p - len) - (p + (c - 1) * len) 

    res += c * 1LL * p * p;
    res += 2 * ((c - 1) * 1LL * c / 2) * len * p;
    res += ((c - 1) * 1LL * c * (2 * (c - 1) + 1) / 6) * len * len; 
    res -= c * 1LL * p;
    res -= ((c - 1) * 1LL * c / 2) * len;
    
    n = n - p + 1;
    c++;
    // (n - c * len) ^ 2 + (n - c * len) + ... + (n - len) ^ 2 + (n - len) + n ^ 2 + n
    res += c * 1LL * n * n;
    res -= 2 * ((c - 1) * 1LL * c / 2) * len * n;
    res += ((c - 1) * 1LL * c * (2 * (c - 1) + 1) / 6) * len * len;
    res += n * 1LL * c;
    res -= ((c - 1) * 1LL * c / 2) * len;
    res -= (n * 1LL * n + n);

    return res / 2;
  };
  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 2 ms 2644 KB Output is correct
2 Correct 2 ms 2620 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 8 ms 3908 KB Output is correct
5 Correct 16 ms 5068 KB Output is correct
6 Correct 37 ms 6720 KB Output is correct
7 Correct 24 ms 6572 KB Output is correct
8 Correct 24 ms 6600 KB Output is correct
9 Correct 22 ms 6592 KB Output is correct
10 Correct 24 ms 6616 KB Output is correct
11 Correct 23 ms 6700 KB Output is correct
12 Correct 23 ms 6728 KB Output is correct
13 Correct 22 ms 6716 KB Output is correct
14 Correct 25 ms 6724 KB Output is correct
15 Correct 23 ms 6600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 8 ms 3908 KB Output is correct
5 Correct 16 ms 5068 KB Output is correct
6 Correct 37 ms 6720 KB Output is correct
7 Correct 24 ms 6572 KB Output is correct
8 Correct 24 ms 6600 KB Output is correct
9 Correct 22 ms 6592 KB Output is correct
10 Correct 24 ms 6616 KB Output is correct
11 Correct 23 ms 6700 KB Output is correct
12 Correct 23 ms 6728 KB Output is correct
13 Correct 22 ms 6716 KB Output is correct
14 Correct 25 ms 6724 KB Output is correct
15 Correct 23 ms 6600 KB Output is correct
16 Correct 2 ms 2620 KB Output is correct
17 Correct 2 ms 2624 KB Output is correct
18 Correct 2 ms 2892 KB Output is correct
19 Correct 9 ms 4024 KB Output is correct
20 Correct 16 ms 5184 KB Output is correct
21 Correct 32 ms 6844 KB Output is correct
22 Correct 25 ms 6848 KB Output is correct
23 Correct 24 ms 6852 KB Output is correct
24 Correct 25 ms 6848 KB Output is correct
25 Correct 25 ms 6828 KB Output is correct
26 Correct 26 ms 6720 KB Output is correct
27 Correct 27 ms 6836 KB Output is correct
28 Correct 25 ms 6724 KB Output is correct
29 Correct 26 ms 6724 KB Output is correct
30 Correct 25 ms 6768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 8 ms 3908 KB Output is correct
5 Correct 16 ms 5068 KB Output is correct
6 Correct 37 ms 6720 KB Output is correct
7 Correct 24 ms 6572 KB Output is correct
8 Correct 24 ms 6600 KB Output is correct
9 Correct 22 ms 6592 KB Output is correct
10 Correct 24 ms 6616 KB Output is correct
11 Correct 23 ms 6700 KB Output is correct
12 Correct 23 ms 6728 KB Output is correct
13 Correct 22 ms 6716 KB Output is correct
14 Correct 25 ms 6724 KB Output is correct
15 Correct 23 ms 6600 KB Output is correct
16 Correct 2 ms 2620 KB Output is correct
17 Correct 2 ms 2624 KB Output is correct
18 Correct 2 ms 2892 KB Output is correct
19 Correct 9 ms 4024 KB Output is correct
20 Correct 16 ms 5184 KB Output is correct
21 Correct 32 ms 6844 KB Output is correct
22 Correct 25 ms 6848 KB Output is correct
23 Correct 24 ms 6852 KB Output is correct
24 Correct 25 ms 6848 KB Output is correct
25 Correct 25 ms 6828 KB Output is correct
26 Correct 26 ms 6720 KB Output is correct
27 Correct 27 ms 6836 KB Output is correct
28 Correct 25 ms 6724 KB Output is correct
29 Correct 26 ms 6724 KB Output is correct
30 Correct 25 ms 6768 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2624 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 3 ms 2888 KB Output is correct
35 Correct 4 ms 2900 KB Output is correct
36 Correct 2 ms 2832 KB Output is correct
37 Correct 6 ms 3400 KB Output is correct
38 Correct 6 ms 3404 KB Output is correct
39 Correct 10 ms 4044 KB Output is correct
40 Correct 19 ms 5328 KB Output is correct
41 Correct 28 ms 7104 KB Output is correct
42 Correct 29 ms 7100 KB Output is correct
43 Correct 30 ms 7104 KB Output is correct
44 Correct 28 ms 7100 KB Output is correct
45 Correct 28 ms 7104 KB Output is correct
46 Correct 27 ms 7104 KB Output is correct
47 Correct 38 ms 7100 KB Output is correct
48 Correct 31 ms 7092 KB Output is correct
49 Correct 30 ms 7104 KB Output is correct
50 Correct 28 ms 7100 KB Output is correct
51 Correct 28 ms 7116 KB Output is correct
52 Correct 28 ms 7096 KB Output is correct
53 Correct 29 ms 7108 KB Output is correct
54 Correct 30 ms 7104 KB Output is correct
55 Correct 30 ms 7100 KB Output is correct
56 Correct 30 ms 7104 KB Output is correct
57 Correct 31 ms 7096 KB Output is correct
58 Correct 31 ms 7104 KB Output is correct
59 Correct 30 ms 7220 KB Output is correct
60 Correct 30 ms 7104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2620 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2616 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 8 ms 3908 KB Output is correct
15 Correct 16 ms 5068 KB Output is correct
16 Correct 37 ms 6720 KB Output is correct
17 Correct 24 ms 6572 KB Output is correct
18 Correct 24 ms 6600 KB Output is correct
19 Correct 22 ms 6592 KB Output is correct
20 Correct 24 ms 6616 KB Output is correct
21 Correct 23 ms 6700 KB Output is correct
22 Correct 23 ms 6728 KB Output is correct
23 Correct 22 ms 6716 KB Output is correct
24 Correct 25 ms 6724 KB Output is correct
25 Correct 23 ms 6600 KB Output is correct
26 Correct 2 ms 2620 KB Output is correct
27 Correct 2 ms 2624 KB Output is correct
28 Correct 2 ms 2892 KB Output is correct
29 Correct 9 ms 4024 KB Output is correct
30 Correct 16 ms 5184 KB Output is correct
31 Correct 32 ms 6844 KB Output is correct
32 Correct 25 ms 6848 KB Output is correct
33 Correct 24 ms 6852 KB Output is correct
34 Correct 25 ms 6848 KB Output is correct
35 Correct 25 ms 6828 KB Output is correct
36 Correct 26 ms 6720 KB Output is correct
37 Correct 27 ms 6836 KB Output is correct
38 Correct 25 ms 6724 KB Output is correct
39 Correct 26 ms 6724 KB Output is correct
40 Correct 25 ms 6768 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2624 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 3 ms 2888 KB Output is correct
45 Correct 4 ms 2900 KB Output is correct
46 Correct 2 ms 2832 KB Output is correct
47 Correct 6 ms 3400 KB Output is correct
48 Correct 6 ms 3404 KB Output is correct
49 Correct 10 ms 4044 KB Output is correct
50 Correct 19 ms 5328 KB Output is correct
51 Correct 28 ms 7104 KB Output is correct
52 Correct 29 ms 7100 KB Output is correct
53 Correct 30 ms 7104 KB Output is correct
54 Correct 28 ms 7100 KB Output is correct
55 Correct 28 ms 7104 KB Output is correct
56 Correct 27 ms 7104 KB Output is correct
57 Correct 38 ms 7100 KB Output is correct
58 Correct 31 ms 7092 KB Output is correct
59 Correct 30 ms 7104 KB Output is correct
60 Correct 28 ms 7100 KB Output is correct
61 Correct 28 ms 7116 KB Output is correct
62 Correct 28 ms 7096 KB Output is correct
63 Correct 29 ms 7108 KB Output is correct
64 Correct 30 ms 7104 KB Output is correct
65 Correct 30 ms 7100 KB Output is correct
66 Correct 30 ms 7104 KB Output is correct
67 Correct 31 ms 7096 KB Output is correct
68 Correct 31 ms 7104 KB Output is correct
69 Correct 30 ms 7220 KB Output is correct
70 Correct 30 ms 7104 KB Output is correct
71 Correct 6 ms 3532 KB Output is correct
72 Correct 6 ms 3412 KB Output is correct
73 Correct 7 ms 3460 KB Output is correct
74 Correct 7 ms 3432 KB Output is correct
75 Correct 8 ms 3532 KB Output is correct
76 Correct 11 ms 4176 KB Output is correct
77 Correct 11 ms 4176 KB Output is correct
78 Correct 13 ms 4176 KB Output is correct
79 Correct 11 ms 4132 KB Output is correct
80 Correct 11 ms 4176 KB Output is correct
81 Correct 20 ms 5572 KB Output is correct
82 Correct 22 ms 5572 KB Output is correct
83 Correct 23 ms 5580 KB Output is correct
84 Correct 20 ms 5576 KB Output is correct
85 Correct 20 ms 5572 KB Output is correct
86 Correct 22 ms 5696 KB Output is correct
87 Correct 22 ms 5692 KB Output is correct
88 Correct 23 ms 5688 KB Output is correct
89 Correct 21 ms 5700 KB Output is correct
90 Correct 22 ms 5616 KB Output is correct
91 Correct 31 ms 7744 KB Output is correct
92 Correct 31 ms 7736 KB Output is correct
93 Correct 32 ms 7748 KB Output is correct
94 Correct 35 ms 7736 KB Output is correct
95 Correct 34 ms 7744 KB Output is correct
96 Correct 37 ms 7856 KB Output is correct
97 Correct 35 ms 7924 KB Output is correct
98 Correct 35 ms 7844 KB Output is correct
99 Correct 35 ms 7876 KB Output is correct
100 Correct 36 ms 7880 KB Output is correct
101 Correct 38 ms 7948 KB Output is correct
102 Correct 40 ms 7864 KB Output is correct
103 Correct 40 ms 7840 KB Output is correct
104 Correct 37 ms 7872 KB Output is correct
105 Correct 35 ms 7876 KB Output is correct
106 Correct 38 ms 7868 KB Output is correct
107 Correct 36 ms 7876 KB Output is correct
108 Correct 36 ms 7872 KB Output is correct
109 Correct 42 ms 7876 KB Output is correct
110 Correct 39 ms 7944 KB Output is correct
111 Correct 40 ms 7992 KB Output is correct
112 Correct 35 ms 7864 KB Output is correct
113 Correct 38 ms 7988 KB Output is correct
114 Correct 38 ms 8004 KB Output is correct
115 Correct 41 ms 7876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2620 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2616 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 8 ms 3908 KB Output is correct
15 Correct 16 ms 5068 KB Output is correct
16 Correct 37 ms 6720 KB Output is correct
17 Correct 24 ms 6572 KB Output is correct
18 Correct 24 ms 6600 KB Output is correct
19 Correct 22 ms 6592 KB Output is correct
20 Correct 24 ms 6616 KB Output is correct
21 Correct 23 ms 6700 KB Output is correct
22 Correct 23 ms 6728 KB Output is correct
23 Correct 22 ms 6716 KB Output is correct
24 Correct 25 ms 6724 KB Output is correct
25 Correct 23 ms 6600 KB Output is correct
26 Correct 2 ms 2620 KB Output is correct
27 Correct 2 ms 2624 KB Output is correct
28 Correct 2 ms 2892 KB Output is correct
29 Correct 9 ms 4024 KB Output is correct
30 Correct 16 ms 5184 KB Output is correct
31 Correct 32 ms 6844 KB Output is correct
32 Correct 25 ms 6848 KB Output is correct
33 Correct 24 ms 6852 KB Output is correct
34 Correct 25 ms 6848 KB Output is correct
35 Correct 25 ms 6828 KB Output is correct
36 Correct 26 ms 6720 KB Output is correct
37 Correct 27 ms 6836 KB Output is correct
38 Correct 25 ms 6724 KB Output is correct
39 Correct 26 ms 6724 KB Output is correct
40 Correct 25 ms 6768 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2624 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 3 ms 2888 KB Output is correct
45 Correct 4 ms 2900 KB Output is correct
46 Correct 2 ms 2832 KB Output is correct
47 Correct 6 ms 3400 KB Output is correct
48 Correct 6 ms 3404 KB Output is correct
49 Correct 10 ms 4044 KB Output is correct
50 Correct 19 ms 5328 KB Output is correct
51 Correct 28 ms 7104 KB Output is correct
52 Correct 29 ms 7100 KB Output is correct
53 Correct 30 ms 7104 KB Output is correct
54 Correct 28 ms 7100 KB Output is correct
55 Correct 28 ms 7104 KB Output is correct
56 Correct 27 ms 7104 KB Output is correct
57 Correct 38 ms 7100 KB Output is correct
58 Correct 31 ms 7092 KB Output is correct
59 Correct 30 ms 7104 KB Output is correct
60 Correct 28 ms 7100 KB Output is correct
61 Correct 28 ms 7116 KB Output is correct
62 Correct 28 ms 7096 KB Output is correct
63 Correct 29 ms 7108 KB Output is correct
64 Correct 30 ms 7104 KB Output is correct
65 Correct 30 ms 7100 KB Output is correct
66 Correct 30 ms 7104 KB Output is correct
67 Correct 31 ms 7096 KB Output is correct
68 Correct 31 ms 7104 KB Output is correct
69 Correct 30 ms 7220 KB Output is correct
70 Correct 30 ms 7104 KB Output is correct
71 Correct 6 ms 3532 KB Output is correct
72 Correct 6 ms 3412 KB Output is correct
73 Correct 7 ms 3460 KB Output is correct
74 Correct 7 ms 3432 KB Output is correct
75 Correct 8 ms 3532 KB Output is correct
76 Correct 11 ms 4176 KB Output is correct
77 Correct 11 ms 4176 KB Output is correct
78 Correct 13 ms 4176 KB Output is correct
79 Correct 11 ms 4132 KB Output is correct
80 Correct 11 ms 4176 KB Output is correct
81 Correct 20 ms 5572 KB Output is correct
82 Correct 22 ms 5572 KB Output is correct
83 Correct 23 ms 5580 KB Output is correct
84 Correct 20 ms 5576 KB Output is correct
85 Correct 20 ms 5572 KB Output is correct
86 Correct 22 ms 5696 KB Output is correct
87 Correct 22 ms 5692 KB Output is correct
88 Correct 23 ms 5688 KB Output is correct
89 Correct 21 ms 5700 KB Output is correct
90 Correct 22 ms 5616 KB Output is correct
91 Correct 31 ms 7744 KB Output is correct
92 Correct 31 ms 7736 KB Output is correct
93 Correct 32 ms 7748 KB Output is correct
94 Correct 35 ms 7736 KB Output is correct
95 Correct 34 ms 7744 KB Output is correct
96 Correct 37 ms 7856 KB Output is correct
97 Correct 35 ms 7924 KB Output is correct
98 Correct 35 ms 7844 KB Output is correct
99 Correct 35 ms 7876 KB Output is correct
100 Correct 36 ms 7880 KB Output is correct
101 Correct 38 ms 7948 KB Output is correct
102 Correct 40 ms 7864 KB Output is correct
103 Correct 40 ms 7840 KB Output is correct
104 Correct 37 ms 7872 KB Output is correct
105 Correct 35 ms 7876 KB Output is correct
106 Correct 38 ms 7868 KB Output is correct
107 Correct 36 ms 7876 KB Output is correct
108 Correct 36 ms 7872 KB Output is correct
109 Correct 42 ms 7876 KB Output is correct
110 Correct 39 ms 7944 KB Output is correct
111 Correct 40 ms 7992 KB Output is correct
112 Correct 35 ms 7864 KB Output is correct
113 Correct 38 ms 7988 KB Output is correct
114 Correct 38 ms 8004 KB Output is correct
115 Correct 41 ms 7876 KB Output is correct
116 Correct 289 ms 4724 KB Output is correct
117 Correct 331 ms 4736 KB Output is correct
118 Correct 589 ms 5368 KB Output is correct
119 Correct 585 ms 5344 KB Output is correct
120 Correct 473 ms 5520 KB Output is correct
121 Correct 1151 ms 6068 KB Output is correct
122 Correct 1048 ms 6120 KB Output is correct
123 Correct 3555 ms 7744 KB Output is correct
124 Correct 3052 ms 7944 KB Output is correct
125 Correct 3149 ms 7808 KB Output is correct
126 Correct 6865 ms 9816 KB Output is correct
127 Correct 6829 ms 9888 KB Output is correct
128 Execution timed out 7054 ms 9784 KB Time limit exceeded
129 Halted 0 ms 0 KB -