#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 nn fishing companies, and each company initially received one continuous river segment. For the ii-th company (ordered from upstream to downstream), the initial segment length is aia_i.

Over the years, the fishing companies on the river went through kk 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 11, and the upstream part is smaller.
  • Event 2: Split
    A company’s segment splits into two. Suppose the original segment length is aa, and a≥2a \geq 2. 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 11, 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 kk 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: nn and pp — the initial number of companies (2≤n≤1000002 \leq n \leq 100 000) and the task number (0≤p≤40 \leq p \leq 4).

The second line contains nn integers: a1,a2,...,ana_1, a_2, ..., a_n, the initial segment lengths owned by each company.

The third line contains an integer kk — the number of events (1≤k≤1000001 \leq k \leq 100 000).

The next kk lines each describe one event. Each event consists of two integers eie_i and viv_i, where:

  • ei=1e_i = 1 means the viv_i-th company goes bankrupt.
  • ei=2e_i = 2 means the viv_i-th company splits.

It is guaranteed that after each event, the company index involved is valid in the current company list.

Output Format

Output k+1k + 1 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)

2≤n≤1002 \leq n \leq 100,1≤k≤1001 \leq k \leq 100,1≤ai≤1001 \leq a_i \leq 100,p=1p = 1。

Subtask 2 (30 points)

2≤n≤1000002 \leq n \leq 100 000,1≤k≤1000001 \leq k \leq 100 000,1≤ai≤1041 \leq a_i \leq 10^4,p=2p = 2。

Subtask 3 (20 points)

2≤n≤1000002 \leq n \leq 100 000,1≤k≤1000001 \leq k \leq 100 000,1≤ai≤1041 \leq a_i \leq 10^4,p=3p = 3。

All event types have ei=1e_i = 1 (that is, only bankruptcies occur, and there are no splits).

Subtask 4 (20 points)

2≤n≤1000002 \leq n \leq 100 000,1≤k≤1000001 \leq k \leq 100 000,1≤ai≤1041 \leq a_i \leq 10^4,p=4p = 4。

Translation source: GPT 5.2.

Translated by ChatGPT 5