#P10071. [CCO 2023] Triangle Collection

[CCO 2023] Triangle Collection

Problem Description

Alice has some sticks. Initially, for each l=1,,Nl = 1, \ldots, N, she has clc_{l} sticks of length ll.

Alice wants to use her sticks to make some isosceles triangles. An isosceles triangle consists of two sticks of length ll and one stick whose length is between 11 and 2l12 l - 1. Note that the three stick lengths must strictly satisfy the triangle inequality, and equilateral triangles are also allowed. Each stick can be used in at most one triangle. Alice wants to know the maximum number of isosceles triangles she can make using her sticks.

There are QQ events that change the number of sticks she has. The ii-th event contains two integers lil_{i} and did_{i}, meaning that the number of sticks of length lil_{i} changes by did_{i}. Note that did_{i} can be any integer, but Alice will never have a negative number of sticks of length ll, nor more than 10910^{9} sticks of length ll.

You need to compute, after each event, the maximum number of isosceles triangles Alice can make using her sticks.

Input Format

The first line contains two integers NN and QQ, separated by spaces.

The second line contains NN integers c1,c2,,cNc_{1}, c_{2}, \ldots, c_{N}, separated by spaces, representing the sticks Alice initially has.

The next QQ lines each contain two integers lil_{i} and did_{i}, separated by spaces, representing an event.

It is guaranteed that before and after each event, for every l=1,,Nl = 1, \ldots, N, the number of sticks of length ll is between 00 and 10910^{9}.

Output Format

Output QQ lines. Each line contains one integer, the answer after the corresponding event.

4 3
3 1 4 1
3 -3
1 6
2 1
1
3
4

Hint

After the first event, Alice can make one triangle using sticks of lengths (1,1,1)(1,1,1).

After the second event, Alice can make three triangles using sticks of lengths (1,1,1)(1,1,1).

After the third event, Alice can make three triangles using sticks of lengths (1,1,1)(1,1,1), and one triangle using sticks of lengths (2,2,3)(2,2,3).

Constraints: for all testdata, 1N,Q2×1051 \leq N, Q \leq 2\times 10^5, 0ci1090 \leq c_{i} \leq 10^{9}, 1liN1 \leq l_{i} \leq N, 109di109-10^{9} \leq d_{i} \leq 10^{9}.

Subtask ID Points Range of N,QN, Q Additional Constraints
1 20 1N,Q20001 \leq N, Q \leq 2000 At all times, there are at most 20002000 sticks in total.
2 No additional constraints.
3 1N,Q2×1051 \leq N, Q \leq 2\times 10^5 At all times, the number of sticks of each length will only be 1,2,31, 2, 3.
4 di=1,1d_{i} = 1, -1.
5 No additional constraints.

Translated by ChatGPT 5