#P15582. [KTSC 2026] 平衡序列 / Balanced Sequence
[KTSC 2026] 平衡序列 / Balanced Sequence
Problem Description
A balanced sequence is defined as follows:
- A sequence of length is a balanced sequence.
- A sequence of length is a balanced sequence if and only if:
- is a balanced sequence.
- is a balanced sequence.
- is the unique maximum element in .
You are given a sequence of length . Define . For example, if , then , and .
There are operations, each being a point update. The operations are cumulative. In the initial state and after each operation, compute the number of pairs that satisfy:
- .
- is a balanced sequence.
Implementation Details
This is a function-based interactive problem. You do not need to, and should not, implement the main function.
You should implement the following functions:
long long initialize(int N, vector<int> A)
- : the length of the sequence .
- : an integer array of length .
- Return the number of pairs such that and is a balanced sequence in the initial state.
- This function is called exactly once at the start of execution.
long long update_sequence(int p, int v)
- This function represents an operation that sets .
- Return the number of pairs such that and is a balanced sequence after the operation.
- This function is called exactly times after
initializeis called.
Your source code must not call any input/output functions.
Input Format
The input format of the sample grader program is as follows:
- Line :
- Line : ...
- For all :
- Line : (the parameters of the -th
update_sequence)
- Line : (the parameters of the -th
Output Format
The sample grader program outputs answers in the following format:
- Line : the return value of
initialize - For all :
- Line : the return value of the -th
update_sequence
- Line : the return value of the -th
4 0
1 1 1 1
4
12 0
8 9 7 9 2 3 2 8 4 6 2 6
18
7 2
1 3 4 4 2 1 6
3 1
3 2
7
9
8
Hint
Constraints
- .
- .
- .
- , .
Subtasks
| ID | Score | Constraints |
|---|---|---|
| , is a balanced sequence | ||
| , | ||
| , | ||
| , | ||
| No additional constraints |
Sample 1
Consider the case , , . The grader program calls:
initialize(4, [1, 1, 1, 1])
The pairs such that is a balanced sequence are , , , , so it should return .
Sample 2
Consider the case , , . The grader program calls:
initialize(12, [8, 9, 7, 9, 2, 3, 2, 8, 4, 6, 2, 6])
The called function returns .
Sample 3
Consider the case , , . The grader program calls the following functions in order:
initialize(7, [1, 3, 4, 4, 2, 1, 6])
update_sequence(3, 1)
update_sequence(3, 2)
The called functions return , , and , respectively.
Translated by ChatGPT 5