#include<bits/stdc++.h>
#define int long long
#define ll long long
#define down cout<<'\n';
#define debug cout<<" cucuucucuuu",down
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "test"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".ans","w",stdout)
#define pb push_back
#define mask(k) (1<<k)
#define mp make_pair
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
#define getchar_unlocked getchar
inline int scan()
{
char c = getchar_unlocked();
int x = 0;
while (c < '0' || c > '9')
{
c = getchar_unlocked();
}
while (c >= '0' && c <= '9')
{
x = (x << 1) + (x << 3) + c - '0';
c = getchar_unlocked();
}
return x;
}
void phongbeo();
const int inf = 1e18;
const ll mod2 = 998244353;
const int mod1 = 998244353;
const ll base=67;
int add(int x,int y)
{
if(x+y>=mod2) x-=mod2;
if(x+y<0)x+=mod2;
return x+y;
}
struct icd
{
long double a;
int b;
};
struct ib
{
int a;
int b;
};
struct ic
{
int a, b, c;
};
struct id
{
int a, b, c, d;
};
struct ie
{
int a, b, c, d, e;
};
int n, m, s1, s2, s4, s3, sf, k, s5, s6, mx, s7, s8, s9, mx2, res, dem2 = 0, dem = 0, s33, dem3, dem4, mid, l2, r2, center;
int i, s10, s12,k1,k2,k3,s11,lim,w,l,r ;
int kk;
int el = 19;
main()
{
if(fopen(task".inp","r"))
{
fin(task);
fou(task);
}
NHP
/// cin>>s1;
// modwwe
phongbeo();
// checktime
}
vector<ib> v;
int ans[300007];
int c[300007];
int a[300007];
int cost1[300007];
int cost2[300007];
int pre[300007];
int fout[300007];
vector<ic> ask;
int cnt[300007];
set<int> s;
int S=316;
bool cmp(ic a,ic b)
{
if(a.a / S != b.a / S) return (a.a < b.a);
else if(!((a.a / S) & 1)) return (a.b < b.b);
else return (a.b > b.b);
}
bool cmp2(ib a,ib b)
{
return a.a>b.a;
}
int get(int x,int y)
{
if(x>y) return 0;
return (y-x+1)*(y+x)/2;
}
int f(int x)
{
///(n-1)*2+(n-2)*3+..+2*(n-1)+n
return x*(x+1)*(x+2)/6-x;
}
int cal(int x,int y)
{
int s=x*(x+1)/2*y;
return x*x*f(y)+s;
}
int get(int x,int dp1,int dp2,int take,int pos)
{
return x*(dp1*get(pos+1,pos+take)-dp2*take)+cal(x,take);
}
int solve(vector<ib>& v)
{
pre[0]=v[0].b;
int dp1,dp2;
dp1=dp2=0;
int sum=0,pos;
for(int i=1; i<v.size(); i++)
pre[i]=pre[i-1]+v[i].b;
for(int i=v.size()-1; i>=0; --i)
{
int take_front=0;
if(pre[i]%2==0) take_front=v[i].b/2;
else take_front=1+(v[i].b-1)/2;
sum+=get(v[i].a,dp1,dp2,take_front,pos);
dp1=dp1+v[i].a*take_front;
dp2=dp2+v[i].a*get(pos,pos+take_front-1);
pos+=take_front;
fout[i]=v[i].b-take_front;
}
for(int i=0; i<v.size(); i++)
{
sum+=get(v[i].a,dp1,dp2,fout[i],pos);
dp1=dp1+v[i].a*fout[i];
dp2=dp2+v[i].a*get(pos,pos+fout[i]-1);
pos+=fout[i];
}
return sum;
}
void addd(int x,int y)
{
cnt[c[x]]--;
if(cnt[c[x]]==0&&c[x]>=S)s.erase(c[x]);
c[x]+=y;
cnt[c[x]]++;
if(cnt[c[x]]==1&&c[x]>=S)s.insert(c[x]);
}
void go(int x,int y)
{
while(l<x)
{
addd(a[l],-1);
l++;
}
while(l>x)
{
l--;
addd(a[l],1);
}
while(r<y)
{
r++;
addd(a[r],1);
}
while(r>y)
{
addd(a[r],-1);
r--;
}
}
void phongbeo()
{
cin>>n>>m;
for(int i=1; i<=n; i++)
cin>>a[i];
for(int i=1; i<=m; i++)
{
cin>>l>>r;
ask.pb({l,r,i});
}
sort(ask.begin(),ask.end(),cmp);
l=1;
r=0;
for(auto x:ask)
{
go(x.a,x.b);
vector<ib>().swap(v);
for(int i=1; i<S; i++)
if(cnt[i]!=0)
v.pb({i,cnt[i]});
for(auto x:s)
v.pb({x,cnt[x]});
ans[x.c]=solve(v);
}
for(int i=1; i<=m; i++)
cout<<ans[i],down
}
Compilation message (stderr)
diversity.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
73 | main()
| ^~~~
diversity.cpp: In function 'int main()':
diversity.cpp:11:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | #define fin(x) freopen(x".inp","r",stdin)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
diversity.cpp:77:9: note: in expansion of macro 'fin'
77 | fin(task);
| ^~~
diversity.cpp:12:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define fou(x) freopen(x".ans","w",stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
diversity.cpp:78:9: note: in expansion of macro 'fou'
78 | fou(task);
| ^~~
# | 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... |