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

#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. 使用Vagrant搭建本地python开发环境

    使用Vagrant搭建本地python开发环境 关于vagrant:Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境,它使用Oracle的开源VirtualBox虚拟化系统也可以使 ...

  2. struct/class等内存字节对齐问题详解

    问题引入 定义一个结构体的一般形式为: struct 结构体名 { //类型说明符 成员名; }; 例如有如下结构体: struct Stu { int id; char sex; float hig ...

  3. iPhone内存溢出——黑白苹果

    一旦iPhone内存长期占用过大,一关机即再难启动,如果你Home键加power键等都不行,那么可以要刷机了. 方式: 1.电脑下载ITunes,这个过程比较漫长,一系列逐渐大概3G,下载完成打开. ...

  4. Android 目前最稳定和高效的UI适配方案

    Android系统发布十多年以来,关于Android的UI的适配一直是开发环节中最重要的问题,但是我看到还是有很多小伙伴对Android适配方案不了解.刚好,近期准备对糗事百科Android客户端设计 ...

  5. Confluence 6 访问日志脚本

    日志访问脚本在连接:https://confluence.atlassian.com/download/attachments/133267635/Atlassian-accessLogScripts ...

  6. Confluence 6 修改日志文件的大小数量和级别

    修改日志文件的大小和数量 在默认的情况下,Confluence 将会保持 5 个日志文件,每一个日志文件的大小超过 20 MB 的时候将会被重写. 你可以修改默认日志文件的大小和数量,通过编辑 < ...

  7. Confluence 6 配置校验和识别

    校验你的设置 查看你 Confluence 当前使用的设置,请参考 Viewing System Properties 页面中的内容. 识别系统属性 请参考 Recognized System Pro ...

  8. Confluence 6 用户目录图例 - 使用 LDAP 授权的内部目录

    上面的图:Confluence 连接 LDAP 服务器仅用做授权 https://www.cwiki.us/display/CONFLUENCEWIKI/Diagrams+of+Possible+Co ...

  9. Linux用户、用户组、文件权限学习笔记

    最近打算更仔细学习一下linux操作系统.先是恶补了一下用户.用户组.文件权限这三样比较重要的知识.学习这几样东西,得先掌握linux的权限系统相关知识.linux的权限系统主要是由用户.用户组和权限 ...

  10. Burp Scanner Report

    1.使用application web 漏洞平台,除此之外还有一款类似的工具 叫做mulidata,其实mulidata功能更好一点. 2.配置之前的问题处理 安装之前要确认 自己之前是否安装过 Ap ...