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的更多相关文章

  1. 【题解】BZOJ4241: 历史研究(魔改莫队)

    [题解]BZOJ4241: 历史研究(魔改莫队) 真的是好题啊 题意 给你一个序列和很多组询问(可以离线),问你这个区间中\(\max\){元素出现个数\(\times\)元素权值} IOI国历史研究 ...

  2. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

  3. 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 ...

  4. 【莫队算法】【权值分块】bzoj3339 Rmq Problem

    如题. #include<cstdio> #include<algorithm> #include<cmath> using namespace std; #def ...

  5. hdu 5373 The shortest problem(杭电多校赛第七场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5373 The shortest problem Time Limit: 3000/1500 MS (J ...

  6. hdu 5328 Problem Killer(杭电多校赛第四场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 题目大意:找到连续的最长的等差数列or等比数列. 解题思路:1.等差等比的性质有很多.其中比较重 ...

  7. hdu 5319 Painter(杭电多校赛第三场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5319 Painter Time Limit: 2000/1000 MS (Java/Others)   ...

  8. hdu 5326 Work(杭电多校赛第三场)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5326 Work Time Limit: 2000/1000 MS (Java/Others)    M ...

  9. 2019杭电多校赛第九场 Rikka with Mista

    Problem Description Rikka is a fervent fan of JoJo's Bizarre Adventure. As the last episode of Golde ...

随机推荐

  1. 最近对latin-1这个字符集产生了不少好感

    [简介] 最近我要解析一个数据库中间件的日志.这个中间件会在日志中记录SQL发往的后台DB ,执行耗时,对应的SQL:中间件直接把SQL写到 了日志中去,并没有对SQL进行适当的编码转换:理想情况下这 ...

  2. 图文剖析自己定义View的绘制(以自己定义滑动button为例)

    自己定义View一直是横在Android开发人员面前的一道坎. 一.View和ViewGroup的关系 从View和ViewGroup的关系来看.ViewGroup继承View. View的子类.多是 ...

  3. win8使用技巧

    windows 8操作系统相信大家已经不再陌生了,虽然正式版本还未发布,但不少朋友已经在使用微软事先推出的windows 消费者预览版,直白的说就是公测版,预览版是免费的,但仅可以使用一年,但其功能与 ...

  4. Android开发(十七)——关闭中间activity

    参考: http://java--hhf.iteye.com/blog/1826880

  5. ssh-remote-port-forwarding

    1.ssh免密码登录(A登录到B) 1.1.在A上执行并在~/.ssh下生成id_rsa和id_rsa.pub两个文件 ssh-keygen -t rsa -P '' 1.2.拷贝id_rsa.pub ...

  6. 使用Delve进行Golang代码的调试

    问题 安装好vscode编辑项目,出现以下错误: Failed to continue: "Cannot find Delve debugger. Ensure it is in your ...

  7. [Installing Metasploit Framework on CentOS_RHEL 6]在CentOS_RHEL 6上安装Metasploit的框架【翻译】

    [Installing Metasploit Framework on CentOS_RHEL 6]在CentOS_RHEL 6上安装Metasploit的框架[翻译] 标记声明:蓝色汉子为翻译上段英 ...

  8. jQuery图片播放插件prettyPhoto使用介绍

    演示效果  http://www.17sucai.com/preview/131993/2014-07-09/mac-Bootstrap/gallery.html 点击之后的效果 使用方法 Query ...

  9. windows 注册表讲解

    注册表存储结构: 整个注册表内容主要由项(键).值(键值)构成.(通过regedit命令打开注册表) 5个根键: HKEY_CLASSES_ROOT    (缩写HKCR) HKEY_CURRENT_ ...

  10. java-信息安全(十四)-初探SSL

    原文地址 http://snowolf.iteye.com/blog/397693 我们需要构建一个由CA机构签发的有效证书,这里我们使用上文中生成的自签名证书zlex.cer     这里,我们将证 ...