cf 442 div2 F. Ann and Books(莫队算法)

题意:

\(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\)

每次查询区间\([l,r]内有多少对(i,j)满足l <= i <= j <= r 且 sum[j] - sum[i-1] = k\)

思路:

区间左右端点的挪动对答案的贡献符合加减性质,直接用莫队算法即可

复杂度\(O(n * sqrt(n) * log(maxsum))\) 过高

考虑先离散化预处理出所有位置 将\(log\)去掉

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5 + 10;
struct Q{
int l,r,bl,id;
Q(){};
bool operator<(const Q&rhs){
if(bl == rhs.bl) return r < rhs.r;
return bl < rhs.bl;
}
}qr[N];
int n,k;
LL ans[N],value[N];
int x[N],y[N],z[N],cnt[N * 3],type[N];
vector<LL> se;
int main(){ while(cin>>n>>k){
memset(cnt, 0, sizeof(cnt));
se.clear();
for(int i = 1;i <= n;i++) scanf("%d",&type[i]);
for(int i = 1;i <= n;i++){
scanf("%d",&value[i]);
if(type[i] == 1) value[i] += value[i-1];
else value[i] = value[i-1] - value[i];
}
for(int i = 0;i <= n;i++) {
se.push_back(value[i]);
se.push_back(value[i] + k);
se.push_back(value[i] - k);
}
sort(se.begin(), se.end());
se.erase(unique(se.begin(),se.end()),se.end());
for(int i = 0;i <= n;i++){
x[i] = lower_bound(se.begin(),se.end(),value[i]) - se.begin();
y[i] = lower_bound(se.begin(),se.end(),value[i] + k) - se.begin();
z[i] = lower_bound(se.begin(),se.end(),value[i] - k) - se.begin();
}
int block_size = sqrt(n + 0.5);
int q;
cin>>q;
for(int i = 0;i < q;i++){
scanf("%d%d",&qr[i].l,&qr[i].r);
qr[i].id = i;
qr[i].l--;
qr[i].bl = qr[i].l / block_size;
}
sort(qr, qr + q);
int L = 0,R = -1;
LL res = 0;
for(int i = 0;i < q;i++){
while(qr[i].l > L) {
cnt[x[L]]--;
res -= cnt[y[L++]];
}
while(qr[i].l < L) {
res += cnt[y[--L]];
cnt[x[L]]++;
}
while(qr[i].r > R){
res += cnt[z[++R]];
cnt[x[R]]++;
}
while(qr[i].r < R) {
cnt[x[R]]--;
res -= cnt[z[R--]];
}
ans[qr[i].id] = res;
}
for(int i = 0;i < q;i++) printf("%lld\n",ans[i]);
}
return 0;
}

cf 442 div2 F. Ann and Books(莫队算法)的更多相关文章

  1. Codeforces 877F Ann and Books 莫队

    转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  2. Codeforces #442 Div2 F

    #442 Div2 F 题意 给出一些包含两种类型(a, b)问题的问题册,每本问题册有一些题目,每次查询某一区间,问有多少子区间中 a 问题的数量等于 b 问题的数量加 \(k\) . 分析 令包含 ...

  3. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  4. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  5. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  6. BZOJ-2038 小Z的袜子(hose) 莫队算法

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MB Submit: 5573 Solved: 2568 [Subm ...

  7. 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...

  8. bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)

    [题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...

  9. bzoj 3289 Mato的文件管理(莫队算法+BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3289 [题意] 回答若干个询问:[l,r]区间内的逆序对个数. [思路] 莫队算法,B ...

随机推荐

  1. Farpoint使用一点小总结

    Farpoint表格编辑的功能是非常强大的,记录下自己常用到的地方. 使用的版本:FarPoint.Win.Spread.5.0 1.Farpoint 设置为不可编辑状态 this.FPProxyIt ...

  2. 初识java atomic

    2018-8-19 昨天看到java.util.concurrent.atomic相关的文章,之前有过留意但并未去了解,正好有空学习一下.本人理解atomic包是concurrent子包,当是为并发所 ...

  3. [CodeForce455A]Boredom

    题面描述 Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long ...

  4. node事件循环

    Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. Node.j ...

  5. 谜题 (Puzzle,ACM/ICPC World Finals 1993,UVa227)

    题目描述:算法竞赛入门经典习题3-5 题目思路:模拟题 #include <stdio.h> #include <string.h> #define maxn 55 char ...

  6. 从零开始的Python学习Episode 3——字符串格式化与for循环

    一.字符串格式化 利用一段注释记录想要输出的字符串格式,并用 %s . %d 或 %f 依次代替要输出的数据(%s代表字符串,%d代表数字,%f代表浮点数),然后在这段注释之后依次加上要输出的数据. ...

  7. 软工1816 · Alpha冲刺(2/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 与前后端敲定接口设计的细节 重新理清业务逻辑,对项目最初的设想进行一定修正 跟 ...

  8. 3dContactPointAnnotationTool开发日志(十九)

      增加了输出接触点信息到文件功能.

  9. Thinkphp5的ajax接口实现

    前一篇讲到thinkphp5从数据库获取数据之后赋给视图view,前一篇从数据渲染方式来说是服务端数据渲染,这一章则是浏览器端数据渲染.按照知识总结依据来划分,这是两种不同的技术场景. 下面介绍具体的 ...

  10. VUE01指令

    一.下载Vue2.0的两个版本: 官方网站:http://vuejs.org/ 开发版本:包含完整的警告和调试模式 生产版本:删除了警告,进行了压缩 二.项目结构搭建 这个部分要视频中有详细讲解. 三 ...