Submission #1052887

# Submission time Handle Problem Language Result Execution time Memory
1052887 2024-08-11T05:31:22 Z kunzaZa183 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 25180 KB
#include <bits/stdc++.h>
using namespace std;
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 {
    long long arr[2 * maxn + 1] = {};

    long long sum(int r) {
      long long 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] = {};

  long long curans = 0;
  auto add = [&](int in) {
    int tmp = status[at[arr[in]]].second - 1;
    curans -= (long long)(at[arr[in]] + 1) * at[arr[in]] / 2;
    curans += (long long)(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 -= (long long)(at[arr[in]] + 1) * at[arr[in]] / 2;
    curans += (long long)(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};
  long long 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:10:28: warning: unused variable 'maxq' [-Wunused-variable]
   10 |   const int maxn = 300005, maxq = 50000;
      |                            ^~~~
diversity.cpp: In lambda function:
diversity.cpp:76:33: warning: 'coord' may be used uninitialized in this function [-Wmaybe-uninitialized]
   76 |               (2 * maxn - coord - 1) * (normal.sum(coord - 1) - normal.sum(0));
      |               ~~~~~~~~~~~~~~~~~~^~~~
diversity.cpp: In lambda function:
diversity.cpp:103:33: warning: 'coord' may be used uninitialized in this function [-Wmaybe-uninitialized]
  103 |               (2 * maxn - coord - 1) * (normal.sum(coord - 1) - normal.sum(0));
      |               ~~~~~~~~~~~~~~~~~~^~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24924 KB Output is correct
2 Correct 10 ms 24924 KB Output is correct
3 Correct 10 ms 24924 KB Output is correct
4 Correct 11 ms 24924 KB Output is correct
5 Correct 10 ms 25088 KB Output is correct
6 Correct 10 ms 24924 KB Output is correct
7 Correct 10 ms 24960 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 11 ms 25104 KB Output is correct
10 Correct 15 ms 24964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24924 KB Output is correct
2 Correct 10 ms 24924 KB Output is correct
3 Correct 12 ms 25104 KB Output is correct
4 Correct 31 ms 24936 KB Output is correct
5 Correct 41 ms 24920 KB Output is correct
6 Correct 59 ms 24920 KB Output is correct
7 Correct 59 ms 25084 KB Output is correct
8 Correct 57 ms 25088 KB Output is correct
9 Correct 64 ms 24920 KB Output is correct
10 Correct 63 ms 24924 KB Output is correct
11 Correct 54 ms 24920 KB Output is correct
12 Correct 60 ms 24920 KB Output is correct
13 Correct 58 ms 24924 KB Output is correct
14 Correct 54 ms 24920 KB Output is correct
15 Correct 66 ms 24924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24924 KB Output is correct
2 Correct 10 ms 24924 KB Output is correct
3 Correct 12 ms 25104 KB Output is correct
4 Correct 31 ms 24936 KB Output is correct
5 Correct 41 ms 24920 KB Output is correct
6 Correct 59 ms 24920 KB Output is correct
7 Correct 59 ms 25084 KB Output is correct
8 Correct 57 ms 25088 KB Output is correct
9 Correct 64 ms 24920 KB Output is correct
10 Correct 63 ms 24924 KB Output is correct
11 Correct 54 ms 24920 KB Output is correct
12 Correct 60 ms 24920 KB Output is correct
13 Correct 58 ms 24924 KB Output is correct
14 Correct 54 ms 24920 KB Output is correct
15 Correct 66 ms 24924 KB Output is correct
16 Correct 11 ms 24920 KB Output is correct
17 Correct 10 ms 25032 KB Output is correct
18 Correct 12 ms 24988 KB Output is correct
19 Correct 26 ms 25112 KB Output is correct
20 Correct 42 ms 24920 KB Output is correct
21 Correct 59 ms 24924 KB Output is correct
22 Correct 60 ms 24920 KB Output is correct
23 Correct 56 ms 25088 KB Output is correct
24 Correct 58 ms 24924 KB Output is correct
25 Correct 58 ms 24924 KB Output is correct
26 Correct 57 ms 25092 KB Output is correct
27 Correct 57 ms 25104 KB Output is correct
28 Correct 56 ms 24920 KB Output is correct
29 Correct 65 ms 25088 KB Output is correct
30 Correct 65 ms 24924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24924 KB Output is correct
2 Correct 10 ms 24924 KB Output is correct
3 Correct 12 ms 25104 KB Output is correct
4 Correct 31 ms 24936 KB Output is correct
5 Correct 41 ms 24920 KB Output is correct
6 Correct 59 ms 24920 KB Output is correct
7 Correct 59 ms 25084 KB Output is correct
8 Correct 57 ms 25088 KB Output is correct
9 Correct 64 ms 24920 KB Output is correct
10 Correct 63 ms 24924 KB Output is correct
11 Correct 54 ms 24920 KB Output is correct
12 Correct 60 ms 24920 KB Output is correct
13 Correct 58 ms 24924 KB Output is correct
14 Correct 54 ms 24920 KB Output is correct
15 Correct 66 ms 24924 KB Output is correct
16 Correct 11 ms 24920 KB Output is correct
17 Correct 10 ms 25032 KB Output is correct
18 Correct 12 ms 24988 KB Output is correct
19 Correct 26 ms 25112 KB Output is correct
20 Correct 42 ms 24920 KB Output is correct
21 Correct 59 ms 24924 KB Output is correct
22 Correct 60 ms 24920 KB Output is correct
23 Correct 56 ms 25088 KB Output is correct
24 Correct 58 ms 24924 KB Output is correct
25 Correct 58 ms 24924 KB Output is correct
26 Correct 57 ms 25092 KB Output is correct
27 Correct 57 ms 25104 KB Output is correct
28 Correct 56 ms 24920 KB Output is correct
29 Correct 65 ms 25088 KB Output is correct
30 Correct 65 ms 24924 KB Output is correct
31 Correct 10 ms 24924 KB Output is correct
32 Correct 10 ms 24924 KB Output is correct
33 Correct 11 ms 24860 KB Output is correct
34 Correct 12 ms 24988 KB Output is correct
35 Correct 18 ms 24924 KB Output is correct
36 Correct 12 ms 24924 KB Output is correct
37 Correct 18 ms 24924 KB Output is correct
38 Correct 18 ms 24952 KB Output is correct
39 Correct 25 ms 25112 KB Output is correct
40 Correct 47 ms 25084 KB Output is correct
41 Correct 60 ms 24924 KB Output is correct
42 Correct 59 ms 25040 KB Output is correct
43 Correct 57 ms 24932 KB Output is correct
44 Correct 61 ms 24920 KB Output is correct
45 Correct 61 ms 24924 KB Output is correct
46 Correct 60 ms 25084 KB Output is correct
47 Correct 63 ms 25084 KB Output is correct
48 Correct 58 ms 24924 KB Output is correct
49 Correct 58 ms 25084 KB Output is correct
50 Correct 59 ms 24924 KB Output is correct
51 Correct 58 ms 25084 KB Output is correct
52 Correct 58 ms 24924 KB Output is correct
53 Correct 58 ms 25088 KB Output is correct
54 Correct 59 ms 25088 KB Output is correct
55 Correct 61 ms 24924 KB Output is correct
56 Correct 59 ms 24924 KB Output is correct
57 Correct 60 ms 24924 KB Output is correct
58 Correct 59 ms 24924 KB Output is correct
59 Correct 60 ms 24920 KB Output is correct
60 Correct 63 ms 24924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24924 KB Output is correct
2 Correct 10 ms 24924 KB Output is correct
3 Correct 10 ms 24924 KB Output is correct
4 Correct 11 ms 24924 KB Output is correct
5 Correct 10 ms 25088 KB Output is correct
6 Correct 10 ms 24924 KB Output is correct
7 Correct 10 ms 24960 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 11 ms 25104 KB Output is correct
10 Correct 15 ms 24964 KB Output is correct
11 Correct 11 ms 24924 KB Output is correct
12 Correct 10 ms 24924 KB Output is correct
13 Correct 12 ms 25104 KB Output is correct
14 Correct 31 ms 24936 KB Output is correct
15 Correct 41 ms 24920 KB Output is correct
16 Correct 59 ms 24920 KB Output is correct
17 Correct 59 ms 25084 KB Output is correct
18 Correct 57 ms 25088 KB Output is correct
19 Correct 64 ms 24920 KB Output is correct
20 Correct 63 ms 24924 KB Output is correct
21 Correct 54 ms 24920 KB Output is correct
22 Correct 60 ms 24920 KB Output is correct
23 Correct 58 ms 24924 KB Output is correct
24 Correct 54 ms 24920 KB Output is correct
25 Correct 66 ms 24924 KB Output is correct
26 Correct 11 ms 24920 KB Output is correct
27 Correct 10 ms 25032 KB Output is correct
28 Correct 12 ms 24988 KB Output is correct
29 Correct 26 ms 25112 KB Output is correct
30 Correct 42 ms 24920 KB Output is correct
31 Correct 59 ms 24924 KB Output is correct
32 Correct 60 ms 24920 KB Output is correct
33 Correct 56 ms 25088 KB Output is correct
34 Correct 58 ms 24924 KB Output is correct
35 Correct 58 ms 24924 KB Output is correct
36 Correct 57 ms 25092 KB Output is correct
37 Correct 57 ms 25104 KB Output is correct
38 Correct 56 ms 24920 KB Output is correct
39 Correct 65 ms 25088 KB Output is correct
40 Correct 65 ms 24924 KB Output is correct
41 Correct 10 ms 24924 KB Output is correct
42 Correct 10 ms 24924 KB Output is correct
43 Correct 11 ms 24860 KB Output is correct
44 Correct 12 ms 24988 KB Output is correct
45 Correct 18 ms 24924 KB Output is correct
46 Correct 12 ms 24924 KB Output is correct
47 Correct 18 ms 24924 KB Output is correct
48 Correct 18 ms 24952 KB Output is correct
49 Correct 25 ms 25112 KB Output is correct
50 Correct 47 ms 25084 KB Output is correct
51 Correct 60 ms 24924 KB Output is correct
52 Correct 59 ms 25040 KB Output is correct
53 Correct 57 ms 24932 KB Output is correct
54 Correct 61 ms 24920 KB Output is correct
55 Correct 61 ms 24924 KB Output is correct
56 Correct 60 ms 25084 KB Output is correct
57 Correct 63 ms 25084 KB Output is correct
58 Correct 58 ms 24924 KB Output is correct
59 Correct 58 ms 25084 KB Output is correct
60 Correct 59 ms 24924 KB Output is correct
61 Correct 58 ms 25084 KB Output is correct
62 Correct 58 ms 24924 KB Output is correct
63 Correct 58 ms 25088 KB Output is correct
64 Correct 59 ms 25088 KB Output is correct
65 Correct 61 ms 24924 KB Output is correct
66 Correct 59 ms 24924 KB Output is correct
67 Correct 60 ms 24924 KB Output is correct
68 Correct 59 ms 24924 KB Output is correct
69 Correct 60 ms 24920 KB Output is correct
70 Correct 63 ms 24924 KB Output is correct
71 Correct 19 ms 24924 KB Output is correct
72 Correct 19 ms 25112 KB Output is correct
73 Correct 19 ms 24924 KB Output is correct
74 Correct 18 ms 25108 KB Output is correct
75 Correct 18 ms 24920 KB Output is correct
76 Correct 26 ms 24924 KB Output is correct
77 Correct 29 ms 24920 KB Output is correct
78 Correct 29 ms 25176 KB Output is correct
79 Correct 27 ms 24920 KB Output is correct
80 Correct 28 ms 24924 KB Output is correct
81 Correct 46 ms 24948 KB Output is correct
82 Correct 47 ms 24924 KB Output is correct
83 Correct 45 ms 24920 KB Output is correct
84 Correct 47 ms 24920 KB Output is correct
85 Correct 43 ms 24924 KB Output is correct
86 Correct 45 ms 24924 KB Output is correct
87 Correct 48 ms 24924 KB Output is correct
88 Correct 44 ms 24920 KB Output is correct
89 Correct 45 ms 24920 KB Output is correct
90 Correct 52 ms 24920 KB Output is correct
91 Correct 69 ms 25180 KB Output is correct
92 Correct 63 ms 25084 KB Output is correct
93 Correct 65 ms 24920 KB Output is correct
94 Correct 67 ms 24924 KB Output is correct
95 Correct 63 ms 25084 KB Output is correct
96 Correct 67 ms 25084 KB Output is correct
97 Correct 67 ms 24924 KB Output is correct
98 Correct 65 ms 24920 KB Output is correct
99 Correct 65 ms 24920 KB Output is correct
100 Correct 64 ms 24924 KB Output is correct
101 Correct 69 ms 24920 KB Output is correct
102 Correct 72 ms 24924 KB Output is correct
103 Correct 66 ms 25084 KB Output is correct
104 Correct 66 ms 24924 KB Output is correct
105 Correct 65 ms 24924 KB Output is correct
106 Correct 68 ms 24964 KB Output is correct
107 Correct 66 ms 24920 KB Output is correct
108 Correct 78 ms 24924 KB Output is correct
109 Correct 67 ms 24924 KB Output is correct
110 Correct 73 ms 24924 KB Output is correct
111 Correct 70 ms 24924 KB Output is correct
112 Correct 68 ms 24924 KB Output is correct
113 Correct 73 ms 24924 KB Output is correct
114 Correct 67 ms 24924 KB Output is correct
115 Correct 65 ms 25088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 24924 KB Output is correct
2 Correct 10 ms 24924 KB Output is correct
3 Correct 10 ms 24924 KB Output is correct
4 Correct 11 ms 24924 KB Output is correct
5 Correct 10 ms 25088 KB Output is correct
6 Correct 10 ms 24924 KB Output is correct
7 Correct 10 ms 24960 KB Output is correct
8 Correct 10 ms 24924 KB Output is correct
9 Correct 11 ms 25104 KB Output is correct
10 Correct 15 ms 24964 KB Output is correct
11 Correct 11 ms 24924 KB Output is correct
12 Correct 10 ms 24924 KB Output is correct
13 Correct 12 ms 25104 KB Output is correct
14 Correct 31 ms 24936 KB Output is correct
15 Correct 41 ms 24920 KB Output is correct
16 Correct 59 ms 24920 KB Output is correct
17 Correct 59 ms 25084 KB Output is correct
18 Correct 57 ms 25088 KB Output is correct
19 Correct 64 ms 24920 KB Output is correct
20 Correct 63 ms 24924 KB Output is correct
21 Correct 54 ms 24920 KB Output is correct
22 Correct 60 ms 24920 KB Output is correct
23 Correct 58 ms 24924 KB Output is correct
24 Correct 54 ms 24920 KB Output is correct
25 Correct 66 ms 24924 KB Output is correct
26 Correct 11 ms 24920 KB Output is correct
27 Correct 10 ms 25032 KB Output is correct
28 Correct 12 ms 24988 KB Output is correct
29 Correct 26 ms 25112 KB Output is correct
30 Correct 42 ms 24920 KB Output is correct
31 Correct 59 ms 24924 KB Output is correct
32 Correct 60 ms 24920 KB Output is correct
33 Correct 56 ms 25088 KB Output is correct
34 Correct 58 ms 24924 KB Output is correct
35 Correct 58 ms 24924 KB Output is correct
36 Correct 57 ms 25092 KB Output is correct
37 Correct 57 ms 25104 KB Output is correct
38 Correct 56 ms 24920 KB Output is correct
39 Correct 65 ms 25088 KB Output is correct
40 Correct 65 ms 24924 KB Output is correct
41 Correct 10 ms 24924 KB Output is correct
42 Correct 10 ms 24924 KB Output is correct
43 Correct 11 ms 24860 KB Output is correct
44 Correct 12 ms 24988 KB Output is correct
45 Correct 18 ms 24924 KB Output is correct
46 Correct 12 ms 24924 KB Output is correct
47 Correct 18 ms 24924 KB Output is correct
48 Correct 18 ms 24952 KB Output is correct
49 Correct 25 ms 25112 KB Output is correct
50 Correct 47 ms 25084 KB Output is correct
51 Correct 60 ms 24924 KB Output is correct
52 Correct 59 ms 25040 KB Output is correct
53 Correct 57 ms 24932 KB Output is correct
54 Correct 61 ms 24920 KB Output is correct
55 Correct 61 ms 24924 KB Output is correct
56 Correct 60 ms 25084 KB Output is correct
57 Correct 63 ms 25084 KB Output is correct
58 Correct 58 ms 24924 KB Output is correct
59 Correct 58 ms 25084 KB Output is correct
60 Correct 59 ms 24924 KB Output is correct
61 Correct 58 ms 25084 KB Output is correct
62 Correct 58 ms 24924 KB Output is correct
63 Correct 58 ms 25088 KB Output is correct
64 Correct 59 ms 25088 KB Output is correct
65 Correct 61 ms 24924 KB Output is correct
66 Correct 59 ms 24924 KB Output is correct
67 Correct 60 ms 24924 KB Output is correct
68 Correct 59 ms 24924 KB Output is correct
69 Correct 60 ms 24920 KB Output is correct
70 Correct 63 ms 24924 KB Output is correct
71 Correct 19 ms 24924 KB Output is correct
72 Correct 19 ms 25112 KB Output is correct
73 Correct 19 ms 24924 KB Output is correct
74 Correct 18 ms 25108 KB Output is correct
75 Correct 18 ms 24920 KB Output is correct
76 Correct 26 ms 24924 KB Output is correct
77 Correct 29 ms 24920 KB Output is correct
78 Correct 29 ms 25176 KB Output is correct
79 Correct 27 ms 24920 KB Output is correct
80 Correct 28 ms 24924 KB Output is correct
81 Correct 46 ms 24948 KB Output is correct
82 Correct 47 ms 24924 KB Output is correct
83 Correct 45 ms 24920 KB Output is correct
84 Correct 47 ms 24920 KB Output is correct
85 Correct 43 ms 24924 KB Output is correct
86 Correct 45 ms 24924 KB Output is correct
87 Correct 48 ms 24924 KB Output is correct
88 Correct 44 ms 24920 KB Output is correct
89 Correct 45 ms 24920 KB Output is correct
90 Correct 52 ms 24920 KB Output is correct
91 Correct 69 ms 25180 KB Output is correct
92 Correct 63 ms 25084 KB Output is correct
93 Correct 65 ms 24920 KB Output is correct
94 Correct 67 ms 24924 KB Output is correct
95 Correct 63 ms 25084 KB Output is correct
96 Correct 67 ms 25084 KB Output is correct
97 Correct 67 ms 24924 KB Output is correct
98 Correct 65 ms 24920 KB Output is correct
99 Correct 65 ms 24920 KB Output is correct
100 Correct 64 ms 24924 KB Output is correct
101 Correct 69 ms 24920 KB Output is correct
102 Correct 72 ms 24924 KB Output is correct
103 Correct 66 ms 25084 KB Output is correct
104 Correct 66 ms 24924 KB Output is correct
105 Correct 65 ms 24924 KB Output is correct
106 Correct 68 ms 24964 KB Output is correct
107 Correct 66 ms 24920 KB Output is correct
108 Correct 78 ms 24924 KB Output is correct
109 Correct 67 ms 24924 KB Output is correct
110 Correct 73 ms 24924 KB Output is correct
111 Correct 70 ms 24924 KB Output is correct
112 Correct 68 ms 24924 KB Output is correct
113 Correct 73 ms 24924 KB Output is correct
114 Correct 67 ms 24924 KB Output is correct
115 Correct 65 ms 25088 KB Output is correct
116 Execution timed out 7063 ms 24924 KB Time limit exceeded
117 Halted 0 ms 0 KB -