Codeforces Round 442 (Div. 2)
F
Ann and Books
Codeforces
2300
原题
← E→
翻译中…

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$, $- 10^{9} ≤ *k* ≤ 10^{9}$) — 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 $*t*_{1}, *t*_{2}, ..., *t*_{*n*}$ ($1 ≤ *t*_{*i*} ≤ 2$), where $*t*_{*i*}$ 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 $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ ($0 ≤ *a*_{*i*} ≤ 10^{9}$), where $*a*_{*i*}$ 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 $*l*_{*i*}$ and $*r*_{*i*}$ ($1 ≤ *l*_{*i*} ≤ *r*_{*i*} ≤ *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.

翻译中…

Note

翻译中…

In the first sample Ann can buy subsegments $[1;1], [2;2], [3;3], [2;4]$ if they fall into the sales segment, because the number of math problems is greater by $1$ on them that the number of economics problems. So we should count for each assumption the number of these subsegments that are subsegments of the given segment.

翻译中…

Segments $[1;1]$ and $[2;2]$ are subsegments of $[1;2]$.

翻译中…

Segments $[1;1], [2;2]$ and $[3;3]$ are subsegments of $[1;3]$.

翻译中…

Segments $[1;1], [2;2], [3;3], [2;4]$ are subsegments of $[1;4]$.

翻译中…

Segment $[3;3]$ is subsegment of $[3;4]$.

翻译中…

样例

Input #1
4 1
1 1 1 2
1 1 1 1
4
1 2
1 3
1 4
3 4
Output #1
2
3
4
1
InputCopy OutputCopy
Input #2
4 0
1 2 1 2
0 0 0 0
1
1 4
Output #2
10
InputCopy OutputCopy
题目信息
题号
877F
时间限制
2 s
内存限制
256 MB
标签
数据结构
网络流
哈希

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。