hdu 5145 NPY and girls (莫队算法)
题意:有一个长度为n的数字序列,m次询问一个区间l-r中数字重新排列的方案数(mod 10^9+7)。
明显的莫队算法,只需要排序,然后预处理一下逆元就可以了。
所谓的莫队算法,最初版本是求曼哈顿距离最小生成树的。但是现在一般是分块排序,这样就可以做到n*sqrt(n)的复杂度,对于这个可以大概yy一下。如果对于每个区间可以O(1)的扩展区间的话,那么莫队算法就可以派上用场了。。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;const int maxn = 30300;const int MOD = 1000000007;int MID;int a[maxn], p[maxn];
int cnt[maxn];struct query
{int l, r, id;bool operator < (const query& rhs) const{return l / MID < rhs.l / MID || (l / MID == rhs.l / MID && r < rhs.r);}
}q[maxn];long long Inv(long long x, long long mod)///mod为质数
{long long r, y;for(r = 1, y = mod - 2; y; x = x * x % mod, y >>= 1)(y & 1) && (r = r * x % mod);return r;
}int inv[maxn];void init()
{inv[0] = 1;for(int i = 1; i < maxn; i ++){inv[i] = Inv(i, MOD);}
}int main()
{int T, n, m;init();scanf("%d", &T);while(T --){scanf("%d%d", &n, &m);for(int i = 1; i <= n; i ++)scanf("%d", &a[i]);a[0] = 0;for(int i = 0; i < m; i ++){scanf("%d%d", &q[i].l, &q[i].r);q[i].id = i;}MID = sqrt(n + 0.0);sort(q, q + m);int L = 1, R = 1;int ans = 1, tot = 1;memset(cnt, 0, sizeof(cnt));cnt[a[1]] ++;for(int i = 0; i < m; i ++){while(R < q[i].r){R ++; tot ++; cnt[a[R]] ++;ans = 1ll * ans * inv[cnt[a[R]]] % MOD;ans = 1ll * ans * tot % MOD;}while(R > q[i].r){ans = 1ll * ans * cnt[a[R]] % MOD;ans = 1ll * ans * inv[tot] % MOD;R --; tot --; cnt[a[R + 1]] --;}while(L < q[i].l){ans = 1ll * ans * cnt[a[L]] % MOD;ans = 1ll * ans * inv[tot] % MOD;L ++; tot --; cnt[a[L - 1]] --;}while(L > q[i].l){L --; tot ++; cnt[a[L]] ++;ans = 1ll * ans * inv[cnt[a[L]]] % MOD;ans = 1ll * ans * tot % MOD;}p[q[i].id] = ans;}for(int i = 0; i < m; i ++){printf("%d\n", p[i]);}}
}
hdu 5145 NPY and girls (莫队算法)相关推荐
- 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 4358(莫队算法+dfs序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 解题思路:用dfs求出整棵树的dfs序列,这样以u为根节点的子树就转化到相对应的区间上了.由于是 ...
- hdu 5213(容斥原理+莫队算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5213 莫队算法是离线处理一类区间不修改查询类问题的算法.就是如果你知道了[L,R]的答案.你可以在O( ...
- hdu 5381 2015多校第八场 莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5381 还没学过莫队算法....网上也找不到莫队算法的论文,只能勉强看着别人的代码打下来... 稍微介绍 ...
- Hdu 6534 Chika and Friendly Pairs 莫队算法+树状数组
题目链接 题意求给区间[L,R]中有少对(i,j)满足i<j且abs(a[i]-a[j])<=k. 首先来说暴力的方法就是离散化,然后用树状数组来维护,但是m次询问,m很大,所以说一定会t ...
- [HDU](6333)Problem B. Harvest of Apples ---- 数论+莫队算法
Problem Description There are n apples on a tree, numbered from 1 to n. Count the number of ways to ...
- NBUT 1457 Sona(莫队算法+离散化)
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- XOR and Favorite Number CF340E 莫队算法
题目链接 题意:求给定询问区间[L,R]问有多少连续区间异或值等k,多次询问可以离线. a[i]^a[i+1]^a[i+2]^a[n]=(a[1]^a[2]^a[3]^...^a[i-1])^(a[1 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)【莫队算法裸题学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 9894 Solved: 4561 [Su ...
最新文章
- Genome-scale de novo assembly using ALGA 使用ALGA进行 基因组规模的从头组装
- Vmware 下安装Arch Linux
- 使用Arduino模块实施无线信号的重放攻击
- rabbitmq-5-案例1-简单的案例
- 若5G的速度不够:那6G和16K是必然么?
- java显示一个钟表_中秋团圆日,月相表来一个呗~
- python序列元素的编号称为_Python序列
- laravel中的自定义函数的加载和第三方扩展库加载
- android /data/data/数据作用,android 清除data/data/ 下其他应用的数据
- java静态类堆栈_Java回归学习-面向对象内存分析-堆栈
- linux下往外ping不通-出现ping: unknown host www.baidu.com
- 如何去掉CAD教育版打印戳记
- 计算机毕业设计Java乡村基层政务管理系统(源码+系统+mysql数据库+Lw文档)
- Codevs 4909 寂寞的堆
- 解决output directory already exists
- Android设置默认输入法
- HTML5中图片路径的几种使用方式
- 跨境电商如何找代运营公司?星之河
- 理解微分方程和线性代数的联系
- 高通骁龙800系列处理器规格型号及代表机型大全
热门文章
- one 主格 复数 宾格_主格、宾格、名词所有格
- 哪个牌子的运动耳机比较好、运动蓝牙耳机排行榜
- 我会java什么梗_抖音不会真的有人什么梗 不会真的有人不会吧不会吧别恶心我梗意思及出处...
- java进行参数快捷校验,ehi-verification
- hibernate 出现 org.hibernate.MappingException: entity class not found 的解决办法
- 程序员是年轻人最向往的职业,突然觉得我们这个职业立马高大上了
- 快码加编——代码生成神器
- tensorflow-tf基础
- 忆暖行动|“ 还可以留一点做成柿饼,做法也很简单,就是挑硬柿子把皮削掉,用开水烫个几秒”
- 转转转转转转转转转转转转转转转转转转转转转转转转转