CF877F.Ann and Books

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

In Ann's favorite book shop are as many as nn books on math and economics. Books are numbered from 11 to nn . Each of them contains non-negative number of problems.

Today there is a sale: any subsegment of a segment from ll to rr 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 kk . Note that kk may be positive, negative or zero.

Unfortunately, Ann is not sure on which segment the sale operates, but she has qq 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 kk .

输入格式

The first line contains two integers nn and kk ( 1<=n<=1000001<=n<=100000 , 109<=k<=109-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 nn integers t1,t2,...,tnt_{1},t_{2},...,t_{n} ( 1<=ti<=21<=t_{i}<=2 ), where tit_{i} is 11 if the ii -th book is on math or 22 if the ii -th is on economics.

The third line contains nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} ( 0<=ai<=1090<=a_{i}<=10^{9} ), where aia_{i} is the number of problems in the ii -th book.

The fourth line contains a single integer qq ( 1<=q<=1000001<=q<=100000 ) — the number of assumptions.

Each of the next qq lines contains two integers lil_{i} and rir_{i} ( 1<=li<=ri<=n1<=l_{i}<=r_{i}<=n ) describing the ii -th Ann's assumption.

输出格式

Print qq lines, in the ii -th of them print the number of subsegments for the ii -th Ann's assumption.

输入输出样例

  • 输入#1

    4 1
    1 1 1 2
    1 1 1 1
    4
    1 2
    1 3
    1 4
    3 4
    

    输出#1

    2
    3
    4
    1
    
  • 输入#2

    4 0
    1 2 1 2
    0 0 0 0
    1
    1 4
    

    输出#2

    10
    

说明/提示

In the first sample Ann can buy subsegments [1;1],[2;2],[3;3],[2;4][1;1],[2;2],[3;3],[2;4] if they fall into the sales segment, because the number of math problems is greater by 11 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][1;1] and [2;2][2;2] are subsegments of [1;2][1;2] .

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

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

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

首页