Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 233 点
問題文
高橋君はフリーマーケットに出店しようとしています。手元には N 個の商品があり、 i 番目の商品の販売価格は A_i 円です。
ただし、各商品を販売するためには、梱包や陳列などの準備が必要です。 i 番目の商品の準備コストは B_i 円です。
高橋君は N 個の商品の中からいくつかの商品を選んで販売することができます。1 つも選ばなくても構いません。各商品について販売するかしないかを独立に決められますが、同じ商品を複数回販売することはできません。
商品の集合 S を選んで販売したときの利益は次のように計算されます。
\text{利益} = \sum_{i \in S} A_i - \sum_{i \in S} B_i
なお、1 つも商品を選ばない場合の利益は 0 円です。
高橋君が得られる利益の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 入力はすべて整数である。
入力
N A_1 B_1 A_2 B_2 \vdots A_N B_N
- 1 行目には、商品の個数を表す整数 N が与えられる。
- 続く N 行のうち i 行目 (1 \leq i \leq N) には、 i 番目の商品の販売価格 A_i と準備コスト B_i がスペース区切りで与えられる。
出力
利益の最大値を 1 行で出力せよ。
入力例 1
3 10 3 5 8 7 2
出力例 1
12
入力例 2
3 3 5 2 7 1 4
出力例 2
0
入力例 3
10 100 50 30 80 200 199 15 15 500 100 60 120 45 30 90 90 300 250 10 1000
出力例 3
516
入力例 4
20 1000000000 1 999999999 1000000000 500000000 400000000 300000000 300000001 750000000 250000000 100 200 999999999 999999998 123456789 987654321 800000000 100000000 450000000 450000000 600000000 500000000 50000000 90000000 1 1 700000000 650000000 350000000 400000000 200000000 100000000 888888888 111111111 10 10 500000000 500000001 999999999 2
出力例 4
4327777774
入力例 5
1 1 1000000000
出力例 5
0
Score : 233 pts
Problem Statement
Takahashi is planning to set up a booth at a flea market. He has N items on hand, and the selling price of the i-th item is A_i yen.
However, each item requires preparation such as packaging and display before it can be sold. The preparation cost of the i-th item is B_i yen.
Takahashi can choose some items from the N items to sell. He may choose not to sell any items at all. He can independently decide whether or not to sell each item, but he cannot sell the same item more than once.
When he chooses a set of items S to sell, the profit is calculated as follows:
\text{Profit} = \sum_{i \in S} A_i - \sum_{i \in S} B_i
If no items are chosen, the profit is 0 yen.
Find the maximum profit that Takahashi can obtain.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- All input values are integers.
Input
N A_1 B_1 A_2 B_2 \vdots A_N B_N
- The first line contains an integer N, representing the number of items.
- The i-th of the following N lines (1 \leq i \leq N) contains the selling price A_i and the preparation cost B_i of the i-th item, separated by a space.
Output
Print the maximum profit in a single line.
Sample Input 1
3 10 3 5 8 7 2
Sample Output 1
12
Sample Input 2
3 3 5 2 7 1 4
Sample Output 2
0
Sample Input 3
10 100 50 30 80 200 199 15 15 500 100 60 120 45 30 90 90 300 250 10 1000
Sample Output 3
516
Sample Input 4
20 1000000000 1 999999999 1000000000 500000000 400000000 300000000 300000001 750000000 250000000 100 200 999999999 999999998 123456789 987654321 800000000 100000000 450000000 450000000 600000000 500000000 50000000 90000000 1 1 700000000 650000000 350000000 400000000 200000000 100000000 888888888 111111111 10 10 500000000 500000001 999999999 2
Sample Output 4
4327777774
Sample Input 5
1 1 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は宇宙探査ロボットを操作して、惑星の表面を調査するミッションに参加しています。ロボットは基地からゴール地点まで一直線のルートを進む必要があります。
ルートの全長は L メートルで、ルート沿いには N 箇所のエネルギーステーションが設置されています。i 番目のエネルギーステーションは基地から P_i メートルの位置にあり、そこでバッテリーを W_i ユニット充電することができます。
ロボットのバッテリー容量は C ユニットで、出発時にはバッテリーが満充電の状態です。ロボットは 1 メートル進むごとに 1 ユニットのエネルギーを消費します。エネルギーステーションに到着したとき、そこで得られるエネルギーをバッテリーに充電できますが、バッテリー容量を超えた分は充電されません。
高橋君はミッション開始前に、各エネルギーステーションの位置と充電量を確認しました。しかし、途中でバッテリーが尽きてしまうとロボットは停止し、ミッション失敗となります。
ロボットがゴール地点に到達したとき、バッテリーに残っているエネルギーの最大値を求めてください。もしゴールに到達できない場合は -1 を出力してください。
制約
- 1 \leq L \leq 10^9
- 0 \leq N \leq 2 \times 10^5
- 1 \leq C \leq 10^9
- 1 \leq P_i \leq L - 1 (1 \leq i \leq N)
- 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
- P_1 < P_2 < \cdots < P_N(エネルギーステーションは基地からの距離の昇順で与えられる)
- 入力はすべて整数
入力
L N C P_1 W_1 P_2 W_2 : P_N W_N
- 1 行目には、ルートの全長を表す L 、エネルギーステーションの数を表す N 、バッテリー容量を表す C が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各エネルギーステーションの情報が与えられる。
- 1 + i 行目では、i 番目のエネルギーステーションの基地からの距離 P_i と、そこで得られるエネルギー量 W_i が、スペース区切りで与えられる。
- エネルギーステーションは基地からの距離の昇順で与えられる。
出力
ロボットがゴール地点に到達したときのバッテリーに残っているエネルギーの最大値を 1 行で出力せよ。ゴールに到達できない場合は -1 を出力せよ。
入力例 1
10 2 8 3 5 7 4
出力例 1
5
入力例 2
20 3 10 5 3 9 2 14 4
出力例 2
-1
入力例 3
100 5 50 10 30 25 20 40 35 60 25 80 15
出力例 3
25
Score : 300 pts
Problem Statement
Takahashi is participating in a mission to investigate the surface of a planet by operating a space exploration robot. The robot needs to travel along a straight route from the base to the goal point.
The total length of the route is L meters, and N energy stations are installed along the route. The i-th energy station is located P_i meters from the base, where the battery can be charged by W_i units.
The robot's battery capacity is C units, and the battery is fully charged at departure. The robot consumes 1 unit of energy for every 1 meter it travels. When the robot arrives at an energy station, it can charge the available energy into its battery, but any amount exceeding the battery capacity will not be charged.
Before the mission begins, Takahashi checked the position and charge amount of each energy station. However, if the battery runs out along the way, the robot will stop and the mission will fail.
Find the maximum amount of energy remaining in the battery when the robot reaches the goal point. If the robot cannot reach the goal, output -1.
Constraints
- 1 \leq L \leq 10^9
- 0 \leq N \leq 2 \times 10^5
- 1 \leq C \leq 10^9
- 1 \leq P_i \leq L - 1 (1 \leq i \leq N)
- 1 \leq W_i \leq 10^9 (1 \leq i \leq N)
- P_1 < P_2 < \cdots < P_N (Energy stations are given in ascending order of distance from the base)
- All input values are integers
Input
L N C P_1 W_1 P_2 W_2 : P_N W_N
- The first line contains L representing the total length of the route, N representing the number of energy stations, and C representing the battery capacity, separated by spaces.
- From the 2nd line to the (N + 1)-th line, the information of each energy station is given.
- The (1 + i)-th line contains the distance P_i of the i-th energy station from the base and the energy amount W_i available there, separated by spaces.
- Energy stations are given in ascending order of distance from the base.
Output
Output in one line the maximum amount of energy remaining in the battery when the robot reaches the goal point. If the robot cannot reach the goal, output -1.
Sample Input 1
10 2 8 3 5 7 4
Sample Output 1
5
Sample Input 2
20 3 10 5 3 9 2 14 4
Sample Output 2
-1
Sample Input 3
100 5 50 10 30 25 20 40 35 60 25 80 15
Sample Output 3
25
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は、N 個のノードと M 本の有向辺からなるグラフを設計しました。ノードには 1 から N までの番号が付いています。
各ノード i には、数直線上の区間 [L_i, R_i] が割り当てられています。また、各ノード i には到着位置 P_i と出発位置 Q_i が記録されています。
各有向辺には始点 U_j、終点 V_j、および分岐ラベル B_j(0 または 1)が付いています。同じ (U_j, V_j, B_j) の組が複数回現れる場合も、それぞれ別の辺として扱います。
このグラフが、次の条件をすべて満たすとき、正しい二分決定木であるとします。
-
根付き木の構造:ノード 1 を根とする根付き木になっている。すなわち、以下をすべて満たす。
- ノード 1 に入る辺(ノード 1 を終点とする辺)がない。
- ノード 2, 3, \dots, N にはそれぞれちょうど 1 本の入る辺がある。
- ノード 1 から有向辺を繰り返したどることで、すべてのノードに到達できる。
-
出次数の制約:各ノードについて、ラベル 0 の出る辺(そのノードを始点とする辺)は高々 1 本、ラベル 1 の出る辺も高々 1 本である。
-
区間の包含:各辺 u \to v について、L_u < L_v かつ R_v < R_u が成り立つ。すなわち、子の区間が親の区間に厳密に含まれる。
-
位置の整合性:各辺 u \to v について、P_v = Q_u が成り立つ。すなわち、辺の始点の出発位置と終点の到着位置が一致する。
-
左右の順序:あるノード u からラベル 0 の辺とラベル 1 の辺がともに存在するとき、それぞれの終点を x, y とすると、R_x < L_y が成り立つ。
与えられたグラフが正しい二分決定木であるか判定してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- N + M \leq 2 \times 10^5
- -10^9 \leq L_i < R_i \leq 10^9
- -10^9 \leq P_i \leq 10^9
- -10^9 \leq Q_i \leq 10^9
- 1 \leq U_j, V_j \leq N
- U_j \neq V_j
- B_j \in \{0, 1\}
- 同じ (U_j, V_j, B_j) の組が複数回与えられることがある。
- 入力はすべて整数である。
入力
N M L_1 R_1 P_1 Q_1 L_2 R_2 P_2 Q_2 \vdots L_N R_N P_N Q_N U_1 V_1 B_1 U_2 V_2 B_2 \vdots U_M V_M B_M
- 1 行目には、ノード数 N と辺数 M がスペース区切りで与えられる。
- 続く N 行では、各ノードの情報が与えられる。
- 1 + i 行目には、ノード i の区間の左端 L_i、右端 R_i、到着位置 P_i、出発位置 Q_i がスペース区切りで与えられる。
- 続く M 行では、辺の情報が与えられる。
- 1 + N + j 行目には、j 番目の辺の始点 U_j、終点 V_j、分岐ラベル B_j がスペース区切りで与えられる。
出力
与えられたグラフが正しい二分決定木であるなら YES を、そうでないなら NO を出力せよ。
入力例 1
3 2 0 10 0 100 1 3 100 200 5 8 100 300 1 2 0 1 3 1
出力例 1
YES
入力例 2
2 2 0 10 0 5 2 8 5 7 1 2 0 1 2 0
出力例 2
NO
入力例 3
10 9 -100 100 0 10 -90 -10 10 20 10 90 10 30 -80 -50 20 40 -40 -20 20 50 20 80 30 60 -75 -60 40 70 -38 -35 50 80 -30 -25 50 90 30 70 60 100 1 2 0 1 3 1 2 4 0 2 5 1 3 6 1 4 7 1 5 8 0 5 9 1 6 10 0
出力例 3
YES
入力例 4
20 19 -1000 1000 0 1 -900 -100 1 2 0 900 1 3 -850 -500 2 4 -400 -150 2 5 20 600 3 6 550 850 3 7 -840 -700 4 8 -650 -550 4 9 -350 -200 5 10 30 200 6 11 250 500 6 12 600 800 7 13 -830 -800 8 14 -640 -600 9 15 -340 -300 10 16 -280 -220 10 17 300 450 12 18 620 680 13 19 700 780 13 20 1 2 0 1 3 1 2 4 0 2 5 1 3 6 0 3 7 1 4 8 0 4 9 1 5 10 0 6 11 0 6 12 1 7 13 1 8 14 0 9 15 1 10 16 0 10 17 1 12 18 0 13 19 0 13 20 1
出力例 4
NO
入力例 5
1 0 -1000000000 1000000000 -1000000000 1000000000
出力例 5
YES
Score : 366 pts
Problem Statement
Takahashi has designed a graph consisting of N nodes and M directed edges. The nodes are numbered from 1 to N.
Each node i is assigned an interval [L_i, R_i] on the number line. Additionally, each node i has a recorded arrival position P_i and departure position Q_i.
Each directed edge has a starting node U_j, an ending node V_j, and a branch label B_j (0 or 1). Even if the same tuple (U_j, V_j, B_j) appears multiple times, each occurrence is treated as a separate edge.
This graph is called a valid binary decision tree if it satisfies all of the following conditions:
-
Rooted tree structure: The graph forms a rooted tree with node 1 as the root. That is, all of the following hold:
- There are no edges entering node 1 (no edges with node 1 as their endpoint).
- Each of nodes 2, 3, \dots, N has exactly one entering edge.
- All nodes are reachable from node 1 by repeatedly following directed edges.
-
Out-degree constraint: For each node, there is at most one outgoing edge (an edge starting from that node) with label 0, and at most one outgoing edge with label 1.
-
Interval containment: For each edge u \to v, L_u < L_v and R_v < R_u hold. That is, the child's interval is strictly contained within the parent's interval.
-
Position consistency: For each edge u \to v, P_v = Q_u holds. That is, the departure position of the edge's starting node matches the arrival position of the edge's ending node.
-
Left-right ordering: When both a label 0 edge and a label 1 edge exist from a node u, letting their respective endpoints be x and y, the condition R_x < L_y holds.
Determine whether the given graph is a valid binary decision tree.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- N + M \leq 2 \times 10^5
- -10^9 \leq L_i < R_i \leq 10^9
- -10^9 \leq P_i \leq 10^9
- -10^9 \leq Q_i \leq 10^9
- 1 \leq U_j, V_j \leq N
- U_j \neq V_j
- B_j \in \{0, 1\}
- The same tuple (U_j, V_j, B_j) may be given multiple times.
- All inputs are integers.
Input
N M L_1 R_1 P_1 Q_1 L_2 R_2 P_2 Q_2 \vdots L_N R_N P_N Q_N U_1 V_1 B_1 U_2 V_2 B_2 \vdots U_M V_M B_M
- The first line contains the number of nodes N and the number of edges M, separated by a space.
- The following N lines give the information for each node.
- The (1 + i)-th line contains the left endpoint L_i, right endpoint R_i, arrival position P_i, and departure position Q_i of node i, separated by spaces.
- The following M lines give the information for each edge.
- The (1 + N + j)-th line contains the starting node U_j, ending node V_j, and branch label B_j of the j-th edge, separated by spaces.
Output
If the given graph is a valid binary decision tree, output YES; otherwise, output NO.
Sample Input 1
3 2 0 10 0 100 1 3 100 200 5 8 100 300 1 2 0 1 3 1
Sample Output 1
YES
Sample Input 2
2 2 0 10 0 5 2 8 5 7 1 2 0 1 2 0
Sample Output 2
NO
Sample Input 3
10 9 -100 100 0 10 -90 -10 10 20 10 90 10 30 -80 -50 20 40 -40 -20 20 50 20 80 30 60 -75 -60 40 70 -38 -35 50 80 -30 -25 50 90 30 70 60 100 1 2 0 1 3 1 2 4 0 2 5 1 3 6 1 4 7 1 5 8 0 5 9 1 6 10 0
Sample Output 3
YES
Sample Input 4
20 19 -1000 1000 0 1 -900 -100 1 2 0 900 1 3 -850 -500 2 4 -400 -150 2 5 20 600 3 6 550 850 3 7 -840 -700 4 8 -650 -550 4 9 -350 -200 5 10 30 200 6 11 250 500 6 12 600 800 7 13 -830 -800 8 14 -640 -600 9 15 -340 -300 10 16 -280 -220 10 17 300 450 12 18 620 680 13 19 700 780 13 20 1 2 0 1 3 1 2 4 0 2 5 1 3 6 0 3 7 1 4 8 0 4 9 1 5 10 0 6 11 0 6 12 1 7 13 1 8 14 0 9 15 1 10 16 0 10 17 1 12 18 0 13 19 0 13 20 1
Sample Output 4
NO
Sample Input 5
1 0 -1000000000 1000000000 -1000000000 1000000000
Sample Output 5
YES
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君はイベント会場の管理者です。
N 件のイベントの予約申請が届いており、i 番目のイベント (1 \leq i \leq N) は時刻 L_i から時刻 R_i までの時間帯に会場を使用します。
2 つのイベント i, j の使用時間帯が重なるとは、ある時刻 t が存在して L_i \leq t < R_i かつ L_j \leq t < R_j を満たすことをいいます。言い換えると、各イベントの使用時間帯を半開区間 [L_i, R_i) で表したとき、2 つの区間が共通部分を持つことを意味します。したがって、一方のイベントの終了時刻と他方の開始時刻が一致する場合(端点で接する場合)は重なりとはみなしません。
会場は 1 つしかないため、使用時間帯が重なるイベントを同時に受理することはできません。高橋君は N 件のイベントのうち一部を受理し、残りを却下します。受理するイベントの集合に含まれるどの 2 つのイベントも使用時間帯が重ならないようにしなければなりません。なお、1 つも受理しない(すべて却下する)ことや、重なりがなければすべて受理することも許されます。
イベントを 1 件受理するごとに、イベントの内容によらず一律 B 万円の収益が得られます。一方、i 番目のイベントを却下すると、キャンセル補償として C_i 万円の費用がかかります。
高橋君は、受理したイベントから得られる収益の合計から、却下したイベントのキャンセル補償費用の合計を引いた利得を最大化したいです。
すなわち、受理するイベントの集合を S \subseteq \{1, 2, \ldots, N\} とし、S に含まれるどの 2 つのイベントも使用時間帯が重ならないという条件のもとで、次の値を最大化してください。
\displaystyle |S| \times B - \sum_{i \in \{1,\ldots,N\} \setminus S} C_i
この最大値を求めてください。なお、答えが負になる場合もあります。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq B \leq 10^9
- 0 \leq L_i < R_i \leq 10^9
- 1 \leq C_i \leq 10^9
- 入力はすべて整数である。
入力
N B L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_N R_N C_N
- 1 行目には、イベントの件数を表す整数 N と、受理 1 件あたりの収益(万円)を表す整数 B が、スペース区切りで与えられる。
- 2 行目から N + 1 行目では、各イベントの情報が 1 行ずつ与えられる。
- 1 + i 行目には、i 番目のイベントの使用開始時刻 L_i、使用終了時刻 R_i、却下時のキャンセル補償費用 C_i(万円)が、スペース区切りの整数で与えられる。
出力
利得の最大値を整数として 1 行で出力せよ。
入力例 1
3 10 0 3 5 2 5 3 5 8 4
出力例 1
17
入力例 2
2 1 0 5 100 1 6 100
出力例 2
-99
入力例 3
8 15 0 10 5 5 20 8 10 25 3 20 30 12 25 35 7 30 40 6 35 50 9 0 50 100
出力例 3
-35
入力例 4
15 100 0 10 50 5 15 30 10 20 40 15 25 60 20 30 20 25 35 70 30 40 10 35 45 55 40 50 25 45 55 80 50 60 15 55 65 35 60 70 45 65 75 90 70 80 65
出力例 4
450
入力例 5
1 1000000000 0 1000000000 1000000000
出力例 5
1000000000
Score : 400 pts
Problem Statement
Takahashi is the manager of an event venue.
He has received reservation requests for N events. The i-th event (1 \leq i \leq N) uses the venue during the time period from time L_i to time R_i.
Two events i, j have overlapping time periods if there exists a time t such that L_i \leq t < R_i and L_j \leq t < R_j. In other words, when each event's time period is represented as a half-open interval [L_i, R_i), the two intervals have a non-empty intersection. Therefore, if one event's end time coincides with another's start time (i.e., the intervals only touch at an endpoint), they are not considered overlapping.
Since there is only one venue, events with overlapping time periods cannot be accepted simultaneously. Takahashi will accept some of the N events and reject the rest. Any two events in the set of accepted events must have non-overlapping time periods. It is also allowed to accept no events (reject all) or, if there are no overlaps, to accept all events.
For each accepted event, a revenue of B ten-thousand yen is earned, regardless of the event's content. On the other hand, rejecting the i-th event incurs a cancellation compensation cost of C_i ten-thousand yen.
Takahashi wants to maximize the profit, which is the total revenue from accepted events minus the total cancellation compensation costs for rejected events.
Formally, let S \subseteq \{1, 2, \ldots, N\} be the set of accepted events, subject to the condition that any two events in S have non-overlapping time periods. Maximize the following value:
\displaystyle |S| \times B - \sum_{i \in \{1,\ldots,N\} \setminus S} C_i
Find this maximum value. Note that the answer may be negative.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq B \leq 10^9
- 0 \leq L_i < R_i \leq 10^9
- 1 \leq C_i \leq 10^9
- All input values are integers.
Input
N B L_1 R_1 C_1 L_2 R_2 C_2 \vdots L_N R_N C_N
- The first line contains an integer N representing the number of events and an integer B representing the revenue per accepted event (in ten-thousand yen), separated by a space.
- From the 2nd line to the (N + 1)-th line, the information for each event is given, one event per line.
- The (1 + i)-th line contains the start time L_i, end time R_i, and cancellation compensation cost C_i (in ten-thousand yen) for the i-th event, given as space-separated integers.
Output
Output the maximum profit as an integer on a single line.
Sample Input 1
3 10 0 3 5 2 5 3 5 8 4
Sample Output 1
17
Sample Input 2
2 1 0 5 100 1 6 100
Sample Output 2
-99
Sample Input 3
8 15 0 10 5 5 20 8 10 25 3 20 30 12 25 35 7 30 40 6 35 50 9 0 50 100
Sample Output 3
-35
Sample Input 4
15 100 0 10 50 5 15 30 10 20 40 15 25 60 20 30 20 25 35 70 30 40 10 35 45 55 40 50 25 45 55 80 50 60 15 55 65 35 60 70 45 65 75 90 70 80 65
Sample Output 4
450
Sample Input 5
1 1000000000 0 1000000000 1000000000
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
N 個の石が円形に並んでいます。石には時計回りに 1 から N までの番号が付いており、石 i には英小文字 S_i が刻まれています。石 N の時計回りの次は石 1 です。
開始位置 x(1 \leq x \leq N)と長さ \ell(1 \leq \ell \leq N)に対して、文字列 C(x, \ell) を次のように定義します。
- C(x, \ell):石 x から時計回りに \ell 個の石を順に読んで得られる長さ \ell の文字列。
すなわち、C(x, \ell) の k 文字目(1 \leq k \leq \ell)は石 ((x + k - 2) \bmod N) + 1 に刻まれた文字です。
高橋君の現在位置を変数 P で表します。P の初期値は 1 です。
Q 個のクエリが順に与えられます。i 番目のクエリでは整数 A_i と L_i が与えられ、以下の操作を行います。
-
移動:現在位置 P を時計回りに A_i 個先の石に更新します。すなわち、
P \leftarrow ((P + A_i - 1) \bmod N) + 1
とします(A_i = 0 のときは P は変化しません)。
-
数え上げ:次の値を求めて出力します。
\#\{\, (x, \ell) \mid 1 \leq x \leq N,\ 1 \leq \ell \leq L_i,\ C(x, \ell) < C(P, \ell) \,\}
ここで、C(x, \ell) < C(P, \ell) は長さ \ell の文字列同士の辞書順比較を意味し、辞書順は英小文字の通常の順序 \mathrm{a} < \mathrm{b} < \cdots < \mathrm{z} に従います。
言い換えると、開始位置 x(1 \leq x \leq N)と長さ \ell(1 \leq \ell \leq L_i)のすべての組 (x, \ell) のうち、C(x, \ell) が C(P, \ell) よりも辞書順で厳密に小さいものの個数を求めてください。
制約
- 1 \leq N \leq 3000
- 1 \leq Q \leq 10^5
- S は長さ N の英小文字からなる文字列
- 0 \leq A_i \leq N - 1(1 \leq i \leq Q)
- 1 \leq L_i \leq N(1 \leq i \leq Q)
- 入力はすべて整数(S を除く)
入力
N Q S A_1 L_1 A_2 L_2 \vdots A_Q L_Q
- 1 行目には、石の個数 N とクエリの個数 Q がスペース区切りで与えられる。
- 2 行目には、長さ N の英小文字列 S が与えられる。S の i 文字目 S_i は石 i に刻まれている文字を表す。
- 2 + i 行目(1 \leq i \leq Q)には、i 番目のクエリにおける移動量 A_i と長さの上限 L_i がスペース区切りで与えられる。
出力
Q 行出力してください。
i 行目には、i 番目のクエリに対する答えを整数で出力してください。
入力例 1
5 4 abaca 0 1 1 3 3 5 4 2
出力例 1
0 9 0 8
入力例 2
4 5 aaaa 0 1 1 2 2 3 3 4 0 4
出力例 2
0 0 0 0 0
入力例 3
12 8 bananaabcxyz 0 6 5 4 11 12 3 7 8 2 1 10 10 11 6 5
出力例 3
24 0 84 34 2 70 86 30
入力例 4
30 15 qwertyuiopasdfghjklzxcvbnmabcd 0 1 29 30 15 10 7 25 3 3 28 30 14 1 1 29 20 15 5 30 0 12 11 20 27 7 6 18 2 30
出力例 4
20 209 100 124 51 750 24 348 30 240 96 180 6 234 450
入力例 5
1 5 z 0 1 0 1 0 1 0 1 0 1
出力例 5
0 0 0 0 0
Score : 466 pts
Problem Statement
N stones are arranged in a circle. The stones are numbered from 1 to N in clockwise order, and stone i has the lowercase English letter S_i engraved on it. The next stone clockwise from stone N is stone 1.
For a starting position x (1 \leq x \leq N) and a length \ell (1 \leq \ell \leq N), define the string C(x, \ell) as follows:
- C(x, \ell): The string of length \ell obtained by reading \ell stones consecutively in clockwise order starting from stone x.
That is, the k-th character (1 \leq k \leq \ell) of C(x, \ell) is the character engraved on stone ((x + k - 2) \bmod N) + 1.
Takahashi's current position is represented by a variable P. The initial value of P is 1.
Q queries are given in order. In the i-th query, integers A_i and L_i are given, and the following operations are performed:
-
Move: Update the current position P to the stone A_i positions ahead in the clockwise direction. That is,
P \leftarrow ((P + A_i - 1) \bmod N) + 1
(When A_i = 0, P does not change.)
-
Count: Compute and output the following value:
\#\{\, (x, \ell) \mid 1 \leq x \leq N,\ 1 \leq \ell \leq L_i,\ C(x, \ell) < C(P, \ell) \,\}
Here, C(x, \ell) < C(P, \ell) denotes lexicographic comparison between strings of length \ell, where the lexicographic order follows the usual ordering of lowercase English letters \mathrm{a} < \mathrm{b} < \cdots < \mathrm{z}.
In other words, among all pairs (x, \ell) with starting position x (1 \leq x \leq N) and length \ell (1 \leq \ell \leq L_i), count the number of pairs for which C(x, \ell) is strictly lexicographically smaller than C(P, \ell).
Constraints
- 1 \leq N \leq 3000
- 1 \leq Q \leq 10^5
- S is a string of length N consisting of lowercase English letters
- 0 \leq A_i \leq N - 1 (1 \leq i \leq Q)
- 1 \leq L_i \leq N (1 \leq i \leq Q)
- All inputs are integers (except for S)
Input
N Q S A_1 L_1 A_2 L_2 \vdots A_Q L_Q
- The first line contains the number of stones N and the number of queries Q, separated by a space.
- The second line contains a lowercase English string S of length N. The i-th character S_i of S represents the character engraved on stone i.
- The (2 + i)-th line (1 \leq i \leq Q) contains the movement amount A_i and the upper bound on length L_i for the i-th query, separated by a space.
Output
Output Q lines.
On the i-th line, output the answer to the i-th query as an integer.
Sample Input 1
5 4 abaca 0 1 1 3 3 5 4 2
Sample Output 1
0 9 0 8
Sample Input 2
4 5 aaaa 0 1 1 2 2 3 3 4 0 4
Sample Output 2
0 0 0 0 0
Sample Input 3
12 8 bananaabcxyz 0 6 5 4 11 12 3 7 8 2 1 10 10 11 6 5
Sample Output 3
24 0 84 34 2 70 86 30
Sample Input 4
30 15 qwertyuiopasdfghjklzxcvbnmabcd 0 1 29 30 15 10 7 25 3 3 28 30 14 1 1 29 20 15 5 30 0 12 11 20 27 7 6 18 2 30
Sample Output 4
20 209 100 124 51 750 24 348 30 240 96 180 6 234 450
Sample Input 5
1 5 z 0 1 0 1 0 1 0 1 0 1
Sample Output 5
0 0 0 0 0