#P15582. [KTSC 2026] 平衡序列 / Balanced Sequence

[KTSC 2026] 平衡序列 / Balanced Sequence

Problem Description

A balanced sequence is defined as follows:

  • A sequence of length 11 is a balanced sequence.
  • A sequence S=[S0,…,S2k]S = [S_0, \ldots, S_{2k}] of length 2k+12k + 1 is a balanced sequence if and only if:
    • [S0,S1,…,Sk−1][S_0, S_1, \ldots, S_{k-1}] is a balanced sequence.
    • [Sk+1,Sk+2,…,S2k][S_{k+1}, S_{k+2}, \ldots, S_{2k}] is a balanced sequence.
    • SkS_k is the unique maximum element in SS.

You are given a sequence AA of length NN. Define A[i…j]=[Ai,Ai+1,…,Aj]A[i \ldots j] = [A_i, A_{i+1}, \ldots, A_j]. For example, if A=[3,5,7,2,9]A = [3, 5, 7, 2, 9], then A[1…3]=[5,7,2]A[1 \ldots 3] = [5, 7, 2], and A[4…4]=[9]A[4 \ldots 4] = [9].

There are QQ operations, each being a point update. The operations are cumulative. In the initial state and after each operation, compute the number of pairs (i,j)(i, j) that satisfy:

  • 0≤i≤j≤N−10 \le i \le j \le N - 1.
  • A[i…j]A[i \ldots j] 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)
  • NN: the length of the sequence AA.
  • AA: an integer array of length NN.
  • Return the number of pairs (i,j)(i, j) such that 0≤i≤j≤N−10 \le i \le j \le N - 1 and A[i…j]A[i \ldots j] 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 A[p]←vA[p] \gets v.
  • Return the number of pairs (i,j)(i, j) such that 0≤i≤j≤N−10 \le i \le j \le N - 1 and A[i…j]A[i \ldots j] is a balanced sequence after the operation.
  • This function is called exactly QQ times after initialize is 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 11: NN QQ
  • Line 22: A[0]A[0] A[1]A[1] ... A[N−1]A[N-1]
  • For all 1≤k≤Q1 \le k \le Q:
    • Line 2+k2 + k: pp vv (the parameters of the kk-th update_sequence)

Output Format

The sample grader program outputs answers in the following format:

  • Line 11: the return value of initialize
  • For all 1≤k≤Q1 \le k \le Q:
    • Line 1+k1 + k: the return value of the kk-th update_sequence
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

  • 1≤N≤1051 \le N \le 10^5.
  • 0≤Q≤1050 \le Q \le 10^5.
  • 1≤A[i]≤1091 \le A[i] \le 10^9.
  • 0≤p≤N−10 \le p \le N - 1, 1≤v≤1091 \le v \le 10^9.

Subtasks

ID Score Constraints
11 33 Q=0Q = 0, AA is a balanced sequence
22 55 Q=0Q = 0, A[i]≤3A[i] \le 3
33 1212 A[i]≤3A[i] \le 3, v≤3v \le 3
44 1818 Q=0Q = 0, N≤2 000N \le 2\,000
55 2626 Q≤10Q \le 10
66 3636 No additional constraints

Sample 1

Consider the case N=4N = 4, Q=0Q = 0, A=[1,1,1,1]A = [1, 1, 1, 1]. The grader program calls:

initialize(4, [1, 1, 1, 1])

The pairs (i,j)(i, j) such that A[i…j]A[i \dots j] is a balanced sequence are (0,0)(0, 0), (1,1)(1, 1), (2,2)(2, 2), (3,3)(3, 3), so it should return 44.

Sample 2

Consider the case N=12N = 12, Q=0Q = 0, A=[8,9,7,9,2,3,2,8,4,6,2,6]A = [8, 9, 7, 9, 2, 3, 2, 8, 4, 6, 2, 6]. The grader program calls:

initialize(12, [8, 9, 7, 9, 2, 3, 2, 8, 4, 6, 2, 6])

The called function returns 1818.

Sample 3

Consider the case N=7N = 7, Q=2Q = 2, A=[1,3,4,4,2,1,6]A = [1, 3, 4, 4, 2, 1, 6]. 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 77, 99, and 88, respectively.

Translated by ChatGPT 5