【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples
http://acm.hdu.edu.cn/showproblem.php?pid=6333
莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线 2.可以O(1)从区间(L,R)更新到(L±1,R±1)就能直接套板子了
这道题不是区间算法,但是有递推式:

把它看成区间更新orz
所以可以莫队orz
#define _CRT_SECURE_NO_WARNINGS
#include <cmath>
#include <iostream>
#include <stdio.h>
#include<algorithm>
#include <map>
#include <cstring>
#include <time.h>
using namespace std;
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
const int maxn = 1e5 + ;
const long long mod = 1e9 + ;
map<int, int> mmp;
typedef long long ll;
struct node {
int l, r, id; }Q[maxn];
ll fac[maxn], infac[maxn];
long long ans[maxn];
long long num[maxn];
int a[maxn], pos[maxn];
int n, m, k;
int L = , R = ;
long long Ans = ; ll cal(ll x) {
ll res = ;
int k = mod - ;
while (k) {
if (k & ) {
res *= x;
res %= mod;
}
x *= x;
x %= mod;
k >>= ;
}
return res;//cal(x^ mod)
}
void init() {
fac[] = ;
rep(i, , maxn - )fac[i] = (fac[i-] * i)%mod;
rep(i, , maxn - )infac[i] = cal(fac[i]);
}
ll C(ll a, ll b)
{
return 1ll * fac[a] * infac[b] % mod * infac[a - b] % mod;
}
bool cmp(node a, node b) {
if (pos[a.r] == pos[b.r])
return a.l < b.l;
return pos[a.r] < pos[b.r]; }
void add(int x) { //num[a[x]]++;
//if (num[a[x] - 1] == 0 && num[a[x] + 1] == 0)Ans++;
//else if (num[a[x] - 1] && num[a[x] + 1])Ans--;
Ans += C(n, m); }
void addl(int x) { }
void del(int x) {
//num[a[x]]--;
//if (num[a[x] - 1] == 0 && num[a[x] + 1] == 0)Ans--;
//else if (num[a[x] - 1] && num[a[x] + 1])Ans++;
Ans -= C(n, m + );
} int smain() {
int t; scanf("%d", &t);
init();
Ans = ;
mmm(num, );
L = , R = ; int sz = sqrt(1e5);
for (int i = ; i <= 1e5; i++) {
pos[i] = i / sz;
} rep(i,,t) {
scanf("%d%d", &Q[i].l, &Q[i].r);
Q[i].id = i;
}
sort(Q + , Q + + t, cmp);
Ans = ;//S(m,n
L = Q[].l; R = -;
rep(i,,t){
while (L < Q[i].l) {
//del(L);
Ans = (2ll * Ans - C(L, R) + mod) % mod;
++L;
} while (L > Q[i].l) {
--L;
//addl(L);
Ans = ((Ans + C(L, R)) *infac[]%mod) % mod;
} while (R < Q[i].r) {
++R;
//add(R);
Ans = (Ans+C(L, R))%mod;
}
while (R > Q[i].r) {
//del(R);
Ans = (Ans-C(L, R)+mod)%mod;
--R; } ans[Q[i].id] = Ans;
}
rep(i,,t)printf("%lld\n", ans[i]);
return ;
}
/* 6
60522 25373
36426 3283
48772 42553
33447 12441
3497 2182
7775 4025 */
#define ONLINE_JUDGE
int main() {
//ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
FILE *myfile;
myfile = freopen("C:\\Users\\acm-14\\Desktop\\test\\b.in", "r", stdin);
if (myfile == NULL)
fprintf(stdout, "error on input freopen\n");
FILE *outfile;
outfile = freopen("C:\\Users\\acm-14\\Desktop\\test\\out.txt", "w", stdout);
if (outfile == NULL)
fprintf(stdout, "error on output freopen\n");
long _begin_time = clock();
#endif
smain();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
cin >> n;
return ;
}
【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples的更多相关文章
- 【题解】BZOJ4241: 历史研究(魔改莫队)
[题解]BZOJ4241: 历史研究(魔改莫队) 真的是好题啊 题意 给你一个序列和很多组询问(可以离线),问你这个区间中\(\max\){元素出现个数\(\times\)元素权值} IOI国历史研究 ...
- 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- 【莫队算法】【权值分块】bzoj3339 Rmq Problem
如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #def ...
- hdu 5373 The shortest problem(杭电多校赛第七场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5373 The shortest problem Time Limit: 3000/1500 MS (J ...
- hdu 5328 Problem Killer(杭电多校赛第四场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重 ...
- hdu 5319 Painter(杭电多校赛第三场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319 Painter Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 5326 Work(杭电多校赛第三场)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Work Time Limit: 2000/1000 MS (Java/Others) M ...
- 2019杭电多校赛第九场 Rikka with Mista
Problem Description Rikka is a fervent fan of JoJo's Bizarre Adventure. As the last episode of Golde ...
随机推荐
- Mac Apache Tomcat WebServer 服务器配置
1.配置准备工作 1)配置服务器准备工作 在 Finder 中创建一个 "workspace" 的文件夹,可直接创建在 /Users/QianChia(当前用户名)目录下. 下载相 ...
- Swift 栈和堆
前言 Swift 中的数据类型分为引用类型(类)和值类型(枚举.结构体). 引用类型存储在 "堆" 上,值类型存储在 "栈" 上. Swift 管理引用类型采用 ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
- [docker]docker网络-直接路由模式
linux namespace连接参考: http://www.cnblogs.com/iiiiher/p/8057922.html docker网络-直接路由模式 参考: https://www.y ...
- Socket网络编程--简单Web服务器(6)
本来是想实现ssl连接的,但是弄了好久都不成功,就索性不做了,等以后有能力再做了.所以这一小节就是本次的最后一节了.就简单的说几个注意点. 1.加个配置文件 使用单例模式,使用一个类,该类保存一些信息 ...
- Fluent动网格【8】:网格节点运动案例
Fluent动网格中的DEFINE_GRID_MOTION宏允许用户定义网格节点的运动.本案例演示采用DEFINE_GRID_MOTION宏指定边界节点的运动. 案例动网格效果如图所示. 案例描述 本 ...
- debian/deepin 15.3 15.4安装jdk 1.7 (或jdk 7),配置默认环境
一.前言 Deepin 15.3是基于Debian开发的,安装jdk 1.7有所不同,默认是openjdk-8-jdk,而我们玩一些编译需要的是jdk 7. 所以本文给出安装JDK 7的教程. Dee ...
- Spark学习笔记——键值对操作
键值对 RDD是 Spark 中许多操作所需要的常见数据类型 键值对 RDD 通常用来进行聚合计算.我们一般要先通过一些初始 ETL(抽取.转化.装载)操作来将数据转化为键值对形式. Spark 为包 ...
- Java8学习笔记(三)--方法引入
基本概念 格式 实例变量名 | 类名 :: 静态方法 | 实例方法 作用 简化Lambda表达式 示例 a -> System.out.println(a); <=> System. ...
- [Tensorflow] Object Detection API - predict through your exclusive model
开始预测 一.训练结果 From: Testing Custom Object Detector - TensorFlow Object Detection API Tutorial p.6 训练结果 ...