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. TLS/HTTPS 证书生成与验证

    最近在研究基于ssl的传输加密,涉及到了key和证书相关的话题,走了不少弯路,现在总结一下做个备忘 科普:TLS.SSL.HTTPS以及证书 不少人可能听过其中的超过3个名词,但它们究竟有什么关联呢? ...

  2. 使用VisualSVN Server搭建SVNserver (Windows环境为例)

    使用 VisualSVN Server来实现主要的 SVN功能则要比使用原始的 SVN和Apache相配合来实现源代码的 SVN管理简单的多,下面就看看详细的说明. VisualSVN Server的 ...

  3. Django-启动文件的制作

    在Django项目下的app.py中写入这几行代码,当启动的时候会找项目下名为:stark.py的文件并先加载 #app.py from django.apps import AppConfig cl ...

  4. Socket网络编程--聊天程序(6)

    这一小节将增加一个用户的结构体,用于保存用户的用户名和密码,然后发给服务器,然后在服务器进行判断验证.这里就有一个问题,以前讲的就是发送字符串是使用char类型进行传输,然后在服务器进行用同样是字符串 ...

  5. 【6集iCore3_ADP触摸屏驱动讲解视频】6-5 底层驱动之SDRAM读写(下)

    源视频包下载地址: 链接:http://pan.baidu.com/s/1jIC2LKy 密码:zyn3   银杏科技优酷视频发布区: http://i.youku.com/gingko8  

  6. Ansible and FileBeta

    使用Ansible通过ssh批量读取和修改Client设备 1. 安装ansible工具 apt-get install ansible 2. 添加需要访问的client信息 ansible配置文件如 ...

  7. 【转】JCR期刊分区及其检索方法

    不少机构依据JCR期刊分区制定科研激励政策,相关科研工作者及科研管理机构密切关注JCR期刊分区及其检索方法.本文作一粗略介绍.    关于JCR(Journal Citation Reports,期刊 ...

  8. 如何搭建WebRTC信令服务器

    WebRTC 有一整套规范,如怎样使用它的接口.使用SDP进行媒体协商.通过ICE收集地址并进行连通性检测等等.除此之外,WebRTC还需要房间服务器将多端聚集到一起管理,以及信令服务器进行信令数据交 ...

  9. Java——安全地停止线程

    错误用例 下面用例中,一个正在sleep的线程,在调用interrupt后,wait方法检查到isInterrupted()为true,抛出异常, 而catch到异常后没有处理.一个抛出了Interr ...

  10. CentOS下安装Vmtools

    基本步骤差不多 [root@rd01 ~]# cd /media [root@rd01 ~]# ls -a [root@rd01 ~]# cp VMwareTools-5.5.1-19175.tar. ...