这题说的是个了n个数字 然后 在L 和R 区间内的数字的排列有多少种方案,

这里我们通过 将 这n长度的字符串 分成sqrt(n) 块然后 一个属性 他们的l 属于 那个快  以这个为第一关键字 ,然后 在按照R 为 第二个关键字,然后sort 每个查询区间

我们知道 当L他们属于一块内的时候 , R 是逐渐递增的 ,那么 转移了 sqrt(n)*a+n个 然后以此方案接近复杂度接近n^(1.5)

#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn = ;
const LL mod = ;
struct seg{
int L,R,id;
}P[maxn];
int b[maxn];
void gcd(LL a, LL b, LL &d, LL &x, LL &y ){
if(b==){
d=a; x=; y=;
}else{
gcd(b,a%b,d,y,x); y-=x*(a/b);
}
}
LL inv( LL a){
LL d, x,y;
gcd(a,mod,d,x,y);
return d==?(x+mod)%mod:-;
}
LL A[maxn],ans[maxn],num[maxn],aa,niA[maxn];
int C[maxn];
bool cmp(seg A, seg B){
return b[A.L]==b[B.L]?A.R<B.R:b[A.L]<b[B.L];
}
void update(int x, int add){
aa= (aa * niA[ num[x] ] )%mod;
num[x]+=add;
aa= (aa * A[num[x] ] )%mod;
}
void solve(int m){
memset(num,,sizeof(num));
aa=;
LL L=, R=;
for(int i=; i<m; i++){
while(R<P[i].R){
R++;
update(C[R],);
}
while(R>P[i].R){
update(C[R],-);R--;
}
while(L<P[i].L){
update(C[L],-);L++;
}
while(L>P[i].L){
L--;
update(C[L],);
}
ans[ P[i].id ]=(inv(aa)*A[ P[i].R-P[i].L+ ])%mod;
}
for(int i=; i<m; i++){
printf("%I64d\n",ans[i]);
}
}
int main()
{
A[]=; niA[]=inv();
for(LL i=; i<maxn-; i++){ A[i]=(A[i-]*i)%mod;
niA[i] = inv(A[i]);
}
int cas;
scanf("%d",&cas);
for(int cc=; cc<=cas; ++cc){
int n,m;
scanf("%d%d",&n,&m);
int boloc=sqrt(n+0.5);
for(int i=; i<=n; i++){
b[i]=(i-)/boloc+;
}
for(int i=; i<=n; i++)scanf("%d",&C[i]);
for(int i=; i<m; i++){
P[i].id=i; scanf("%d%d",&P[i].L,&P[i].R);
}
sort(P,P+m,cmp);
solve(m);
}
return ;
}

hdu5145 莫队算法的更多相关文章

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

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

  2. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  3. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  4. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  5. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  6. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  7. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  8. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  9. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

随机推荐

  1. python pytest测试框架介绍三

    之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...

  2. vue之单表输入绑定

    vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...

  3. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  4. MTU-TCP/IP协议栈-linux kernel-TCP丢包重传-UDP高性能-AI- ip数据报 tcp数据报

    1.IP协议首部 TCP报文段的首部  UDP分组结构   ip数据报 tcp数据报 UDP校验 w 报文长度该字段指定UDP报头和数据总共占用的长度.可能的最小长度是8字节,因为UDP报头已经占用了 ...

  5. spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法

    前两天在学spring boot的时候,出现了一个很奇怪的错误,因为是第一次使用spring boot,所以没想到会遇到这种莫名其妙的bug,即调用接口删除数据库中一条记录的时候,数据库中记录事实上以 ...

  6. Maven中的pom.xml配置文件详解

    原文:http://blog.csdn.net/u012152619/article/details/51485297 <project xmlns="http://maven.apa ...

  7. 《Nginx - location配置》

    一:Location 作用 - location 定位 ,也就是可以通过不同URL进行定位,可以很大的增加它配置的灵活性. 二:相关变量 示例: http://192.168.27.27/xxxx $ ...

  8. rem布局,flexible.js

    //author:caibaojian //website:http://caibaojian.com //weibo:http:weibo.com/kujian //这段js的最后面有两个参数记得要 ...

  9. zookeeper源码导入

    1 搭建步骤 1.1 到github中下载该项目 项目地址 https://github.com/apache/zookeeper.下载.zip包到本地解压. 解压后文件目录: 1.2 使用ant对源 ...

  10. HTTP 协议详解(转载)

    原文: http://kb.cnblogs.com/page/130970/#httpmeessagestructe HTTP协议详解 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, ...