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. 在Win10中通过命令行打开UWP应用

    近期由于需要在WinX菜单中添加几个UWP应用,但发现很难找到相应的命令行,Universal Apps 的快捷方式属性里也没有. 于是到网上搜了很久才找到一个E文的页面,试了一下确实可行,分享给大家 ...

  2. Windows运行机理——主程序—WinMain

    Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 在windows 操作系统下,用C 或者C++来编写MS-DOS 应用 ...

  3. Linux命令应用大词典-第10章 Shell相关命令

    10.1 commond:抑制正常的Shell函数查找 10.2 exec:使用执行命令替换当前的shell进程 10.3 bash:GNU的Bourne-Again Shell解释器 10.4 bu ...

  4. Siki_Unity_2-1_API常用方法和类详细讲解(下)

    Unity 2-1 API常用方法和类详细讲解(下) 任务101&102:射线检测 射线origin + direction:射线检测:射线是否碰撞到物体 (物体需要有碰撞器),碰撞物体的信息 ...

  5. spark操作数据库的几种方法

    一.使用jdbcRDD的接口: SparkConf conf = new SparkConf(); conf.setAppName("Simple Application").se ...

  6. HTML 之 表单

    关于HTML的表单 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...

  7. linux 命令行基础

    命令行基础 一些名词 「图形界面」 「命令行」 「终端」 「shell」 「bash」 安装使用 Windws: 安装git, 打开 gitbash Linux 打开终端 Mac 打开终端 基本命令 ...

  8. [ Continuously Update ] The Paper List of Image / Video Captioning

    Papers Published in 2018 Convolutional Image Captioning - Jyoti Aneja et al., CVPR 2018 - [ Paper Re ...

  9. Map Reduce Application(Join)

    We are going to explain how join works in MR , we will focus on reduce side join and map side join. ...

  10. LeetCode - 167. Two Sum II - Input array is sorted - O(n) - ( C++ ) - 解题报告

    1.题目大意 Given an array of integers that is already sorted in ascending order, find two numbers such t ...