답안 #1052878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1052878 2024-08-11T05:26:21 Z kunzaZa183 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 33368 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
int block;
signed main() {
  cin.tie(0)->ios::sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int n, q;
  cin >> n >> q;

  const int maxn = 300005, maxq = 50000;
  
  block = sqrt(n) + 3;

  int arr[maxn];
  for (int i = 0; i < n; i++) cin >> arr[i];

  struct query {
    pair<int, int> pii;
    int in;
    bool operator<(query x) const {
      if (pii.first / block != x.pii.first / block) return pii < x.pii;
      return pii.second > x.pii.second;
    }
  };
  query arrpii[maxn];
  for (int i = 0; i < q; i++) {
    cin >> arrpii[i].pii.first >> arrpii[i].pii.second;
    arrpii[i].pii.first--, arrpii[i].pii.second--;
    arrpii[i].in = i;
  }

  sort(arrpii, arrpii + n);

  struct fenwick {
    int arr[2 * maxn + 1] = {};

    int sum(int r) {
      int ret = 0;
      for (r++; r > 0; r -= r & (-r)) ret += arr[r];
      return ret;
    }

    void add(int in, int val) {
      for (in++; in <= 2 * maxn; in += in & (-in)) {
        arr[in] += val;
      }
    }
  } ok, rev, normal;

  pair<int, int> status[maxn + 1];  // bef, cur
  status[0] = {0, n};
  for (int i = 1; i <= maxn; i++) status[i] = {n, n};

  int at[maxn] = {};

  int curans = 0;
  auto add = [&](int in) {
    int tmp = status[at[arr[in]]].second - 1;
    curans -= (at[arr[in]] + 1) * at[arr[in]] / 2;
    curans += (at[arr[in]] + 2) * (at[arr[in]] + 1) / 2;

    int coord;
    if (tmp % 2 == 1) {
      coord = maxn + 1 + (n - tmp - 1) / 2;
    } else if (tmp % 2 == 0) {
      coord = maxn - (n - tmp - 1) / 2;
    }

    normal.add(coord, 1);
    ok.add(coord, coord);
    rev.add(coord, 2 * maxn - coord);

    curans += ok.sum(2 * maxn - 1) - ok.sum(coord) -
              (coord - 1) * (normal.sum(2 * maxn - 1) - normal.sum(coord));
    curans += rev.sum(coord - 1) - rev.sum(0) -
              (2 * maxn - coord - 1) * (normal.sum(coord - 1) - normal.sum(0));

    status[at[arr[in]]].second--;
    at[arr[in]]++;

    status[at[arr[in]]].first--;
  };

  auto rem = [&](int in) {
    int tmp = status[at[arr[in]]].first;
    curans -= (at[arr[in]] + 1) * at[arr[in]] / 2;
    curans += (at[arr[in]]) * (at[arr[in]] - 1) / 2;

    int coord;
    if (tmp % 2 == 1) {
      coord = maxn + 1 + (n - tmp - 1) / 2;
    } else if (tmp % 2 == 0) {
      coord = maxn - (n - tmp - 1) / 2;
    }

    normal.add(coord, -1);
    ok.add(coord, -coord);
    rev.add(coord, -(2 * maxn - coord));

    curans -= ok.sum(2 * maxn - 1) - ok.sum(coord) -
              (coord - 1) * (normal.sum(2 * maxn - 1) - normal.sum(coord));
    curans -= rev.sum(coord - 1) - rev.sum(0) -
              (2 * maxn - coord - 1) * (normal.sum(coord - 1) - normal.sum(0));

    status[at[arr[in]]].first++;
    at[arr[in]]--;

    status[at[arr[in]]].second++;
  };

  pair<int, int> last = {0, -1};
  int ans[maxn];
  for (int i = 0; i < q; i++) {
    while (arrpii[i].pii.second > last.second) {
      last.second++;
      add(last.second);
    }

    while (arrpii[i].pii.first < last.first) {
      last.first--;
      add(last.first);
    }

    while (arrpii[i].pii.second < last.second) {
      rem(last.second);
      last.second--;
    }

    while (arrpii[i].pii.first > last.first) {
      rem(last.first);
      last.first++;
    }
    ans[arrpii[i].in] = curans;
  }
  for (int i = 0; i < q; i++) cout << ans[i] << '\n';
}

Compilation message

diversity.cpp: In function 'int main()':
diversity.cpp:11:28: warning: unused variable 'maxq' [-Wunused-variable]
   11 |   const int maxn = 300005, maxq = 50000;
      |                            ^~~~
diversity.cpp: In lambda function:
diversity.cpp:77:33: warning: 'coord' may be used uninitialized in this function [-Wmaybe-uninitialized]
   77 |               (2 * maxn - coord - 1) * (normal.sum(coord - 1) - normal.sum(0));
      |               ~~~~~~~~~~~~~~~~~~^~~~
diversity.cpp: In lambda function:
diversity.cpp:104:33: warning: 'coord' may be used uninitialized in this function [-Wmaybe-uninitialized]
  104 |               (2 * maxn - coord - 1) * (normal.sum(coord - 1) - normal.sum(0));
      |               ~~~~~~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 33116 KB Output is correct
2 Correct 13 ms 33116 KB Output is correct
3 Correct 13 ms 33152 KB Output is correct
4 Correct 14 ms 33116 KB Output is correct
5 Correct 14 ms 33112 KB Output is correct
6 Correct 14 ms 33116 KB Output is correct
7 Correct 14 ms 33200 KB Output is correct
8 Correct 14 ms 33112 KB Output is correct
9 Correct 14 ms 33112 KB Output is correct
10 Correct 14 ms 33112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 33116 KB Output is correct
2 Correct 15 ms 33252 KB Output is correct
3 Correct 15 ms 33140 KB Output is correct
4 Correct 31 ms 33176 KB Output is correct
5 Correct 49 ms 33116 KB Output is correct
6 Correct 64 ms 33116 KB Output is correct
7 Correct 65 ms 33116 KB Output is correct
8 Correct 64 ms 33304 KB Output is correct
9 Correct 64 ms 33368 KB Output is correct
10 Correct 64 ms 33116 KB Output is correct
11 Correct 71 ms 33116 KB Output is correct
12 Correct 64 ms 33116 KB Output is correct
13 Correct 65 ms 33116 KB Output is correct
14 Correct 64 ms 33236 KB Output is correct
15 Correct 64 ms 33116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 33116 KB Output is correct
2 Correct 15 ms 33252 KB Output is correct
3 Correct 15 ms 33140 KB Output is correct
4 Correct 31 ms 33176 KB Output is correct
5 Correct 49 ms 33116 KB Output is correct
6 Correct 64 ms 33116 KB Output is correct
7 Correct 65 ms 33116 KB Output is correct
8 Correct 64 ms 33304 KB Output is correct
9 Correct 64 ms 33368 KB Output is correct
10 Correct 64 ms 33116 KB Output is correct
11 Correct 71 ms 33116 KB Output is correct
12 Correct 64 ms 33116 KB Output is correct
13 Correct 65 ms 33116 KB Output is correct
14 Correct 64 ms 33236 KB Output is correct
15 Correct 64 ms 33116 KB Output is correct
16 Correct 14 ms 33112 KB Output is correct
17 Correct 14 ms 33324 KB Output is correct
18 Correct 15 ms 33116 KB Output is correct
19 Correct 29 ms 33112 KB Output is correct
20 Correct 59 ms 33112 KB Output is correct
21 Correct 63 ms 33112 KB Output is correct
22 Correct 66 ms 33116 KB Output is correct
23 Correct 67 ms 33116 KB Output is correct
24 Correct 74 ms 33112 KB Output is correct
25 Correct 71 ms 33304 KB Output is correct
26 Correct 72 ms 33112 KB Output is correct
27 Correct 75 ms 33112 KB Output is correct
28 Correct 68 ms 33116 KB Output is correct
29 Correct 66 ms 33112 KB Output is correct
30 Correct 85 ms 33112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 33116 KB Output is correct
2 Correct 15 ms 33252 KB Output is correct
3 Correct 15 ms 33140 KB Output is correct
4 Correct 31 ms 33176 KB Output is correct
5 Correct 49 ms 33116 KB Output is correct
6 Correct 64 ms 33116 KB Output is correct
7 Correct 65 ms 33116 KB Output is correct
8 Correct 64 ms 33304 KB Output is correct
9 Correct 64 ms 33368 KB Output is correct
10 Correct 64 ms 33116 KB Output is correct
11 Correct 71 ms 33116 KB Output is correct
12 Correct 64 ms 33116 KB Output is correct
13 Correct 65 ms 33116 KB Output is correct
14 Correct 64 ms 33236 KB Output is correct
15 Correct 64 ms 33116 KB Output is correct
16 Correct 14 ms 33112 KB Output is correct
17 Correct 14 ms 33324 KB Output is correct
18 Correct 15 ms 33116 KB Output is correct
19 Correct 29 ms 33112 KB Output is correct
20 Correct 59 ms 33112 KB Output is correct
21 Correct 63 ms 33112 KB Output is correct
22 Correct 66 ms 33116 KB Output is correct
23 Correct 67 ms 33116 KB Output is correct
24 Correct 74 ms 33112 KB Output is correct
25 Correct 71 ms 33304 KB Output is correct
26 Correct 72 ms 33112 KB Output is correct
27 Correct 75 ms 33112 KB Output is correct
28 Correct 68 ms 33116 KB Output is correct
29 Correct 66 ms 33112 KB Output is correct
30 Correct 85 ms 33112 KB Output is correct
31 Correct 14 ms 33116 KB Output is correct
32 Correct 14 ms 33116 KB Output is correct
33 Correct 24 ms 33112 KB Output is correct
34 Correct 18 ms 33328 KB Output is correct
35 Correct 15 ms 33116 KB Output is correct
36 Correct 17 ms 33116 KB Output is correct
37 Correct 22 ms 33332 KB Output is correct
38 Correct 24 ms 33192 KB Output is correct
39 Correct 32 ms 33116 KB Output is correct
40 Correct 52 ms 33112 KB Output is correct
41 Correct 78 ms 33308 KB Output is correct
42 Correct 69 ms 33308 KB Output is correct
43 Correct 64 ms 33116 KB Output is correct
44 Correct 64 ms 33116 KB Output is correct
45 Correct 65 ms 33116 KB Output is correct
46 Correct 63 ms 33112 KB Output is correct
47 Correct 68 ms 33364 KB Output is correct
48 Correct 67 ms 33368 KB Output is correct
49 Correct 69 ms 33112 KB Output is correct
50 Correct 75 ms 33128 KB Output is correct
51 Correct 65 ms 33112 KB Output is correct
52 Correct 67 ms 33112 KB Output is correct
53 Correct 65 ms 33112 KB Output is correct
54 Correct 66 ms 33116 KB Output is correct
55 Correct 65 ms 33116 KB Output is correct
56 Correct 65 ms 33296 KB Output is correct
57 Correct 65 ms 33308 KB Output is correct
58 Correct 70 ms 33324 KB Output is correct
59 Correct 65 ms 33308 KB Output is correct
60 Correct 66 ms 33308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 33116 KB Output is correct
2 Correct 13 ms 33116 KB Output is correct
3 Correct 13 ms 33152 KB Output is correct
4 Correct 14 ms 33116 KB Output is correct
5 Correct 14 ms 33112 KB Output is correct
6 Correct 14 ms 33116 KB Output is correct
7 Correct 14 ms 33200 KB Output is correct
8 Correct 14 ms 33112 KB Output is correct
9 Correct 14 ms 33112 KB Output is correct
10 Correct 14 ms 33112 KB Output is correct
11 Correct 13 ms 33116 KB Output is correct
12 Correct 15 ms 33252 KB Output is correct
13 Correct 15 ms 33140 KB Output is correct
14 Correct 31 ms 33176 KB Output is correct
15 Correct 49 ms 33116 KB Output is correct
16 Correct 64 ms 33116 KB Output is correct
17 Correct 65 ms 33116 KB Output is correct
18 Correct 64 ms 33304 KB Output is correct
19 Correct 64 ms 33368 KB Output is correct
20 Correct 64 ms 33116 KB Output is correct
21 Correct 71 ms 33116 KB Output is correct
22 Correct 64 ms 33116 KB Output is correct
23 Correct 65 ms 33116 KB Output is correct
24 Correct 64 ms 33236 KB Output is correct
25 Correct 64 ms 33116 KB Output is correct
26 Correct 14 ms 33112 KB Output is correct
27 Correct 14 ms 33324 KB Output is correct
28 Correct 15 ms 33116 KB Output is correct
29 Correct 29 ms 33112 KB Output is correct
30 Correct 59 ms 33112 KB Output is correct
31 Correct 63 ms 33112 KB Output is correct
32 Correct 66 ms 33116 KB Output is correct
33 Correct 67 ms 33116 KB Output is correct
34 Correct 74 ms 33112 KB Output is correct
35 Correct 71 ms 33304 KB Output is correct
36 Correct 72 ms 33112 KB Output is correct
37 Correct 75 ms 33112 KB Output is correct
38 Correct 68 ms 33116 KB Output is correct
39 Correct 66 ms 33112 KB Output is correct
40 Correct 85 ms 33112 KB Output is correct
41 Correct 14 ms 33116 KB Output is correct
42 Correct 14 ms 33116 KB Output is correct
43 Correct 24 ms 33112 KB Output is correct
44 Correct 18 ms 33328 KB Output is correct
45 Correct 15 ms 33116 KB Output is correct
46 Correct 17 ms 33116 KB Output is correct
47 Correct 22 ms 33332 KB Output is correct
48 Correct 24 ms 33192 KB Output is correct
49 Correct 32 ms 33116 KB Output is correct
50 Correct 52 ms 33112 KB Output is correct
51 Correct 78 ms 33308 KB Output is correct
52 Correct 69 ms 33308 KB Output is correct
53 Correct 64 ms 33116 KB Output is correct
54 Correct 64 ms 33116 KB Output is correct
55 Correct 65 ms 33116 KB Output is correct
56 Correct 63 ms 33112 KB Output is correct
57 Correct 68 ms 33364 KB Output is correct
58 Correct 67 ms 33368 KB Output is correct
59 Correct 69 ms 33112 KB Output is correct
60 Correct 75 ms 33128 KB Output is correct
61 Correct 65 ms 33112 KB Output is correct
62 Correct 67 ms 33112 KB Output is correct
63 Correct 65 ms 33112 KB Output is correct
64 Correct 66 ms 33116 KB Output is correct
65 Correct 65 ms 33116 KB Output is correct
66 Correct 65 ms 33296 KB Output is correct
67 Correct 65 ms 33308 KB Output is correct
68 Correct 70 ms 33324 KB Output is correct
69 Correct 65 ms 33308 KB Output is correct
70 Correct 66 ms 33308 KB Output is correct
71 Correct 23 ms 33116 KB Output is correct
72 Correct 22 ms 33152 KB Output is correct
73 Correct 22 ms 33204 KB Output is correct
74 Correct 22 ms 33116 KB Output is correct
75 Correct 22 ms 33116 KB Output is correct
76 Correct 34 ms 33332 KB Output is correct
77 Correct 35 ms 33116 KB Output is correct
78 Correct 38 ms 33116 KB Output is correct
79 Correct 31 ms 33336 KB Output is correct
80 Correct 31 ms 33304 KB Output is correct
81 Correct 51 ms 33304 KB Output is correct
82 Correct 50 ms 33116 KB Output is correct
83 Correct 49 ms 33304 KB Output is correct
84 Correct 48 ms 33304 KB Output is correct
85 Correct 51 ms 33116 KB Output is correct
86 Correct 51 ms 33112 KB Output is correct
87 Correct 50 ms 33116 KB Output is correct
88 Correct 51 ms 33116 KB Output is correct
89 Correct 50 ms 33116 KB Output is correct
90 Correct 52 ms 33116 KB Output is correct
91 Correct 74 ms 33296 KB Output is correct
92 Correct 71 ms 33116 KB Output is correct
93 Correct 81 ms 33304 KB Output is correct
94 Correct 69 ms 33112 KB Output is correct
95 Correct 70 ms 33312 KB Output is correct
96 Correct 77 ms 33112 KB Output is correct
97 Correct 71 ms 33112 KB Output is correct
98 Correct 74 ms 33116 KB Output is correct
99 Correct 74 ms 33112 KB Output is correct
100 Correct 72 ms 33112 KB Output is correct
101 Correct 74 ms 33308 KB Output is correct
102 Correct 81 ms 33116 KB Output is correct
103 Correct 73 ms 33112 KB Output is correct
104 Correct 73 ms 33116 KB Output is correct
105 Correct 72 ms 33252 KB Output is correct
106 Correct 72 ms 33116 KB Output is correct
107 Correct 71 ms 33248 KB Output is correct
108 Correct 75 ms 33116 KB Output is correct
109 Correct 85 ms 33116 KB Output is correct
110 Correct 73 ms 33116 KB Output is correct
111 Correct 73 ms 33304 KB Output is correct
112 Correct 72 ms 33364 KB Output is correct
113 Correct 72 ms 33112 KB Output is correct
114 Correct 72 ms 33116 KB Output is correct
115 Correct 71 ms 33308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 33116 KB Output is correct
2 Correct 13 ms 33116 KB Output is correct
3 Correct 13 ms 33152 KB Output is correct
4 Correct 14 ms 33116 KB Output is correct
5 Correct 14 ms 33112 KB Output is correct
6 Correct 14 ms 33116 KB Output is correct
7 Correct 14 ms 33200 KB Output is correct
8 Correct 14 ms 33112 KB Output is correct
9 Correct 14 ms 33112 KB Output is correct
10 Correct 14 ms 33112 KB Output is correct
11 Correct 13 ms 33116 KB Output is correct
12 Correct 15 ms 33252 KB Output is correct
13 Correct 15 ms 33140 KB Output is correct
14 Correct 31 ms 33176 KB Output is correct
15 Correct 49 ms 33116 KB Output is correct
16 Correct 64 ms 33116 KB Output is correct
17 Correct 65 ms 33116 KB Output is correct
18 Correct 64 ms 33304 KB Output is correct
19 Correct 64 ms 33368 KB Output is correct
20 Correct 64 ms 33116 KB Output is correct
21 Correct 71 ms 33116 KB Output is correct
22 Correct 64 ms 33116 KB Output is correct
23 Correct 65 ms 33116 KB Output is correct
24 Correct 64 ms 33236 KB Output is correct
25 Correct 64 ms 33116 KB Output is correct
26 Correct 14 ms 33112 KB Output is correct
27 Correct 14 ms 33324 KB Output is correct
28 Correct 15 ms 33116 KB Output is correct
29 Correct 29 ms 33112 KB Output is correct
30 Correct 59 ms 33112 KB Output is correct
31 Correct 63 ms 33112 KB Output is correct
32 Correct 66 ms 33116 KB Output is correct
33 Correct 67 ms 33116 KB Output is correct
34 Correct 74 ms 33112 KB Output is correct
35 Correct 71 ms 33304 KB Output is correct
36 Correct 72 ms 33112 KB Output is correct
37 Correct 75 ms 33112 KB Output is correct
38 Correct 68 ms 33116 KB Output is correct
39 Correct 66 ms 33112 KB Output is correct
40 Correct 85 ms 33112 KB Output is correct
41 Correct 14 ms 33116 KB Output is correct
42 Correct 14 ms 33116 KB Output is correct
43 Correct 24 ms 33112 KB Output is correct
44 Correct 18 ms 33328 KB Output is correct
45 Correct 15 ms 33116 KB Output is correct
46 Correct 17 ms 33116 KB Output is correct
47 Correct 22 ms 33332 KB Output is correct
48 Correct 24 ms 33192 KB Output is correct
49 Correct 32 ms 33116 KB Output is correct
50 Correct 52 ms 33112 KB Output is correct
51 Correct 78 ms 33308 KB Output is correct
52 Correct 69 ms 33308 KB Output is correct
53 Correct 64 ms 33116 KB Output is correct
54 Correct 64 ms 33116 KB Output is correct
55 Correct 65 ms 33116 KB Output is correct
56 Correct 63 ms 33112 KB Output is correct
57 Correct 68 ms 33364 KB Output is correct
58 Correct 67 ms 33368 KB Output is correct
59 Correct 69 ms 33112 KB Output is correct
60 Correct 75 ms 33128 KB Output is correct
61 Correct 65 ms 33112 KB Output is correct
62 Correct 67 ms 33112 KB Output is correct
63 Correct 65 ms 33112 KB Output is correct
64 Correct 66 ms 33116 KB Output is correct
65 Correct 65 ms 33116 KB Output is correct
66 Correct 65 ms 33296 KB Output is correct
67 Correct 65 ms 33308 KB Output is correct
68 Correct 70 ms 33324 KB Output is correct
69 Correct 65 ms 33308 KB Output is correct
70 Correct 66 ms 33308 KB Output is correct
71 Correct 23 ms 33116 KB Output is correct
72 Correct 22 ms 33152 KB Output is correct
73 Correct 22 ms 33204 KB Output is correct
74 Correct 22 ms 33116 KB Output is correct
75 Correct 22 ms 33116 KB Output is correct
76 Correct 34 ms 33332 KB Output is correct
77 Correct 35 ms 33116 KB Output is correct
78 Correct 38 ms 33116 KB Output is correct
79 Correct 31 ms 33336 KB Output is correct
80 Correct 31 ms 33304 KB Output is correct
81 Correct 51 ms 33304 KB Output is correct
82 Correct 50 ms 33116 KB Output is correct
83 Correct 49 ms 33304 KB Output is correct
84 Correct 48 ms 33304 KB Output is correct
85 Correct 51 ms 33116 KB Output is correct
86 Correct 51 ms 33112 KB Output is correct
87 Correct 50 ms 33116 KB Output is correct
88 Correct 51 ms 33116 KB Output is correct
89 Correct 50 ms 33116 KB Output is correct
90 Correct 52 ms 33116 KB Output is correct
91 Correct 74 ms 33296 KB Output is correct
92 Correct 71 ms 33116 KB Output is correct
93 Correct 81 ms 33304 KB Output is correct
94 Correct 69 ms 33112 KB Output is correct
95 Correct 70 ms 33312 KB Output is correct
96 Correct 77 ms 33112 KB Output is correct
97 Correct 71 ms 33112 KB Output is correct
98 Correct 74 ms 33116 KB Output is correct
99 Correct 74 ms 33112 KB Output is correct
100 Correct 72 ms 33112 KB Output is correct
101 Correct 74 ms 33308 KB Output is correct
102 Correct 81 ms 33116 KB Output is correct
103 Correct 73 ms 33112 KB Output is correct
104 Correct 73 ms 33116 KB Output is correct
105 Correct 72 ms 33252 KB Output is correct
106 Correct 72 ms 33116 KB Output is correct
107 Correct 71 ms 33248 KB Output is correct
108 Correct 75 ms 33116 KB Output is correct
109 Correct 85 ms 33116 KB Output is correct
110 Correct 73 ms 33116 KB Output is correct
111 Correct 73 ms 33304 KB Output is correct
112 Correct 72 ms 33364 KB Output is correct
113 Correct 72 ms 33112 KB Output is correct
114 Correct 72 ms 33116 KB Output is correct
115 Correct 71 ms 33308 KB Output is correct
116 Execution timed out 7038 ms 33112 KB Time limit exceeded
117 Halted 0 ms 0 KB -