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>
using namespace std;
int n,q;
int arr[300005];
int cnt[300005];
const int sq=1500;
typedef pair<long long,long long> P;
long long tree1[300005];
long long tree2[300005];
typedef pair<P,int> Pi;
const int MAX=300000;
long long h1,h2;
P pl(P a,P b) {
return P(a.first+b.first,a.second+b.second);
}
P mi(P a,P b) {
return P(a.first-b.first,a.second-b.second);
}
inline long long sum1(int i) {
long long ret=0;
while (i>0) {
ret+=tree1[i];
i-=(i&-i);
}
return ret;
}
inline long long sum2(int i) {
long long ret=0;
while (i>0) {
ret+=tree2[i];
i-=(i&-i);
}
return ret;
}
inline P sum(int l,int r) {
return P(sum1(r+1)-sum1(l),sum2(r+1)-sum2(l));
}
inline void upd1(int i,int val) {
h1+=val;
while (i<=300004) {
tree1[i]+=val;
i+=(i&-i);
}
}
inline void upd2(int i,int val) {
h2+=val;
while (i<=300004) {
tree2[i]+=val;
i+=(i&-i);
}
}
inline void update(int i,P val) {
upd1(i+1,val.first);
upd2(i+1,val.second);
}
int pos[300005];
int tree[300005];
inline int sum(int i) {
i++;
int ret=0;
while (i>0) {
ret+=tree[i];
i-=(i&-i);
}
return ret;
}
inline void upd(int i,int val) {
i++;
while (i<=300004) {
tree[i]+=val;
i+=(i&-i);
}
}
bool comp(Pi a,Pi b) {
if (a.first.first/sq==b.first.first/sq) {
return a.first.second<b.first.second;
}
return a.first.first/sq<b.first.first/sq;
}
long long value=0;
inline void push(int ind) {
int now=arr[ind];
int p=sum(cnt[now])-1;
p=pos[p];
upd(cnt[now],-1);
upd(cnt[now]+1,1);
cnt[now]++;
long long one=sum1(p);
long long two=sum2(p);
P got=P(one,two);
value+=p*got.second-got.first;
got=P(h1-one,h2-two);
value+=got.first-p*got.second;
update(p,P(p,1));
}
inline void out(int ind) {
int now=arr[ind];
int p=sum(cnt[now]-1);
p=pos[p];
upd(cnt[now],-1);
upd(cnt[now]-1,1);
cnt[now]--;
long long one=sum1(p);
long long two=sum2(p);
P got=P(one,two);
value-=p*got.second-got.first;
got=P(h1-one,h2-two);
value-=got.first-p*got.second;
update(p,P(-p,-1));
}
long long ret[50005];
int main(void) {
scanf("%d %d",&n,&q);
for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
vector<int> v;
for(int i=1;i<=300000;i++) {
v.push_back(i-1);
}
sort(v.begin(),v.end());
vector<long long> v1;
for(int i=0;i<v.size();i+=2) {
v1.push_back(v[i]);
}
vector<long long> v2;
for(int i=1;i<v.size();i+=2) {
v2.push_back(v[i]);
}
reverse(v2.begin(),v2.end());
for(auto x:v2) {
v1.push_back(x);
}
for(int i=0;i<300000;i++) {
pos[v1[i]]=i;
}
upd(0,300000);
vector<Pi> query;
for(int i=0;i<q;i++) {
int l,r;
scanf("%d %d",&l,&r);
l--;
r--;
query.push_back(Pi(P(l,r),i));
}
sort(query.begin(),query.end(),comp);
int l=query[0].first.first;
int r=query[0].first.first-1;
for(int i=0;i<q;i++) {
int nl=query[i].first.first;
int nr=query[i].first.second;
while (l>nl) {
l--;
push(l);
}
while (r<nr) {
r++;
push(r);
}
while (l<nl) {
out(l);
l++;
}
while (r>nr) {
out(r);
r--;
}
int len=nr-nl+1;
ret[query[i].second]=value+1LL*len*(len+1)/2;
}
for(int i=0;i<q;i++) {
printf("%lld\n",ret[i]);
}
}
Compilation message (stderr)
diversity.cpp: In function 'int main()':
diversity.cpp:141:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for(int i=0;i<v.size();i+=2) {
| ~^~~~~~~~~
diversity.cpp:145:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for(int i=1;i<v.size();i+=2) {
| ~^~~~~~~~~
diversity.cpp:131:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%d %d",&n,&q);
| ~~~~~^~~~~~~~~~~~~~~
diversity.cpp:133:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | scanf("%d",&arr[i]);
| ~~~~~^~~~~~~~~~~~~~
diversity.cpp:159:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | scanf("%d %d",&l,&r);
| ~~~~~^~~~~~~~~~~~~~~
# | 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... |