This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define fr first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
#define USING_ORDERED_SET 0
#if USING_ORDERED_SET
#include<bits/extc++.h>
using namespace __gnu_pbds;
template<class T>using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#endif
template<class T>void umax(T &a,T b){if(a<b)a=b;}
template<class T>void umin(T &a,T b){if(b<a)a=b;}
#ifdef juggernaut
#define printl(args...)printf(args)
#else
#define printl(args...)((void)0)
#endif
int a[300005];
ll tree[4*300005];
ll lazy[4*300005];
int cnt[300005];
void push(int v,int l,int r){
if(l!=r){
lazy[v<<1]+=lazy[v];
lazy[v<<1|1]+=lazy[v];
}
tree[v]+=lazy[v]*(r-l+1);
lazy[v]=0;
}
void update(int v,int l,int r,int ql,int qr){
push(v,l,r);
if(qr<l||r<ql)return;
if(ql<=l&&r<=qr){
lazy[v]++;
push(v,l,r);
return;
}
int mid=(l+r)>>1;
update(v<<1,l,mid,ql,qr);
update(v<<1|1,mid+1,r,ql,qr);
tree[v]=tree[v<<1]+tree[v<<1|1];
}
ll get(int v,int l,int r,int ql,int qr){
if(qr<l||r<ql)return 0ll;
push(v,l,r);
if(ql<=l&&r<=qr)return tree[v];
int mid=(l+r)>>1;
return get(v<<1,l,mid,ql,qr)+get(v<<1|1,mid+1,r,ql,qr);
}
int main(){
int n,q;
scanf("%d%d",&n,&q);
assert(q==1);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
cnt[x]++;
}
vector<int>vec;
for(int i=1;i<=300000;i++)if(cnt[i])vec.push_back(cnt[i]);
sort(vec.begin(),vec.end());
int idx=1;
int ide=n;
int val=1;
for(int to:vec){
if(val&1)
while(to--)a[idx++]=val;
else
while(to--)a[ide--]=val;
val++;
}
ll sum=0;
for(int i=1;i<=n;i++){
update(1,1,n,(a[i]==a[i-1]?i:1),i);
sum+=get(1,1,n,1,i);
}
cout<<sum;
}
Compilation message (stderr)
diversity.cpp: In function 'int main()':
diversity.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d%d",&n,&q);
| ~~~~~^~~~~~~~~~~~~~
diversity.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d",&x);
| ~~~~~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |