Under construction

Still being built. Things may change

Butterfly and Telephone Poles (Hard)

Time limit4.5sMemory limit1024 MB

Summary
Pole heights change over time. For each query at start pole p, find the valid wire chain with the largest total length and print its squared cost.
Level

Hard9 of 10

Topics
Dynamic programming, Segment tree, Geometry
Solved
No attempts yet

Problem

Nabi is in charge of managing the utility poles in a new city. The wires are not connected yet, so Nabi must connect the wires between these poles. NN poles stand in a straight line, spaced 11 apart, numbered 11 through NN from left to right. The first pole is at position 11, and initially the height of pole ii is HiH_i. On a coordinate plane, pole ii is the segment from (i,0)(i,0) to (i,Hi)(i,H_i).

A wire connects the tops of two poles by the shortest distance. If pole ii and pole jj are connected, the segment from (i,Hi)(i,H_i) to (j,Hj)(j,H_j) is drawn. The connection cost is the square of the wire length.

Nabi chooses the start pole pp and some poles including pole pp, and connects them with wires. Sort the chosen poles by number as S1,S2,…,SkS_1, S_2, \ldots, S_k. Then pole SiS_i is connected to pole Si+1S_{i+1} for 1≤i<k1 \le i < k.

Nabi must choose and connect the poles under these conditions.

  • A wire must not cross a pole. However, a wire may meet pole ii at its top (i,Hi)(i,H_i).
  • Moving left from pp, the heights of the chosen poles are non-decreasing. Moving right from pp, the heights of the chosen poles are also non-decreasing. In other words, if St=pS_t = p, then HS1≥HS2≥⋯≥HSt−1≥Hp≤HSt+1≤HSt+2≤⋯≤HSkH_{S_1} \ge H_{S_2} \ge \cdots \ge H_{S_{t-1}} \ge H_p \le H_{S_{t+1}} \le H_{S_{t+2}} \le \cdots \le H_{S_k}.
  • Nabi wants the total wire length to be as large as possible. If several choices give the same maximum, Nabi chooses the one with the smallest total connection cost.

The city is still under construction, so the plan may change and pole heights may be updated. Junhyuk gives Nabi QQ tasks. Each task either changes the height of a pole, or gives a start pole and asks for the total connection cost.

Input

The first line contains NN (1≤N≤250 000)(1 \le N \le 250\,000).

The second line contains integers H1,H2,…,HNH_1, H_2, \ldots, H_N separated by spaces (1≤Hi≤106)(1 \le H_i \le 10^6).

The third line contains the number of tasks QQ (1≤Q≤250 000)(1 \le Q \le 250\,000).

Each of the next QQ lines contains two integers aa and bb.

  • If aa is 00, print the total connection cost when the start pole is p=bp = b (1≤b≤N)(1 \le b \le N).
  • Otherwise, change the height of pole aa to bb (1≤b≤106)(1 \le b \le 10^6).

Output

For each task with a=0a = 0, print the total connection cost when p=bp = b on its own line.

Examples2

  1. Example 1

    Input
    5
    5 9 3 5 6
    3
    0 1
    0 3
    0 5
    
    Expected output
    17
    44
    18
    
  2. Example 2

    Input
    5
    1 2 3 2 1
    3
    0 3
    1 4
    0 3
    
    Expected output
    0
    5