#P1110. [ZJOI2007] 报表统计

    ID: 110 远端评测题 2000~2500ms 159MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2007线段树各省省选平衡树二叉堆浙江概率论,统计

[ZJOI2007] 报表统计

Description

Little Q’s mother is a cashier and often needs to prepare statistical reports. Today is her birthday, and Little Q wants to help with some of the work as a birthday present.

After careful observation, Little Q finds that preparing a report is essentially maintaining a sequence of non-negative integers and performing some queries.

Initially, there is an integer sequence aa of length nn, and the following three operations are supported:

  • INSERT i k: Append a new element kk immediately after the ii-th element of the original sequence. If some elements have already been appended after the ii-th original element, append kk after all those appended elements (see the sample explanation).
  • MIN_GAP: Query the minimum absolute difference between any two adjacent elements.
  • MIN_SORT_GAP: Query the minimum absolute difference between the two closest elements among all elements.

Little Q wrote a program to automate these operations, but it runs slowly on large reports. Can you help improve it?

Input Format

  • The first line contains two integers, the length nn of the original sequence and the number of operations mm.
  • The second line contains nn integers, the initial sequence; the ii-th integer is aia_i.
  • The next mm lines each contain one operation, which is one of INSERT i k, MIN_GAP, or MIN_SORT_GAP (no extra spaces or blank lines).

Output Format

For each MIN_GAP and MIN_SORT_GAP command, output the answer on its own line.

3 5
5 3 1
INSERT 2 9
MIN_SORT_GAP
INSERT 2 6
MIN_GAP
MIN_SORT_GAP

2
2
1

Hint

Explanation of Sample Input/Output 1

The initial sequence is {5,3,1}\{5,3,1\}.

After performing INSERT 2 9, the sequence becomes {5,3,9,1}\{5,3,9,1\}. At this time, MIN_GAP is 22, and MIN_SORT_GAP is 22.

Next, performing INSERT 2 6 yields {5,3,9,6,1}\{5,3,9,6,1\}.

Note that by this time one 99 has already been appended after the 2nd element of the original sequence, so the newly inserted 66 should be placed after 99. Now MIN_GAP is 22, and MIN_SORT_GAP is 11.


Constraints

For all test points, it is guaranteed that 2n,m5×1052 \le n, m \le 5 \times 10^5, 1in1 \le i \le n, and 0ai,k5×1080 \le a_i, k \le 5 \times 10^8.

Translated by ChatGPT 5