# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
639517 | pliam | Diversity (CEOI21_diversity) | C++14 | 1 ms | 256 KiB |
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;
#define MAXN 300005
typedef long long ll;
int N,Q;
int a[MAXN];
vector<pair<int,int>> queries;
int main(){
scanf("%d%d",&N,&Q);
for(int i=0;i<N;i++){
scanf("%d",&a[i+1]);
}
for(int i=0;i<Q;i++){
int l,r;
scanf("%d%d",&l,&r);
queries.push_back({l,r});
}
for(auto q:queries){
ll l,r;
tie(l,r)=q;
ll m=r-l+1;
vector<int> items;
map<int,pair<ll,ll>> spec_pos;
for(int i=l;i<=r;i++){
items.push_back(a[i]);
}
sort(items.begin(),items.end());
for(int i=0;i<m;i++){
ll pos=l+i;
int spec=items[i];
if(!spec_pos.count(spec)){
spec_pos[spec]={pos,pos};
}else{
spec_pos[spec].second=max(spec_pos[spec].second,pos);
}
}
ll ans=0;
for(auto p:spec_pos){
//p.second are the positions
ll locans=(m*(m+1))/2;
//now subtract those that do not contain it
ll rangel=p.second.first-l;
ll ranger=r-p.second.second;
locans-=(rangel*(rangel+1))/2+(ranger*(ranger+1))/2;
ans+=locans;
}
printf("%lld\n",ans);
}
}
Compilation message (stderr)
# | 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... |