hdu5145 莫队算法
这题说的是个了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 莫队算法的更多相关文章
- HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- NPY and girls-HDU5145莫队算法
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...
- Codeforces 617E XOR and Favorite Number(莫队算法)
题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...
随机推荐
- C语言中的数组的使用——混乱的内存管理
在C语言中想要创建数组只能自己malloc或者calloc,数组复制则是memcpy. 这样创建出来的数组在调用时是不会检测数组边界的,即你声明了一个长度为5的数组,却可以访问第6个位置……也可以给第 ...
- yii---获取当前sql语句
我们在用YII开发项目的时候,会查看当前执行的SQL语句来排查错误,那么YII该如何获取当前的SQL语句呢? $list = user::find()->where(['title'=>$ ...
- 对Aspose.Cells Excel文件操作的扩展
工作中对Excel操作的需求很是常见,今天其他项目组的同事在进行Excel数据导入时,使用Aspose.Cells Excel 遇到了些问题. 刚好闲来不忙,回想自己用过的Excel文件操作,有NPO ...
- Pycharm中如何使用科学计算库
1.简便起见 比起麻烦的安装各种库,我们选择最方便的Anaconda的conda或pip(兼容支持)安装相关库. Pycharm本身缺少numpy和matplotlib这些库,而另一个Python的开 ...
- Python的一个命名空间冲突,关于from-import机制
from os import * #import os def foo(): a = listdir("trainingDigits") b = open("traini ...
- 疯狂java讲义 第三版 笔记
java7新加特性: 0B010101 二进制数 int c=0B0111_1111; 数值中使用下划线分隔 switch 支持String类型 字符串常量放在常量池 String s0 ...
- docker and ssh issues
docker run -i -t qcdatainc/centos-jdkyum update yum install openssh-server ssh -vvv -p 222 jenkins@1 ...
- 【python-opencv】几何变换
"""几何变换-缩放""" img = cv.imread(r'pictures\family.jpg') ""&quo ...
- 实现一个2008serve的IIS的虚拟目录(通过网络路径(UNC)的形式,共享在另外一个2008服务器上
转载:http://www.cnblogs.com/top5/archive/2012/12/10/2812133.html 目的:实现一个2008serve的IIS的虚拟目录(通过网络路径(UNC) ...
- Linux文件目录介绍及文件颜色区别
文件颜色代表含义: 蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件: 白色表示其他文件: 黄色是设备文件,包括block, char, fifo. 常见目录解释 Linu ...