Submission #730436

# Submission time Handle Problem Language Result Execution time Memory
730436 2023-04-25T21:36:46 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 readint() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + (c - '0');
    c = getchar();
  }
  return x;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);  
  int n = readint();
  int q = readint();
  //cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    //cin >> a[i];
    a[i] = readint();
  }
  vector<int> l(q), r(q);
  for (int i = 0; i < q; i++) {
    //cin >> l[i] >> r[i];
    l[i] = readint();
    r[i] = readint();
    --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.0001e5;
  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';
    printf("%lld\n", ans[i]);
  }
  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 1 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 2 ms 2644 KB Output is correct
7 Correct 1 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 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2680 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 4 ms 3664 KB Output is correct
5 Correct 8 ms 4556 KB Output is correct
6 Correct 13 ms 6024 KB Output is correct
7 Correct 12 ms 6016 KB Output is correct
8 Correct 12 ms 5960 KB Output is correct
9 Correct 12 ms 5960 KB Output is correct
10 Correct 12 ms 5960 KB Output is correct
11 Correct 11 ms 5960 KB Output is correct
12 Correct 12 ms 5960 KB Output is correct
13 Correct 13 ms 5960 KB Output is correct
14 Correct 12 ms 5988 KB Output is correct
15 Correct 13 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2680 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 4 ms 3664 KB Output is correct
5 Correct 8 ms 4556 KB Output is correct
6 Correct 13 ms 6024 KB Output is correct
7 Correct 12 ms 6016 KB Output is correct
8 Correct 12 ms 5960 KB Output is correct
9 Correct 12 ms 5960 KB Output is correct
10 Correct 12 ms 5960 KB Output is correct
11 Correct 11 ms 5960 KB Output is correct
12 Correct 12 ms 5960 KB Output is correct
13 Correct 13 ms 5960 KB Output is correct
14 Correct 12 ms 5988 KB Output is correct
15 Correct 13 ms 6100 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 5 ms 3664 KB Output is correct
20 Correct 10 ms 4556 KB Output is correct
21 Correct 13 ms 6032 KB Output is correct
22 Correct 14 ms 5992 KB Output is correct
23 Correct 14 ms 6036 KB Output is correct
24 Correct 14 ms 5968 KB Output is correct
25 Correct 13 ms 6024 KB Output is correct
26 Correct 14 ms 6024 KB Output is correct
27 Correct 13 ms 5960 KB Output is correct
28 Correct 13 ms 5976 KB Output is correct
29 Correct 12 ms 6064 KB Output is correct
30 Correct 12 ms 5972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 2 ms 2680 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 4 ms 3664 KB Output is correct
5 Correct 8 ms 4556 KB Output is correct
6 Correct 13 ms 6024 KB Output is correct
7 Correct 12 ms 6016 KB Output is correct
8 Correct 12 ms 5960 KB Output is correct
9 Correct 12 ms 5960 KB Output is correct
10 Correct 12 ms 5960 KB Output is correct
11 Correct 11 ms 5960 KB Output is correct
12 Correct 12 ms 5960 KB Output is correct
13 Correct 13 ms 5960 KB Output is correct
14 Correct 12 ms 5988 KB Output is correct
15 Correct 13 ms 6100 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2772 KB Output is correct
19 Correct 5 ms 3664 KB Output is correct
20 Correct 10 ms 4556 KB Output is correct
21 Correct 13 ms 6032 KB Output is correct
22 Correct 14 ms 5992 KB Output is correct
23 Correct 14 ms 6036 KB Output is correct
24 Correct 14 ms 5968 KB Output is correct
25 Correct 13 ms 6024 KB Output is correct
26 Correct 14 ms 6024 KB Output is correct
27 Correct 13 ms 5960 KB Output is correct
28 Correct 13 ms 5976 KB Output is correct
29 Correct 12 ms 6064 KB Output is correct
30 Correct 12 ms 5972 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Correct 2 ms 2772 KB Output is correct
35 Correct 2 ms 2844 KB Output is correct
36 Correct 3 ms 2772 KB Output is correct
37 Correct 4 ms 3284 KB Output is correct
38 Correct 4 ms 3284 KB Output is correct
39 Correct 6 ms 3664 KB Output is correct
40 Correct 10 ms 4556 KB Output is correct
41 Correct 17 ms 5960 KB Output is correct
42 Correct 15 ms 5960 KB Output is correct
43 Correct 16 ms 5960 KB Output is correct
44 Correct 15 ms 5988 KB Output is correct
45 Correct 16 ms 5960 KB Output is correct
46 Correct 16 ms 6032 KB Output is correct
47 Correct 17 ms 5972 KB Output is correct
48 Correct 17 ms 5972 KB Output is correct
49 Correct 17 ms 5960 KB Output is correct
50 Correct 17 ms 5968 KB Output is correct
51 Correct 17 ms 6000 KB Output is correct
52 Correct 15 ms 5960 KB Output is correct
53 Correct 16 ms 5980 KB Output is correct
54 Correct 17 ms 6008 KB Output is correct
55 Correct 16 ms 5960 KB Output is correct
56 Correct 18 ms 5968 KB Output is correct
57 Correct 15 ms 5960 KB Output is correct
58 Correct 16 ms 5980 KB Output is correct
59 Correct 18 ms 6036 KB Output is correct
60 Correct 15 ms 5960 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 1 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 2 ms 2644 KB Output is correct
7 Correct 1 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 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2680 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 4 ms 3664 KB Output is correct
15 Correct 8 ms 4556 KB Output is correct
16 Correct 13 ms 6024 KB Output is correct
17 Correct 12 ms 6016 KB Output is correct
18 Correct 12 ms 5960 KB Output is correct
19 Correct 12 ms 5960 KB Output is correct
20 Correct 12 ms 5960 KB Output is correct
21 Correct 11 ms 5960 KB Output is correct
22 Correct 12 ms 5960 KB Output is correct
23 Correct 13 ms 5960 KB Output is correct
24 Correct 12 ms 5988 KB Output is correct
25 Correct 13 ms 6100 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 5 ms 3664 KB Output is correct
30 Correct 10 ms 4556 KB Output is correct
31 Correct 13 ms 6032 KB Output is correct
32 Correct 14 ms 5992 KB Output is correct
33 Correct 14 ms 6036 KB Output is correct
34 Correct 14 ms 5968 KB Output is correct
35 Correct 13 ms 6024 KB Output is correct
36 Correct 14 ms 6024 KB Output is correct
37 Correct 13 ms 5960 KB Output is correct
38 Correct 13 ms 5976 KB Output is correct
39 Correct 12 ms 6064 KB Output is correct
40 Correct 12 ms 5972 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 2 ms 2844 KB Output is correct
46 Correct 3 ms 2772 KB Output is correct
47 Correct 4 ms 3284 KB Output is correct
48 Correct 4 ms 3284 KB Output is correct
49 Correct 6 ms 3664 KB Output is correct
50 Correct 10 ms 4556 KB Output is correct
51 Correct 17 ms 5960 KB Output is correct
52 Correct 15 ms 5960 KB Output is correct
53 Correct 16 ms 5960 KB Output is correct
54 Correct 15 ms 5988 KB Output is correct
55 Correct 16 ms 5960 KB Output is correct
56 Correct 16 ms 6032 KB Output is correct
57 Correct 17 ms 5972 KB Output is correct
58 Correct 17 ms 5972 KB Output is correct
59 Correct 17 ms 5960 KB Output is correct
60 Correct 17 ms 5968 KB Output is correct
61 Correct 17 ms 6000 KB Output is correct
62 Correct 15 ms 5960 KB Output is correct
63 Correct 16 ms 5980 KB Output is correct
64 Correct 17 ms 6008 KB Output is correct
65 Correct 16 ms 5960 KB Output is correct
66 Correct 18 ms 5968 KB Output is correct
67 Correct 15 ms 5960 KB Output is correct
68 Correct 16 ms 5980 KB Output is correct
69 Correct 18 ms 6036 KB Output is correct
70 Correct 15 ms 5960 KB Output is correct
71 Correct 4 ms 3284 KB Output is correct
72 Correct 4 ms 3284 KB Output is correct
73 Correct 4 ms 3284 KB Output is correct
74 Correct 4 ms 3284 KB Output is correct
75 Correct 5 ms 3220 KB Output is correct
76 Correct 6 ms 3664 KB Output is correct
77 Correct 6 ms 3664 KB Output is correct
78 Correct 6 ms 3668 KB Output is correct
79 Correct 6 ms 3664 KB Output is correct
80 Correct 6 ms 3664 KB Output is correct
81 Correct 12 ms 4556 KB Output is correct
82 Correct 12 ms 4564 KB Output is correct
83 Correct 11 ms 4556 KB Output is correct
84 Correct 11 ms 4556 KB Output is correct
85 Correct 11 ms 4556 KB Output is correct
86 Correct 11 ms 4644 KB Output is correct
87 Correct 12 ms 4568 KB Output is correct
88 Correct 13 ms 4556 KB Output is correct
89 Correct 13 ms 4564 KB Output is correct
90 Correct 12 ms 4556 KB Output is correct
91 Correct 19 ms 5976 KB Output is correct
92 Correct 19 ms 5968 KB Output is correct
93 Correct 18 ms 5960 KB Output is correct
94 Correct 19 ms 5956 KB Output is correct
95 Correct 18 ms 5960 KB Output is correct
96 Correct 21 ms 5960 KB Output is correct
97 Correct 20 ms 5980 KB Output is correct
98 Correct 20 ms 5996 KB Output is correct
99 Correct 20 ms 5960 KB Output is correct
100 Correct 21 ms 5996 KB Output is correct
101 Correct 21 ms 5972 KB Output is correct
102 Correct 21 ms 5960 KB Output is correct
103 Correct 24 ms 5960 KB Output is correct
104 Correct 20 ms 5952 KB Output is correct
105 Correct 22 ms 5956 KB Output is correct
106 Correct 21 ms 6012 KB Output is correct
107 Correct 20 ms 5952 KB Output is correct
108 Correct 20 ms 6040 KB Output is correct
109 Correct 26 ms 6012 KB Output is correct
110 Correct 21 ms 5960 KB Output is correct
111 Correct 21 ms 5964 KB Output is correct
112 Correct 23 ms 6044 KB Output is correct
113 Correct 22 ms 5956 KB Output is correct
114 Correct 22 ms 5960 KB Output is correct
115 Correct 24 ms 6072 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 1 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 2 ms 2644 KB Output is correct
7 Correct 1 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 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 2 ms 2680 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 4 ms 3664 KB Output is correct
15 Correct 8 ms 4556 KB Output is correct
16 Correct 13 ms 6024 KB Output is correct
17 Correct 12 ms 6016 KB Output is correct
18 Correct 12 ms 5960 KB Output is correct
19 Correct 12 ms 5960 KB Output is correct
20 Correct 12 ms 5960 KB Output is correct
21 Correct 11 ms 5960 KB Output is correct
22 Correct 12 ms 5960 KB Output is correct
23 Correct 13 ms 5960 KB Output is correct
24 Correct 12 ms 5988 KB Output is correct
25 Correct 13 ms 6100 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 2 ms 2772 KB Output is correct
29 Correct 5 ms 3664 KB Output is correct
30 Correct 10 ms 4556 KB Output is correct
31 Correct 13 ms 6032 KB Output is correct
32 Correct 14 ms 5992 KB Output is correct
33 Correct 14 ms 6036 KB Output is correct
34 Correct 14 ms 5968 KB Output is correct
35 Correct 13 ms 6024 KB Output is correct
36 Correct 14 ms 6024 KB Output is correct
37 Correct 13 ms 5960 KB Output is correct
38 Correct 13 ms 5976 KB Output is correct
39 Correct 12 ms 6064 KB Output is correct
40 Correct 12 ms 5972 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 1 ms 2644 KB Output is correct
43 Correct 1 ms 2644 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 2 ms 2844 KB Output is correct
46 Correct 3 ms 2772 KB Output is correct
47 Correct 4 ms 3284 KB Output is correct
48 Correct 4 ms 3284 KB Output is correct
49 Correct 6 ms 3664 KB Output is correct
50 Correct 10 ms 4556 KB Output is correct
51 Correct 17 ms 5960 KB Output is correct
52 Correct 15 ms 5960 KB Output is correct
53 Correct 16 ms 5960 KB Output is correct
54 Correct 15 ms 5988 KB Output is correct
55 Correct 16 ms 5960 KB Output is correct
56 Correct 16 ms 6032 KB Output is correct
57 Correct 17 ms 5972 KB Output is correct
58 Correct 17 ms 5972 KB Output is correct
59 Correct 17 ms 5960 KB Output is correct
60 Correct 17 ms 5968 KB Output is correct
61 Correct 17 ms 6000 KB Output is correct
62 Correct 15 ms 5960 KB Output is correct
63 Correct 16 ms 5980 KB Output is correct
64 Correct 17 ms 6008 KB Output is correct
65 Correct 16 ms 5960 KB Output is correct
66 Correct 18 ms 5968 KB Output is correct
67 Correct 15 ms 5960 KB Output is correct
68 Correct 16 ms 5980 KB Output is correct
69 Correct 18 ms 6036 KB Output is correct
70 Correct 15 ms 5960 KB Output is correct
71 Correct 4 ms 3284 KB Output is correct
72 Correct 4 ms 3284 KB Output is correct
73 Correct 4 ms 3284 KB Output is correct
74 Correct 4 ms 3284 KB Output is correct
75 Correct 5 ms 3220 KB Output is correct
76 Correct 6 ms 3664 KB Output is correct
77 Correct 6 ms 3664 KB Output is correct
78 Correct 6 ms 3668 KB Output is correct
79 Correct 6 ms 3664 KB Output is correct
80 Correct 6 ms 3664 KB Output is correct
81 Correct 12 ms 4556 KB Output is correct
82 Correct 12 ms 4564 KB Output is correct
83 Correct 11 ms 4556 KB Output is correct
84 Correct 11 ms 4556 KB Output is correct
85 Correct 11 ms 4556 KB Output is correct
86 Correct 11 ms 4644 KB Output is correct
87 Correct 12 ms 4568 KB Output is correct
88 Correct 13 ms 4556 KB Output is correct
89 Correct 13 ms 4564 KB Output is correct
90 Correct 12 ms 4556 KB Output is correct
91 Correct 19 ms 5976 KB Output is correct
92 Correct 19 ms 5968 KB Output is correct
93 Correct 18 ms 5960 KB Output is correct
94 Correct 19 ms 5956 KB Output is correct
95 Correct 18 ms 5960 KB Output is correct
96 Correct 21 ms 5960 KB Output is correct
97 Correct 20 ms 5980 KB Output is correct
98 Correct 20 ms 5996 KB Output is correct
99 Correct 20 ms 5960 KB Output is correct
100 Correct 21 ms 5996 KB Output is correct
101 Correct 21 ms 5972 KB Output is correct
102 Correct 21 ms 5960 KB Output is correct
103 Correct 24 ms 5960 KB Output is correct
104 Correct 20 ms 5952 KB Output is correct
105 Correct 22 ms 5956 KB Output is correct
106 Correct 21 ms 6012 KB Output is correct
107 Correct 20 ms 5952 KB Output is correct
108 Correct 20 ms 6040 KB Output is correct
109 Correct 26 ms 6012 KB Output is correct
110 Correct 21 ms 5960 KB Output is correct
111 Correct 21 ms 5964 KB Output is correct
112 Correct 23 ms 6044 KB Output is correct
113 Correct 22 ms 5956 KB Output is correct
114 Correct 22 ms 5960 KB Output is correct
115 Correct 24 ms 6072 KB Output is correct
116 Correct 285 ms 4236 KB Output is correct
117 Correct 316 ms 4276 KB Output is correct
118 Correct 580 ms 4636 KB Output is correct
119 Correct 577 ms 4644 KB Output is correct
120 Correct 493 ms 4652 KB Output is correct
121 Correct 1153 ms 5164 KB Output is correct
122 Correct 999 ms 5176 KB Output is correct
123 Correct 3456 ms 6284 KB Output is correct
124 Correct 3071 ms 6244 KB Output is correct
125 Correct 3081 ms 6428 KB Output is correct
126 Correct 6759 ms 8176 KB Output is correct
127 Correct 6657 ms 8316 KB Output is correct
128 Correct 6754 ms 8128 KB Output is correct
129 Correct 6678 ms 9876 KB Output is correct
130 Correct 6654 ms 9888 KB Output is correct
131 Execution timed out 7040 ms 9844 KB Time limit exceeded
132 Halted 0 ms 0 KB -