Butterfly and Telephone Poles (Hard)
Time limit4.5sMemory limit1024 MB
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. poles stand in a straight line, spaced apart, numbered through from left to right. The first pole is at position , and initially the height of pole is . On a coordinate plane, pole is the segment from to .
A wire connects the tops of two poles by the shortest distance. If pole and pole are connected, the segment from to is drawn. The connection cost is the square of the wire length.
Nabi chooses the start pole and some poles including pole , and connects them with wires. Sort the chosen poles by number as . Then pole is connected to pole for .
Nabi must choose and connect the poles under these conditions.
- A wire must not cross a pole. However, a wire may meet pole at its top .
- Moving left from , the heights of the chosen poles are non-decreasing. Moving right from , the heights of the chosen poles are also non-decreasing. In other words, if , then .
- 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 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 .
The second line contains integers separated by spaces .
The third line contains the number of tasks .
Each of the next lines contains two integers and .
- If is , print the total connection cost when the start pole is .
- Otherwise, change the height of pole to .
Output
For each task with , print the total connection cost when on its own line.