[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 ...
随机推荐
- [poj 3252]数位dp前导0的处理
通过这个题对于数位dp中前导0的处理有了新的认识. 题目链接:http://poj.org/problem?id=3252 //http://poj.org/problem?id=3252 #incl ...
- java 保护内存操作的方法
1.与c++不同,在java中,没有通过使用强制转换指针类型或者通过进行指针运算直接访问内存的方法.在java中使用对象时,需要严格地遵守类型规则.如果存在一个Mountain类对象的引用(类似于c+ ...
- 常见通用的 JOIN 查询
SQL执行循序: 手写: SELECT DISTINCT <query_list> FROM <left_table> <join type> JOIN <r ...
- 关于applePay详细讲解
https://www.cnblogs.com/diweinan/p/6225501.html
- [USACO07FEB]新牛棚Building A New Barn
洛谷题目链接:[USACO07FEB]新牛棚Building A New Barn 题目描述 After scrimping and saving for years, Farmer John has ...
- 【FJWC2017】交错和查询 [线段树]
交错和查询 Time Limit: 10 Sec Memory Limit: 256 MB Description 无限循环数字串S由长度为n的循环节s构成.设s为12345(n=5),则数字串S为 ...
- 汕头市队赛 SRM16 T2
描述 猫和老鼠,看过吧?猫来了,老鼠要躲进洞里.在一条数轴上,一共有n个洞,位置分别在xi,能容纳vi只老鼠.一共有m只老鼠位置分别在Xi,要躲进洞里,问所有老鼠跑进洞里的距离总和最小是多少. 输入格 ...
- mysql 等 null 空值排序
[sqlserver]: sqlserver 认为 null 最小. 升序排列:null 值默认排在最前. 要想排后面,则:order by case when col is null then 1 ...
- Kuangbin 带你飞 数位DP题解
以前一直不知道该咋搞这个比较好. 感觉推起来那个数字好麻烦.后来有一种比较好的写法就是直接的DFS写法.相应的ismax表示当前位是否有限制. 数位DP也是有一种类似模版的东西,不过需要好好理解.与其 ...
- processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(“ 132501”).active().singleResult();
JAVA: processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(“ 132501”).ac ...