Submission #730435

# Submission time Handle Problem Language Result Execution time Memory
730435 2023-04-25T21:33:46 Z MilosMilutinovic Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 8140 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.0001e5;
  vector<int> cnt(MAX);
  vector<int> f(MAX);
  vector<int> b;
  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 && f[x] > 0) {
        new_b.push_back(x); 
      }
    }
    b.swap(new_b);
    sort(b.begin(), b.end());
    b.erase(unique(b.begin(), b.end()), 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 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 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 2 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 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 9 ms 3672 KB Output is correct
5 Correct 15 ms 4564 KB Output is correct
6 Correct 24 ms 5980 KB Output is correct
7 Correct 27 ms 5980 KB Output is correct
8 Correct 20 ms 5936 KB Output is correct
9 Correct 22 ms 5968 KB Output is correct
10 Correct 23 ms 5984 KB Output is correct
11 Correct 23 ms 5968 KB Output is correct
12 Correct 21 ms 5984 KB Output is correct
13 Correct 22 ms 5968 KB Output is correct
14 Correct 21 ms 5968 KB Output is correct
15 Correct 21 ms 5964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 9 ms 3672 KB Output is correct
5 Correct 15 ms 4564 KB Output is correct
6 Correct 24 ms 5980 KB Output is correct
7 Correct 27 ms 5980 KB Output is correct
8 Correct 20 ms 5936 KB Output is correct
9 Correct 22 ms 5968 KB Output is correct
10 Correct 23 ms 5984 KB Output is correct
11 Correct 23 ms 5968 KB Output is correct
12 Correct 21 ms 5984 KB Output is correct
13 Correct 22 ms 5968 KB Output is correct
14 Correct 21 ms 5968 KB Output is correct
15 Correct 21 ms 5964 KB Output is correct
16 Correct 2 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 9 ms 3672 KB Output is correct
20 Correct 15 ms 4564 KB Output is correct
21 Correct 24 ms 6036 KB Output is correct
22 Correct 24 ms 5968 KB Output is correct
23 Correct 24 ms 5980 KB Output is correct
24 Correct 25 ms 5980 KB Output is correct
25 Correct 24 ms 5968 KB Output is correct
26 Correct 31 ms 6000 KB Output is correct
27 Correct 22 ms 5980 KB Output is correct
28 Correct 27 ms 5968 KB Output is correct
29 Correct 25 ms 5968 KB Output is correct
30 Correct 27 ms 5984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 9 ms 3672 KB Output is correct
5 Correct 15 ms 4564 KB Output is correct
6 Correct 24 ms 5980 KB Output is correct
7 Correct 27 ms 5980 KB Output is correct
8 Correct 20 ms 5936 KB Output is correct
9 Correct 22 ms 5968 KB Output is correct
10 Correct 23 ms 5984 KB Output is correct
11 Correct 23 ms 5968 KB Output is correct
12 Correct 21 ms 5984 KB Output is correct
13 Correct 22 ms 5968 KB Output is correct
14 Correct 21 ms 5968 KB Output is correct
15 Correct 21 ms 5964 KB Output is correct
16 Correct 2 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 9 ms 3672 KB Output is correct
20 Correct 15 ms 4564 KB Output is correct
21 Correct 24 ms 6036 KB Output is correct
22 Correct 24 ms 5968 KB Output is correct
23 Correct 24 ms 5980 KB Output is correct
24 Correct 25 ms 5980 KB Output is correct
25 Correct 24 ms 5968 KB Output is correct
26 Correct 31 ms 6000 KB Output is correct
27 Correct 22 ms 5980 KB Output is correct
28 Correct 27 ms 5968 KB Output is correct
29 Correct 25 ms 5968 KB Output is correct
30 Correct 27 ms 5984 KB Output is correct
31 Correct 2 ms 2644 KB Output is correct
32 Correct 2 ms 2644 KB Output is correct
33 Correct 2 ms 2644 KB Output is correct
34 Correct 2 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 7 ms 3416 KB Output is correct
38 Correct 7 ms 3416 KB Output is correct
39 Correct 13 ms 4180 KB Output is correct
40 Correct 24 ms 5720 KB Output is correct
41 Correct 36 ms 6860 KB Output is correct
42 Correct 38 ms 6852 KB Output is correct
43 Correct 41 ms 6752 KB Output is correct
44 Correct 48 ms 6892 KB Output is correct
45 Correct 37 ms 6868 KB Output is correct
46 Correct 38 ms 6876 KB Output is correct
47 Correct 35 ms 6860 KB Output is correct
48 Correct 36 ms 6792 KB Output is correct
49 Correct 34 ms 6856 KB Output is correct
50 Correct 45 ms 6732 KB Output is correct
51 Correct 41 ms 6872 KB Output is correct
52 Correct 51 ms 6756 KB Output is correct
53 Correct 37 ms 6852 KB Output is correct
54 Correct 35 ms 6860 KB Output is correct
55 Correct 34 ms 6852 KB Output is correct
56 Correct 35 ms 6872 KB Output is correct
57 Correct 36 ms 6872 KB Output is correct
58 Correct 36 ms 6860 KB Output is correct
59 Correct 36 ms 6852 KB Output is correct
60 Correct 43 ms 6872 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 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 2 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 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 9 ms 3672 KB Output is correct
15 Correct 15 ms 4564 KB Output is correct
16 Correct 24 ms 5980 KB Output is correct
17 Correct 27 ms 5980 KB Output is correct
18 Correct 20 ms 5936 KB Output is correct
19 Correct 22 ms 5968 KB Output is correct
20 Correct 23 ms 5984 KB Output is correct
21 Correct 23 ms 5968 KB Output is correct
22 Correct 21 ms 5984 KB Output is correct
23 Correct 22 ms 5968 KB Output is correct
24 Correct 21 ms 5968 KB Output is correct
25 Correct 21 ms 5964 KB Output is correct
26 Correct 2 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 9 ms 3672 KB Output is correct
30 Correct 15 ms 4564 KB Output is correct
31 Correct 24 ms 6036 KB Output is correct
32 Correct 24 ms 5968 KB Output is correct
33 Correct 24 ms 5980 KB Output is correct
34 Correct 25 ms 5980 KB Output is correct
35 Correct 24 ms 5968 KB Output is correct
36 Correct 31 ms 6000 KB Output is correct
37 Correct 22 ms 5980 KB Output is correct
38 Correct 27 ms 5968 KB Output is correct
39 Correct 25 ms 5968 KB Output is correct
40 Correct 27 ms 5984 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 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 7 ms 3416 KB Output is correct
48 Correct 7 ms 3416 KB Output is correct
49 Correct 13 ms 4180 KB Output is correct
50 Correct 24 ms 5720 KB Output is correct
51 Correct 36 ms 6860 KB Output is correct
52 Correct 38 ms 6852 KB Output is correct
53 Correct 41 ms 6752 KB Output is correct
54 Correct 48 ms 6892 KB Output is correct
55 Correct 37 ms 6868 KB Output is correct
56 Correct 38 ms 6876 KB Output is correct
57 Correct 35 ms 6860 KB Output is correct
58 Correct 36 ms 6792 KB Output is correct
59 Correct 34 ms 6856 KB Output is correct
60 Correct 45 ms 6732 KB Output is correct
61 Correct 41 ms 6872 KB Output is correct
62 Correct 51 ms 6756 KB Output is correct
63 Correct 37 ms 6852 KB Output is correct
64 Correct 35 ms 6860 KB Output is correct
65 Correct 34 ms 6852 KB Output is correct
66 Correct 35 ms 6872 KB Output is correct
67 Correct 36 ms 6872 KB Output is correct
68 Correct 36 ms 6860 KB Output is correct
69 Correct 36 ms 6852 KB Output is correct
70 Correct 43 ms 6872 KB Output is correct
71 Correct 8 ms 3416 KB Output is correct
72 Correct 8 ms 3348 KB Output is correct
73 Correct 7 ms 3416 KB Output is correct
74 Correct 8 ms 3416 KB Output is correct
75 Correct 7 ms 3416 KB Output is correct
76 Correct 14 ms 4184 KB Output is correct
77 Correct 15 ms 4176 KB Output is correct
78 Correct 14 ms 4180 KB Output is correct
79 Correct 14 ms 4180 KB Output is correct
80 Correct 16 ms 4180 KB Output is correct
81 Correct 35 ms 5712 KB Output is correct
82 Correct 27 ms 5712 KB Output is correct
83 Correct 25 ms 5720 KB Output is correct
84 Correct 29 ms 5724 KB Output is correct
85 Correct 27 ms 5704 KB Output is correct
86 Correct 27 ms 5720 KB Output is correct
87 Correct 33 ms 5704 KB Output is correct
88 Correct 27 ms 5748 KB Output is correct
89 Correct 26 ms 5712 KB Output is correct
90 Correct 28 ms 5712 KB Output is correct
91 Correct 40 ms 8016 KB Output is correct
92 Correct 47 ms 8004 KB Output is correct
93 Correct 38 ms 8068 KB Output is correct
94 Correct 42 ms 8008 KB Output is correct
95 Correct 45 ms 8016 KB Output is correct
96 Correct 43 ms 8048 KB Output is correct
97 Correct 43 ms 8016 KB Output is correct
98 Correct 44 ms 8080 KB Output is correct
99 Correct 54 ms 8008 KB Output is correct
100 Correct 46 ms 8052 KB Output is correct
101 Correct 41 ms 8016 KB Output is correct
102 Correct 45 ms 8000 KB Output is correct
103 Correct 43 ms 8036 KB Output is correct
104 Correct 46 ms 8084 KB Output is correct
105 Correct 45 ms 8008 KB Output is correct
106 Correct 43 ms 8016 KB Output is correct
107 Correct 41 ms 8096 KB Output is correct
108 Correct 43 ms 8016 KB Output is correct
109 Correct 38 ms 8012 KB Output is correct
110 Correct 40 ms 8004 KB Output is correct
111 Correct 39 ms 8020 KB Output is correct
112 Correct 40 ms 8004 KB Output is correct
113 Correct 39 ms 8096 KB Output is correct
114 Correct 46 ms 8016 KB Output is correct
115 Correct 48 ms 8140 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 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 2 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 2644 KB Output is correct
13 Correct 2 ms 2772 KB Output is correct
14 Correct 9 ms 3672 KB Output is correct
15 Correct 15 ms 4564 KB Output is correct
16 Correct 24 ms 5980 KB Output is correct
17 Correct 27 ms 5980 KB Output is correct
18 Correct 20 ms 5936 KB Output is correct
19 Correct 22 ms 5968 KB Output is correct
20 Correct 23 ms 5984 KB Output is correct
21 Correct 23 ms 5968 KB Output is correct
22 Correct 21 ms 5984 KB Output is correct
23 Correct 22 ms 5968 KB Output is correct
24 Correct 21 ms 5968 KB Output is correct
25 Correct 21 ms 5964 KB Output is correct
26 Correct 2 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 9 ms 3672 KB Output is correct
30 Correct 15 ms 4564 KB Output is correct
31 Correct 24 ms 6036 KB Output is correct
32 Correct 24 ms 5968 KB Output is correct
33 Correct 24 ms 5980 KB Output is correct
34 Correct 25 ms 5980 KB Output is correct
35 Correct 24 ms 5968 KB Output is correct
36 Correct 31 ms 6000 KB Output is correct
37 Correct 22 ms 5980 KB Output is correct
38 Correct 27 ms 5968 KB Output is correct
39 Correct 25 ms 5968 KB Output is correct
40 Correct 27 ms 5984 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2644 KB Output is correct
44 Correct 2 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 7 ms 3416 KB Output is correct
48 Correct 7 ms 3416 KB Output is correct
49 Correct 13 ms 4180 KB Output is correct
50 Correct 24 ms 5720 KB Output is correct
51 Correct 36 ms 6860 KB Output is correct
52 Correct 38 ms 6852 KB Output is correct
53 Correct 41 ms 6752 KB Output is correct
54 Correct 48 ms 6892 KB Output is correct
55 Correct 37 ms 6868 KB Output is correct
56 Correct 38 ms 6876 KB Output is correct
57 Correct 35 ms 6860 KB Output is correct
58 Correct 36 ms 6792 KB Output is correct
59 Correct 34 ms 6856 KB Output is correct
60 Correct 45 ms 6732 KB Output is correct
61 Correct 41 ms 6872 KB Output is correct
62 Correct 51 ms 6756 KB Output is correct
63 Correct 37 ms 6852 KB Output is correct
64 Correct 35 ms 6860 KB Output is correct
65 Correct 34 ms 6852 KB Output is correct
66 Correct 35 ms 6872 KB Output is correct
67 Correct 36 ms 6872 KB Output is correct
68 Correct 36 ms 6860 KB Output is correct
69 Correct 36 ms 6852 KB Output is correct
70 Correct 43 ms 6872 KB Output is correct
71 Correct 8 ms 3416 KB Output is correct
72 Correct 8 ms 3348 KB Output is correct
73 Correct 7 ms 3416 KB Output is correct
74 Correct 8 ms 3416 KB Output is correct
75 Correct 7 ms 3416 KB Output is correct
76 Correct 14 ms 4184 KB Output is correct
77 Correct 15 ms 4176 KB Output is correct
78 Correct 14 ms 4180 KB Output is correct
79 Correct 14 ms 4180 KB Output is correct
80 Correct 16 ms 4180 KB Output is correct
81 Correct 35 ms 5712 KB Output is correct
82 Correct 27 ms 5712 KB Output is correct
83 Correct 25 ms 5720 KB Output is correct
84 Correct 29 ms 5724 KB Output is correct
85 Correct 27 ms 5704 KB Output is correct
86 Correct 27 ms 5720 KB Output is correct
87 Correct 33 ms 5704 KB Output is correct
88 Correct 27 ms 5748 KB Output is correct
89 Correct 26 ms 5712 KB Output is correct
90 Correct 28 ms 5712 KB Output is correct
91 Correct 40 ms 8016 KB Output is correct
92 Correct 47 ms 8004 KB Output is correct
93 Correct 38 ms 8068 KB Output is correct
94 Correct 42 ms 8008 KB Output is correct
95 Correct 45 ms 8016 KB Output is correct
96 Correct 43 ms 8048 KB Output is correct
97 Correct 43 ms 8016 KB Output is correct
98 Correct 44 ms 8080 KB Output is correct
99 Correct 54 ms 8008 KB Output is correct
100 Correct 46 ms 8052 KB Output is correct
101 Correct 41 ms 8016 KB Output is correct
102 Correct 45 ms 8000 KB Output is correct
103 Correct 43 ms 8036 KB Output is correct
104 Correct 46 ms 8084 KB Output is correct
105 Correct 45 ms 8008 KB Output is correct
106 Correct 43 ms 8016 KB Output is correct
107 Correct 41 ms 8096 KB Output is correct
108 Correct 43 ms 8016 KB Output is correct
109 Correct 38 ms 8012 KB Output is correct
110 Correct 40 ms 8004 KB Output is correct
111 Correct 39 ms 8020 KB Output is correct
112 Correct 40 ms 8004 KB Output is correct
113 Correct 39 ms 8096 KB Output is correct
114 Correct 46 ms 8016 KB Output is correct
115 Correct 48 ms 8140 KB Output is correct
116 Correct 307 ms 4196 KB Output is correct
117 Correct 386 ms 4168 KB Output is correct
118 Correct 610 ms 4596 KB Output is correct
119 Correct 849 ms 4596 KB Output is correct
120 Correct 794 ms 4616 KB Output is correct
121 Correct 1835 ms 5068 KB Output is correct
122 Correct 1739 ms 5140 KB Output is correct
123 Correct 5427 ms 6400 KB Output is correct
124 Correct 5074 ms 6376 KB Output is correct
125 Correct 4954 ms 6376 KB Output is correct
126 Execution timed out 7070 ms 8080 KB Time limit exceeded
127 Halted 0 ms 0 KB -