【魔改】莫队算法+组合数公式 杭电多校赛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 ...
随机推荐
- WEBAPI 的简单示例
一.webapi 1.在webapiconfig中移除xml的返回格式,返回格式就自动使用Json格式 config.Formatters.Remove(config.Formatters.XmlFo ...
- Android 组件系列-----Activity生命周期
本篇随笔将会深入学习Activity,包括如何定义多个Activity,并设置为默认的Activity.如何从一个Activity跳转到另一个Activity,还有就是详细分析Activity的生命周 ...
- xml解析、写入遇到的坑
前言 最近在看一个线上xml文件导出的问题,需求如下: 从我们平台导出一个后缀为tmx的术语语料数据(实际内容为xml文件),然后导入到其他第三方平台发现无法导入. 从其他平台导入的tmx文件无法导入 ...
- 每日英语:Who Needs to Know How to Code
Like many 10-year-olds, Nick Wald takes private lessons. His once-a-week tutor isn't helping him wit ...
- Python 读取csv的某行
站长用Python写了一个可以提取csv任一列的代码,欢迎使用.Github链接 csv是Comma-Separated Values的缩写,是用文本文件形式储存的表格数据,比如如下的表格: 就可以存 ...
- 解决 meld 出现 locale.setlocale(locale.LC_ALL,'') 失败的问题
. . . . . meld 是一款免费的文件比较工具,官网地址:http://meldmerge.org/ 在 Linux 环境使用 meld 的时候,可能会由于语言区域的配置问题导致它无法启动,会 ...
- PwniumCTF2014 - JJSN总结
Write-ups 本文最早发布在TSRC,详细地址:http://security.tencent.com/index.php/blog/msg/55 Forensics USB is FUN 这道 ...
- 【转】XML 特殊字符处理
from: http://gdutlzh.blog.163.com/blog/static/164746951201222934328455/ 1. 在XML文件中,如果内容包含一些特殊字符会导致XM ...
- 转载——githup的提交
Github是管理软件开发的首选托管网站,12306的火车票插件一时让国内当时很多小白开发者(当然也包括我)认识到了这个网站.GitHub可以托管各种git库,并提供一个web界面,与 SourceF ...
- 汽车行业与 Telematics
Telematics Telematics是远距离通信的电信(Telecommunications)与信息科学(Informatics)的合成词,按字面可定义为通过内置在汽车.航空.船舶.火车等运输工 ...