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前缀和为某数的个数并增加或减 ...
随机推荐
- 【CF799E】Aquarium decoration 线段树
[CF799E]Aquarium decoration 题意:有n个物品,小A和小B各自喜欢其中的某些物品,一件物品可能既被小A喜欢又被小B喜欢,也可能既不被小A喜欢又不被小B喜欢.每个物品都有一个价 ...
- [HTML5]移动平台的HTML5开发框架
jQuery Mobile http://jquerymobile.com/ jQTouch http://jqtouch.com/ DHTMLX Touch http://dhtmlx.com/to ...
- SDWebImage第三方库使用注意的一些问题
1.利用"UIImageView+WebCache.h"加载图片数据 例如: UIImage *placeHolderImg = [UIImage imageNamed:@&quo ...
- 对Aspose.Cells Excel文件操作的扩展
工作中对Excel操作的需求很是常见,今天其他项目组的同事在进行Excel数据导入时,使用Aspose.Cells Excel 遇到了些问题. 刚好闲来不忙,回想自己用过的Excel文件操作,有NPO ...
- Saltstack之SSH
salt-minion也可以不安装通过在master安装salt-ssh 1,安装 yum -y install salt-ssh 2,配置salt的花名册 vim /etc/salt/roster ...
- POJ-2081 Terrible Sets(暴力,单调栈)
Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4113 Accepted: 2122 Descrip ...
- hdu4998 Rotate【计算几何】
Noting is more interesting than rotation! Your little sister likes to rotate things. To put it easi ...
- UVA 11768 - Lattice Point or Not
首先本题需要用到扩展欧几里得算法…… 关于exgcd算法的一点简略证明: 那么,对于函数exgcd(a,b)=(d,x,y),其中d满足d=gcd(a,b); (x,y)满足ax+by=d; 则exg ...
- 设计模式之——flyweight模式
flyweight模式,又叫做享元模式. 顾名思义,享元模式就是共享一个元素. 百度百科 解释为: 享元模式(英语:Flyweight Pattern)是一种软件设计模式.它使用共享物件,用来尽可能减 ...
- 站内SEO规范
一:文章的原创性 1.修改文章的标题,尽可能的选择新颖的,符合用户搜索习惯的标题. 2.修 改首段内容或自己动手来添加首段文字信息. 3.文章中图片ALT属性的修改和添加. 4.在不影响阅读的情况下, ...