HDU 5145 NPY and girls (莫队分块离线)
题目地址: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 (莫队分块离线)的更多相关文章
- HDU 5145 NPY and girls 莫队+逆元
NPY and girls Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more! ...
- HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...
- HDU 5145 NPY and girls(莫队算法+乘法逆元)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...
- 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 <= ...
- hdu_5145_NPY and girls(莫队算法+组合)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 题意:给你n,m,共有n个女孩,标号为1—n,n个数xi表示第ith个女孩在第xi个教室,然后下 ...
- Bzoj 3236: [Ahoi2013]作业 莫队,分块
3236: [Ahoi2013]作业 Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1113 Solved: 428[Submit][Status ...
- [BZOJ 3585] mex 【莫队+分块】
题目链接:BZOJ - 3585 题目分析 区间mex,即区间中没有出现的最小自然数. 那么我们使用一种莫队+分块的做法,使用莫队维护当前区间的每个数字的出现次数. 然后求mex用分块,将权值分块(显 ...
- BZOJ_3585_mex && BZOJ_3339_Rmq Problem_莫队+分块
BZOJ_3585_mex && BZOJ_3339_Rmq Problem_莫队+分块 Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一 ...
- BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块
BZOJ_3809_Gty的二逼妹子序列 && BZOJ_3236_[Ahoi2013]作业 _莫队+分块 Description Autumn和Bakser又在研究Gty的妹子序列了 ...
随机推荐
- input最大长度限制问题
<input type="text" maxlength="5" /> //可以 <input type="number" ...
- 刷题总结——Human Gene Functions(hdu1080)
题目: Problem Description It is well known that a human gene can be considered as a sequence, consisti ...
- 给某个li标签家样式
HTML: <div class="tabs clearfix"> <ul id="der"> <li ><a hre ...
- K3Cloud调用存储过程
SQLScriptServiceHelper.GetDataFromStoredProc( this.Context, DatabaseType.Oracle, "/*dialect*/TJ ...
- codeforces 449D DP+容斥
Jzzhu and Numbers Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- SPOJ CIRU The area of the union of circles
You are given N circles and expected to calculate the area of the union of the circles ! Input The f ...
- hdu 1077(单位圆覆盖问题)
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 转载:Flappy Bird源代码 win32 console版
#include"StdAfx.h" #include<stdio.h> #include<stdlib.h> #include<conio.h> ...
- WinRT 开发:在 MVVM 模式中,关于绑定的几处技巧
以下会提到三个绑定的技巧,分别是 在 ListView 中为 ListViewItem 的 MenuFlyout 绑定 Command: 在 ListView 的 事件中绑定所选择项目,即其 Sele ...
- [原创][FPGA][IP-Core]altlvds_tx & altlvds_rx
1. 概述 Alter公司的QuartusII软件提供了LVDS发送和接收的IP核供我们使用,其在本质上可以理解为并行-串行数据的转换器.其在官方文档(见附件)上也这样说过.其中的应用场景有告诉AD/ ...