莫队算法的两种情况

1.求相同的数有多少

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。
Source
bzoj 2038: [2009国家集训队]小Z的袜子(hose)
code:

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
struct question
{
     int l,r;
     long long a,b;
     int x;
}ask[50001];
int a[50001],s[50001];
int pos[50001];
long long ans;
inline bool cmp1(question x,question y)
{
     if(pos[x.l]==pos[y.l])
     {
          if(x.r<y.r)
               return true;
          return false;
     }
     if(x.l<y.l)
          return true;
     return false;
}
inline bool cmp2(question x,question y)
{
     if(x.x<y.x)
          return true;
     return false;
}
inline void update(int x,int xx)
{
     ans-=s[a[x]]*s[a[x]];
     s[a[x]]+=xx;
     ans+=s[a[x]]*s[a[x]];
}
inline long long gcd(long long x,long long y)
{
     long long m=x%y;
     while(m!=0)
     {
          x=y;
          y=m;
          m=x%y;
     }
     return y;
}
int main()
{
     int n,m;
     scanf("%d%d",&n,&m);
     int i;
     int space=sqrt(n);
     for(i=1;i<=n;i++)
          scanf("%d",&a[i]);
     for(i=1;i<=m;i++)
     {
          scanf("%d%d",&ask[i].l,&ask[i].r);
          ask[i].x=i;
     }
     for(i=1;i<=n;i++)
          pos[i]=(i-1)/space+1;
     sort(ask+1,ask+1+m,cmp1);
     int l=1,r=0;
     for(i=1;i<=m;i++)
     {
          while(r<ask[i].r)
          {
               r++;
               update(r,1);
          }
          while(r>ask[i].r)
          {
               update(r,-1);
               r--;
          }
          while(l<ask[i].l)
          {
               update(l,-1);
               l++;
          }
          while(l>ask[i].l)
          {
               l--;
               update(l,1);
          }
          ask[i].a=ans-(ask[i].r-ask[i].l+1);
          ask[i].b=(long long)(ask[i].r-ask[i].l+1)*(ask[i].r-ask[i].l);
          long long k=gcd(ask[i].a,ask[i].b);
          ask[i].a/=k;
          ask[i].b/=k;
          if(ask[i].a==0)
               ask[i].b=1;
     }
     sort(ask+1,ask+1+m,cmp2);
     for(i=1;i<=m;i++)
          printf("%lld/%lld\n",ask[i].a,ask[i].b);
     return 0;
}

2.求不同的数有多少

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
Given a sequence of integers a1, a2, …, an and q pairs of integers (l1, r1), (l2, r2), …, (lq, rq), find count(l1, r1), count(l2, r2), …, count(lq, rq) where count(i, j) is the number of different integers among a1, a2, …, ai, aj, aj + 1, …, an.
输入描述:
The input consists of several test cases and is terminated by end-of-file.
The first line of each test cases contains two integers n and q.
The second line contains n integers a1, a2, …, an.
The i-th of the following q lines contains two integers li and ri.
输出描述:
For each test case, print q integers which denote the result.
示例
输入
3 2
1 2 1
1 2
1 3
4 1
1 2 3 4
1 3
输出
2
1
3
备注:

  • 1 ≤ n, q ≤ 105
  • 1 ≤ ai ≤ n
  • 1 ≤ li, ri ≤ n
  • The number of test cases does not exceed 10.

题目来源:
https://ac.nowcoder.com/acm/contest/20322/J
code:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 1e5 + 100;
typedef long long ll;
int n, a[maxn];
int q;
int block[maxn];
int cnt[maxn];
int cnt_dif;

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

struct Query
{
    int l, r, id;
    int ans;
} querys[maxn];

bool cmp(const Query & x, const Query & y)
{
    if (block[x.l] == block[y.l]) return x.r < y.r;
    return x.l < y.l;
}
bool cmp_id(const Query & x, const Query & y)
{
    return x.id < y.id;
}

void update(int p, int add)
{
    if (add == -1)
    {
        if (cnt[a[p]] == 1) --cnt_dif;
        cnt[a[p]]--;
    }
    else
    {
        if (cnt[a[p]] == 0) ++cnt_dif;
        cnt[a[p]]++;
    }
}


int main()
{
    while (scanf("%d%d", &n, &q) == 2)
    {
        memset(cnt, 0, sizeof(int) * (1 + n));
        cnt_dif = 0;
        for (int i = 1; i <= n; ++i)
        {
            a[i] = read();
            if (cnt[a[i]] == 0) ++cnt_dif;
            cnt[a[i]]++;
        }
        int block_size = (int)sqrt(n);
        for (int i = 1; i <= n; ++i)
            block[i] = (i - 1) / block_size + 1;
        for (int i = 1; i <= q; ++i)
        {
            querys[i].l = read();
            querys[i].r = read();
            if (querys[i].l >= querys[i].r) querys[i].r = querys[i].l + 1;
            querys[i].id = i;
        }

        sort(querys + 1, querys + 1 + q, cmp);

        int l = 1, r = 2;
        for (int i = 1; i <= q; ++i)
        {
            for (; r < querys[i].r; ++r) update(r, -1);
            for (; r > querys[i].r; --r) update(r - 1, 1);
            for (; l < querys[i].l; ++l) update(l + 1, 1);
            for (; l > querys[i].l; --l) update(l, -1);
            querys[i].ans = cnt_dif;
        }
        sort(querys + 1, querys + 1 + q, cmp_id);
        for (int i = 1; i <= q; ++i) printf("%d\n", querys[i].ans);
    }

    return 0;
}

3.个人看法

第一次接触莫队算法,复杂度为n*sqrt(n),参考了一些大佬的博客,勉强弄懂了大致的想法。
纵观两道题,都是分块加暴力处理,分块就是将一个区间分成几个小区间,暴力处理就是用每次循环用update函数处理(我理解的),update函数因题而异,没有固定模板,但用法大致相同,可以理解下这两道题的异同,找到怎么用调用update函数的方法,总结出规律,以后遇到这种类型的题就迎刃而解了。难点就在于update函数的写法,说实话,第一道题的update函数的写法我还没完全弄懂,只是知道他怎么想的,但我无法证明(我真菜。。。)。

===========================================
现在懂了。
证明:
在这里插入图片描述

4.参考博客

https://blog.csdn.net/lqybzx/article/details/41761769
https://ac.nowcoder.com/acm/discuss/blogs?tagId=143087(该链接的第一个题解中的莫队解法)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值