题目大意:
  有$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的更多相关文章

  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 877F Ann and Books 莫队

    转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...

  3. 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 ...

  4. 从下往上看--新皮层资料的读后感 第三部分 70年前的逆向推演- 从NN到ANN

    第三部分 NN-ANN 70年前的逆向推演 从这部分开始,调整一下视角主要学习神经网络算法,将其与生物神经网络进行横向的比较,以窥探一二. 现在基于NN的AI应用几乎是满地都是,效果也不错,这种貌似神 ...

  5. TCP/IP BOOKS

    TCP/IP Fundamentals for Microsoft Windows: Overview https://technet.microsoft.com/en-us/library/bb72 ...

  6. UVa 714 Copying Books(二分)

    题目链接: 传送门 Copying Books Time Limit: 3000MS     Memory Limit: 32768 KB Description Before the inventi ...

  7. LightOJ1283 Shelving Books(DP)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1283 Description You are a librarian ...

  8. 抄书 Copying Books UVa 714

    Copying  Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...

  9. [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 ...

随机推荐

  1. Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency

    E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...

  2. recycleview的基础Adapter

    .封装了一个基础的adapter.,用于recycleview的快捷使用有BaseAdapter,BaseViewHolder,PAdapter,MainActivity public abstrac ...

  3. 树形DP小结

    树形DP1.简介:树是一种数据结构,因为树具有良好的子结构,而恰好DP是从最优子问题更新而来,那么在树上做DP操作就是从树的根节点开始深搜(也就是记忆化搜索),保存每一步的最优结果.tips:树的遍历 ...

  4. 在linux环境下让java代码生效的步骤

    1.kill jboss 2.compile 3.deploy 4.bootstrap jboss.

  5. angular-translate加载.json文件进行翻译

    这是这个demo的目录结构,总共有两个文件:locale-chinese.json和translation11.html locale-chinese.json文件的内容是: { "beau ...

  6. 程序员的那些问题---转载自veryCD

    展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告   走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过 ...

  7. Spring学习--使用 utility scheme 定义集合及 p命名空间

    util schema 定义集合: 使用基本的集合标签定义集合时 , 不能将集合作为独立的 Bean 定义 , 导致其他 Bean 无法引用该集合 , 所以无法在不同 Bean 之间共享集合. 可以用 ...

  8. css做中划线与文字排版

    html: <div class="spilt">    <span class="left"></span>    < ...

  9. 51nod 扔盘子

    题目传送门 这道题一开始写了n方的算法 果不其然 它T了 所以就想想o(n)的算法 写不出来 就像sbzhq学习了一下 这道题啊 要维护一下从深度1到n每一段的最小值以及他的位置 然后就暴力搞一搞就o ...

  10. SQLSERVER数据库置疑、可疑、脱机、单用户、紧急模式等的修复

    数据库出现置疑.可疑.脱机.单用户.紧急模式主要是因为数据库的日志文件除了问题,2000和2008修复方式不一样,2008的修复脚本在2000中不适用,主要是不被2000识别. 假设数据库名为:eis ...