Kircho has an array of integers (not necessarily non-negative) β . He can perform operations on the array. Each operation is characterized by a pair such that , and for this operation he can choose to perform exactly one of the following two actions:
- for every , add to ;
- for every , subtract from .
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 is a subarray of an array if can be obtained from 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 contains integers - , representing the initial array.
long long balance(int u, int v);
This function represents a query for the balance of the subarray . 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 to . 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 be the total number of calls to the functions
balance()andupdate()within a test. - 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 | Other constraints | ||
|---|---|---|---|---|---|
| 0 | 0 | - | - | - | Sample test. |
| 1 | 11 | - | The answer to each query is at most . | ||
| 2 | 7 | - | - | - | No updates and for all . |
| 3 | 9 | - | - | - | No updates, the array is alternating and $ |
| 4 | 14 | - | - | No updates, $ | |
| 5 | 18 | - | - | 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) |
|
| 3 | balance(1,3) |
|
| 4 | update(0,2) |
|
| 5 | balance(0,2) |
Explanation
In this example, the initial array is . For the first query, Kircho can perform the following sequence of operations:
For the second query:
After the update, the array becomes , and Kircho can perform:
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 and . On the second line, enter integers β β representing the initial array. Each of the next lines describes either a query or an update. At this point, the grader will call init() with parameter . At the beginning of each query, enter an integer , where . If , then enter two integers and , representing a call to balance() with parameters and . If , then enter two integers and , representing a call to update() with parameters and .
For each call to balance(), the local grader will output the returned value. The order of function calls follows the input.