#FT0000215. Thao tác vô hạn (Infinite Operations)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thao tác vô hạn (Infinite Operations)
Infinite Operations
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a sequence of positive integers and point updates. After each update, answer the following optimization problem for the updated sequence.
A legal operation is performed as follows:
- Choose indices satisfying .
- Choose a nonnegative real number satisfying .
- Increase by , decrease by , and earn points.
Operations may be repeated on the resulting sequence. Let denote the maximum total points obtainable by performing exactly operations. The limit is guaranteed to exist and to be rational.
Output this limit modulo after each update. More precisely, if the limit is the reduced fraction , print the unique integer in satisfying
The existence and uniqueness of is guaranteed. Each answer starts from the sequence just after its point update, not from an intermediate state of any previous optimization process.
Input
- The first line contains two integers , the sequence length and number of updates.
- The second line contains positive integers .
- Each of the next lines contains two integers , setting . Indices are one-based.
Output
Print exactly lines. Line contains the limiting maximum score after the -th point update, reduced modulo using the rational-number convention defined above.
Subtasks
- Subtask 1 (20 points): ; ; for every ; and for each .
- Subtask 2 (30 points): ; ; for every ; and for each .
- Subtask 3 (50 points): ; ; for every ; and for each .
Examples
Example 1
Input
3 4
7 5 5
1 5
2 6
1 7
3 5
Output
0
1
2
2
Explanation
After the four assignments the sequence is , , , and . The pairwise absolute-difference sums are respectively , , , and , so their halves are , , , and . The last assignment leaves the sequence unchanged.
Example 2
Input
2 4
1 2
2 5
1 3
1 2
2 3
Output
2
1
499122178
499122177
Explanation
After the updates, the sequences are , , , and . Their limiting scores are , , , and . Since the inverse of modulo is , the representations of and are and , respectively.