[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 ...
随机推荐
- 解读python小练习
1.新建一个函数,判断是不是int 类型,并测试,不是抛出错误def adder(x, y):"""Return x + y if they are both integ ...
- 网络编程:connect函数
TCP客户用connect函数来建立与TCP服务器的连接: cpp #include<sys/socket.h> int connect(int sockfd, const struct ...
- JVM 性能排查--查看哪个对象占用内存大
参考:http://blog.csdn.net/chenleixing/article/details/44227327/ 1. 在IE地址栏中输入:http://localhost/test/in ...
- 25个常规方法优化你的jquery代码
原文发布时间为:2011-06-06 -- 来源于本人的百度文章 [由搬家工具导入] http://www.tvidesign.co.uk/blog/improve-your-jquery-25-ex ...
- 标签 JLable 类
标签JLable上可以添加图像,当鼠标停留在标签上时,可以显示一段提示文字. package first; import javax.swing.*; import java.awt.*; impor ...
- [bzoj4765]普通计算姬——分块
Brief Description 给定一棵n个节点的带权树,节点编号为1到n,以root为根,设sum[p]表示以点p为根的这棵子树中所有节点的权 值和.支持下列两种操作: 1 给定两个整数u,v, ...
- CDLinux 自动休眠功能的关闭方法
CDLinux 自动休眠功能的关闭方法: 控制台下使用xset命令来完成. xset q 可以查看当前屏幕保护和电源管理的状态信息 具体设置时,常用的有以下参数: xset s //这个参数设置屏 ...
- yum软件包安装
使用yum安装软件 配置yum配置文件 cd /etc/yum.repos.d/ vim rhel7.repo [rhel7-source] name=rhel7-source baseurl=fil ...
- DRF最高封装的子类视图
# 转载请留言联系 子类视图: 视图 作用 方法 父类 ListAPIView 查询多条数据 get GenericAPIView ListModelMixin CreateAPIView 新增一条数 ...
- python中进程池的应用
#原创,转载请联系 假设我们写的一个程序需要运行100个子进程的时候,那么写程序时,不可能循环创建销毁100个进程吧?进程的创建与销毁是很耗系统的资源的. 进程池的作用就体现出来了. 进程池可以控制进 ...