题目地址:HDU 5145

莫队真的好奇妙。。

这种复杂度竟然仅仅有n*sqrt(n)。。。

裸的莫队分块,先离线。然后按左端点分块,按块数作为第一关键字排序。然后按r值作为第二关键字进行排序。

都是从小到大,能够证明这种复杂度仅仅有n*sqrt(n)。

然后进行块之间的转移。

代码例如以下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
#include <time.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
//#pragma comment(linker, "/STACK:1024000000")
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=30000+10;
int a[MAXN];
LL ans[MAXN], inv[MAXN], ha[MAXN];
struct node
{
int l, r, id, pos;
}fei[MAXN];
bool cmp(node x, node y)
{
return x.pos<y.pos||(x.pos==y.pos&&x.r<y.r);
}
LL Pow(LL x, int k)
{
LL ans=1;
while(k){
if(k&1) ans=ans*x%mod;
x=x*x%mod;
k>>=1;
}
return ans;
}
void init()
{
for(int i=0;i<=30000;i++){
inv[i]=Pow((LL)i,mod-2);
}
}
int main()
{
int t, n, m, i, j, l, r, k, Case=0;
LL res;
//freopen("1.txt","r",stdin);
//freopen("2.txt","w",stdout);
init();
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
k=sqrt(n*1.0);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(i=0;i<m;i++){
scanf("%d%d",&fei[i].l,&fei[i].r);
fei[i].id=i;
fei[i].pos=fei[i].l/k;
}
sort(fei,fei+m,cmp);
l=1;r=1;
res=1;
memset(ha,0,sizeof(ha));
ha[a[1]]=1;
for(i=0;i<m;i++){
while(r<fei[i].r){
r++;
ha[a[r]]++;
res=res*(r-l+1)%mod*inv[ha[a[r]]]%mod;
}
while(r>fei[i].r){
res=res*ha[a[r]]%mod*inv[r-l+1]%mod;
ha[a[r]]--;
r--;
}
while(l>fei[i].l){
l--;
ha[a[l]]++;
res=res*(r-l+1)%mod*inv[ha[a[l]]]%mod;
}
while(l<fei[i].l){
res=res*ha[a[l]]%mod*inv[r-l+1]%mod;
ha[a[l]]--;
l++;
}
ans[fei[i].id]=res;
}
for(i=0;i<m;i++){
printf("%I64d\n",ans[i]);
}
}
return 0;
}

HDU 5145 NPY and girls (莫队分块离线)的更多相关文章

  1. HDU 5145 NPY and girls 莫队+逆元

    NPY and girls Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more! ...

  2. HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)

    传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...

  3. HDU 5145 NPY and girls(莫队算法+乘法逆元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...

  4. HDU 5145 - NPY and girls

    题意: cases T(1≤T≤10) (0<n,m≤30000) (0<ai≤30000)    n个数ai 表示n个女孩所在教室 m次询问 [L,R](1 <= L <= ...

  5. hdu_5145_NPY and girls(莫队算法+组合)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 题意:给你n,m,共有n个女孩,标号为1—n,n个数xi表示第ith个女孩在第xi个教室,然后下 ...

  6. Bzoj 3236: [Ahoi2013]作业 莫队,分块

    3236: [Ahoi2013]作业 Time Limit: 100 Sec  Memory Limit: 512 MBSubmit: 1113  Solved: 428[Submit][Status ...

  7. [BZOJ 3585] mex 【莫队+分块】

    题目链接:BZOJ - 3585 题目分析 区间mex,即区间中没有出现的最小自然数. 那么我们使用一种莫队+分块的做法,使用莫队维护当前区间的每个数字的出现次数. 然后求mex用分块,将权值分块(显 ...

  8. BZOJ_3585_mex && BZOJ_3339_Rmq Problem_莫队+分块

    BZOJ_3585_mex && BZOJ_3339_Rmq Problem_莫队+分块 Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一 ...

  9. BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块

    BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块 Description Autumn和Bakser又在研究Gty的妹子序列了 ...

随机推荐

  1. 刷题总结——ball(ssoj)

    题目: 题目背景 SOURCE:NOIP2015-SHY-9 题目描述 Alice 与 Bob 在玩游戏.他们一共玩了 t 轮游戏.游戏中,他们分别获得了 n 个和 m 个小球.每个球上有一个分数.每 ...

  2. Excel VBA基础教程

    https://www.w3cschool.cn/excelvba/excelvba-basics.html Excel VBA语言基础 VBA语言的基础认识 详解VBA编程是什么 excel处理录制 ...

  3. POJ2594Treasure Exploration(最小路径覆盖,相交)

    Treasure Exploration Have you ever read any book about treasure exploration? Have you ever see any f ...

  4. jacoco功能测试覆盖率统计

    1.在java程序的启动脚本(或者tomcat)中加入javaagent参数-javaagent:/home/apps/jacocoagent.jar=destfile=/home/apps/jaco ...

  5. CentOS7开启docker远程访问

    在 CentOS 中没有 /etc/default/docker,另外在 CentOS7 中也没有找到 /etc/sysconfig/docker这个配置文件. 在 /usr/lib/systemd/ ...

  6. 让旧浏览器支持HTML5新增标签

    首先我们使用JS进行标签创建,为HTML文件创建我们需要的这几个HTML5标签. 接下来,我们需要使用CSS进行这几个HTML5标签的样式控制,这是因为,通过这种方法创建的新标签,默认是行内元素.   ...

  7. Delphi将图片缩放成指定大小

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  8. 洛谷—— P1503 鬼子进村

    https://www.luogu.org/problemnew/show/P1503 题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的 ...

  9. codevs贪吃的九头龙

    传说中的九头龙是一种特别贪吃的动物.虽然名字叫“九头龙”,但这只是说它出生的时候有九个头,而在成长的过程中,它有时会长出很多的新头,头的总数会远大于九,当然也会有旧头因衰老而自己脱落.有一天,有M 个 ...

  10. golang实现dns域名解析(一)

    本文将详细讲解如何用go语言一步一步实现dns域名解析的过程,并简单介绍点dns有关的知识,直接开始正题吧. 首先我们要了解dns解析的过程,没有了解的请看这里DNS入门(转)很详细.扫盲结束后,我们 ...