답안 #827559

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
827559 2023-08-16T14:42:36 Z NeroZein Diversity (CEOI21_diversity) C++17
38 / 100
23 ms 2388 KB
#include "bits/stdc++.h"
using namespace std;
 
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif

const int N = 3e5 + 5;

int a[N]; 
int freq[N];
bitset<N> se;
int cnt_freq[N];
long long prop_ps[N];

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;
}

void modify(int i, int v) {
  if (freq[a[i]]) {
    if (cnt_freq[freq[a[i]]] == 1) {
      se[freq[a[i]]] = 0;
    }
    cnt_freq[freq[a[i]]]--;
  }
  freq[a[i]] += v;
  if (freq[a[i]]) {
    cnt_freq[freq[a[i]]]++;
    if (cnt_freq[freq[a[i]]] == 1) {
      se[freq[a[i]]] = 1; 
    }
  }
}

vector<pair<int, int>> construct() {
  vector<pair<int, int>> left;
  vector<pair<int, int>> right;
  bool go_left = true;
  vector<int> xz;
  int cur_id = 0; 
  int tot = se.count(); 
  int cur_cnt = 0;
  while (cur_cnt < tot) {
    int next_id = se._Find_next(cur_id);
    xz.push_back(next_id);
    cur_id = next_id; 
    cur_cnt++; 
  }
  for (auto i : xz) {
    int x = cnt_freq[i] / 2;
    int y = cnt_freq[i] - x;
    if (cnt_freq[i] % 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 = (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];
  }
  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(); 
  }
  vector<long long> ans(q); 
  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(); 
    ans[id] = get(v);
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\n';
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 328 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 852 KB Output is correct
5 Correct 9 ms 1492 KB Output is correct
6 Correct 17 ms 1756 KB Output is correct
7 Correct 15 ms 2216 KB Output is correct
8 Correct 13 ms 1840 KB Output is correct
9 Correct 14 ms 1772 KB Output is correct
10 Correct 14 ms 1956 KB Output is correct
11 Correct 14 ms 1872 KB Output is correct
12 Correct 14 ms 1856 KB Output is correct
13 Correct 14 ms 1768 KB Output is correct
14 Correct 14 ms 1920 KB Output is correct
15 Correct 16 ms 1872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 852 KB Output is correct
5 Correct 9 ms 1492 KB Output is correct
6 Correct 17 ms 1756 KB Output is correct
7 Correct 15 ms 2216 KB Output is correct
8 Correct 13 ms 1840 KB Output is correct
9 Correct 14 ms 1772 KB Output is correct
10 Correct 14 ms 1956 KB Output is correct
11 Correct 14 ms 1872 KB Output is correct
12 Correct 14 ms 1856 KB Output is correct
13 Correct 14 ms 1768 KB Output is correct
14 Correct 14 ms 1920 KB Output is correct
15 Correct 16 ms 1872 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 5 ms 852 KB Output is correct
20 Correct 10 ms 1356 KB Output is correct
21 Correct 19 ms 1860 KB Output is correct
22 Correct 20 ms 1832 KB Output is correct
23 Correct 16 ms 1828 KB Output is correct
24 Correct 17 ms 1692 KB Output is correct
25 Correct 16 ms 1740 KB Output is correct
26 Correct 15 ms 1748 KB Output is correct
27 Correct 17 ms 1736 KB Output is correct
28 Correct 18 ms 1888 KB Output is correct
29 Correct 16 ms 1752 KB Output is correct
30 Correct 15 ms 1696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 852 KB Output is correct
5 Correct 9 ms 1492 KB Output is correct
6 Correct 17 ms 1756 KB Output is correct
7 Correct 15 ms 2216 KB Output is correct
8 Correct 13 ms 1840 KB Output is correct
9 Correct 14 ms 1772 KB Output is correct
10 Correct 14 ms 1956 KB Output is correct
11 Correct 14 ms 1872 KB Output is correct
12 Correct 14 ms 1856 KB Output is correct
13 Correct 14 ms 1768 KB Output is correct
14 Correct 14 ms 1920 KB Output is correct
15 Correct 16 ms 1872 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 5 ms 852 KB Output is correct
20 Correct 10 ms 1356 KB Output is correct
21 Correct 19 ms 1860 KB Output is correct
22 Correct 20 ms 1832 KB Output is correct
23 Correct 16 ms 1828 KB Output is correct
24 Correct 17 ms 1692 KB Output is correct
25 Correct 16 ms 1740 KB Output is correct
26 Correct 15 ms 1748 KB Output is correct
27 Correct 17 ms 1736 KB Output is correct
28 Correct 18 ms 1888 KB Output is correct
29 Correct 16 ms 1752 KB Output is correct
30 Correct 15 ms 1696 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 3 ms 596 KB Output is correct
38 Correct 3 ms 596 KB Output is correct
39 Correct 8 ms 868 KB Output is correct
40 Correct 12 ms 1260 KB Output is correct
41 Correct 18 ms 1600 KB Output is correct
42 Correct 18 ms 1548 KB Output is correct
43 Correct 19 ms 1620 KB Output is correct
44 Correct 18 ms 1556 KB Output is correct
45 Correct 18 ms 1616 KB Output is correct
46 Correct 18 ms 1620 KB Output is correct
47 Correct 17 ms 1620 KB Output is correct
48 Correct 18 ms 1656 KB Output is correct
49 Correct 18 ms 1640 KB Output is correct
50 Correct 18 ms 1648 KB Output is correct
51 Correct 18 ms 1616 KB Output is correct
52 Correct 19 ms 1608 KB Output is correct
53 Correct 17 ms 1672 KB Output is correct
54 Correct 18 ms 1580 KB Output is correct
55 Correct 21 ms 1840 KB Output is correct
56 Correct 18 ms 1612 KB Output is correct
57 Correct 17 ms 1632 KB Output is correct
58 Correct 19 ms 1640 KB Output is correct
59 Correct 18 ms 1600 KB Output is correct
60 Correct 19 ms 1612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 328 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 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 852 KB Output is correct
15 Correct 9 ms 1492 KB Output is correct
16 Correct 17 ms 1756 KB Output is correct
17 Correct 15 ms 2216 KB Output is correct
18 Correct 13 ms 1840 KB Output is correct
19 Correct 14 ms 1772 KB Output is correct
20 Correct 14 ms 1956 KB Output is correct
21 Correct 14 ms 1872 KB Output is correct
22 Correct 14 ms 1856 KB Output is correct
23 Correct 14 ms 1768 KB Output is correct
24 Correct 14 ms 1920 KB Output is correct
25 Correct 16 ms 1872 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 5 ms 852 KB Output is correct
30 Correct 10 ms 1356 KB Output is correct
31 Correct 19 ms 1860 KB Output is correct
32 Correct 20 ms 1832 KB Output is correct
33 Correct 16 ms 1828 KB Output is correct
34 Correct 17 ms 1692 KB Output is correct
35 Correct 16 ms 1740 KB Output is correct
36 Correct 15 ms 1748 KB Output is correct
37 Correct 17 ms 1736 KB Output is correct
38 Correct 18 ms 1888 KB Output is correct
39 Correct 16 ms 1752 KB Output is correct
40 Correct 15 ms 1696 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 3 ms 596 KB Output is correct
49 Correct 8 ms 868 KB Output is correct
50 Correct 12 ms 1260 KB Output is correct
51 Correct 18 ms 1600 KB Output is correct
52 Correct 18 ms 1548 KB Output is correct
53 Correct 19 ms 1620 KB Output is correct
54 Correct 18 ms 1556 KB Output is correct
55 Correct 18 ms 1616 KB Output is correct
56 Correct 18 ms 1620 KB Output is correct
57 Correct 17 ms 1620 KB Output is correct
58 Correct 18 ms 1656 KB Output is correct
59 Correct 18 ms 1640 KB Output is correct
60 Correct 18 ms 1648 KB Output is correct
61 Correct 18 ms 1616 KB Output is correct
62 Correct 19 ms 1608 KB Output is correct
63 Correct 17 ms 1672 KB Output is correct
64 Correct 18 ms 1580 KB Output is correct
65 Correct 21 ms 1840 KB Output is correct
66 Correct 18 ms 1612 KB Output is correct
67 Correct 17 ms 1632 KB Output is correct
68 Correct 19 ms 1640 KB Output is correct
69 Correct 18 ms 1600 KB Output is correct
70 Correct 19 ms 1612 KB Output is correct
71 Correct 3 ms 596 KB Output is correct
72 Correct 4 ms 680 KB Output is correct
73 Correct 3 ms 596 KB Output is correct
74 Correct 3 ms 596 KB Output is correct
75 Correct 4 ms 596 KB Output is correct
76 Correct 6 ms 852 KB Output is correct
77 Correct 6 ms 816 KB Output is correct
78 Correct 7 ms 868 KB Output is correct
79 Correct 6 ms 788 KB Output is correct
80 Correct 8 ms 960 KB Output is correct
81 Correct 12 ms 1292 KB Output is correct
82 Correct 12 ms 1172 KB Output is correct
83 Correct 12 ms 1236 KB Output is correct
84 Correct 12 ms 1276 KB Output is correct
85 Correct 11 ms 1280 KB Output is correct
86 Correct 13 ms 1324 KB Output is correct
87 Correct 13 ms 1304 KB Output is correct
88 Correct 15 ms 1228 KB Output is correct
89 Correct 17 ms 1244 KB Output is correct
90 Correct 13 ms 1232 KB Output is correct
91 Correct 21 ms 1944 KB Output is correct
92 Correct 20 ms 1968 KB Output is correct
93 Correct 20 ms 2036 KB Output is correct
94 Correct 20 ms 2012 KB Output is correct
95 Correct 20 ms 1968 KB Output is correct
96 Incorrect 23 ms 2388 KB Output isn't correct
97 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 328 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 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 852 KB Output is correct
15 Correct 9 ms 1492 KB Output is correct
16 Correct 17 ms 1756 KB Output is correct
17 Correct 15 ms 2216 KB Output is correct
18 Correct 13 ms 1840 KB Output is correct
19 Correct 14 ms 1772 KB Output is correct
20 Correct 14 ms 1956 KB Output is correct
21 Correct 14 ms 1872 KB Output is correct
22 Correct 14 ms 1856 KB Output is correct
23 Correct 14 ms 1768 KB Output is correct
24 Correct 14 ms 1920 KB Output is correct
25 Correct 16 ms 1872 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 5 ms 852 KB Output is correct
30 Correct 10 ms 1356 KB Output is correct
31 Correct 19 ms 1860 KB Output is correct
32 Correct 20 ms 1832 KB Output is correct
33 Correct 16 ms 1828 KB Output is correct
34 Correct 17 ms 1692 KB Output is correct
35 Correct 16 ms 1740 KB Output is correct
36 Correct 15 ms 1748 KB Output is correct
37 Correct 17 ms 1736 KB Output is correct
38 Correct 18 ms 1888 KB Output is correct
39 Correct 16 ms 1752 KB Output is correct
40 Correct 15 ms 1696 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 3 ms 596 KB Output is correct
49 Correct 8 ms 868 KB Output is correct
50 Correct 12 ms 1260 KB Output is correct
51 Correct 18 ms 1600 KB Output is correct
52 Correct 18 ms 1548 KB Output is correct
53 Correct 19 ms 1620 KB Output is correct
54 Correct 18 ms 1556 KB Output is correct
55 Correct 18 ms 1616 KB Output is correct
56 Correct 18 ms 1620 KB Output is correct
57 Correct 17 ms 1620 KB Output is correct
58 Correct 18 ms 1656 KB Output is correct
59 Correct 18 ms 1640 KB Output is correct
60 Correct 18 ms 1648 KB Output is correct
61 Correct 18 ms 1616 KB Output is correct
62 Correct 19 ms 1608 KB Output is correct
63 Correct 17 ms 1672 KB Output is correct
64 Correct 18 ms 1580 KB Output is correct
65 Correct 21 ms 1840 KB Output is correct
66 Correct 18 ms 1612 KB Output is correct
67 Correct 17 ms 1632 KB Output is correct
68 Correct 19 ms 1640 KB Output is correct
69 Correct 18 ms 1600 KB Output is correct
70 Correct 19 ms 1612 KB Output is correct
71 Correct 3 ms 596 KB Output is correct
72 Correct 4 ms 680 KB Output is correct
73 Correct 3 ms 596 KB Output is correct
74 Correct 3 ms 596 KB Output is correct
75 Correct 4 ms 596 KB Output is correct
76 Correct 6 ms 852 KB Output is correct
77 Correct 6 ms 816 KB Output is correct
78 Correct 7 ms 868 KB Output is correct
79 Correct 6 ms 788 KB Output is correct
80 Correct 8 ms 960 KB Output is correct
81 Correct 12 ms 1292 KB Output is correct
82 Correct 12 ms 1172 KB Output is correct
83 Correct 12 ms 1236 KB Output is correct
84 Correct 12 ms 1276 KB Output is correct
85 Correct 11 ms 1280 KB Output is correct
86 Correct 13 ms 1324 KB Output is correct
87 Correct 13 ms 1304 KB Output is correct
88 Correct 15 ms 1228 KB Output is correct
89 Correct 17 ms 1244 KB Output is correct
90 Correct 13 ms 1232 KB Output is correct
91 Correct 21 ms 1944 KB Output is correct
92 Correct 20 ms 1968 KB Output is correct
93 Correct 20 ms 2036 KB Output is correct
94 Correct 20 ms 2012 KB Output is correct
95 Correct 20 ms 1968 KB Output is correct
96 Incorrect 23 ms 2388 KB Output isn't correct
97 Halted 0 ms 0 KB -