转换成前缀和, 预处理一下然后莫队。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; const int B = ; struct Qus {
int L, R, id;
bool operator < (const Qus& rhs) const {
if(L / B == rhs.L / B) return R < rhs.R;
return L / B < rhs.L / B;
}
} qus[N]; int n, k, q, l, r, tot, t[N];
int after[N], befor[N], cnt[N];
LL ans[N], a[N], hs[N], val; int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++) scanf("%d", &t[i]);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
if(t[i] == ) a[i] = a[i - ] + a[i];
else a[i] = a[i - ] - a[i];
hs[tot++] = a[i];
}
hs[tot++] = ;
sort(hs, hs + tot);
tot = unique(hs, hs + tot) - hs;
for(int i = ; i <= n; i++) a[i] = lower_bound(hs, hs + tot, a[i]) - hs;
for(int i = , p; i < tot; i++) {
after[i] = befor[i] = -;
p = lower_bound(hs, hs + tot, hs[i] + k) - hs;
if(p < n && hs[p] == hs[i] + k) after[i] = p;
p = lower_bound(hs, hs + tot, hs[i] - k) - hs;
if(p < n && hs[p] == hs[i] - k) befor[i] = p;
}
scanf("%d", &q);
for(int i = ; i <= q; i++) {
scanf("%d%d", &qus[i].L, &qus[i].R);
qus[i].id = i; qus[i].L--;
}
sort(qus + , qus + + q);
l = , r = -;
for(int i = ; i <= q; i++) {
int L = qus[i].L, R = qus[i].R, id = qus[i].id;
while(r < R) r++, val += befor[a[r]] == - ? : cnt[befor[a[r]]], cnt[a[r]]++;
while(l > L) l--, val += after[a[l]] == - ? : cnt[after[a[l]]], cnt[a[l]]++;
while(r > R) cnt[a[r]]--, val -= befor[a[r]] == - ? : cnt[befor[a[r]]], r--;
while(l < L) cnt[a[l]]--, val -= after[a[l]] == - ? : cnt[after[a[l]]], l++;
ans[id] = val;
}
for(int i = ; i <= q; i++) printf("%lld\n", ans[i]);
puts("");
return ;
}
/*
*/

Codeforces 877F Ann and Books 莫队的更多相关文章

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

    cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...

  2. codeforces 86D,Powerful array 莫队

    传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...

  3. CodeForces 577E Points on Plane(莫队思维题)

    题目描述 On a plane are nn points ( x_{i}xi​ , y_{i}yi​ ) with integer coordinates between 00 and 10^{6} ...

  4. Codeforces 86D Powerful array (莫队算法)

    题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000,   1<=s< ...

  5. codeforces 940F 带修改的莫队

    F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  6. CodeForces 86 D Powerful array 莫队

    Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...

  7. CodeForces 375D Tree and Queries 莫队||DFS序

    Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...

  8. Codeforces D. Powerful array(莫队)

    题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...

  9. Codeforces 86D Powerful array (莫队)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. ansible配合shell脚本批量编译安装python3.6.6

    [root@node1:/etc]# tree /etc/ansible/ /etc/ansible/ ├── ansible.cfg ├── hosts ├── python.yml └── rol ...

  2. 响应式页面-@media介绍

    01 响应式页面-@media介绍,   我们为什么要写自适应的页面(响应式页面) 众所周知,电脑.平板.手机的屏幕是差距很大的,假如在电脑上写好了一个页面,在电脑上看起来不错,但是如果放到手机上的话 ...

  3. Ex 2_25 n位十进制整数转换为二进制形式..._第四次作业

    (a)   当n=1时,(10)d=(1010)b 当n=2时,(100)d=(10)d x (10)d=(1010)b x (1010)b 当n=4时,(10000)d=(100)d x (100) ...

  4. Maven多模块项目加载

      Maven多模块项目中如何让Spring运行时成功加载指定的子模块   将子模块pom加入到父模块pom的定义中,并继承父模块   在web.xml中配置加载子模块的Spring配置文件   在启 ...

  5. 数据库-mysql-DDL-表记录操作

  6. linux流量异常查看哪些程序占用的

    Linux下进程/程序网络带宽占用情况查看工具 -- NetHogs   http://www.vpser.net/manage/nethogs.html   来自.  最后略有修改 之前VPS侦探曾 ...

  7. centos7编译安装lnmp

    1.前言 本文适合于已经对Linux操作系统具有基本操作经验,并且能够在Linux或Windows上通过一键搭建工具或者yum命令行进行环境搭建的读者,阅读本文需具有一定的专业知识,本文不建议初学者阅 ...

  8. 将文字数字转阿拉伯数字(Python)

    今天改进爬虫的时候有这样的需求,如果是文字的数字把他转化成数字存储. 然后百度了一下没什么能看的博客. 其实挺无奈的,搜出来的博客全是一样的代码,有的图都是COPY,尤指CSDN,博客质量大多很差.怀 ...

  9. Sybase·调用存储过程并返回结果

    最近项目要用Sybase数据库实现分页,第一次使用Sybase数据库,也是第一次使用他的存储过程.2个多小时才调用成功,在此记录: 项目架构:SSM 1.Sybase本身不支持分页操作,需要写存储过程 ...

  10. liunx 安装jdk1.8

    采用解压压缩文件形式安装 第一步:将jdk压缩文件上传至home目录下面 第二步:目录切换至/usr目录下   cd /usr 创建java目录   mkdir   java 第三步:将jdk 压缩文 ...