Codeforces Round #442 (Div. 2) F. Ann and Books 莫队,离散化
来源:互联网 发布:无情对 知乎 编辑:程序博客网 时间:2024/06/06 09:26
F. Ann and Books
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In Ann’s favorite book shop are as many as n books on math and economics. Books are numbered from 1 to n. Each of them contains non-negative number of problems.
Today there is a sale: any subsegment of a segment from l to r can be bought at a fixed price.
Ann decided that she wants to buy such non-empty subsegment that the sale operates on it and the number of math problems is greater than the number of economics problems exactly by k. Note that k may be positive, negative or zero.
Unfortunately, Ann is not sure on which segment the sale operates, but she has q assumptions. For each of them she wants to know the number of options to buy a subsegment satisfying the condition (because the time she spends on choosing depends on that).
Currently Ann is too busy solving other problems, she asks you for help. For each her assumption determine the number of subsegments of the given segment such that the number of math problems is greaten than the number of economics problems on that subsegment exactly by k.
Input
The first line contains two integers n and k (1 ≤ n ≤ 100 000, - 109 ≤ k ≤ 109) — the number of books and the needed difference between the number of math problems and the number of economics problems.
The second line contains n integers t1, t2, …, tn (1 ≤ ti ≤ 2), where ti is 1 if the i-th book is on math or 2 if the i-th is on economics.
The third line contains n integers a1, a2, …, an (0 ≤ ai ≤ 109), where ai is the number of problems in the i-th book.
The fourth line contains a single integer q (1 ≤ q ≤ 100 000) — the number of assumptions.
Each of the next q lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n) describing the i-th Ann’s assumption.
Output
Print q lines, in the i-th of them print the number of subsegments for the i-th Ann’s assumption.
Examples
input
4 1
1 1 1 2
1 1 1 1
4
1 2
1 3
1 4
3 4
output
2
3
4
1
input
4 0
1 2 1 2
0 0 0 0
1
1 4
output
10
题意:问区间l,r之间有多少对区间和为k,
做法:莫队,在处理区间里有多少个数的时候不能用map,map会超时,可以用先用map离散化,我试过unorder_map但是超时了,后来手写了hash_map,也超时了,后来发现是在hashmap里计算hash值得时候用了太多时间,然后预处理了hash值,卡过去了,hashmap室友常数的,如果预处理hash值得话大概是4-5,如果不预处理的话,估计更大。
#include<bits/stdc++.h>#define ll long longusing namespace std;const int N = 1e5+100;const int B = 333;struct node{ int l,r,id; bool operator<(const node & p)const{ if(l/B == p.l/B) return r < p.r; return l<p.l; }};node pt[N];ll ans[N];ll now = 0;ll num[N];int n,q,k;set<ll> s;map<ll,int> mp;int pos[3][N];ll sum[N*3];void Move(int x,int op){ //cout <<"!!" << x << ' ' << op << endl; if(op==1){ //mp[num[x]]++; //now += mp[num[x]-k]; sum[pos[0][x]]++; now += sum[pos[2][x]]; if(k == 0) now --; } else if(op == 2){ //now -= mp[num[x]-k]; //mp[num[x]] --; now -= sum[pos[2][x]]; sum[pos[0][x]]--; if(k == 0) now ++; } else if(op == 3){ //mp[num[x-1]]++; //now += mp[num[x-1]+k]; sum[pos[0][x-1]]++; now += sum[pos[1][x-1]]; if(k == 0) now --; } else{ //now -= mp[num[x-1]+k]; //mp[num[x-1]] --; now -= sum[pos[1][x-1]]; sum[pos[0][x-1]] --; if(k == 0) now ++; }}int main(){ scanf("%d %d",&n,&k); for(int i = 1;i <= n;i ++) scanf("%lld",&num[i]); for(int i = 1;i <= n;i ++){ if(num[i] == 2) num[i] = -1; int now; scanf("%d",&now); num[i] *= now; } for(int i = 0;i <= n;i ++) { num[i] = num[i-1]+num[i]; s.insert(num[i]); s.insert(num[i]+k); s.insert(num[i]-k); } int tot = 0; for(auto it:s){ mp[it] = tot++; } for(int i = 0;i <= n;i ++){ pos[0][i] = mp[num[i]]; pos[1][i] = mp[num[i]+k]; pos[2][i] = mp[num[i]-k]; } scanf("%d",&q); for(int i =1;i <= q;i ++){ scanf("%d %d",&pt[i].l,&pt[i].r); pt[i].id = i; } sort(pt+1,pt+1+q); int L = 1,R = 1; sum[pos[0][0]]++; sum[pos[0][1]]++; if(num[1] == k) now ++; for(int i = 1;i <= q;i ++){ while(R < pt[i].r) Move(++R,1); while(R > pt[i].r) Move(R--,2); while(L > pt[i].l) Move(--L,3); while(L < pt[i].l) Move(L++,4); ans[pt[i].id] = now; } for(int i = 1;i <= q;i ++){ printf("%lld\n",ans[i]); } return 0;}
- Codeforces Round #442 (Div. 2) F. Ann and Books 莫队,离散化
- Codeforces Round #442 (Div. 2) F. Ann and Books (莫队算法+离散化)
- Codeforces Round #442 (Div. 2) F. Ann and Books
- Codeforces Round #442 (Div. 2) F. Ann and Books(莫队)
- Codeforces Round #442 (Div. 2) F. Ann and Books (莫队分块)
- [Codeforces 877F] Ann and Books
- B. Vanya and Books(Codeforces Round #308 (Div. 2))
- Codeforces Round #308 (Div. 2) Vanya and Books
- Codeforces Round #308 (Div. 2) B. Vanya and Books
- codeforces 877F F. Ann and Books hash+莫队算法
- Codeforces Round #442 (Div. 2) 877 F
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome (DP+字典树)
- Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)
- Codeforces Round #311 (Div. 2) E. Ann and Half-Palindrome 字典树
- DP Codeforces Round #322 (Div. 2) F. Zublicanes and Mumocrates
- Codeforces Round #379 (Div. 2) F. Anton and School
- Codeforces Round #111 (Div. 2) E题 Buses and People 线段树+离散化
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem 离散化+树状数组
- Edubuntu是为教育量身定制的系统
- Android 如何加速./mk snod打包
- 228. Summary Ranges
- 友盟第三方 登录与分享功能实现
- 155. Min Stack
- Codeforces Round #442 (Div. 2) F. Ann and Books 莫队,离散化
- Pace 12 (自定义类型:结构体 位段 枚举 联合)
- myeclips 提交代码到bitbucket或者github
- 基于live555的一个简单RTSP服务器
- 当下的生活
- Toast的优化操作
- python数字图像处理(1):环境安装与配置
- pythonic
- java获取文件的详细信息