您的位置:首页 > 其它

BZOJ 2038 [2009国家集训队]小Z的袜子(hose)

2016-10-01 17:55 337 查看
莫队

裸莫队算法(orz莫队)

详见http://www.cnblogs.com/hzf-sbit/p/4056874.html

答案,要开,long long!!!

#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 50005
#define ll long long
using namespace std;
struct query
{
int l, r, id;
}q
;
int n, m, c
, belong
, s
;
ll print
[2], ans;
bool cmp(query a, query b)
{
if(belong[a.l]==belong[b.l])return  a.r<b.r;
else return a.l<b.l;
}
void init_block()
{
int block = (int)sqrt(n);
for(int i = 1; i <= n; i++)
belong[i]=i/block+1;
}
void update(int col, int f)
{
ans-=1ll*(s[col]-1)*s[col];
s[col]+=f;
ans+=1ll*(s[col]-1)*s[col];
}
ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
int main()
{
scanf("%d%d",&n,&m);
init_block();
for(int i = 1; i <= n; i++)
scanf("%d",&c[i]);
for(int i = 1; i <= m; i++)
scanf("%d%d",&q[i].l,&q[i].r),q[i].id=i;
sort(q+1,q+1+m,cmp);
int l = 1, r = 0;
for(int i = 1; i <= m; i++)
{
for(;l<q[i].l;l++)
update(c[l],-1);
for(;l>q[i].l;l--)
update(c[l-1],1);
for(;r<q[i].r;r++)
update(c[r+1],1);
for(;r>q[i].r;r--)
update(c[r],-1);
int id=q[i].id;
if(!ans)
{
print[id][0]=0;
print[id][1]=1;
continue;
}
print[id][0]=ans;
print[id][1]=1ll*(r-l+1)*(r-l);
ll g = gcd(print[id][0],print[id][1]);
print[id][0]/=g;
print[id][1]/=g;
}
for(int i = 1; i <= m; i++)
printf("%lld/%lld\n",print[i][0],print[i][1]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: