#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=0; i<m; i++) cout << b[i] << " ";
cout << "\n";
}
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;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |