A - Password Verification

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 266

問題文

高橋君はあるシステムの管理者です。このシステムにログインするためには、長さ N の英小文字からなるパスワードを正しく入力する必要があります。

しかし、サーバーの障害により、パスワードを保存していたデータの一部が破損してしまいました。幸い、パスワードの M 文字分は復旧に成功しました。具体的には、i 番目(1 \leq i \leq M)の復旧データとして、パスワードの P_i 文字目が文字 C_i であることが判明しています。それ以外の位置の文字は不明です。

高橋君は応急処置として、以下のようにパスワード照合システムを修正しました:

  • 入力された長さ N の文字列の各位置 j1 \leq j \leq N)について、位置 j のデータが復旧されている場合(すなわち、ある i に対して P_i = j である場合)、入力文字列の j 文字目が C_i と一致しているかを確認する。
  • 位置 j のデータが復旧されていない場合、その位置はどんな文字でも受理する。
  • すべての復旧済み位置で文字が一致していれば、パスワードは正しいと判定する。

青木君がこのシステムにログインを試みており、Q 個の候補文字列 T_1, T_2, \ldots, T_Q を用意しています。それぞれの候補文字列について、パスワードとして正しいと判定されるかどうかを判定してください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq N
  • 1 \leq Q \leq 10^3
  • 1 \leq P_i \leq N
  • P_i はすべて互いに異なる
  • C_i は英小文字
  • T_j は長さ N の英小文字からなる文字列

入力

N M Q
P_1 C_1
P_2 C_2
\vdots
P_M C_M
T_1
T_2
\vdots
T_Q
  • 1 行目には、パスワードの長さを表す整数 N、復旧できた文字数を表す整数 M、候補文字列の数を表す整数 Q が、スペース区切りで与えられる。
  • 続く M 行では、復旧できた各文字の情報が与えられる。M = 0 の場合、この部分は存在しない。
  • このうち i 行目(1 \leq i \leq M)では、復旧データの位置を表す整数 P_i1 以上 N 以下)と、その位置の英小文字 C_i が、スペース区切りで与えられる。
  • すべての P_i は互いに異なる。
  • 続く Q 行では、青木君が試す候補文字列が 1 行に 1 つずつ与えられる。
  • このうち j 行目(1 \leq j \leq Q)には、長さ N の英小文字からなる文字列 T_j が与えられる。

出力

Q 行出力せよ。j 行目には、候補文字列 T_j がパスワードとして正しいと判定される場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

5 2 3
1 a
4 d
abcde
xbcde
abcdz

出力例 1

Yes
No
Yes

入力例 2

3 0 2
abc
xyz

出力例 2

Yes
Yes

入力例 3

10 4 5
2 b
5 e
7 g
10 z
abcdefghiz
xbxdexgxxz
abcdeagxyz
xbxdexhxxz
abcdefghij

出力例 3

Yes
Yes
Yes
No
No

Score : 266 pts

Problem Statement

Takahashi is the administrator of a certain system. To log in to this system, one must correctly enter a password consisting of lowercase English letters of length N.

However, due to a server failure, part of the data storing the password has been corrupted. Fortunately, M characters of the password were successfully recovered. Specifically, for the i-th (1 \leq i \leq M) piece of recovered data, it is known that the P_i-th character of the password is the character C_i. The characters at all other positions are unknown.

As a temporary fix, Takahashi modified the password verification system as follows:

  • For each position j (1 \leq j \leq N) of the input string of length N: if the data at position j has been recovered (that is, P_i = j for some i), check whether the j-th character of the input string matches C_i.
  • If the data at position j has not been recovered, any character is accepted at that position.
  • If the characters match at all recovered positions, the password is judged to be correct.

Aoki is attempting to log in to this system and has prepared Q candidate strings T_1, T_2, \ldots, T_Q. For each candidate string, determine whether it would be judged as a correct password.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq M \leq N
  • 1 \leq Q \leq 10^3
  • 1 \leq P_i \leq N
  • All P_i are distinct
  • C_i is a lowercase English letter
  • Each T_j is a string of length N consisting of lowercase English letters

Input

N M Q
P_1 C_1
P_2 C_2
\vdots
P_M C_M
T_1
T_2
\vdots
T_Q
  • The first line contains three space-separated integers: N representing the length of the password, M representing the number of recovered characters, and Q representing the number of candidate strings.
  • The following M lines give information about each recovered character. If M = 0, this part does not exist.
  • The i-th line (1 \leq i \leq M) contains a space-separated integer P_i (1 or greater and N or less) representing the position of the recovered data, and a lowercase English letter C_i at that position.
  • All P_i are distinct.
  • The following Q lines give the candidate strings that Aoki will try, one per line.
  • The j-th line (1 \leq j \leq Q) contains a string T_j of length N consisting of lowercase English letters.

Output

Output Q lines. On the j-th line, output Yes if the candidate string T_j is judged to be a correct password, and No otherwise.


Sample Input 1

5 2 3
1 a
4 d
abcde
xbcde
abcdz

Sample Output 1

Yes
No
Yes

Sample Input 2

3 0 2
abc
xyz

Sample Output 2

Yes
Yes

Sample Input 3

10 4 5
2 b
5 e
7 g
10 z
abcdefghiz
xbxdexgxxz
abcdeagxyz
xbxdexhxxz
abcdefghij

Sample Output 3

Yes
Yes
Yes
No
No
B - Student Grade Management

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は学校の教務担当として、生徒たちのテスト結果を管理しています。この学校には N 人の生徒がおり、各生徒は 1 から N までの出席番号で識別されます。

先日、複数回にわたって小テストが実施され、合計 M 件の答案が提出されました。各答案には、提出した生徒の出席番号と、その答案の得点( 0 以上 100 以下の整数)が記録されています。同じ生徒が複数の答案を提出している場合もあれば、 1 件も答案を提出していない生徒(すべての小テストを欠席した生徒)もいる可能性があります。

高橋君は、成績が振るわない生徒を把握するために、以下の基準で「要補習生徒」を特定することにしました。

1 件以上の答案を提出した生徒について、その生徒が提出したすべての答案の得点の算術平均が、あらかじめ定められた基準点 T 未満であるとき、その生徒を「要補習生徒」とみなします。 1 件も答案を提出していない生徒は、判定の対象外とし、要補習生徒には含めません。

高橋君に代わって、要補習生徒の人数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq T \leq 100
  • 1 \leq c_i \leq N (1 \leq i \leq M)
  • 0 \leq s_i \leq 100 (1 \leq i \leq M)
  • 入力はすべて整数である。

入力

N M T
c_1 s_1
c_2 s_2
\vdots
c_M s_M
  • 1 行目には、生徒の人数 N 、答案の総数 M 、基準点 T が、スペース区切りで与えられる。
  • 続く M 行のうち i 行目 (1 \leq i \leq M) には、 i 番目の答案を提出した生徒の出席番号 c_i と、その答案の得点 s_i が、スペース区切りで与えられる。

出力

要補習生徒の人数を 1 行で出力せよ。


入力例 1

5 7 60
1 50
1 40
2 80
2 70
3 55
4 60
4 65

出力例 1

2

入力例 2

8 10 50
1 50
1 50
2 49
2 51
3 0
3 0
4 100
5 30
5 20
6 49

出力例 2

3

入力例 3

10 15 70
1 90
1 80
1 70
2 60
2 50
3 100
4 65
4 70
4 68
6 40
6 30
7 70
8 69
8 71
9 0

出力例 3

4

Score : 300 pts

Problem Statement

Takahashi is in charge of academic affairs at a school and manages the students' test results. There are N students in this school, and each student is identified by a student number from 1 to N.

Recently, multiple quizzes were administered, and a total of M answer sheets were submitted. Each answer sheet records the student number of the student who submitted it and the score of that answer sheet (an integer between 0 and 100, inclusive). It is possible that the same student submitted multiple answer sheets, and there may also be students who did not submit any answer sheets (students who were absent from all quizzes).

To identify students with poor performance, Takahashi decided to determine "students requiring supplementary lessons" based on the following criteria:

For students who submitted at least one answer sheet, if the arithmetic mean of the scores of all answer sheets submitted by that student is strictly less than a predetermined threshold score T, that student is considered a "student requiring supplementary lessons." Students who did not submit any answer sheets are excluded from the evaluation and are not counted as students requiring supplementary lessons.

On behalf of Takahashi, find the number of students requiring supplementary lessons.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq T \leq 100
  • 1 \leq c_i \leq N (1 \leq i \leq M)
  • 0 \leq s_i \leq 100 (1 \leq i \leq M)
  • All input values are integers.

Input

N M T
c_1 s_1
c_2 s_2
\vdots
c_M s_M
  • The first line contains the number of students N, the total number of answer sheets M, and the threshold score T, separated by spaces.
  • The i-th of the following M lines (1 \leq i \leq M) contains the student number c_i of the student who submitted the i-th answer sheet and the score s_i of that answer sheet, separated by spaces.

Output

Output the number of students requiring supplementary lessons in a single line.


Sample Input 1

5 7 60
1 50
1 40
2 80
2 70
3 55
4 60
4 65

Sample Output 1

2

Sample Input 2

8 10 50
1 50
1 50
2 49
2 51
3 0
3 0
4 100
5 30
5 20
6 49

Sample Output 2

3

Sample Input 3

10 15 70
1 90
1 80
1 70
2 60
2 50
3 100
4 65
4 70
4 68
6 40
6 30
7 70
8 69
8 71
9 0

Sample Output 3

4
C - Organizing the Bookshelf

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

高橋君は自宅の本棚を整理しています。本棚には N 冊の本が一列に並んでおり、左から順に 1, 2, \ldots, N の番号が付けられています。本 i の満足度は A_i 、重さは B_i です。

高橋君は、本棚から 1冊以上の連続する本 を選んで取り出し、新しい棚に移動させようとしています。具体的には、整数 l, r1 \leq l \leq r \leq N )を選び、本 l, l+1, \ldots, r をすべて取り出します。

ただし、高橋君が一度に運べる重さには限界があり、選んだ区間に含まれる本の重さの合計が K 以下でなければなりません。すなわち、

B_l + B_{l+1} + \cdots + B_r \leq K

を満たす必要があります。

この条件を満たすような l, r の選び方のうち、満足度の合計 A_l + A_{l+1} + \cdots + A_r が最大となる値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{15}
  • 1 \leq A_i \leq 10^91 \leq i \leq N
  • 1 \leq B_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である
  • 重さの合計が K 以下となる区間 (l, r)1 \leq l \leq r \leq N )が少なくとも1つ存在する

入力

N K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
  • 1 行目には、本の冊数を表す整数 N と、重さの上限を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各本の満足度を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 3 行目には、各本の重さを表す整数 B_1, B_2, \ldots, B_N が、スペース区切りで与えられる。

出力

重さの合計が K 以下となる連続区間を選んだときの、満足度の合計の最大値を 1 行で出力せよ。


入力例 1

5 10
3 5 2 8 1
4 3 2 5 6

出力例 1

15

入力例 2

8 20
1 4 6 2 10 3 7 5
5 3 4 8 6 2 3 9

出力例 2

25

入力例 3

10 1000000000000000
100 200 300 400 500 600 700 800 900 1000
1 1 1 1 1 1 1 1 1 1

出力例 3

5500

Score : 366 pts

Problem Statement

Takahashi is organizing the bookshelf in his house. The bookshelf contains N books arranged in a row, numbered 1, 2, \ldots, N from left to right. Book i has a satisfaction value of A_i and a weight of B_i.

Takahashi wants to select one or more consecutive books from the bookshelf and move them to a new shelf. Specifically, he chooses integers l, r (1 \leq l \leq r \leq N) and takes out all books l, l+1, \ldots, r.

However, there is a limit to the weight Takahashi can carry at once, so the total weight of the books in the selected interval must be at most K. That is, the following condition must be satisfied:

B_l + B_{l+1} + \cdots + B_r \leq K

Among all choices of l, r that satisfy this condition, find the maximum value of the total satisfaction A_l + A_{l+1} + \cdots + A_r.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{15}
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq B_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers
  • There exists at least one interval (l, r) (1 \leq l \leq r \leq N) such that the total weight is at most K

Input

N K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
  • The first line contains an integer N representing the number of books and an integer K representing the weight limit, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the satisfaction values of each book, separated by spaces.
  • The third line contains integers B_1, B_2, \ldots, B_N representing the weights of each book, separated by spaces.

Output

Print in one line the maximum total satisfaction when selecting a contiguous interval whose total weight is at most K.


Sample Input 1

5 10
3 5 2 8 1
4 3 2 5 6

Sample Output 1

15

Sample Input 2

8 20
1 4 6 2 10 3 7 5
5 3 4 8 6 2 3 9

Sample Output 2

25

Sample Input 3

10 1000000000000000
100 200 300 400 500 600 700 800 900 1000
1 1 1 1 1 1 1 1 1 1

Sample Output 3

5500
D - Fastest Delivery Route

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は配達員として働いています。今日は荷物を届けるために、できるだけ早くお届け先に向かわなければなりません。

配達エリアには N 個の地点があり、地点は 1 から N まで番号が付けられています。高橋君は地点 1 にある配送センターからスタートし、地点 N にあるお届け先を目指します。

地点間には M 本の一方通行の道路があります。i 番目の道路(1 \leq i \leq M)は地点 U_i から地点 V_i へ向かう一方通行で、この道路を通るのに C_i の時間がかかります。同じ地点の組 (U_i, V_i) に対して複数の道路が存在することもあります。

高橋君は同じ地点を複数回通ることもできます。配送センター(地点 1)からお届け先(地点 N)までの最短所要時間を求めてください。

なお、地点 1 から地点 N へ到達可能であることは保証されています。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq U_i \leq N
  • 1 \leq V_i \leq N
  • U_i \neq V_i
  • 1 \leq C_i \leq 10^9
  • 同じ (U_i, V_i) の組が複数回与えられることがある
  • 地点 1 から地点 N へ到達可能である
  • 入力はすべて整数である

入力

N M
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M
  • 1 行目には、地点の数 N と道路の数 M がスペース区切りで与えられる。
  • 続く M 行のうち i 行目(1 \leq i \leq M)には、i 番目の道路の始点 U_i、終点 V_i、通過にかかる時間 C_i がスペース区切りで与えられる。この道路は地点 U_i から地点 V_i への一方通行である。

出力

地点 1 から地点 N へ到達するまでの最短所要時間を整数で 1 行に出力せよ。


入力例 1

4 5
1 2 3
1 3 5
2 3 1
2 4 10
3 4 2

出力例 1

6

入力例 2

6 9
1 2 7
1 3 9
1 4 14
2 3 10
2 5 15
3 4 2
3 5 11
4 5 9
5 6 6

出力例 2

26

入力例 3

10 15
1 2 1000000000
1 3 500000000
2 4 300000000
3 4 200000000
3 5 400000000
4 6 100000000
5 6 150000000
5 7 250000000
6 7 50000000
6 8 200000000
7 8 100000000
7 9 300000000
8 9 150000000
8 10 400000000
9 10 100000000

出力例 3

1200000000

Score : 400 pts

Problem Statement

Takahashi works as a delivery person. Today, he must head to the delivery destination as quickly as possible to deliver a package.

The delivery area contains N locations, numbered from 1 to N. Takahashi starts from the distribution center at location 1 and aims to reach the delivery destination at location N.

There are M one-way roads between the locations. The i-th road (1 \leq i \leq M) is a one-way road from location U_i to location V_i, and it takes C_i time to travel along this road. There may be multiple roads for the same pair of locations (U_i, V_i).

Takahashi may pass through the same location more than once. Find the shortest time required to travel from the distribution center (location 1) to the delivery destination (location N).

It is guaranteed that location N is reachable from location 1.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq U_i \leq N
  • 1 \leq V_i \leq N
  • U_i \neq V_i
  • 1 \leq C_i \leq 10^9
  • The same pair (U_i, V_i) may be given multiple times
  • Location N is reachable from location 1
  • All input values are integers

Input

N M
U_1 V_1 C_1
U_2 V_2 C_2
\vdots
U_M V_M C_M
  • The first line contains the number of locations N and the number of roads M, separated by a space.
  • The following M lines each describe a road: the i-th of these lines (1 \leq i \leq M) contains the starting point U_i, the ending point V_i, and the travel time C_i of the i-th road, separated by spaces. This road is one-way from location U_i to location V_i.

Output

Print the shortest time required to travel from location 1 to location N as an integer on a single line.


Sample Input 1

4 5
1 2 3
1 3 5
2 3 1
2 4 10
3 4 2

Sample Output 1

6

Sample Input 2

6 9
1 2 7
1 3 9
1 4 14
2 3 10
2 5 15
3 4 2
3 5 11
4 5 9
5 6 6

Sample Output 2

26

Sample Input 3

10 15
1 2 1000000000
1 3 500000000
2 4 300000000
3 4 200000000
3 5 400000000
4 6 100000000
5 6 150000000
5 7 250000000
6 7 50000000
6 8 200000000
7 8 100000000
7 9 300000000
8 9 150000000
8 10 400000000
9 10 100000000

Sample Output 3

1200000000
E - Library Book Search

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466

問題文

高橋君はとある図書館の司書です。この図書館には N 個の棚があり、各棚には 1 から N までの番号が付けられています。

この図書館には M 冊の本が所蔵されています。1 つの棚に複数の本が置かれていることもあります。各本 i (1 \leq i \leq M) について、その本が置かれている棚の番号 S_i と、ページ数 D_i が記録されています。

青木君は図書館の利用者です。青木君は Q 回の検索を行います。各検索 j (1 \leq j \leq Q) では、棚番号の区間 [L_j, R_j] とページ数の下限 T_j を指定し、「棚の番号が L_j 以上 R_j 以下である棚に置かれている本のうち、ページ数が T_j 以上である本の冊数」を調べたいと考えています。

しかし、図書館の検索システムには不具合があり、検索結果として表示される冊数は、実際の冊数から非負整数 K を引いた値になってしまいます。ただし、結果が負になる場合は 0 と表示されます。

各検索に対して、システムが表示する値を求めてください。

すなわち、棚の番号が L_j 以上 R_j 以下であり、かつページ数が T_j 以上である本の冊数を C_j としたとき、\max(C_j - K, 0) を出力してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq Q \leq 10^5
  • 0 \leq K \leq M
  • 1 \leq S_i \leq N (1 \leq i \leq M)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq M)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
  • 1 \leq T_j \leq 10^9 (1 \leq j \leq Q)
  • 入力はすべて整数である。

入力

N M Q K
S_1 D_1
S_2 D_2
\vdots
S_M D_M
L_1 R_1 T_1
L_2 R_2 T_2
\vdots
L_Q R_Q T_Q
  • 1 行目には、棚の数 N、本の冊数 M、検索の回数 Q、システムが差し引く非負整数 K が、スペース区切りで与えられる。
  • 続く M 行のうち i 番目の行 (1 \leq i \leq M) には、i 冊目の本が置かれている棚の番号 S_i とページ数 D_i が、スペース区切りで与えられる。
  • 続く Q 行のうち j 番目の行 (1 \leq j \leq Q) には、j 番目の検索における棚番号の区間の左端 L_j、右端 R_j、ページ数の下限 T_j が、スペース区切りで与えられる。

出力

Q 行出力せよ。j 行目 (1 \leq j \leq Q) には、j 番目の検索に対してシステムが表示する値を出力せよ。


入力例 1

3 4 3 1
1 100
1 50
2 200
3 150
1 2 100
2 3 150
1 3 10

出力例 1

1
1
3

入力例 2

4 3 4 0
4 120
2 80
4 200
1 1 50
1 3 100
3 4 150
2 2 1

出力例 2

0
0
1
1

入力例 3

8 12 8 2
1 300
2 120
2 500
3 450
3 200
4 600
5 50
5 700
6 400
7 800
8 10
8 900
1 8 400
2 5 200
5 8 600
1 3 1000
4 4 1
6 8 350
3 7 750
8 8 5

出力例 3

5
3
1
0
0
1
0
0

入力例 4

20 30 15 3
1 100
1 500
2 250
2 800
3 150
3 900
4 400
4 50
5 1000
5 300
6 600
6 610
7 200
7 700
8 720
9 330
9 340
10 100
10 10000
11 450
12 460
12 470
13 480
14 490
15 5000
16 510
17 520
18 530
19 540
20 550
1 20 500
1 5 200
6 10 600
10 10 1000
11 15 460
16 20 525
3 7 700
8 12 50
13 17 480
2 2 1
4 9 1000
15 15 4999
1 20 10001
9 14 335
5 16 510

出力例 4

12
4
2
0
2
0
0
5
2
0
0
0
0
4
5

入力例 5

1 1 5 1
1 1000000000
1 1 1
1 1 999999999
1 1 1000000000
1 1 1000000000
1 1 1000000000

出力例 5

0
0
0
0
0

Score : 466 pts

Problem Statement

Takahashi is a librarian at a certain library. This library has N shelves, each numbered from 1 to N.

The library holds M books in its collection. Multiple books may be placed on the same shelf. For each book i (1 \leq i \leq M), the shelf number S_i where the book is placed and its page count D_i are recorded.

Aoki is a library user. Aoki performs Q searches. For each search j (1 \leq j \leq Q), he specifies a shelf number range [L_j, R_j] and a minimum page count T_j, and wants to find "the number of books placed on shelves with numbers between L_j and R_j (inclusive) that have a page count of at least T_j."

However, the library's search system has a bug: the number of books displayed as the search result is the actual count minus a non-negative integer K. If the result would be negative, 0 is displayed instead.

For each search, determine the value displayed by the system.

That is, letting C_j be the number of books placed on shelves with numbers between L_j and R_j (inclusive) and with a page count of at least T_j, output \max(C_j - K, 0).

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq Q \leq 10^5
  • 0 \leq K \leq M
  • 1 \leq S_i \leq N (1 \leq i \leq M)
  • 1 \leq D_i \leq 10^9 (1 \leq i \leq M)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq Q)
  • 1 \leq T_j \leq 10^9 (1 \leq j \leq Q)
  • All input values are integers.

Input

N M Q K
S_1 D_1
S_2 D_2
\vdots
S_M D_M
L_1 R_1 T_1
L_2 R_2 T_2
\vdots
L_Q R_Q T_Q
  • The first line contains the number of shelves N, the number of books M, the number of searches Q, and the non-negative integer K subtracted by the system, separated by spaces.
  • The i-th of the following M lines (1 \leq i \leq M) contains the shelf number S_i where the i-th book is placed and its page count D_i, separated by a space.
  • The j-th of the following Q lines (1 \leq j \leq Q) contains the left endpoint L_j and right endpoint R_j of the shelf number range, and the minimum page count T_j for the j-th search, separated by spaces.

Output

Output Q lines. The j-th line (1 \leq j \leq Q) should contain the value displayed by the system for the j-th search.


Sample Input 1

3 4 3 1
1 100
1 50
2 200
3 150
1 2 100
2 3 150
1 3 10

Sample Output 1

1
1
3

Sample Input 2

4 3 4 0
4 120
2 80
4 200
1 1 50
1 3 100
3 4 150
2 2 1

Sample Output 2

0
0
1
1

Sample Input 3

8 12 8 2
1 300
2 120
2 500
3 450
3 200
4 600
5 50
5 700
6 400
7 800
8 10
8 900
1 8 400
2 5 200
5 8 600
1 3 1000
4 4 1
6 8 350
3 7 750
8 8 5

Sample Output 3

5
3
1
0
0
1
0
0

Sample Input 4

20 30 15 3
1 100
1 500
2 250
2 800
3 150
3 900
4 400
4 50
5 1000
5 300
6 600
6 610
7 200
7 700
8 720
9 330
9 340
10 100
10 10000
11 450
12 460
12 470
13 480
14 490
15 5000
16 510
17 520
18 530
19 540
20 550
1 20 500
1 5 200
6 10 600
10 10 1000
11 15 460
16 20 525
3 7 700
8 12 50
13 17 480
2 2 1
4 9 1000
15 15 4999
1 20 10001
9 14 335
5 16 510

Sample Output 4

12
4
2
0
2
0
0
5
2
0
0
0
0
4
5

Sample Input 5

1 1 5 1
1 1000000000
1 1 1
1 1 999999999
1 1 1000000000
1 1 1000000000
1 1 1000000000

Sample Output 5

0
0
0
0
0