题目大意:
  有$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. PAT团体程序设计大赛---(模拟)

    L1-1 古风排版(20 分) 中国的古人写文字,是从右向左竖向排版的.本题就请你编写程序,把一段文字按古风排版. 输入格式: 输入在第一行给出一个正整数N(<100),是每一列的字符数.第二行 ...

  2. jquery的ajax实现方式

    在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 首先我们看$.get(): .代码如下: $.get("test.jsp", { ...

  3. Install the Active Directory Administration Tools on Windows Server

    安装 Active Directory 管理工具 To manage your directory from an EC2 Windows instance, you need to install ...

  4. 代码管理工具 (含git、npm、gulp)

    1 Git 分布式代码管理工具(基于Linux,可在本地进行提交)代码同时储存在本地和服务器中 ① Git基本操作命令 (1)初始化,创建初始化仓库 git init ------- 文件初始化,初始 ...

  5. AtCoder Regular Contest 082 E

    Problem Statement You are given N points (xi,yi) located on a two-dimensional plane. Consider a subs ...

  6. [BZOJ1151][CTSC2007]动物园zoo 解题报告|DP|位运算

    Description 最近一直在为了学习算法而做题,这道题是初一小神犇让我看的.感觉挺不错于是写了写. 这道题如果是一条线的话我们可以构造一个DP f[i,j]表示以i为起点,i,i+1...i+4 ...

  7. 【STSRM12】整除

    [题意]给定长度为n的序列A,求最长的区间满足区间内存在数字能整除区间所有数字,同时求所有方案.n<=5*10^5,Ai<2^31. [算法]数论??? [题解]首先一个区间的基准数一定是 ...

  8. The service base of EF I am using

    using CapMon.Data; using System; using System.Collections.Generic; using System.Linq; using System.T ...

  9. [bzoj4602][Sdoi2016]齿轮——dfs

    题目 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x : y.即如果只考虑这两个组合齿轮,编号为u的齿轮转动x圈,编号为v的齿轮会转动y圈.传 ...

  10. K-D树问题 HDU 4347

    K-D树可以看看这个博客写的真心不错!这里存个版 http://blog.csdn.net/zhjchengfeng5/article/details/7855241 HDU 4349 #includ ...