Submission #696581

# Submission time Handle Problem Language Result Execution time Memory
696581 2023-02-07T00:20:30 Z cig32 Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 15832 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 freq[MAXN], n;

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

bool byfreq(int x, int y) {
  return freq[x] < freq[y];
}
void solve(int tc) {
  int q;
  cin >> n >> q;
  int c = 300000;
  block = ceil(sqrt(n));
  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;
  vector<int> v;
  for(int i=1; i<=q; i++) {
    while(l > qry[i].first.first) {
      l--;
      freq[arr[l]]++;
      if(freq[arr[l]] == 1) v.push_back(arr[l]);
    }
    while(r > qry[i].first.second) {
      freq[arr[r]]--;
      r--;
    }
    while(r < qry[i].first.second) {
      r++;
      freq[arr[r]]++;
      if(freq[arr[r]] == 1) v.push_back(arr[r]);
    }
    while(l < qry[i].first.first) {
      freq[arr[l]]--;
      l++;
    }
    vector<int> w,w2;
    sort(v.begin(), v.end(), byfreq);
    for(int j=0; j<v.size(); j++) {
      if((j==0 && freq[v[j]] > 0) || (v[j] != v[j-1] && freq[v[j]] > 0)) {
        w.push_back(v[j]);
        w2.push_back(freq[v[j]]);
      }
    }
    v=w;
    int m = w2.size();
    int b[m];
    int lb=0, rb=m-1, tog=0;
    for(int x: w2) {
      if(tog&1) b[rb--] = x;
      else b[lb++] = x;
      tog^=1;
    }
    int ans = 0;
    int ws=0,rs=0;
    for(int i=m-1; i>=0; i--) {
      ans += b[i] * ws - b[i] * (i-1) * rs;
      ws += b[i] * i, rs += b[i];
    }
    for(int i=0; i<m; i++) ans += b[i] * (b[i] + 1) / 2;
    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);
}

Compilation message

diversity.cpp: In function 'void solve(long long int)':
diversity.cpp:72:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |     for(int j=0; j<v.size(); j++) {
      |                  ~^~~~~~~~~
diversity.cpp:38:7: warning: unused variable 'c' [-Wunused-variable]
   38 |   int c = 300000;
      |       ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 5 ms 1308 KB Output is correct
5 Correct 10 ms 2260 KB Output is correct
6 Correct 16 ms 3304 KB Output is correct
7 Correct 16 ms 3156 KB Output is correct
8 Correct 15 ms 3284 KB Output is correct
9 Correct 16 ms 3284 KB Output is correct
10 Correct 17 ms 3284 KB Output is correct
11 Correct 16 ms 3300 KB Output is correct
12 Correct 16 ms 3296 KB Output is correct
13 Correct 18 ms 3300 KB Output is correct
14 Correct 15 ms 3292 KB Output is correct
15 Correct 17 ms 3276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 5 ms 1308 KB Output is correct
5 Correct 10 ms 2260 KB Output is correct
6 Correct 16 ms 3304 KB Output is correct
7 Correct 16 ms 3156 KB Output is correct
8 Correct 15 ms 3284 KB Output is correct
9 Correct 16 ms 3284 KB Output is correct
10 Correct 17 ms 3284 KB Output is correct
11 Correct 16 ms 3300 KB Output is correct
12 Correct 16 ms 3296 KB Output is correct
13 Correct 18 ms 3300 KB Output is correct
14 Correct 15 ms 3292 KB Output is correct
15 Correct 17 ms 3276 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 6 ms 1348 KB Output is correct
20 Correct 12 ms 2384 KB Output is correct
21 Correct 17 ms 3424 KB Output is correct
22 Correct 18 ms 3412 KB Output is correct
23 Correct 18 ms 3432 KB Output is correct
24 Correct 17 ms 3408 KB Output is correct
25 Correct 17 ms 3412 KB Output is correct
26 Correct 21 ms 3408 KB Output is correct
27 Correct 17 ms 3428 KB Output is correct
28 Correct 17 ms 3284 KB Output is correct
29 Correct 19 ms 3420 KB Output is correct
30 Correct 18 ms 3424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 5 ms 1308 KB Output is correct
5 Correct 10 ms 2260 KB Output is correct
6 Correct 16 ms 3304 KB Output is correct
7 Correct 16 ms 3156 KB Output is correct
8 Correct 15 ms 3284 KB Output is correct
9 Correct 16 ms 3284 KB Output is correct
10 Correct 17 ms 3284 KB Output is correct
11 Correct 16 ms 3300 KB Output is correct
12 Correct 16 ms 3296 KB Output is correct
13 Correct 18 ms 3300 KB Output is correct
14 Correct 15 ms 3292 KB Output is correct
15 Correct 17 ms 3276 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 6 ms 1348 KB Output is correct
20 Correct 12 ms 2384 KB Output is correct
21 Correct 17 ms 3424 KB Output is correct
22 Correct 18 ms 3412 KB Output is correct
23 Correct 18 ms 3432 KB Output is correct
24 Correct 17 ms 3408 KB Output is correct
25 Correct 17 ms 3412 KB Output is correct
26 Correct 21 ms 3408 KB Output is correct
27 Correct 17 ms 3428 KB Output is correct
28 Correct 17 ms 3284 KB Output is correct
29 Correct 19 ms 3420 KB Output is correct
30 Correct 18 ms 3424 KB Output is correct
31 Correct 0 ms 340 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 336 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 4 ms 836 KB Output is correct
38 Correct 4 ms 852 KB Output is correct
39 Correct 7 ms 1500 KB Output is correct
40 Correct 13 ms 2648 KB Output is correct
41 Correct 19 ms 3804 KB Output is correct
42 Correct 20 ms 3796 KB Output is correct
43 Correct 19 ms 3752 KB Output is correct
44 Correct 27 ms 3916 KB Output is correct
45 Correct 23 ms 3804 KB Output is correct
46 Correct 19 ms 3804 KB Output is correct
47 Correct 19 ms 3796 KB Output is correct
48 Correct 19 ms 3788 KB Output is correct
49 Correct 19 ms 3804 KB Output is correct
50 Correct 19 ms 3796 KB Output is correct
51 Correct 19 ms 3804 KB Output is correct
52 Correct 22 ms 3808 KB Output is correct
53 Correct 21 ms 3808 KB Output is correct
54 Correct 25 ms 3800 KB Output is correct
55 Correct 27 ms 3788 KB Output is correct
56 Correct 20 ms 3840 KB Output is correct
57 Correct 42 ms 3804 KB Output is correct
58 Correct 22 ms 3800 KB Output is correct
59 Correct 22 ms 3804 KB Output is correct
60 Correct 19 ms 3808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 5 ms 1308 KB Output is correct
15 Correct 10 ms 2260 KB Output is correct
16 Correct 16 ms 3304 KB Output is correct
17 Correct 16 ms 3156 KB Output is correct
18 Correct 15 ms 3284 KB Output is correct
19 Correct 16 ms 3284 KB Output is correct
20 Correct 17 ms 3284 KB Output is correct
21 Correct 16 ms 3300 KB Output is correct
22 Correct 16 ms 3296 KB Output is correct
23 Correct 18 ms 3300 KB Output is correct
24 Correct 15 ms 3292 KB Output is correct
25 Correct 17 ms 3276 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 6 ms 1348 KB Output is correct
30 Correct 12 ms 2384 KB Output is correct
31 Correct 17 ms 3424 KB Output is correct
32 Correct 18 ms 3412 KB Output is correct
33 Correct 18 ms 3432 KB Output is correct
34 Correct 17 ms 3408 KB Output is correct
35 Correct 17 ms 3412 KB Output is correct
36 Correct 21 ms 3408 KB Output is correct
37 Correct 17 ms 3428 KB Output is correct
38 Correct 17 ms 3284 KB Output is correct
39 Correct 19 ms 3420 KB Output is correct
40 Correct 18 ms 3424 KB Output is correct
41 Correct 0 ms 340 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 336 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 4 ms 836 KB Output is correct
48 Correct 4 ms 852 KB Output is correct
49 Correct 7 ms 1500 KB Output is correct
50 Correct 13 ms 2648 KB Output is correct
51 Correct 19 ms 3804 KB Output is correct
52 Correct 20 ms 3796 KB Output is correct
53 Correct 19 ms 3752 KB Output is correct
54 Correct 27 ms 3916 KB Output is correct
55 Correct 23 ms 3804 KB Output is correct
56 Correct 19 ms 3804 KB Output is correct
57 Correct 19 ms 3796 KB Output is correct
58 Correct 19 ms 3788 KB Output is correct
59 Correct 19 ms 3804 KB Output is correct
60 Correct 19 ms 3796 KB Output is correct
61 Correct 19 ms 3804 KB Output is correct
62 Correct 22 ms 3808 KB Output is correct
63 Correct 21 ms 3808 KB Output is correct
64 Correct 25 ms 3800 KB Output is correct
65 Correct 27 ms 3788 KB Output is correct
66 Correct 20 ms 3840 KB Output is correct
67 Correct 42 ms 3804 KB Output is correct
68 Correct 22 ms 3800 KB Output is correct
69 Correct 22 ms 3804 KB Output is correct
70 Correct 19 ms 3808 KB Output is correct
71 Correct 5 ms 964 KB Output is correct
72 Correct 5 ms 980 KB Output is correct
73 Correct 4 ms 980 KB Output is correct
74 Correct 4 ms 984 KB Output is correct
75 Correct 4 ms 984 KB Output is correct
76 Correct 12 ms 1868 KB Output is correct
77 Correct 8 ms 1836 KB Output is correct
78 Correct 8 ms 1876 KB Output is correct
79 Correct 9 ms 1856 KB Output is correct
80 Correct 8 ms 1876 KB Output is correct
81 Correct 15 ms 3400 KB Output is correct
82 Correct 16 ms 3340 KB Output is correct
83 Correct 16 ms 3412 KB Output is correct
84 Correct 16 ms 3408 KB Output is correct
85 Correct 15 ms 3364 KB Output is correct
86 Correct 18 ms 3916 KB Output is correct
87 Correct 21 ms 3916 KB Output is correct
88 Correct 18 ms 3916 KB Output is correct
89 Correct 18 ms 3924 KB Output is correct
90 Correct 18 ms 3828 KB Output is correct
91 Correct 32 ms 8372 KB Output is correct
92 Correct 32 ms 8456 KB Output is correct
93 Correct 32 ms 8452 KB Output is correct
94 Correct 32 ms 8452 KB Output is correct
95 Correct 31 ms 8456 KB Output is correct
96 Correct 43 ms 14152 KB Output is correct
97 Correct 43 ms 14252 KB Output is correct
98 Correct 53 ms 14152 KB Output is correct
99 Correct 44 ms 14164 KB Output is correct
100 Correct 43 ms 14168 KB Output is correct
101 Correct 42 ms 14172 KB Output is correct
102 Correct 42 ms 14152 KB Output is correct
103 Correct 42 ms 14164 KB Output is correct
104 Correct 42 ms 14148 KB Output is correct
105 Correct 49 ms 14360 KB Output is correct
106 Correct 47 ms 15816 KB Output is correct
107 Correct 45 ms 15828 KB Output is correct
108 Correct 47 ms 15804 KB Output is correct
109 Correct 45 ms 15816 KB Output is correct
110 Correct 45 ms 15772 KB Output is correct
111 Correct 43 ms 15832 KB Output is correct
112 Correct 48 ms 15808 KB Output is correct
113 Correct 46 ms 15828 KB Output is correct
114 Correct 45 ms 15788 KB Output is correct
115 Correct 42 ms 15816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 5 ms 1308 KB Output is correct
15 Correct 10 ms 2260 KB Output is correct
16 Correct 16 ms 3304 KB Output is correct
17 Correct 16 ms 3156 KB Output is correct
18 Correct 15 ms 3284 KB Output is correct
19 Correct 16 ms 3284 KB Output is correct
20 Correct 17 ms 3284 KB Output is correct
21 Correct 16 ms 3300 KB Output is correct
22 Correct 16 ms 3296 KB Output is correct
23 Correct 18 ms 3300 KB Output is correct
24 Correct 15 ms 3292 KB Output is correct
25 Correct 17 ms 3276 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 6 ms 1348 KB Output is correct
30 Correct 12 ms 2384 KB Output is correct
31 Correct 17 ms 3424 KB Output is correct
32 Correct 18 ms 3412 KB Output is correct
33 Correct 18 ms 3432 KB Output is correct
34 Correct 17 ms 3408 KB Output is correct
35 Correct 17 ms 3412 KB Output is correct
36 Correct 21 ms 3408 KB Output is correct
37 Correct 17 ms 3428 KB Output is correct
38 Correct 17 ms 3284 KB Output is correct
39 Correct 19 ms 3420 KB Output is correct
40 Correct 18 ms 3424 KB Output is correct
41 Correct 0 ms 340 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 336 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 4 ms 836 KB Output is correct
48 Correct 4 ms 852 KB Output is correct
49 Correct 7 ms 1500 KB Output is correct
50 Correct 13 ms 2648 KB Output is correct
51 Correct 19 ms 3804 KB Output is correct
52 Correct 20 ms 3796 KB Output is correct
53 Correct 19 ms 3752 KB Output is correct
54 Correct 27 ms 3916 KB Output is correct
55 Correct 23 ms 3804 KB Output is correct
56 Correct 19 ms 3804 KB Output is correct
57 Correct 19 ms 3796 KB Output is correct
58 Correct 19 ms 3788 KB Output is correct
59 Correct 19 ms 3804 KB Output is correct
60 Correct 19 ms 3796 KB Output is correct
61 Correct 19 ms 3804 KB Output is correct
62 Correct 22 ms 3808 KB Output is correct
63 Correct 21 ms 3808 KB Output is correct
64 Correct 25 ms 3800 KB Output is correct
65 Correct 27 ms 3788 KB Output is correct
66 Correct 20 ms 3840 KB Output is correct
67 Correct 42 ms 3804 KB Output is correct
68 Correct 22 ms 3800 KB Output is correct
69 Correct 22 ms 3804 KB Output is correct
70 Correct 19 ms 3808 KB Output is correct
71 Correct 5 ms 964 KB Output is correct
72 Correct 5 ms 980 KB Output is correct
73 Correct 4 ms 980 KB Output is correct
74 Correct 4 ms 984 KB Output is correct
75 Correct 4 ms 984 KB Output is correct
76 Correct 12 ms 1868 KB Output is correct
77 Correct 8 ms 1836 KB Output is correct
78 Correct 8 ms 1876 KB Output is correct
79 Correct 9 ms 1856 KB Output is correct
80 Correct 8 ms 1876 KB Output is correct
81 Correct 15 ms 3400 KB Output is correct
82 Correct 16 ms 3340 KB Output is correct
83 Correct 16 ms 3412 KB Output is correct
84 Correct 16 ms 3408 KB Output is correct
85 Correct 15 ms 3364 KB Output is correct
86 Correct 18 ms 3916 KB Output is correct
87 Correct 21 ms 3916 KB Output is correct
88 Correct 18 ms 3916 KB Output is correct
89 Correct 18 ms 3924 KB Output is correct
90 Correct 18 ms 3828 KB Output is correct
91 Correct 32 ms 8372 KB Output is correct
92 Correct 32 ms 8456 KB Output is correct
93 Correct 32 ms 8452 KB Output is correct
94 Correct 32 ms 8452 KB Output is correct
95 Correct 31 ms 8456 KB Output is correct
96 Correct 43 ms 14152 KB Output is correct
97 Correct 43 ms 14252 KB Output is correct
98 Correct 53 ms 14152 KB Output is correct
99 Correct 44 ms 14164 KB Output is correct
100 Correct 43 ms 14168 KB Output is correct
101 Correct 42 ms 14172 KB Output is correct
102 Correct 42 ms 14152 KB Output is correct
103 Correct 42 ms 14164 KB Output is correct
104 Correct 42 ms 14148 KB Output is correct
105 Correct 49 ms 14360 KB Output is correct
106 Correct 47 ms 15816 KB Output is correct
107 Correct 45 ms 15828 KB Output is correct
108 Correct 47 ms 15804 KB Output is correct
109 Correct 45 ms 15816 KB Output is correct
110 Correct 45 ms 15772 KB Output is correct
111 Correct 43 ms 15832 KB Output is correct
112 Correct 48 ms 15808 KB Output is correct
113 Correct 46 ms 15828 KB Output is correct
114 Correct 45 ms 15788 KB Output is correct
115 Correct 42 ms 15816 KB Output is correct
116 Correct 111 ms 2892 KB Output is correct
117 Correct 208 ms 2852 KB Output is correct
118 Correct 224 ms 3488 KB Output is correct
119 Correct 1080 ms 3556 KB Output is correct
120 Correct 2274 ms 3588 KB Output is correct
121 Correct 2561 ms 4184 KB Output is correct
122 Correct 5513 ms 4292 KB Output is correct
123 Correct 2834 ms 5492 KB Output is correct
124 Execution timed out 7026 ms 5604 KB Time limit exceeded
125 Halted 0 ms 0 KB -