#P15525. [ROIR 2015 Day 1] river 河流
[ROIR 2015 Day 1] river 河流
Problem Description
In Plainland, the rich Great Plain River flows across the vast plain. Many years ago, this river was divided among fishing companies, and each company initially received one continuous river segment. For the -th company (ordered from upstream to downstream), the initial segment length is .
Over the years, the fishing companies on the river went through change events. Each event is one of two types: bankruptcy and split.
Event types:
-
Event 1: Bankruptcy
A company goes bankrupt, and the segment it occupies is transferred to its neighboring companies. If the bankrupt company has only one neighbor, that neighbor takes over the entire segment. If the bankrupt company has two neighbors, the segment is divided into two parts as follows:- If the segment length is even, split it evenly into two parts.
- If the segment length is odd, split it into two parts whose difference is , and the upstream part is smaller.
-
Event 2: Split
A company’s segment splits into two. Suppose the original segment length is , and . Then it is split by the following rules:- If the segment length is even, split it evenly into two parts.
- If the segment length is odd, split it into two parts whose difference is , and the upstream part is smaller.
After each bankruptcy or split, the number of companies changes. A bankruptcy event makes one company disappear, and a split event creates two new companies.
Therefore, after each event, every company owns a new river segment.
The Ministry of Finance suggests imposing a tax on fishing companies proportional to the square of the length of the segment they own. To analyze how this tax works, the Minister of Finance wants to know, from the given data, how the sum of squares of all companies’ segment lengths changes after each event.
Task: Write a program that, given the initial segment partition and the subsequent events, computes the sum of squares of the segment lengths owned by all companies after each event.
Input Format
The first line contains two integers: and — the initial number of companies () and the task number ().
The second line contains integers: , the initial segment lengths owned by each company.
The third line contains an integer — the number of events ().
The next lines each describe one event. Each event consists of two integers and , where:
- means the -th company goes bankrupt.
- means the -th company splits.
It is guaranteed that after each event, the company index involved is valid in the current company list.
Output Format
Output integers. The first integer is the sum of squares of all companies’ segment lengths at the initial moment. Then output one integer per line for the sum after each event.
4 0
3 5 5 4
5
1 1
2 1
1 3
2 2
1 3
75
105
73
101
83
113
Hint
Explanation of the example
After each event, the segment allocation among companies is shown in the figure below:

Task grading system and subtasks
Subtask 1 (30 points)
,,,。
Subtask 2 (30 points)
,,,。
Subtask 3 (20 points)
,,,。
All event types have (that is, only bankruptcies occur, and there are no splits).
Subtask 4 (20 points)
,,,。
Translation source: GPT 5.2.
Translated by ChatGPT 5