답안 #696525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
696525 2023-02-06T17:31:57 Z cig32 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 10952 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }

int f[MAXN], ans, n, sum1all, sum2all;

int bit1[MAXN];
void add1(int x, int v) {
  sum1all += v;
  for(;x<MAXN;x+=x&-x) bit1[x] += v;
}
int sum1(int x) {
  int s=0;
  for(;x;x-=x&-x) s += bit1[x];
  return s;
}
int bit2[MAXN];
void add2(int x, int v) {
  sum2all += v;
  for(;x<MAXN;x+=x&-x) bit2[x] += v;
}
int sum2(int x) {
  int s=0;
  for(;x;x-=x&-x) s += bit2[x];
  return s;
}
int bit3[MAXN];
void add3(int x, int v) {
  x++;
  for(;x<MAXN;x+=x&-x) bit3[x] += v;
}
int sum3(int x) {
  x++;
  int s=0;
  for(;x;x-=x&-x) s += bit3[x];
  return s;
}
void edit(int i, int x) {
  int cal1 = sum1(i), cal2 = sum2(i);
  ans -= (cal1 - f[i]) * f[i] * i;
  ans += (cal2 - f[i] * (i-1)) * f[i];
  ans -= f[i] * (sum2all - cal2 + sum1all - cal1);
  ans += f[i] * (i-1) * (sum1all - cal1);
  add1(i, x-f[i]);
  add2(i, (x-f[i]) * (i-1));
  ans -= f[i] * (f[i] + 1) / 2;
  f[i] = x;
  ans += f[i] * (f[i] + 1) / 2;
  cal1 += x-f[i];
  cal2 += (x-f[i]) * (i-1);
  ans += (cal1 - f[i]) * f[i] * i;
  ans -= (cal2 - f[i] * (i-1)) * f[i];
  ans += f[i] * (sum2all - cal2 + sum1all - cal1);
  ans -= f[i] * (i-1) * (sum1all - cal1);
}
int block;
bool modui(pair<pair<int,int>,int> a, pair<pair<int,int>,int> b) {
  if(a.first.first / block != b.first.first / block) return a.first.first / block < b.first.first / block;
  return a.first.second < b.first.second;
}
void solve(int tc) {
  int q;
  cin >> n >> q;
  int c = 300000;
  block = ceil(sqrt(c));
  int arr[n+1];
  for(int i=1; i<=n; i++) cin >> arr[i];
  pair<pair<int,int>,int> qry[q+1];
  for(int i=1; i<=q; i++) {
    cin >> qry[i].first.first >> qry[i].first.second;
    qry[i].second = i;
  }
  sort(qry+1, qry+q+1, modui);
  int res[q+1];
  int l = qry[1].first.first, r = qry[1].first.first - 1;
  int freq[c+1];
  for(int i=1; i<=c; i++) freq[i] = 0;
  add3(0, n);
  for(int i=1; i<=q; i++) {
    while(l > qry[i].first.first) {
      l--;
      add3(freq[arr[l]], -1);
      freq[arr[l]]++;
      add3(freq[arr[l]], 1);
      int rank = sum3(freq[arr[l]] - 1) + 1;
      int pos = (rank & 1 ? (rank + 1) / 2 : n + 1 - rank / 2);
      edit(pos, freq[arr[l]]);
    }
    while(r > qry[i].first.second) {
      add3(freq[arr[r]], -1);
      freq[arr[r]]--;
      add3(freq[arr[r]], 1);
      int rank = sum3(freq[arr[r]]);
      int pos = (rank & 1 ? (rank + 1) / 2 : n + 1 - rank / 2);
      edit(pos, freq[arr[r]]);
      r--;
    }
    while(r < qry[i].first.second) {
      r++;
      add3(freq[arr[r]], -1);
      freq[arr[r]]++;
      add3(freq[arr[r]], 1);
      int rank = sum3(freq[arr[r]] - 1) + 1;
      int pos = (rank & 1 ? (rank + 1) / 2 : n + 1 - rank / 2);
      edit(pos, freq[arr[r]]);
    }
    while(l < qry[i].first.first) {
      add3(freq[arr[l]], -1);
      freq[arr[l]]--;
      add3(freq[arr[l]], 1);
      int rank = sum3(freq[arr[l]]);
      int pos = (rank & 1 ? (rank + 1) / 2 : n + 1 - rank / 2);
      edit(pos, freq[arr[l]]);
      l++;
    }
    res[qry[i].second] = ans;
  }
  for(int i=1; i<=q; i++) cout << res[i] << "\n";

}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 1 ms 2772 KB Output is correct
8 Correct 1 ms 2764 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2888 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 3 ms 2900 KB Output is correct
4 Correct 19 ms 3836 KB Output is correct
5 Correct 39 ms 5204 KB Output is correct
6 Correct 63 ms 5968 KB Output is correct
7 Correct 46 ms 6860 KB Output is correct
8 Correct 48 ms 5944 KB Output is correct
9 Correct 49 ms 5964 KB Output is correct
10 Correct 52 ms 6292 KB Output is correct
11 Correct 50 ms 6092 KB Output is correct
12 Correct 52 ms 5940 KB Output is correct
13 Correct 50 ms 6008 KB Output is correct
14 Correct 50 ms 6136 KB Output is correct
15 Correct 50 ms 6092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2888 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 3 ms 2900 KB Output is correct
4 Correct 19 ms 3836 KB Output is correct
5 Correct 39 ms 5204 KB Output is correct
6 Correct 63 ms 5968 KB Output is correct
7 Correct 46 ms 6860 KB Output is correct
8 Correct 48 ms 5944 KB Output is correct
9 Correct 49 ms 5964 KB Output is correct
10 Correct 52 ms 6292 KB Output is correct
11 Correct 50 ms 6092 KB Output is correct
12 Correct 52 ms 5940 KB Output is correct
13 Correct 50 ms 6008 KB Output is correct
14 Correct 50 ms 6136 KB Output is correct
15 Correct 50 ms 6092 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 3 ms 2876 KB Output is correct
19 Correct 18 ms 3948 KB Output is correct
20 Correct 38 ms 5052 KB Output is correct
21 Correct 57 ms 6060 KB Output is correct
22 Correct 54 ms 5892 KB Output is correct
23 Correct 53 ms 5908 KB Output is correct
24 Correct 52 ms 5840 KB Output is correct
25 Correct 56 ms 5952 KB Output is correct
26 Correct 58 ms 5832 KB Output is correct
27 Correct 57 ms 5896 KB Output is correct
28 Correct 52 ms 6220 KB Output is correct
29 Correct 52 ms 5888 KB Output is correct
30 Correct 54 ms 5944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2888 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 3 ms 2900 KB Output is correct
4 Correct 19 ms 3836 KB Output is correct
5 Correct 39 ms 5204 KB Output is correct
6 Correct 63 ms 5968 KB Output is correct
7 Correct 46 ms 6860 KB Output is correct
8 Correct 48 ms 5944 KB Output is correct
9 Correct 49 ms 5964 KB Output is correct
10 Correct 52 ms 6292 KB Output is correct
11 Correct 50 ms 6092 KB Output is correct
12 Correct 52 ms 5940 KB Output is correct
13 Correct 50 ms 6008 KB Output is correct
14 Correct 50 ms 6136 KB Output is correct
15 Correct 50 ms 6092 KB Output is correct
16 Correct 2 ms 2772 KB Output is correct
17 Correct 2 ms 2772 KB Output is correct
18 Correct 3 ms 2876 KB Output is correct
19 Correct 18 ms 3948 KB Output is correct
20 Correct 38 ms 5052 KB Output is correct
21 Correct 57 ms 6060 KB Output is correct
22 Correct 54 ms 5892 KB Output is correct
23 Correct 53 ms 5908 KB Output is correct
24 Correct 52 ms 5840 KB Output is correct
25 Correct 56 ms 5952 KB Output is correct
26 Correct 58 ms 5832 KB Output is correct
27 Correct 57 ms 5896 KB Output is correct
28 Correct 52 ms 6220 KB Output is correct
29 Correct 52 ms 5888 KB Output is correct
30 Correct 54 ms 5944 KB Output is correct
31 Correct 3 ms 2772 KB Output is correct
32 Correct 2 ms 2772 KB Output is correct
33 Correct 2 ms 2760 KB Output is correct
34 Correct 5 ms 2900 KB Output is correct
35 Correct 4 ms 2900 KB Output is correct
36 Correct 3 ms 2900 KB Output is correct
37 Correct 11 ms 3268 KB Output is correct
38 Correct 11 ms 3348 KB Output is correct
39 Correct 20 ms 3924 KB Output is correct
40 Correct 40 ms 4888 KB Output is correct
41 Correct 57 ms 5196 KB Output is correct
42 Correct 57 ms 5168 KB Output is correct
43 Correct 56 ms 5148 KB Output is correct
44 Correct 56 ms 5140 KB Output is correct
45 Correct 56 ms 5116 KB Output is correct
46 Correct 57 ms 5144 KB Output is correct
47 Correct 56 ms 5140 KB Output is correct
48 Correct 58 ms 5160 KB Output is correct
49 Correct 61 ms 5076 KB Output is correct
50 Correct 60 ms 5140 KB Output is correct
51 Correct 63 ms 5148 KB Output is correct
52 Correct 57 ms 5116 KB Output is correct
53 Correct 58 ms 5140 KB Output is correct
54 Correct 60 ms 5144 KB Output is correct
55 Correct 59 ms 5152 KB Output is correct
56 Correct 58 ms 5112 KB Output is correct
57 Correct 56 ms 5076 KB Output is correct
58 Correct 69 ms 5108 KB Output is correct
59 Correct 56 ms 5160 KB Output is correct
60 Correct 56 ms 5052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 1 ms 2772 KB Output is correct
8 Correct 1 ms 2764 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2888 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 19 ms 3836 KB Output is correct
15 Correct 39 ms 5204 KB Output is correct
16 Correct 63 ms 5968 KB Output is correct
17 Correct 46 ms 6860 KB Output is correct
18 Correct 48 ms 5944 KB Output is correct
19 Correct 49 ms 5964 KB Output is correct
20 Correct 52 ms 6292 KB Output is correct
21 Correct 50 ms 6092 KB Output is correct
22 Correct 52 ms 5940 KB Output is correct
23 Correct 50 ms 6008 KB Output is correct
24 Correct 50 ms 6136 KB Output is correct
25 Correct 50 ms 6092 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 3 ms 2876 KB Output is correct
29 Correct 18 ms 3948 KB Output is correct
30 Correct 38 ms 5052 KB Output is correct
31 Correct 57 ms 6060 KB Output is correct
32 Correct 54 ms 5892 KB Output is correct
33 Correct 53 ms 5908 KB Output is correct
34 Correct 52 ms 5840 KB Output is correct
35 Correct 56 ms 5952 KB Output is correct
36 Correct 58 ms 5832 KB Output is correct
37 Correct 57 ms 5896 KB Output is correct
38 Correct 52 ms 6220 KB Output is correct
39 Correct 52 ms 5888 KB Output is correct
40 Correct 54 ms 5944 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2760 KB Output is correct
44 Correct 5 ms 2900 KB Output is correct
45 Correct 4 ms 2900 KB Output is correct
46 Correct 3 ms 2900 KB Output is correct
47 Correct 11 ms 3268 KB Output is correct
48 Correct 11 ms 3348 KB Output is correct
49 Correct 20 ms 3924 KB Output is correct
50 Correct 40 ms 4888 KB Output is correct
51 Correct 57 ms 5196 KB Output is correct
52 Correct 57 ms 5168 KB Output is correct
53 Correct 56 ms 5148 KB Output is correct
54 Correct 56 ms 5140 KB Output is correct
55 Correct 56 ms 5116 KB Output is correct
56 Correct 57 ms 5144 KB Output is correct
57 Correct 56 ms 5140 KB Output is correct
58 Correct 58 ms 5160 KB Output is correct
59 Correct 61 ms 5076 KB Output is correct
60 Correct 60 ms 5140 KB Output is correct
61 Correct 63 ms 5148 KB Output is correct
62 Correct 57 ms 5116 KB Output is correct
63 Correct 58 ms 5140 KB Output is correct
64 Correct 60 ms 5144 KB Output is correct
65 Correct 59 ms 5152 KB Output is correct
66 Correct 58 ms 5112 KB Output is correct
67 Correct 56 ms 5076 KB Output is correct
68 Correct 69 ms 5108 KB Output is correct
69 Correct 56 ms 5160 KB Output is correct
70 Correct 56 ms 5052 KB Output is correct
71 Correct 12 ms 3232 KB Output is correct
72 Correct 11 ms 3156 KB Output is correct
73 Correct 12 ms 3156 KB Output is correct
74 Correct 13 ms 3156 KB Output is correct
75 Correct 11 ms 3164 KB Output is correct
76 Correct 22 ms 3688 KB Output is correct
77 Correct 21 ms 3680 KB Output is correct
78 Correct 22 ms 3668 KB Output is correct
79 Correct 21 ms 3676 KB Output is correct
80 Correct 24 ms 3668 KB Output is correct
81 Correct 41 ms 4572 KB Output is correct
82 Correct 40 ms 4560 KB Output is correct
83 Correct 43 ms 4556 KB Output is correct
84 Correct 41 ms 4568 KB Output is correct
85 Correct 39 ms 4464 KB Output is correct
86 Correct 42 ms 4716 KB Output is correct
87 Correct 42 ms 4684 KB Output is correct
88 Correct 42 ms 4708 KB Output is correct
89 Correct 44 ms 4772 KB Output is correct
90 Correct 43 ms 4692 KB Output is correct
91 Correct 65 ms 7396 KB Output is correct
92 Correct 65 ms 7448 KB Output is correct
93 Correct 72 ms 7404 KB Output is correct
94 Correct 70 ms 7396 KB Output is correct
95 Correct 64 ms 7372 KB Output is correct
96 Correct 77 ms 9680 KB Output is correct
97 Correct 72 ms 9720 KB Output is correct
98 Correct 74 ms 9808 KB Output is correct
99 Correct 72 ms 9704 KB Output is correct
100 Correct 74 ms 9716 KB Output is correct
101 Correct 70 ms 9716 KB Output is correct
102 Correct 69 ms 9804 KB Output is correct
103 Correct 73 ms 9804 KB Output is correct
104 Correct 74 ms 9736 KB Output is correct
105 Correct 83 ms 9768 KB Output is correct
106 Correct 70 ms 10864 KB Output is correct
107 Correct 72 ms 10920 KB Output is correct
108 Correct 74 ms 10872 KB Output is correct
109 Correct 74 ms 10892 KB Output is correct
110 Correct 73 ms 10952 KB Output is correct
111 Correct 87 ms 10852 KB Output is correct
112 Correct 70 ms 10900 KB Output is correct
113 Correct 70 ms 10920 KB Output is correct
114 Correct 72 ms 10864 KB Output is correct
115 Correct 75 ms 10852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 1 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2772 KB Output is correct
7 Correct 1 ms 2772 KB Output is correct
8 Correct 1 ms 2764 KB Output is correct
9 Correct 2 ms 2772 KB Output is correct
10 Correct 2 ms 2772 KB Output is correct
11 Correct 2 ms 2888 KB Output is correct
12 Correct 2 ms 2772 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 19 ms 3836 KB Output is correct
15 Correct 39 ms 5204 KB Output is correct
16 Correct 63 ms 5968 KB Output is correct
17 Correct 46 ms 6860 KB Output is correct
18 Correct 48 ms 5944 KB Output is correct
19 Correct 49 ms 5964 KB Output is correct
20 Correct 52 ms 6292 KB Output is correct
21 Correct 50 ms 6092 KB Output is correct
22 Correct 52 ms 5940 KB Output is correct
23 Correct 50 ms 6008 KB Output is correct
24 Correct 50 ms 6136 KB Output is correct
25 Correct 50 ms 6092 KB Output is correct
26 Correct 2 ms 2772 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 3 ms 2876 KB Output is correct
29 Correct 18 ms 3948 KB Output is correct
30 Correct 38 ms 5052 KB Output is correct
31 Correct 57 ms 6060 KB Output is correct
32 Correct 54 ms 5892 KB Output is correct
33 Correct 53 ms 5908 KB Output is correct
34 Correct 52 ms 5840 KB Output is correct
35 Correct 56 ms 5952 KB Output is correct
36 Correct 58 ms 5832 KB Output is correct
37 Correct 57 ms 5896 KB Output is correct
38 Correct 52 ms 6220 KB Output is correct
39 Correct 52 ms 5888 KB Output is correct
40 Correct 54 ms 5944 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2760 KB Output is correct
44 Correct 5 ms 2900 KB Output is correct
45 Correct 4 ms 2900 KB Output is correct
46 Correct 3 ms 2900 KB Output is correct
47 Correct 11 ms 3268 KB Output is correct
48 Correct 11 ms 3348 KB Output is correct
49 Correct 20 ms 3924 KB Output is correct
50 Correct 40 ms 4888 KB Output is correct
51 Correct 57 ms 5196 KB Output is correct
52 Correct 57 ms 5168 KB Output is correct
53 Correct 56 ms 5148 KB Output is correct
54 Correct 56 ms 5140 KB Output is correct
55 Correct 56 ms 5116 KB Output is correct
56 Correct 57 ms 5144 KB Output is correct
57 Correct 56 ms 5140 KB Output is correct
58 Correct 58 ms 5160 KB Output is correct
59 Correct 61 ms 5076 KB Output is correct
60 Correct 60 ms 5140 KB Output is correct
61 Correct 63 ms 5148 KB Output is correct
62 Correct 57 ms 5116 KB Output is correct
63 Correct 58 ms 5140 KB Output is correct
64 Correct 60 ms 5144 KB Output is correct
65 Correct 59 ms 5152 KB Output is correct
66 Correct 58 ms 5112 KB Output is correct
67 Correct 56 ms 5076 KB Output is correct
68 Correct 69 ms 5108 KB Output is correct
69 Correct 56 ms 5160 KB Output is correct
70 Correct 56 ms 5052 KB Output is correct
71 Correct 12 ms 3232 KB Output is correct
72 Correct 11 ms 3156 KB Output is correct
73 Correct 12 ms 3156 KB Output is correct
74 Correct 13 ms 3156 KB Output is correct
75 Correct 11 ms 3164 KB Output is correct
76 Correct 22 ms 3688 KB Output is correct
77 Correct 21 ms 3680 KB Output is correct
78 Correct 22 ms 3668 KB Output is correct
79 Correct 21 ms 3676 KB Output is correct
80 Correct 24 ms 3668 KB Output is correct
81 Correct 41 ms 4572 KB Output is correct
82 Correct 40 ms 4560 KB Output is correct
83 Correct 43 ms 4556 KB Output is correct
84 Correct 41 ms 4568 KB Output is correct
85 Correct 39 ms 4464 KB Output is correct
86 Correct 42 ms 4716 KB Output is correct
87 Correct 42 ms 4684 KB Output is correct
88 Correct 42 ms 4708 KB Output is correct
89 Correct 44 ms 4772 KB Output is correct
90 Correct 43 ms 4692 KB Output is correct
91 Correct 65 ms 7396 KB Output is correct
92 Correct 65 ms 7448 KB Output is correct
93 Correct 72 ms 7404 KB Output is correct
94 Correct 70 ms 7396 KB Output is correct
95 Correct 64 ms 7372 KB Output is correct
96 Correct 77 ms 9680 KB Output is correct
97 Correct 72 ms 9720 KB Output is correct
98 Correct 74 ms 9808 KB Output is correct
99 Correct 72 ms 9704 KB Output is correct
100 Correct 74 ms 9716 KB Output is correct
101 Correct 70 ms 9716 KB Output is correct
102 Correct 69 ms 9804 KB Output is correct
103 Correct 73 ms 9804 KB Output is correct
104 Correct 74 ms 9736 KB Output is correct
105 Correct 83 ms 9768 KB Output is correct
106 Correct 70 ms 10864 KB Output is correct
107 Correct 72 ms 10920 KB Output is correct
108 Correct 74 ms 10872 KB Output is correct
109 Correct 74 ms 10892 KB Output is correct
110 Correct 73 ms 10952 KB Output is correct
111 Correct 87 ms 10852 KB Output is correct
112 Correct 70 ms 10900 KB Output is correct
113 Correct 70 ms 10920 KB Output is correct
114 Correct 72 ms 10864 KB Output is correct
115 Correct 75 ms 10852 KB Output is correct
116 Correct 1177 ms 4784 KB Output is correct
117 Correct 1179 ms 4812 KB Output is correct
118 Correct 1691 ms 5324 KB Output is correct
119 Correct 1764 ms 5264 KB Output is correct
120 Correct 1754 ms 5268 KB Output is correct
121 Correct 3578 ms 5668 KB Output is correct
122 Correct 3563 ms 5676 KB Output is correct
123 Execution timed out 7060 ms 5944 KB Time limit exceeded
124 Halted 0 ms 0 KB -