balance

Time limit: 0.2s Memory limit: 1024MB Input: Output:

Kircho has an array of NN integers (not necessarily non-negative) β€” a0,a1,...aNβˆ’2,aNβˆ’1a_0, a_1, ... a_{N-2}, a_{N-1}. He can perform operations on the array. Each operation is characterized by a pair (l,r)(l,r) such that 0≀l≀r<N0 \leq l \leq r < N, and for this operation he can choose to perform exactly one of the following two actions:

  • for every x∈{0,1,...,rβˆ’lβˆ’1,rβˆ’l}x \in \{0, 1, ..., r-l-1, r-l\}, add (βˆ’1)x(-1)^x to al+xa_{l+x};
  • for every x∈{0,1,...,rβˆ’lβˆ’1,rβˆ’l}x \in \{0, 1, ..., r-l-1, r-l\}, subtract (βˆ’1)x(-1)^x from al+xa_{l+x}.

Let the balance of an array be defined as the minimum number of operations required to make all its elements equal to zero. Kircho wants to determine the balance of several arrays, each of which is a subarray of the main array. However, between queries the array may change via updates to individual elements. For more information about updates, see the Implementation details section. Unfortunately, Kircho cannot answer the queries himself, so he asks you to write a program balance that solves the problem.

An array pp is a subarray of an array qq if pp can be obtained from qq by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

Implementation details

This is an interactive problem, which means that information between your program and the jury program will not be exchanged through standard input and output, but through special functions.

You must implement the following functions:

void init(const std::vector<int>& A);

This function is called exactly once at the beginning of each test. The vector AA contains NN integers - a0,a1,...,aNβˆ’1a_0, a_1, ..., a_{N-1}, representing the initial array.

long long balance(int u, int v);

This function represents a query for the balance of the subarray au,au+1,...,avβˆ’1,ava_u, a_{u+1}, ..., a_{v-1}, a_v. The function must return the balance of the subarray. All calls to balance() are independent of each other.

void update(int p, int x);

This function represents an update to the array β€” it sets apa_p to xx. This change applies to all subsequent function calls within the current test.

You must submit a file balance.cpp, which contains the functions init(), balance(), and update(). It may also contain other code and helper functions necessary for your program, but it must not contain the main function main(). At the beginning, your file must include:

#include "balance.h"

Your program will be executed once for each test.

Constraints and clarifications

  • Let QQ be the total number of calls to the functions balance() and update() within a test.
  • 1≀N,Q≀1051 \leq N,Q \leq 10^5
  • βˆ’109≀ai,x≀109-10^9 \leq a_i,x \leq 10^9
  • 0≀ui≀vi<N0 \leq u_i \leq v_i < N
  • 0≀pi<N0 \leq p_i < N
  • It is guaranteed that there is at least one call to balance()
  • An alternating array is defined as one in which all numbers are nonzero and their signs alternate.
Subtask Points Required subtasks NN QQ Other constraints
0 0 - - - Sample test.
1 11 - ≀20\leq 20 ≀100\leq 100 The answer to each query is at most 33.
2 7 - - - No updates and aiβ‰₯0a_i \geq 0 for all ii.
3 9 - - - No updates, the array is alternating and $
4 14 - - =1= 1 No updates, $
5 18 - - =1= 1 No updates, the array is alternating.
6 15 2-5 - - No updates.
7 26 0-6 - - -

Sample interaction

Number Jury actions Your program's response
1 init({3,-4,2,-4,3})
2 balance(0,4) 66
3 balance(1,3) 66
4 update(0,2)
5 balance(0,2) 44

Explanation

In this example, the initial array is a=(3,βˆ’4,2,βˆ’4,3)a = (3, βˆ’4, 2, βˆ’4, 3). For the first query, Kircho can perform the following sequence of operations:

(3,βˆ’4,2,βˆ’4,3)β†’(3,βˆ’4,3,βˆ’4,3)β†’(3,βˆ’4,4,βˆ’4,3)β†’(3,βˆ’3,3,βˆ’3,3)β†’(2,βˆ’2,2,βˆ’2,2)β†’(1,βˆ’1,1,βˆ’1,1)β†’(0,0,0,0,0).(3, βˆ’4, 2, βˆ’4, 3) \rightarrow (3, βˆ’4, 3, βˆ’4, 3) \rightarrow (3, βˆ’4, 4, βˆ’4, 3) \rightarrow (3, βˆ’3, 3, βˆ’3, 3) \rightarrow \\ (2, βˆ’2, 2, βˆ’2, 2) \rightarrow (1, βˆ’1, 1, βˆ’1, 1) \rightarrow (0, 0, 0, 0, 0).

For the second query:

(βˆ’4,2,βˆ’4)β†’(βˆ’4,3,βˆ’4)β†’(βˆ’4,4,βˆ’4)β†’(βˆ’3,3,βˆ’3)β†’(βˆ’2,2,βˆ’2)β†’(βˆ’1,1,βˆ’1)β†’(0,0,0).(βˆ’4, 2, βˆ’4) \rightarrow (βˆ’4, 3, βˆ’4) \rightarrow (βˆ’4, 4, βˆ’4) \rightarrow (βˆ’3, 3, βˆ’3) \rightarrow \\ (βˆ’2, 2, βˆ’2) \rightarrow (βˆ’1, 1, βˆ’1) \rightarrow (0, 0, 0).

After the update, the array becomes a=(2,βˆ’4,2,βˆ’4,3)a = (2, βˆ’4, 2, βˆ’4, 3), and Kircho can perform:

(2,βˆ’4,2)β†’(1,βˆ’3,1)β†’(0,βˆ’2,0)β†’(0,βˆ’1,0)β†’(0,0,0).(2, βˆ’4, 2) \rightarrow (1, βˆ’3, 1) \rightarrow (0, βˆ’2, 0) \rightarrow (0, βˆ’1, 0) \rightarrow (0, 0, 0).

It can be proven that these sequences of operations are optimal.

Local testing

You are given the files balance.h and Lgrader.cpp, which you can compile together with your program to test it locally, along with compilation commands.

When running the local grader, on the first line of standard input you must enter the positive integers NN and QQ. On the second line, enter NN integers β€” a0,...,aNβˆ’1a_0, ..., a_{N-1} β€” representing the initial array. Each of the next QQ lines describes either a query or an update. At this point, the grader will call init() with parameter A={a0,...,aNβˆ’1}A = \{a_0, ..., a_{N-1}\}. At the beginning of each query, enter an integer tit_i , where 1≀ti≀21 \leq t_i \leq 2. If ti=1t_i = 1, then enter two integers uiu_i and viv_i, representing a call to balance() with parameters uiu_i and viv_i. If ti=2t_i = 2, then enter two integers pip_i and xix_i, representing a call to update() with parameters pip_i and xix_i.

For each call to balance(), the local grader will output the returned value. The order of function calls follows the input.

Log in or sign up to be able to send submissions!