Submission #827665

# Submission time Handle Problem Language Result Execution time Memory
827665 2023-08-16T16:10:17 Z NeroZein Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 7152 KB
#include "bits/stdc++.h"
#define int long long
using namespace std;
 
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int B = 400;
const int N = 3e5 + 5;
 
int a[N]; 
int res[N]; 
int freq[N];
int tot_freq[N];
int cnt_freq[B + 1];
vector<int> large_vals;
 
inline int64_t hilbertOrder(int x, int y, int pow, int rotate) {
  if (pow == 0) {
    return 0;
  }
  int hpow = 1 << (pow - 1);
  int seg = (x < hpow) ? ((y < hpow) ? 0 : 3) : ((y < hpow) ? 1 : 2);
  seg = (seg + rotate) & 3;
  const int rotateDelta[4] = {3, 0, 0, 1};
  int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
  int nrot = (rotate + rotateDelta[seg]) & 3;
  int64_t subSquareSize = int64_t(1) << (2 * pow - 2);
  int64_t ans = seg * subSquareSize;
  int64_t add = hilbertOrder(nx, ny, pow - 1, nrot);
  ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
  return ans;
}
 
struct Query{
  int l, r, id; 
  int64_t ord;
  inline void calcOrder() {
    ord = hilbertOrder(l, r, 18, 0);
  }
};
 
inline bool operator<(const Query &x, const Query &y){
  return x.ord < y.ord;
}
 
inline void modify(int i, int v) {
  if (freq[a[i]] && tot_freq[a[i]] <= B) {
    cnt_freq[freq[a[i]]]--;
  }
  freq[a[i]] += v;
  if (freq[a[i]] && tot_freq[a[i]] <= B) {
    cnt_freq[freq[a[i]]]++;
  } 
}
 
inline vector<pair<int, int>> construct() {
  vector<pair<int, int>> left;
  vector<pair<int, int>> right;
  bool go_left = true;
  vector<pair<int, int>> xz;
  for (int i = 1; i <= B; ++i) {
    if (cnt_freq[i]) {
      xz.push_back({i, cnt_freq[i]}); 
    }
  }
  for (int i : large_vals) {
    if (freq[i]) {
      xz.push_back({freq[i], 1}); 
    }
  }
  sort(xz.begin(), xz.end());
  for (auto [i, z] : xz) {
    int x = z / 2;
    int y = z - x;
    if ((x + y) % 2 == 0) {
      left.push_back({i, x});
      right.push_back({i, x});
    } else {
      if (go_left) {
        left.push_back({i, y});
        if (x) {
          right.push_back({i, x});    
        }
      } else {
        if (x) {
          left.push_back({i, x});     
        }
        right.push_back({i, y}); 
      }
      go_left ^= 1;
    }
  }
  vector<pair<int, int>> ret = left; 
  reverse(right.begin(), right.end()); 
  for (auto [x, y] : right) {
    if (ret.back().first == x) {
      ret.back().second += y;
    } else {
      ret.push_back({x, y});      
    }
  }
  return ret;
}
 
long long prop(int x) {
  return (long long) x * (x + 1) / 2; 
}
 
long long get(vector<pair<int, int>>& v) {
  long long ret = 0; 
  long long last = 0;
  long long normal_sum = 0;
  for (int i = (int) v.size() - 1; i >= 0; --i) {
    long long tmp = 0; 
    auto [element_freq, rep] = v[i];
    long long z = rep - 1; 
    tmp += (long long) element_freq * z * (z + 1) * (z + 2) / 6 * element_freq;
    tmp += last * element_freq * rep; 
    tmp += normal_sum * element_freq * rep * (rep + 1) / 2;
    last = (long long) (rep - 1) * rep / 2 * element_freq + last + normal_sum * rep;
    ret += tmp; 
    normal_sum += (long long) v[i].first * v[i].second; 
  }
  ret += prop(normal_sum); 
  return ret;
}
 
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    tot_freq[a[i]]++; 
  }
  for (int i = 1; i < N; ++i) {
    if (tot_freq[i] > B) {
      large_vals.push_back(i); 
    }
  }
  vector<Query> qs(q); 
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    qs[i].l = l, qs[i].r = r;
    qs[i].id = i;
    qs[i].calcOrder(); 
  }
  int l = 0, r = -1; 
  for (auto &pq : qs) {
    int ql = pq.l, qr = pq.r, id = pq.id;
    while (l > ql) modify(--l, 1);
    while (r < qr) modify(++r, 1);
    while (l < ql) modify(l++, -1);
    while (r > qr) modify(r--, -1); 
    vector<pair<int, int>> v = construct(); 
    res[id] = get(v);
  }
  for (int i = 0; i < q; ++i) {
    cout << res[i] << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 5 ms 1236 KB Output is correct
5 Correct 10 ms 2260 KB Output is correct
6 Correct 14 ms 3288 KB Output is correct
7 Correct 14 ms 3276 KB Output is correct
8 Correct 14 ms 3264 KB Output is correct
9 Correct 14 ms 3212 KB Output is correct
10 Correct 16 ms 3304 KB Output is correct
11 Correct 16 ms 3328 KB Output is correct
12 Correct 15 ms 3344 KB Output is correct
13 Correct 14 ms 3284 KB Output is correct
14 Correct 13 ms 3284 KB Output is correct
15 Correct 14 ms 3208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 5 ms 1236 KB Output is correct
5 Correct 10 ms 2260 KB Output is correct
6 Correct 14 ms 3288 KB Output is correct
7 Correct 14 ms 3276 KB Output is correct
8 Correct 14 ms 3264 KB Output is correct
9 Correct 14 ms 3212 KB Output is correct
10 Correct 16 ms 3304 KB Output is correct
11 Correct 16 ms 3328 KB Output is correct
12 Correct 15 ms 3344 KB Output is correct
13 Correct 14 ms 3284 KB Output is correct
14 Correct 13 ms 3284 KB Output is correct
15 Correct 14 ms 3208 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 7 ms 1364 KB Output is correct
20 Correct 10 ms 2412 KB Output is correct
21 Correct 15 ms 3372 KB Output is correct
22 Correct 15 ms 3436 KB Output is correct
23 Correct 14 ms 3408 KB Output is correct
24 Correct 15 ms 3396 KB Output is correct
25 Correct 15 ms 3444 KB Output is correct
26 Correct 15 ms 3412 KB Output is correct
27 Correct 15 ms 3372 KB Output is correct
28 Correct 15 ms 3412 KB Output is correct
29 Correct 15 ms 3412 KB Output is correct
30 Correct 15 ms 3408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 5 ms 1236 KB Output is correct
5 Correct 10 ms 2260 KB Output is correct
6 Correct 14 ms 3288 KB Output is correct
7 Correct 14 ms 3276 KB Output is correct
8 Correct 14 ms 3264 KB Output is correct
9 Correct 14 ms 3212 KB Output is correct
10 Correct 16 ms 3304 KB Output is correct
11 Correct 16 ms 3328 KB Output is correct
12 Correct 15 ms 3344 KB Output is correct
13 Correct 14 ms 3284 KB Output is correct
14 Correct 13 ms 3284 KB Output is correct
15 Correct 14 ms 3208 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 7 ms 1364 KB Output is correct
20 Correct 10 ms 2412 KB Output is correct
21 Correct 15 ms 3372 KB Output is correct
22 Correct 15 ms 3436 KB Output is correct
23 Correct 14 ms 3408 KB Output is correct
24 Correct 15 ms 3396 KB Output is correct
25 Correct 15 ms 3444 KB Output is correct
26 Correct 15 ms 3412 KB Output is correct
27 Correct 15 ms 3372 KB Output is correct
28 Correct 15 ms 3412 KB Output is correct
29 Correct 15 ms 3412 KB Output is correct
30 Correct 15 ms 3408 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 1 ms 472 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 4 ms 852 KB Output is correct
38 Correct 3 ms 980 KB Output is correct
39 Correct 6 ms 1500 KB Output is correct
40 Correct 12 ms 2732 KB Output is correct
41 Correct 20 ms 3464 KB Output is correct
42 Correct 22 ms 3492 KB Output is correct
43 Correct 18 ms 3472 KB Output is correct
44 Correct 21 ms 3468 KB Output is correct
45 Correct 18 ms 3560 KB Output is correct
46 Correct 19 ms 3460 KB Output is correct
47 Correct 18 ms 3540 KB Output is correct
48 Correct 18 ms 3532 KB Output is correct
49 Correct 21 ms 3508 KB Output is correct
50 Correct 18 ms 3452 KB Output is correct
51 Correct 18 ms 3468 KB Output is correct
52 Correct 18 ms 3500 KB Output is correct
53 Correct 18 ms 3424 KB Output is correct
54 Correct 18 ms 3464 KB Output is correct
55 Correct 17 ms 3408 KB Output is correct
56 Correct 19 ms 3484 KB Output is correct
57 Correct 18 ms 3532 KB Output is correct
58 Correct 18 ms 3484 KB Output is correct
59 Correct 18 ms 3520 KB Output is correct
60 Correct 18 ms 3472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 5 ms 1236 KB Output is correct
15 Correct 10 ms 2260 KB Output is correct
16 Correct 14 ms 3288 KB Output is correct
17 Correct 14 ms 3276 KB Output is correct
18 Correct 14 ms 3264 KB Output is correct
19 Correct 14 ms 3212 KB Output is correct
20 Correct 16 ms 3304 KB Output is correct
21 Correct 16 ms 3328 KB Output is correct
22 Correct 15 ms 3344 KB Output is correct
23 Correct 14 ms 3284 KB Output is correct
24 Correct 13 ms 3284 KB Output is correct
25 Correct 14 ms 3208 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 7 ms 1364 KB Output is correct
30 Correct 10 ms 2412 KB Output is correct
31 Correct 15 ms 3372 KB Output is correct
32 Correct 15 ms 3436 KB Output is correct
33 Correct 14 ms 3408 KB Output is correct
34 Correct 15 ms 3396 KB Output is correct
35 Correct 15 ms 3444 KB Output is correct
36 Correct 15 ms 3412 KB Output is correct
37 Correct 15 ms 3372 KB Output is correct
38 Correct 15 ms 3412 KB Output is correct
39 Correct 15 ms 3412 KB Output is correct
40 Correct 15 ms 3408 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 1 ms 472 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 4 ms 852 KB Output is correct
48 Correct 3 ms 980 KB Output is correct
49 Correct 6 ms 1500 KB Output is correct
50 Correct 12 ms 2732 KB Output is correct
51 Correct 20 ms 3464 KB Output is correct
52 Correct 22 ms 3492 KB Output is correct
53 Correct 18 ms 3472 KB Output is correct
54 Correct 21 ms 3468 KB Output is correct
55 Correct 18 ms 3560 KB Output is correct
56 Correct 19 ms 3460 KB Output is correct
57 Correct 18 ms 3540 KB Output is correct
58 Correct 18 ms 3532 KB Output is correct
59 Correct 21 ms 3508 KB Output is correct
60 Correct 18 ms 3452 KB Output is correct
61 Correct 18 ms 3468 KB Output is correct
62 Correct 18 ms 3500 KB Output is correct
63 Correct 18 ms 3424 KB Output is correct
64 Correct 18 ms 3464 KB Output is correct
65 Correct 17 ms 3408 KB Output is correct
66 Correct 19 ms 3484 KB Output is correct
67 Correct 18 ms 3532 KB Output is correct
68 Correct 18 ms 3484 KB Output is correct
69 Correct 18 ms 3520 KB Output is correct
70 Correct 18 ms 3472 KB Output is correct
71 Correct 4 ms 980 KB Output is correct
72 Correct 4 ms 1016 KB Output is correct
73 Correct 4 ms 980 KB Output is correct
74 Correct 5 ms 1000 KB Output is correct
75 Correct 4 ms 980 KB Output is correct
76 Correct 9 ms 1692 KB Output is correct
77 Correct 7 ms 1656 KB Output is correct
78 Correct 7 ms 1620 KB Output is correct
79 Correct 7 ms 1688 KB Output is correct
80 Correct 7 ms 1624 KB Output is correct
81 Correct 13 ms 2808 KB Output is correct
82 Correct 12 ms 2832 KB Output is correct
83 Correct 14 ms 2772 KB Output is correct
84 Correct 13 ms 2828 KB Output is correct
85 Correct 12 ms 2848 KB Output is correct
86 Correct 17 ms 2892 KB Output is correct
87 Correct 14 ms 2980 KB Output is correct
88 Correct 15 ms 2944 KB Output is correct
89 Correct 15 ms 3012 KB Output is correct
90 Correct 14 ms 2896 KB Output is correct
91 Correct 25 ms 4960 KB Output is correct
92 Correct 24 ms 4972 KB Output is correct
93 Correct 23 ms 4940 KB Output is correct
94 Correct 22 ms 4956 KB Output is correct
95 Correct 23 ms 4988 KB Output is correct
96 Correct 26 ms 6488 KB Output is correct
97 Correct 27 ms 6512 KB Output is correct
98 Correct 31 ms 6496 KB Output is correct
99 Correct 31 ms 6540 KB Output is correct
100 Correct 26 ms 6472 KB Output is correct
101 Correct 27 ms 6460 KB Output is correct
102 Correct 27 ms 6568 KB Output is correct
103 Correct 28 ms 6340 KB Output is correct
104 Correct 28 ms 6320 KB Output is correct
105 Correct 31 ms 6440 KB Output is correct
106 Correct 26 ms 7124 KB Output is correct
107 Correct 26 ms 7152 KB Output is correct
108 Correct 26 ms 6976 KB Output is correct
109 Correct 34 ms 7048 KB Output is correct
110 Correct 33 ms 7036 KB Output is correct
111 Correct 27 ms 7020 KB Output is correct
112 Correct 26 ms 6988 KB Output is correct
113 Correct 29 ms 7128 KB Output is correct
114 Correct 28 ms 7100 KB Output is correct
115 Correct 26 ms 6732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 5 ms 1236 KB Output is correct
15 Correct 10 ms 2260 KB Output is correct
16 Correct 14 ms 3288 KB Output is correct
17 Correct 14 ms 3276 KB Output is correct
18 Correct 14 ms 3264 KB Output is correct
19 Correct 14 ms 3212 KB Output is correct
20 Correct 16 ms 3304 KB Output is correct
21 Correct 16 ms 3328 KB Output is correct
22 Correct 15 ms 3344 KB Output is correct
23 Correct 14 ms 3284 KB Output is correct
24 Correct 13 ms 3284 KB Output is correct
25 Correct 14 ms 3208 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 7 ms 1364 KB Output is correct
30 Correct 10 ms 2412 KB Output is correct
31 Correct 15 ms 3372 KB Output is correct
32 Correct 15 ms 3436 KB Output is correct
33 Correct 14 ms 3408 KB Output is correct
34 Correct 15 ms 3396 KB Output is correct
35 Correct 15 ms 3444 KB Output is correct
36 Correct 15 ms 3412 KB Output is correct
37 Correct 15 ms 3372 KB Output is correct
38 Correct 15 ms 3412 KB Output is correct
39 Correct 15 ms 3412 KB Output is correct
40 Correct 15 ms 3408 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 2 ms 468 KB Output is correct
45 Correct 1 ms 472 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 4 ms 852 KB Output is correct
48 Correct 3 ms 980 KB Output is correct
49 Correct 6 ms 1500 KB Output is correct
50 Correct 12 ms 2732 KB Output is correct
51 Correct 20 ms 3464 KB Output is correct
52 Correct 22 ms 3492 KB Output is correct
53 Correct 18 ms 3472 KB Output is correct
54 Correct 21 ms 3468 KB Output is correct
55 Correct 18 ms 3560 KB Output is correct
56 Correct 19 ms 3460 KB Output is correct
57 Correct 18 ms 3540 KB Output is correct
58 Correct 18 ms 3532 KB Output is correct
59 Correct 21 ms 3508 KB Output is correct
60 Correct 18 ms 3452 KB Output is correct
61 Correct 18 ms 3468 KB Output is correct
62 Correct 18 ms 3500 KB Output is correct
63 Correct 18 ms 3424 KB Output is correct
64 Correct 18 ms 3464 KB Output is correct
65 Correct 17 ms 3408 KB Output is correct
66 Correct 19 ms 3484 KB Output is correct
67 Correct 18 ms 3532 KB Output is correct
68 Correct 18 ms 3484 KB Output is correct
69 Correct 18 ms 3520 KB Output is correct
70 Correct 18 ms 3472 KB Output is correct
71 Correct 4 ms 980 KB Output is correct
72 Correct 4 ms 1016 KB Output is correct
73 Correct 4 ms 980 KB Output is correct
74 Correct 5 ms 1000 KB Output is correct
75 Correct 4 ms 980 KB Output is correct
76 Correct 9 ms 1692 KB Output is correct
77 Correct 7 ms 1656 KB Output is correct
78 Correct 7 ms 1620 KB Output is correct
79 Correct 7 ms 1688 KB Output is correct
80 Correct 7 ms 1624 KB Output is correct
81 Correct 13 ms 2808 KB Output is correct
82 Correct 12 ms 2832 KB Output is correct
83 Correct 14 ms 2772 KB Output is correct
84 Correct 13 ms 2828 KB Output is correct
85 Correct 12 ms 2848 KB Output is correct
86 Correct 17 ms 2892 KB Output is correct
87 Correct 14 ms 2980 KB Output is correct
88 Correct 15 ms 2944 KB Output is correct
89 Correct 15 ms 3012 KB Output is correct
90 Correct 14 ms 2896 KB Output is correct
91 Correct 25 ms 4960 KB Output is correct
92 Correct 24 ms 4972 KB Output is correct
93 Correct 23 ms 4940 KB Output is correct
94 Correct 22 ms 4956 KB Output is correct
95 Correct 23 ms 4988 KB Output is correct
96 Correct 26 ms 6488 KB Output is correct
97 Correct 27 ms 6512 KB Output is correct
98 Correct 31 ms 6496 KB Output is correct
99 Correct 31 ms 6540 KB Output is correct
100 Correct 26 ms 6472 KB Output is correct
101 Correct 27 ms 6460 KB Output is correct
102 Correct 27 ms 6568 KB Output is correct
103 Correct 28 ms 6340 KB Output is correct
104 Correct 28 ms 6320 KB Output is correct
105 Correct 31 ms 6440 KB Output is correct
106 Correct 26 ms 7124 KB Output is correct
107 Correct 26 ms 7152 KB Output is correct
108 Correct 26 ms 6976 KB Output is correct
109 Correct 34 ms 7048 KB Output is correct
110 Correct 33 ms 7036 KB Output is correct
111 Correct 27 ms 7020 KB Output is correct
112 Correct 26 ms 6988 KB Output is correct
113 Correct 29 ms 7128 KB Output is correct
114 Correct 28 ms 7100 KB Output is correct
115 Correct 26 ms 6732 KB Output is correct
116 Correct 1355 ms 2916 KB Output is correct
117 Correct 625 ms 2892 KB Output is correct
118 Correct 4551 ms 3504 KB Output is correct
119 Correct 3153 ms 3264 KB Output is correct
120 Correct 2329 ms 3328 KB Output is correct
121 Correct 6365 ms 3696 KB Output is correct
122 Correct 4493 ms 4712 KB Output is correct
123 Execution timed out 7078 ms 5072 KB Time limit exceeded
124 Halted 0 ms 0 KB -