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

#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. android SDK与ADT版本更新问题

    android SDK与ADT版本更新问题 问题:This Android SDK requires Android Developer Toolkit version 14.0.0 or above ...

  2. wireshark找(检测)不到(捕获)网卡的解决办法

    1 前言 有时候打开wireshark,会提示找不到可用网卡,此时是因为NetGroup Packet Filter Driver 服务没有开启. 环境:笔记本 系统:Win10 网络:WIFI 2  ...

  3. linux进程内存布局

      一个程序本质上都是由 BSS 段.data段.text段三个组成的.这样的概念在当前的计算机程序设计中是很重要的一个基本概念,而且在嵌入式系统的设计中也非常重要,牵涉到嵌入式系统运行时的内存大小分 ...

  4. Codeforces 1117G Recursive Queries [线段树]

    Codeforces 洛谷:咕咕咕 思路 设\(L_i,R_i\)为\(i\)左右第一个大于它的位置. 对于每一个询问\(l,r\),考虑区间每一个位置的贡献就是\(\min(r,R_i-1)-\ma ...

  5. struts2 过滤器和拦截器的区别和使用

    java web 过滤器和拦截器的区别和使用  1.1 什么是拦截器:      拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然 ...

  6. bat命令查询硬件信息

    bat命令查询硬件信息 50 需求是这样的写一个bat命令,当命令执行的时候,先请用户输入姓名,然后继续执行查询出以下信息并写入一个文件,文件名称随便,文件可以放在与当前命令同一个文件夹下.最终文件中 ...

  7. Confluence 6 链接到其他应用

    应用链接(Application Links)有时候也被称为 "AppLinks" 是一系列测插件能够允许你在 Atlassian  的应用中互相链接.链接 2 个应用能够允许你在 ...

  8. Confluence 6 为外部用户管理获得支持

    本页面描述了如果你在配置外部用户管理的时候遇到了问题,如何向 Atlassian 支持项目组寻求帮助.外部用户目录挂你包括 Active Directory,其他 LDAP 服务器,Atlassian ...

  9. python之vscode配置开发调试环境

    在vscode中下载python插件,下载量最多的就是 打开launch.json,把以下代码粘贴进去即可 { // 使用 IntelliSense 了解相关属性. // 悬停以查看现有属性的描述. ...

  10. matlab 测试 数字二次混频

    % test2 clear; clf; close all Fs=800000;%采样频率800k fz=80000;%载波频率80k fz1=3000;%载波频率3k fj=79000;%基波频率7 ...