您的位置:首页 > 其它

莫队算法一(小z的袜子)

2016-08-15 21:00 204 查看
莫队算法是一种离线的区间查询的算法,复杂度为n*sqrt(n)。他的具体思路是将需要查询的区间分成sqrt(N)块,然后将询问的区间按照L/sqrt(N)的大小排序,我们再按照排好序的区间不断的维护答案,对于每次已排好序的询问L最多的移动次数不会超过sqrt(N),R区间是一直递增的(对于L/sqrt(N)相等的询问区间说)。一般来说莫对算法处理的查询区间大小和范围约为(1e5).

例一:

小Z的袜子(hose)

Time Limit: 20000MS Memory Limit: 265216KB 64bit IO Format: %lld & %llu
Submit Status

Description点击打开链接

作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……
具体来说,小Z把这N只袜子从1到N编号,然后从编号L到R(L 尽管小Z并不在意两只袜子是不是完整的一双,甚至不在意两只袜子是否一左一右,他却很在意袜子的颜色,毕竟穿两只不同色的袜子会很尴尬。
你的任务便是告诉小Z,他有多大的概率抽到两只颜色相同的袜子。当然,小Z希望这个概率尽量高,所以他可能会询问多个(L,R)以方便自己选择。

Input

输入文件第一行包含两个正整数N和M。N为袜子的数量,M为小Z所提的询问的数量。接下来一行包含N个正整数Ci,其中Ci表示第i只袜子的颜色,相同的颜色用相同的数字表示。再接下来M行,每行两个正整数L,R表示一个询问。

Output

包含M行,对于每个询问在一行中输出分数A/B表示从该询问的区间[L,R]中随机抽出两只袜子颜色相同的概率。若该概率为0则输出0/1,否则输出的A/B必须为最简分数。(详见样例)

Sample Input

6 4
1 2 3 3 3 2
2 6
1 3
3 5
1 6


Sample Output

2/5
0/1
1/1
4/15
【样例解释】
询问1:共C(5,2)=10种可能,其中抽出两个2有1种可能,抽出两个3有3种可能,概率为(1+3)/10=4/10=2/5。
询问2:共C(3,2)=3种可能,无法抽到颜色相同的袜子,概率为0/3=0/1。
询问3:共C(3,2)=3种可能,均为抽出两个3,概率为3/3=1/1。
注:上述C(a, b)表示组合数,组合数C(a, b)等价于在a个不同的物品中选取b个的选取方案数。
【数据规模和约定】
30%的数据中 N,M ≤ 5000;
60%的数据中 N,M ≤ 25000;
100%的数据中 N,M ≤ 50000,1 ≤ L < R ≤ N,Ci ≤ N。


Hint

Source

2009国家集训队

解法(莫队ppt上的解法):

令Ti表示数i在 中的出现次数,容易看出:



我们用莫队算法维护答案即可。

AC:

#include<algorithm>
#include<iostream>
#include<cmath>
#include<map>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<stdio.h>
using namespace std;
#define ll long long int
const int maxn=400008;
int tmp;
struct query
{
int l,r,id;
bool operator < (const query &a) const
{
return r<a.r;
};
}q[maxn];
ll gcd(ll a,ll b)
{
if(b==0) return a;
else return gcd(b,a%b);
}
struct aa
{
ll a,b;
void change()
{
ll t=gcd(a,b);
a=a/t;
b=b/t;
}

}ans[maxn];
int c[maxn];
ll num[maxn];
vector<query>qq[250];
int main()
{
int n,m;
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)==2)
{
tmp=(int)sqrt(n);
for(int i=0;i<=tmp+1;i++) qq[i].clear();
for(int i=1;i<=n;i++) scanf("%d",&c[i]);
for(int i=0;i<m;i++)
{
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
qq[q[i].l/tmp].push_back(q[i]);
}
int tp,l,r;
tp=0,l=1;r=0;
for(int i=0;i<=tmp;i++)
{
if(qq[i].empty()) continue;
else
{
memset(num,0,sizeof(num));
tp=0; r=l-1;
sort(qq[i].begin(),qq[i].end());
for(int k=0;k<qq[i].size();k++)
{
while(l<qq[i][k].l)
{
tp-=num[c[l]]*num[c[l]];
num[c[l]]--;
tp+=num[c[l]]*num[c[l]];
l++;
}
while(l>qq[i][k].l)
{
l--;
tp-=num[c[l]]*num[c[l]];
num[c[l]]++;
tp+=num[c[l]]*num[c[l]];
}
while(r<qq[i][k].r)
{
r++;
tp-=num[c[r]]*num[c[r]];
num[c[r]]++;
tp+=num[c[r]]*num[c[r]];
}
ans[qq[i][k].id].a=tp-(r-l+1);
ans[qq[i][k].id].b=(ll)(r-l+1)*(r-l) ;
ans[qq[i][k].id].change();
}
}
}
for(int i=0;i<m;i++)
{
printf("%lld/%lld\n",ans[i].a,ans[i].b);
}
}
}

例二:

Sona

Time Limit: 5000MS Memory Limit: 65535KB 64bit IO Format:
Submit Status

Description


Sona , Maven
of the Strings . Of cause, she can play the zither. 

Sona can't speak but she can make fancy music. Her music can attack, heal, encourage and enchant.

There're an ancient score(乐谱). But because it's
too long, Sona can't play it in a short moment. So Sona decide
to just play a part of it and revise it.

A score is composed of notes. There are 109 kinds
of notes and a score has 105 notes
at most.

To diversify Sona's
own score, she have to select several parts of it. The energy of each part is calculated like that:

Count the number of times that each notes appear. Sum each of the number of times' cube together. And the sum is the energy.

You should help Sona to calculate out the energy
of each part.

Input

This problem contains several cases. And this problem provides 2 seconds to run. 

The first line of each case is an integer N (1 ≤ N ≤ 10^5), indicates the number of notes. 

Then N numbers followed. Each number is a kind of note. (1 ≤ NOTE ≤ 10^9) 

Next line is an integer Q (1 ≤ Q ≤ 10^5), indicates the number of parts. 

Next Q parts followed. Each part contains 2 integers Li and Ri, indicates the left side of the part and the right side of the part.

Output

For each part, you should output the energy of that part.

Sample Input

8
1 1 3 1 3 1 3 3
4
1 8
3 8
5 6
5 5


Sample Output

128
72
2
1


Hint



Source

XadillaX
AC:

#include<algorithm>
#include<iostream>
#include<cmath>
#include<map>
#include<string.h>
#include<cstring>
#include<vector>
#include<queue>
#include<stdio.h>
using namespace std;
#define ll long long int
const int maxn=200008;
int tmp;
struct query
{
int l,r,id;
bool operator < (const query &a) const
{
if( l/tmp == a.l/tmp) return r<a.r;
else return l/tmp<a.l/tmp;
};
}q[maxn];
ll ans[maxn];
int c[maxn];
ll num[maxn];
struct ss
{
int id;
int v;
bool operator < (const ss &a) const
{
return v<a.v;
}
}c1[maxn];
int main()
{
int n,m;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)==1)
{
memset(num,0,sizeof num);
for(int i=0;i<n;i++)
{
scanf("%d",&c1[i].v);
c1[i].id=i+1;
}

sort(c1,c1+n);
int v=0;
c[c1[0].id]=v;
for(int i=1;i<n;i++)
{
if(c1[i].v>c1[i-1].v)v++;
c[c1[i].id]=v;
}
scanf("%d",&m);
for(int i=0;i<m;i++)
{
int l1,r1;
scanf("%d%d",&q[i].l,& q[i].r);
q[i].id=i;
}
tmp=(int)sqrt((double)n);
sort(q,q+m);
int l,r;
ll tp=0;
l=1; r=0;
for(int i=0;i<m;i++)
{
while(l<q[i].l)
{
tp-=num[c[l]]*num[c[l]]*num[c[l]];
num[c[l]]--;
tp+=num[c[l]]*num[c[l]]*num[c[l]];;
l++;
}
while(l>q[i].l)
{
l--;
tp-=num[c[l]]*num[c[l]]*num[c[l]];
num[c[l]]++;
tp+=num[c[l]]*num[c[l]]*num[c[l]];
}
while(r<q[i].r)
{
r++;
tp-=num[c[r]]*num[c[r]]*num[c[r]];
num[c[r]]++;
tp+=num[c[r]]*num[c[r]]*num[c[r]];

}
while(r>q[i].r)
{
tp-=num[c[r]]*num[c[r]]*num[c[r]];
num[c[r]]--;
tp+=num[c[r]]*num[c[r]]*num[c[r]];
r--;
}
ans[q[i].id]=tp;
}
for(int i=0;i<m;i++)
{
printf("%I64d\n",ans[i]);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: