# 题解 - Codeforces Round #791 Div. 2 (A-D)

## A - AvtoBus

Spring has come, and the management of the AvtoBus bus fleet has given the order to replace winter tires with summer tires on all buses

You own a small bus service business and you have just received an order to replace \(n\) tires. You know that the bus fleet owns two types of buses: with two axles (these buses have \(4\) wheels) and with three axles (these buses have \(6\) wheels)

You don't know how many buses of which type the AvtoBus bus fleet owns, so you wonder how many buses the fleet might have. You have to determine the minimum and the maximum number of buses that can be in the fleet if you know that the total number of wheels for all buses is \(n\)

### Input

The first line contains an integer \(t\) (\(1 \le t \le 1\,000\)) — the number of test cases. The following lines contain description of test cases

The only line of each test case contains one integer \(n\) (\(1 \le n \le 10^{18}\)) — the total number of wheels for all buses

### Output

For each test case print the answer in a single line using the following format

Print two integers \(x\) and \(y\) (\(1 \le x \le y\)) — the minimum and the maximum possible number of buses that can be in the bus fleet

If there is no suitable number of buses for the given \(n\), print the number \(-1\) as the answer

### Example

#### input

1 | 4 |

#### output

1 | 1 1 |

### Note

In the first test case the total number of wheels is \(4\). It means that there is the only one bus with two axles in the bus fleet

In the second test case it's easy to show that there is no suitable number of buses with \(7\) wheels in total

In the third test case the total number of wheels is \(24\). The following options are possible:

- Four buses with three axles
- Three buses with two axles and two buses with three axles
- Six buses with two axles

So the minimum number of buses is \(4\) and the maximum number of buses is \(6\)

### 代码参考

## Show code

1 | /* |

## B - Stone Age Problem

Once upon a time Mike and Mike decided to come up with an outstanding problem for some stage of ROI (rare olympiad in informatics). One of them came up with a problem prototype but another stole the idea and proposed that problem for another stage of the same olympiad. Since then the first Mike has been waiting for an opportunity to propose the original idea for some other contest... Mike waited until this moment!

You are given an array \(a\) of \(n\) integers. You are also given \(q\) queries of two types:

- Replace \(i\)-th element in the array with integer \(x\)
- Replace each element in the array with integer \(x\)

After performing each query you have to calculate the sum of all elements in the array

### Input

The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) — the number of elements in the array and the number of queries, respectively

The second line contains \(n\) integers \(a_1, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — elements of the array \(a\)

Each of the following \(q\) lines contains a description of the corresponding query. Description begins with integer \(t\) (\(t \in \{1, 2\}\)) which denotes a type of the query:

- If \(t = 1\), then two integers \(i\) and \(x\) are following (\(1 \le i \le n\), \(1 \le x \le 10^9\)) — position of replaced element and it's new value
- If \(t = 2\), then integer \(x\) is following (\(1 \le x \le 10^9\)) — new value of each element in the array

### Output

Print \(q\) integers, each on a separate line. In the \(i\)-th line print the sum of all elements in the array after performing the first \(i\) queries

### Example

#### input

1 | 5 5 |

#### output

1 | 19 |

### Note

Consider array from the example and the result of performing each query:

- Initial array is \([1, 2, 3, 4, 5]\)
- After performing the first query, array equals to \([5, 2, 3, 4, 5]\). The sum of all elements is \(19\)
- After performing the second query, array equals to \([10, 10, 10, 10, 10]\). The sum of all elements is \(50\)
- After performing the third query, array equals to \([10, 10, 10, 10, 11\)]. The sum of all elements is \(51\)
- After performing the fourth query, array equals to \([10, 10, 10, 1, 11]\). The sum of all elements is \(42\)
- After performing the fifth query, array equals to \([1, 1, 1, 1, 1]\). The sum of all elements is \(5\)

### 解题思路

\(O(n)\) 做法属实是典中典了，懒得想也可以直接套线段树，略

### 代码参考

## Show code

1 | /* |

## C - Rooks Defenders

You have a square chessboard of size \(n \times n\). Rows are numbered from top to bottom with numbers from \(1\) to \(n\), and columns — from left to right with numbers from \(1\) to \(n\). So, each cell is denoted with pair of integers \((x, y)\) (\(1 \le x, y \le n\)), where \(x\) is a row number and \(y\) is a column number

You have to perform \(q\) queries of three types:

- Put a new rook in cell \((x, y)\)
- Remove a rook from cell \((x, y)\). It's guaranteed that the rook was put in this cell before
- Check if each cell of subrectangle \((x_1, y_1) - (x_2, y_2)\) of the board is attacked by at least one rook

Subrectangle is a set of cells \((x, y)\) such that for each cell two conditions are satisfied: \(x_1 \le x \le x_2\) and \(y_1 \le y \le y_2\)

Recall that cell \((a, b)\) is attacked by a rook placed in cell \((c, d)\) if either \(a = c\) or \(b = d\). In particular, the cell containing a rook is attacked by this rook

### Input

The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 10^5\), \(1 \le q \le 2 \cdot 10^5\)) — the size of the chessboard and the number of queries, respectively

Each of the following \(q\) lines contains description of a query. Description begins with integer \(t\) (\(t \in \{1, 2, 3\}\)) which denotes type of a query:

- If \(t = 1\), two integers \(x\) and \(y\) follows (\(1 \le x, y \le n\)) — coordinated of the cell where the new rook should be put in. It's guaranteed that there is no rook in the cell \((x, y)\) at the moment of the given query
- If \(t = 2\), two integers \(x\) and \(y\) follows (\(1 \le x, y \le n\)) — coordinates of the cell to remove a rook from. It's guaranteed that there is a rook in the cell \((x, y)\) at the moment of the given query
- If \(t = 3\), four integers \(x_1, y_1, x_2\) and \(y_2\) follows (\(1 \le x_1 \le x_2 \le n\), \(1 \le y_1 \le y_2 \le n\)) — subrectangle to check if each cell of it is attacked by at least one rook

It's guaranteed that among \(q\) queries there is at least one query of the third type

### Output

Print the answer for each query of the third type in a separate line. Print "Yes" (without quotes) if each cell of the subrectangle is attacked by at least one rook

Otherwise print "No" (without quotes)

### Example

#### input

1 | 8 10 |

#### output

1 | No |

### Note

Consider example. After the first two queries the board will look like the following picture (the letter \(R\) denotes cells in which rooks are located, the subrectangle of the query of the third type is highlighted in green):

Chessboard after performing the third and the fourth queries:

Chessboard after performing the fifth and the sixth queries:

Chessboard after performing the seventh and the eighth queries:

Chessboard after performing the last two queries:

### 解题思路

显然两个树状数组维护一下哪些点有 Rooks 就行

### 代码参考

## Show code

1 | /* |

## D - Toss a Coin to Your Graph..

One day Masha was walking in the park and found a graph under a tree... Surprised? Did you think that this problem would have some logical and reasoned story? No way! So, the problem..

Masha has an oriented graph which \(i\)-th vertex contains some positive integer \(a_i\). Initially Masha can put a coin at some vertex. In one operation she can move a coin placed in some vertex \(u\) to any other vertex \(v\) such that there is an oriented edge \(u \to v\) in the graph. Each time when the coin is placed in some vertex \(i\), Masha write down an integer \(a_i\) in her notebook (in particular, when Masha initially puts a coin at some vertex, she writes an integer written at this vertex in her notebook). Masha wants to make exactly \(k - 1\) operations in such way that the maximum number written in her notebook is as small as possible

### Input

The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 2 \cdot 10^5\), \(1 \le k \le 10^{18}\)) — the number of vertices and edges in the graph, and the number of operation that Masha should make

The second line contains \(n\) integers \(a_i\) (\(1 \le a_i \le 10^9\)) — the numbers written in graph vertices

Each of the following \(m\) lines contains two integers \(u\) and \(v\) (\(1 \le u \ne v \le n\)) — it means that there is an edge \(u \to v\) in the graph

It's guaranteed that graph doesn't contain loops and multi-edges

### Output

Print one integer — the minimum value of the maximum number that Masha wrote in her notebook during optimal coin movements

If Masha won't be able to perform \(k - 1\) operations, print \(-1\)

### Examples

#### input

1 | 6 7 4 |

#### output

1 | 4 |

#### input

1 | 6 7 100 |

#### output

1 | 10 |

#### input

1 | 2 1 5 |

#### output

1 | -1 |

#### input

1 | 1 0 1 |

#### output

1 | 1000000000 |

### Note

Graph described in the first and the second examples is illustrated below

In the first example Masha can initially put a coin at vertex \(1\). After that she can perform three operations: \(1 \to 3\), \(3 \to 4\) and \(4 \to 5\). Integers \(1, 2, 3\) and \(4\) will be written in the notepad

In the second example Masha can initially put a coin at vertex \(2\). After that she can perform \(99\) operations: \(2 \to 5\), \(5 \to 6\), \(6 \to 2\), \(2 \to 5\), and so on. Integers \(10, 4, 5, 10, 4, 5, \ldots, 10, 4, 5, 10\) will be written in the notepad

In the third example Masha won't be able to perform \(4\) operations

### 解题思路

面向验题人编程

二分 + 找 \(k\) 长路 + 判环

### 代码参考

## Show code

1 | /* |