Submission #696680

# Submission time Handle Problem Language Result Execution time Memory
696680 2023-02-07T03:35:19 Z cig32 Diversity (CEOI21_diversity) C++17
18 / 100
63 ms 4444 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], ff[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;
  // O(sqrt(n)) frequencies
  set<int> all; // set of all frequencies
  ff[0] = n;
  all.insert(0);
  for(int i=1; i<=q; i++) {
    while(l > qry[i].first.first) {
      l--;
      ff[freq[arr[l]]]--;
      if(ff[freq[arr[l]]] == 0) all.erase(freq[arr[l]]);
      freq[arr[l]]++;
      ff[freq[arr[l]]]++;
      if(ff[freq[arr[l]]] == 1) all.insert(freq[arr[l]]);
    }
    while(r > qry[i].first.second) {
      ff[freq[arr[r]]]--;
      if(ff[freq[arr[r]]] == 0) all.erase(freq[arr[r]]);
      freq[arr[r]]--;
      ff[freq[arr[r]]]++;
      if(ff[freq[arr[r]]] == 1) all.insert(freq[arr[r]]);
      r--;
    }
    while(r < qry[i].first.second) {
      r++;
      ff[freq[arr[r]]]--;
      if(ff[freq[arr[r]]] == 0) all.erase(freq[arr[r]]);
      freq[arr[r]]++;
      ff[freq[arr[r]]]++;
      if(ff[freq[arr[r]]] == 1) all.insert(freq[arr[r]]);
    }
    while(l < qry[i].first.first) {
      ff[freq[arr[l]]]--;
      if(ff[freq[arr[l]]] == 0) all.erase(freq[arr[l]]);
      freq[arr[l]]--;
      ff[freq[arr[l]]]++;
      if(ff[freq[arr[l]]] == 1) all.insert(freq[arr[l]]);
      l++;
    }
    
    queue<pair<int, int> > ln; stack<pair<int, int> > rn;
    int m = 0;
    for(int x: all) {
      // {x, ff[x]}
      if(ff[x] / 2 > 0) {
        ln.push({x, ff[x] / 2});
        rn.push({x, ff[x] / 2});
        m += 2;
      }
      if(ff[x] % 2) {
        if(ln.size() > rn.size()) rn.push({x, 1});
        else ln.push({x, 1});
        m++;
      }
    }
    pair<int, int> b[m];
    int it = 0;
    while(ln.size()) {
      b[it++] = ln.front(); ln.pop();
    }
    while(rn.size()) {
      b[it++] = rn.top(); rn.pop();
    }
    int ans = 0;
    int ws=0,rs=0;
    int cur = n;
    for(int i=m-1; i>=0; i--) {
      int cnt = b[i].second;
      int val = b[i].first;
      ans += val * ws * cnt + val * val * ((cur - 1) + (cur - cnt + 1)) * (cnt - 1) / 2;
      ans -= val * cnt * cur * rs;
      ans -= val * cur * val * cnt * (cnt - 1) / 2;
      ans += val * rs * (2 + cnt + 1) * cnt / 2;
      ans += val * val * (cnt * (cnt + 1) * (2 * cnt + 1) / 6 - cnt);
      ws += val * ((cur - 1) + (cur - cnt)) * cnt / 2;
      rs += val * cnt;
      cur -= cnt;
    }
    for(int i=0; i<m; i++) ans += b[i].first * (b[i].first + 1) / 2 * b[i].second;
    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:38:7: warning: unused variable 'c' [-Wunused-variable]
   38 |   int c = 300000;
      |       ^
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 424 KB Output is correct
8 Incorrect 1 ms 340 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 14 ms 1492 KB Output is correct
5 Correct 30 ms 2876 KB Output is correct
6 Correct 43 ms 3712 KB Output is correct
7 Correct 37 ms 4444 KB Output is correct
8 Correct 42 ms 3660 KB Output is correct
9 Correct 43 ms 3680 KB Output is correct
10 Correct 43 ms 4112 KB Output is correct
11 Correct 42 ms 3856 KB Output is correct
12 Correct 42 ms 3728 KB Output is correct
13 Correct 42 ms 3632 KB Output is correct
14 Correct 43 ms 3804 KB Output is correct
15 Correct 42 ms 3836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 14 ms 1492 KB Output is correct
5 Correct 30 ms 2876 KB Output is correct
6 Correct 43 ms 3712 KB Output is correct
7 Correct 37 ms 4444 KB Output is correct
8 Correct 42 ms 3660 KB Output is correct
9 Correct 43 ms 3680 KB Output is correct
10 Correct 43 ms 4112 KB Output is correct
11 Correct 42 ms 3856 KB Output is correct
12 Correct 42 ms 3728 KB Output is correct
13 Correct 42 ms 3632 KB Output is correct
14 Correct 43 ms 3804 KB Output is correct
15 Correct 42 ms 3836 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 16 ms 1496 KB Output is correct
20 Correct 32 ms 2552 KB Output is correct
21 Correct 46 ms 3872 KB Output is correct
22 Correct 48 ms 3660 KB Output is correct
23 Correct 49 ms 3788 KB Output is correct
24 Correct 49 ms 3680 KB Output is correct
25 Correct 49 ms 3760 KB Output is correct
26 Correct 49 ms 3660 KB Output is correct
27 Correct 48 ms 3800 KB Output is correct
28 Correct 50 ms 3928 KB Output is correct
29 Correct 49 ms 3676 KB Output is correct
30 Correct 63 ms 3788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 14 ms 1492 KB Output is correct
5 Correct 30 ms 2876 KB Output is correct
6 Correct 43 ms 3712 KB Output is correct
7 Correct 37 ms 4444 KB Output is correct
8 Correct 42 ms 3660 KB Output is correct
9 Correct 43 ms 3680 KB Output is correct
10 Correct 43 ms 4112 KB Output is correct
11 Correct 42 ms 3856 KB Output is correct
12 Correct 42 ms 3728 KB Output is correct
13 Correct 42 ms 3632 KB Output is correct
14 Correct 43 ms 3804 KB Output is correct
15 Correct 42 ms 3836 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 2 ms 344 KB Output is correct
19 Correct 16 ms 1496 KB Output is correct
20 Correct 32 ms 2552 KB Output is correct
21 Correct 46 ms 3872 KB Output is correct
22 Correct 48 ms 3660 KB Output is correct
23 Correct 49 ms 3788 KB Output is correct
24 Correct 49 ms 3680 KB Output is correct
25 Correct 49 ms 3760 KB Output is correct
26 Correct 49 ms 3660 KB Output is correct
27 Correct 48 ms 3800 KB Output is correct
28 Correct 50 ms 3928 KB Output is correct
29 Correct 49 ms 3676 KB Output is correct
30 Correct 63 ms 3788 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Incorrect 1 ms 340 KB Output isn't correct
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 424 KB Output is correct
8 Incorrect 1 ms 340 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 424 KB Output is correct
8 Incorrect 1 ms 340 KB Output isn't correct
9 Halted 0 ms 0 KB -