[CF877F]Ann and Books
题目大意:
有$n(n\le10^5)$个数$w_{1\sim n}(|w_i|\le10^9)$,并给定一个数$k(|k|\le10^9)$。$q(q\le10^5)$次询问,每次询问区间$[l,r]$中满足数字之和等于$k$的子区间数。
思路:
莫队卡常。
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<unordered_map>
#include<tr1/unordered_map>
using int64=long long;
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return neg?-x:x;
}
constexpr int N=1e5+,M=1e5;
int k,block;
int64 a[N],ans[N],tmp;
struct Query {
int l,r,id;
bool operator < (const Query &another) const {
return l/block==another.l/block?r<another.r:l/block<another.l/block;
}
};
Query q[M];
std::tr1::unordered_map<int64,int> map;
inline void ins(const int64 &x,const bool &b) {
tmp+=map[x-k*(b?:-)];
map[x]++;
}
inline void del(const int64 &x,const bool &b) {
map[x]--;
tmp-=map[x-k*(b?:-)];
}
int main() {
const int n=getint();
block=sqrt(n)*,k=getint();
for(register int i=;i<=n;i++) {
a[i]=getint()==?:-;
}
for(register int i=;i<=n;i++) {
(a[i]*=getint())+=a[i-];
}
const int m=getint();
for(register int i=;i<m;i++) {
const int l=getint()-,r=getint();
q[i]={l,r,i};
}
std::sort(&q[],&q[m]);
for(register int i=,l=,r=-;i<m;i++) {
while(l>q[i].l) ins(a[--l],);
while(r<q[i].r) ins(a[++r],);
while(l<q[i].l) del(a[l++],);
while(r>q[i].r) del(a[r--],);
ans[q[i].id]=tmp;
}
for(register int i=;i<m;i++) {
printf("%I64d%c",ans[i]," \n"[i==m-]);
}
return ;
}
[CF877F]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 877F Ann and Books 莫队
转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...
- 7 Must Read Python Books
7 Must Read Python Books I started learning Python just two years ago. Coming from a C++ and Java ba ...
- 从下往上看--新皮层资料的读后感 第三部分 70年前的逆向推演- 从NN到ANN
第三部分 NN-ANN 70年前的逆向推演 从这部分开始,调整一下视角主要学习神经网络算法,将其与生物神经网络进行横向的比较,以窥探一二. 现在基于NN的AI应用几乎是满地都是,效果也不错,这种貌似神 ...
- TCP/IP BOOKS
TCP/IP Fundamentals for Microsoft Windows: Overview https://technet.microsoft.com/en-us/library/bb72 ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- LightOJ1283 Shelving Books(DP)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1283 Description You are a librarian ...
- 抄书 Copying Books UVa 714
Copying Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...
- [LintCode] Copy Books 复印书籍
Given an array A of integer with size of n( means n books and number of pages of each book) and k pe ...
随机推荐
- import pymongo exceptions.ImportError: No module named pymongo
最近用Scrapy写爬虫,将爬取的数据存入Mongodb中,使用的是pymongo这个库,但是运行的时候报错如标题所示 搜了好多网站包括stackoverflow都没有解决,后来发现自己用的是虚拟环境 ...
- 获取html元素内容
html: <!DOCTYPE ><html> <head> <meta http-equiv="Content-Type" conten ...
- VC关于置顶窗口的方法小结
转摘自:http://blog.csdn.net/wirror800/article/details/4002381 将窗体置顶的方法有: //将窗体置顶的API函数 ::SetWindowPos(m ...
- iOS 单元测试(Unit Test 和 UI Test)
之前一直搞过~~最近试了一下下,完美~~ 附上一篇文章,不同的伙伴可以看看: http://www.jianshu.com/p/009844a0b9edUnitTest(简单的单元测试使用) http ...
- Spring - IoC(3): Bean 实例的创建方式
创建一个 Bean 实例对象的方法通常有如下方式: 调用构造器创建 Bean 实例 调用静态工厂方法创建 Bean 实例 调用实例工厂方法创建 Bean 实例 使用构造器创建 Bean 实例 XML ...
- 计算代码行数Demo源码
源码下载:04-计算代码行数.zip24.1 KB//// main.m// 计算代码行数//// Created by apple on 13-8-12.//技术博客http://www.cn ...
- System and method for parallel execution of memory transactions using multiple memory models, including SSO, TSO, PSO and RMO
A data processor supports the use of multiple memory models by computer programs. At a device extern ...
- 5.flask知识点补充
1.WTForms表单验证基本使用 flask-wtf是一个简化了WTForms操作的一个第三方库,WTForms表单的两个主要的功能jiushi就是验证用户提交数据的合法性以及渲染模板.当然还包括其 ...
- 【 Linux 】三大主流软件负载均衡器对比(LVS、Nginx、HAproxy)
三大主流软件负载均衡器对比(LVS.Nginx.HAproxy) (资料来自网络,做了部分的补充说明) LVS: 1. 抗负载能力强,性能高,能达到F5的60%,对内存和CPU资源消耗比较低 ...
- 曹政:CTO这点事
几乎整个互联网行业都缺 CTO,特别是一些草根背景的创业者,这个问题更加显著.从我自己的感受,身边各种朋友委托我找 CTO 的需求,嗯,算下来超过两位数了,光最近一个月就有 3 个,而且这三家都是刚拿 ...