PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000068. Ann và những cuốn sách (Ann and Books)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ann và những cuốn sách (Ann and Books)

Ann and Books

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn books, with type ti=1t_i=1 (math) or ti=2t_i=2 (economics) and aia_i exercises. For each range [l,r][l,r], count nonempty subarrays whose total math exercises minus total economics exercises equals kk; kk can be negative or zero.

Input

First n,kn,k, then all book types, all exercise counts, the number qq, and qq inclusive ranges.

Output

Print one 64-bit count per query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤401\le n\le 40, 1≤q≤601\le q\le 60. ti∈{1,2}t_i\in\{1,2\}; 0≤ai≤1090\le a_i\le10^9; −109≤k≤109-10^9\le k\le10^9; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (20 points): 1≤n≤25001\le n\le 2500, 1≤q≤25001\le q\le 2500. ti∈{1,2}t_i\in\{1,2\}; 0≤ai≤1090\le a_i\le10^9; −109≤k≤109-10^9\le k\le10^9; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (30 points): 1≤n≤200001\le n\le 20000, 1≤q≤200001\le q\le 20000. ti∈{1,2}t_i\in\{1,2\}; 0≤ai≤1090\le a_i\le10^9; −109≤k≤109-10^9\le k\le10^9; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (40 points): 1≤n≤1000001\le n\le 100000, 1≤q≤1000001\le q\le 100000. ti∈{1,2}t_i\in\{1,2\}; 0≤ai≤1090\le a_i\le10^9; −109≤k≤109-10^9\le k\le10^9; 1≤l≤r≤n1\le l\le r\le n.

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

Explanation

The signed sequence is [1,1,1,−1][1,1,1,-1]. Within [1,4][1,4], the four subarrays of sum 1 are [1,1][1,1], [2,2][2,2], [3,3][3,3], and [2,4][2,4]; the other queries yield 2, 3, and 1.