# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
859589 | Tenis0206 | Diversity (CEOI21_diversity) | C++11 | 7013 ms | 5020 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;
const int nmax = 3e5;
int n,q;
int v[nmax + 5];
int fr[nmax + 5];
long long query(int st, int dr)
{
for(int i=st;i<=dr;i++)
{
++fr[v[i]];
}
vector<int> l;
for(int i=st;i<=dr;i++)
{
if(fr[v[i]] > 0)
{
l.push_back(fr[v[i]]);
}
fr[v[i]] = 0;
}
sort(l.begin(),l.end(),greater<int>());
deque<int> d;
for(int i=0;i<l.size();i++)
{
if(i % 2 == 0)
{
d.push_back(l[i]);
}
else
{
d.push_front(l[i]);
}
}
int sum_st = 0, sum_dr = dr - st + 1;
long long rez = 0;
for(auto it : d)
{
sum_dr -= it;
rez += 1LL * (dr - st + 1) * (dr - st + 2) / 2;
rez -= 1LL * sum_st * (sum_st + 1) / 2;
rez -= 1LL * sum_dr * (sum_dr + 1) / 2;
sum_st += it;
}
return rez;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>v[i];
}
for(int i=1;i<=q;i++)
{
int st, dr;
cin>>st>>dr;
cout<<query(st,dr)<<'\n';
}
return 0;
}
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... |