Codeforces 877F Ann and Books 莫队
转换成前缀和, 预处理一下然后莫队。
#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 莫队的更多相关文章
- 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, ...
- codeforces 86D,Powerful array 莫队
传送门:https://codeforces.com/contest/86/problem/D 题意: 给你n个数,m次询问,每次询问问你在区间l,r内每个数字出现的次数的平方于当前这个数的乘积的和 ...
- 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} ...
- Codeforces 86D Powerful array (莫队算法)
题目链接 Powerful array 给你n个数,m次询问,Ks为区间内s的数目,求区间[L,R]之间所有Ks*Ks*s的和. $1<=n,m<=200000, 1<=s< ...
- codeforces 940F 带修改的莫队
F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
- CodeForces 86 D Powerful array 莫队
Powerful array 题意:求区间[l, r] 内的数的出现次数的平方 * 该数字. 题解:莫队离线操作, 然后加减位置的时候直接修改答案就好了. 这个题目中发现了一个很神奇的事情,本来数组开 ...
- CodeForces 375D Tree and Queries 莫队||DFS序
Tree and Queries 题意:有一颗以1号节点为根的树,每一个节点有一个自己的颜色,求出节点v的子数上颜色出现次数>=k的颜色种类. 题解:使用莫队处理这个问题,将树转变成DFS序区间 ...
- Codeforces D. Powerful array(莫队)
题目描述: Problem Description An array of positive integers a1, a2, ..., an is given. Let us consider it ...
- Codeforces 86D Powerful array (莫队)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
随机推荐
- 用Go的风格实现素数筛选
package main import ( "fmt" "time" ) func source(ch chan<- int) { ; i < En ...
- HDU 5446
题意: 大组合数取余 (素数连乘) 思路: 对于答案 X X % pi = ai === C(m,n) % pi: 然后就是用孙子定理求出X, ai 用 卢卡斯定理求得 中间 LL * LL 会爆, ...
- linux shell 数组建立及使用技巧
参考网址:http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html linux shell在编程方面比windows 批处理强大太多 ...
- MR1和MR2(Yarn)工作原理流程
一.Mapreduce1 图1 MR1工作原理图 工作流程主要分为以下6个步骤: 1 作业的提交 1)客户端向jobtracker请求一个新的作业ID(通过JobTracker的getNewJobI ...
- Vue -cli 入门 --项目搭建(一)
一. 安装node.js环境. 在node.js官网下载稳定版本(https://nodejs.org/en/) 下载完成后点击安装,安装过程很简单,一直next即可,安装完成会自动添加node及np ...
- orm分组,聚合查询,执行原生sql语句
from django.db.models import Avg from app01 import models annotate:(聚合查询) ret=models.Article.objects ...
- python-并发编程之多进程
一.操作系统基础: 进程的概念起源于操作系统,操作系统其它所有概念都是围绕进程来的,所以我们了解进程之前先来了解一下操作系统 操作系统位于计算机硬件与应用软件之间,本质也是一个软件.操作系统由操作系统 ...
- WPF窗体的生命周期
和所有类一样,窗口也有生存期,在第一次实例化窗口时生存期开始,然后就可以显示.激活和停用窗口,直到最终关闭窗口. 1.显示窗体 构造函数 Show().ShowDialog()方法:Show()方法显 ...
- Modbus库开发笔记之九:利用协议栈开发Modbus TCP Server应用
前面我们已经完成了Modbus协议栈的开发,但这不是我们的目的.我们开发它的目的当然是要使用它来解决我们的实际问题.接下来我们就使用刚开发的Modbus协议栈开发一个Modbus TCP Server ...
- Walle,一个开源的web代码发布管理系统
前言 Walle 一个web部署系统工具,可能也是个持续发布工具,配置简单.功能完善.界面流畅.开箱即用!支持git.svn版本管理,支持各种web代码发布,静态的HTML,动态PHP,需要编译的JA ...