In geometry, a convex polygon is a polygon that is the boundary of a convex set. This means that the line segment between two points of the polygon is contained in the union of the interior and the boundary of the polygon. In particular, it is a simple polygon (not self-intersecting). Equivalently, a polygon is convex if every line that does not contain any edge intersects the polygon in at most two points.
You are given $$$n$$$ distinct points $$$p_1,p_2,\dots,p_n$$$ on the 2D plane. It is guaranteed that all points are distinct and no three points are collinear.
A fence can be regarded as a convex polygon such that:
The profit of the fence $$$F$$$ comes from the points on the boundary or inside it. Formally: $$$$$$\text{Profit}(F)=\sum_{p_i\text{ is on the boundary of }F}a_i+\sum_{p_i\text{ is strictly inside }F}b_i$$$$$$
You are required to build exactly two fences $$$A,B$$$ such that:
There is only one test case in each test.
The test case starts with a line containing a single integer $$$n$$$ ($$$6 \leq n \leq 100$$$), denoting the number of points.
Each of the next $$$n$$$ lines contains four integers $$$x_i,y_i,a_i$$$ and $$$b_i$$$ ($$$1\leq x_i,y_i\leq 10^9$$$, $$$1\leq a_i,b_i\leq 10^6$$$), describing the $$$i$$$-th point $$$p_i$$$ located at $$$(x_i,y_i)$$$.
It is guaranteed that no two points are identical and no three points are collinear.
Print a single line containing a single integer, denoting the maximum possible value of $$$\text{Profit}(A)\cdot\text{Profit}(B)$$$. You can assume that the answer always exists.
91 1 10 110 1 10 110 10 10 11 10 10 12 3 1 10104 102 10 1106 102 10 1105 109 10 1105 108 1 10
2000
The first king of Byteland was buried with his $$$n$$$ knights a thousand years ago. In the ruins, the archaeologists have found $$$n$$$ stone tablets in a row, labeled by $$$1,2,\dots,n$$$ from left to right. Each stone tablet belongs to a knight. On the surface of the $$$k$$$-th stone tablet, five numbers describe the ability of the $$$k$$$-th knight in five aspects:
Little Q is visiting the stone tablets from left to right, according to the labels from $$$1$$$ to $$$n$$$. After visiting a stone tablet, before moving right to the next one, he can choose to take a photo with it or do nothing. Little Q estimates the value of each stone tablet, he wants to maximize the total value that he takes photos with, and the knight corresponding to the next photo always dominates the current one. Here the $$$x$$$-th knight dominates the $$$y$$$-th knight if and only if $$$w_x\geq w_y$$$, $$$g_x\geq g_y$$$, $$$i_x\geq i_y$$$, $$$f_x\geq f_y$$$ and $$$l_x\geq l_y$$$.
There are so many stone tablets that Little Q can not determine which to take photos with. Please write a program to help him.
There is only one test case in each test.
The test case starts with a line containing a single integer $$$n$$$ ($$$1 \leq n \leq 100\,000$$$), denoting the number of stone tablets.
In the next $$$n$$$ lines, the $$$k$$$-th line contains six integers $$$w_k$$$, $$$g_k$$$, $$$i_k$$$, $$$f_k$$$, $$$l_k$$$ and $$$v_k$$$ ($$$1\leq w_k,g_k,i_k,f_k,l_k\leq n$$$, $$$1\leq v_k\leq 10\,000$$$), describing the $$$k$$$-th stone tablet, $$$v_k$$$ denoting the value of the photo with it.
Output $$$n$$$ lines, the $$$k$$$-th ($$$1\leq k\leq n$$$) of which containing an integer, denoting the maximum total value when the last photo is taken with the $$$k$$$-th stone tablet.
41 2 1 2 1 302 1 2 1 2 403 3 3 3 3 502 3 3 2 4 100
30 40 90 140
In this problem, your task is to maintain a multiset $$$T$$$ whose elements are pairs $$$(v_i,w_i)$$$. Initially, $$$T$$$ is empty. You are required to perform $$$n$$$ operations, each of which is one of the following three types:
There is only one test case in each test.
The test case starts with a line containing a single integer $$$n$$$ ($$$1 \leq n \leq 200\,000$$$), denoting the number of operations.
Each of the next $$$n$$$ lines describes an operation in the formats described in the statement above.
For each query, print a single line containing a single integer, denoting the value of $$$w$$$ that you find, or "-1" if there is no finding.
6+ 4 5+ 3 4? 2? 3- 3 4? 4
4 5 -1
To celebrate Children's Day, a mother of $$$k$$$ kids comes to the shop to buy sweets. There are $$$n$$$ boxes of sweets in the shop, the $$$i$$$-th box has $$$w_i$$$ sweets. The mother will choose which boxes to buy, and will divide all the bought boxes among the $$$k$$$ kids. Each kid will receive several (maybe zero) boxes, and will count the total number of sweets he receives. Assume the $$$i$$$-th kid receives $$$c_i$$$ sweets, the unfairness is defined as $$$\sum_{i=1}^k c_i^2$$$. To make all the kids happy, the mother will always divide boxes to minimize the unfairness. Note that she can not open any box to adjust the number of sweets inside it.
The mother is wondering which boxes to buy. She can not make her final decision. Please write a program to help the mother try all possible $$$2^n-1$$$ non-empty subsets of boxes to buy, and figure out the minimum unfairness for each subset. Note that you only need to report the sum of the minimum unfairness among all $$$2^n-1$$$ non-empty subsets.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 50$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq m\leq n \leq 20$$$, $$$n+m\leq 23$$$), denoting the number of boxes and the parameter $$$m$$$.
The second line contains $$$n$$$ integers $$$w_1,w_2,\dots,w_n$$$ ($$$1\leq w_i\leq 50\,000$$$), denoting the number of sweets in each box.
It is guaranteed that there are at most $$$2$$$ cases such that $$$n \gt 12$$$.
For each test case, output $$$m$$$ lines, the $$$i$$$-th ($$$1\leq i\leq m$$$) of which containing an integer, denoting the answer when $$$k=i$$$. Note that the answer may be extremely large, so please print it modulo $$$998\,244\,353$$$ instead.
1 5 4 1 2 3 4 5
2240 1180 930 884
Alice and Bob are playing a game on an undirected graph with $$$n$$$ vertices and $$$m$$$ edges. The vertices are labeled by $$$1,2,\dots,n$$$. The edges are labeled by $$$1,2,\dots,m$$$. The $$$i$$$-th edge connects the $$$u_i$$$-th vertex and the $$$v_i$$$-th vertex directly, and its weight will be chosen from the given two values $$$a_i$$$ and $$$b_i$$$.
First, Alice needs to assign weights to all the $$$m$$$ edges such that there are exactly $$$k$$$ edges whose weights are taken from $$$a$$$ while the weights of other $$$m-k$$$ edges are taken from $$$b$$$. Then, Bob needs to choose exactly $$$n-1$$$ edges from the graph such that every pair of different vertices are connected by these $$$n-1$$$ edges directly or indirectly.
The final score of the game is equal to the total weights of the $$$n-1$$$ edges chosen by Bob. Alice wants to maximize the score while Bob wants to minimize it. Please write a program to predict the final score for $$$k=0,1,2,\dots,m$$$ if both of the players play optimally.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 20$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 9$$$, $$$n-1\leq m\leq 30$$$), denoting the number of vertices and the number of edges.
Each of the following $$$m$$$ lines contains four integers $$$u_i,v_i,a_i$$$ and $$$b_i$$$ ($$$1 \leq u_i,v_i \leq n$$$, $$$u_i\neq v_i$$$, $$$1\leq a_i,b_i\leq 1\,000\,000$$$), describing an edge.
It is guaranteed that the graph is connected. Note that there can be multiple edges between same pair of vertices.
For each test case, output $$$m+1$$$ lines, the $$$i$$$-th ($$$1\leq i\leq m+1$$$) of which containing an integer, denoting the final score when $$$k=i-1$$$.
1 3 3 1 2 4 6 1 3 2 7 2 3 3 5
11 9 7 5
You are given $$$n$$$ points $$$p_1,p_2,\dots,p_n$$$ on the 2D plane. You need to perform $$$q$$$ operations. Each operation is one of the following:
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 3$$$), the number of test cases. For each test case:
The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n,q \leq 100\,000$$$), denoting the number of points and the number of operations.
In the next $$$n$$$ lines, the $$$i$$$-th line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1\leq x_i,y_i\leq 10^8$$$), describing the coordinate of $$$p_i$$$.
Each of the next $$$q$$$ lines describes an operation in formats described in the statement above.
It is guaranteed that all the values of $$$x_i$$$, $$$y_i$$$, $$$x$$$, $$$y$$$ are chosen uniformly at random from integers in their corresponding ranges. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well.
For each query, print a single line containing an integer, denoting the minimum radius.
15 51 12 23 13 32 52 1 52 1 12 1 21 1 10 12 1 5
3 0 1 5
You are given two sequences $$$a_0,a_1,\dots,a_{n-1}$$$ and $$$b_0,b_1,\dots,b_{n-1}$$$, you are required to perform $$$q$$$ operations. For each operation, you will be given an integer $$$k$$$ ($$$0\leq k \lt n$$$), you need to update $$$a_i$$$ into $$$\max(a_i,b_{(i+k)\bmod n})$$$ for each $$$i$$$ ($$$0\leq i \lt n$$$). To prove that you do perform these operations, output the value of $$$\sum_{i=0}^{n-1} a_i$$$ after each operation.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 2$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1\leq n,q\leq 250\,000$$$), denoting the length of the sequence and the number of operations.
The second line contains $$$n$$$ positive integers $$$a_0,a_1,\dots,a_{n-1}$$$ ($$$1\leq a_i\leq 10^9$$$).
The third line contains $$$n$$$ positive integers $$$b_0,b_1,\dots,b_{n-1}$$$ ($$$1\leq b_i\leq 10^9$$$).
Each of the next $$$q$$$ lines contains a single integer $$$k$$$ ($$$0\leq k \lt n$$$), describing each operation.
It is guaranteed that all the values of $$$a_i$$$, $$$b_i$$$ are chosen uniformly at random from integers in $$$[1,10^9]$$$, and each $$$k$$$ is chosen uniformly at random from integers in $$$[0,n)$$$. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well.
For each test case, output $$$q$$$ lines, the $$$i$$$-th ($$$1\leq i\leq q$$$) of which containing an integer, denoting the value of $$$\sum_{i=0}^{n-1} a_i$$$ after the $$$i$$$-th operation is applied.
15 51 5 3 6 82 5 4 7 332410
29 31 33 35 36
Little Q and Little T are playing a guessing game. In each round, Little T comes up with $$$n$$$ positive integers $$$x_1,x_2,\dots,x_n$$$ and writes down them on the paper. He should ensure these integers are all in the range $$$[1,m]$$$, and he is not allowed to change them during the game. Then Little Q can ask as many questions as he likes. There are two types of questions Little Q can ask:
For each question, Little T will always answer it honestly in the form "It's in the range $$$[l,r]$$$."
Now Little Q has asked $$$q$$$ questions, he still can't figure out the value of $$$x_1,x_2,\dots,x_n$$$, can you please tell him the minimum possible value of $$$x_1+x_2+\dots+x_n$$$ and the maximum possible value of $$$x_1+x_2+\dots+x_n$$$?
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 5$$$), the number of test cases. For each test case:
The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \leq n \leq 8$$$, $$$1\leq m\leq 1\,000\,000$$$, $$$1\leq q\leq 500$$$), denoting the number of numbers, the upper bound of numbers and the number of questions.
Each of the following $$$q$$$ lines starts with two integers $$$op_i$$$ and $$$k_i$$$ ($$$1\leq op_i\leq 2,1\leq k_i\leq n$$$), followed by $$$k_i$$$ distinct integers $$$p_{i,1},p_{i,2},\dots,p_{i,k_i}$$$ ($$$1\leq p_{i,j}\leq n$$$), ending with two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq m$$$), describing each question and the answer:
For each test case, output a single line containing two integers $$$\min\{sum\}$$$ and $$$\max\{sum\}$$$, denoting the minimum possible value of $$$x_1+x_2+\dots+x_n$$$ and the maximum possible value of $$$x_1+x_2+\dots+x_n$$$.
It is guaranteed that the answer always exists.
23 10 11 1 1 5 55 100 21 3 2 3 4 7 92 3 2 3 4 7 9
7 25 23 227
There are $$$n$$$ towns in Byteland, labeled by $$$1,2,\dots,n$$$. The $$$i$$$-th town's location is $$$(x_i,y_i)$$$. Little Q got a taxi VIP card, he can use the VIP card to cut down the taxi fare. Formally, assume Little Q is at $$$(x',y')$$$, if he calls a taxi to drive him to the $$$k$$$-th town, the VIP card will reduce $$$\min(|x'-x_k|+|y'-y_k|,w_k)$$$ dollars.
Little Q wants to make full use of his VIP card. He will give you $$$q$$$ queries, in each query you will be given his location, and you need to choose a town such that the VIP card will reduce the most taxi fare.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n,q \leq 100\,000$$$), denoting the number of towns and the number of queries.
Each of the following $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$ and $$$w_i$$$ ($$$1 \leq x_i,y_i,w_i \leq 10^9$$$), describing a town.
Each of the following $$$q$$$ lines contains two integers $$$x'$$$ and $$$y'$$$ ($$$1 \leq x',y' \leq 10^9$$$), describing a query.
It is guaranteed that the sum of all $$$n$$$ is at most $$$500\,000$$$, and the sum of all $$$q$$$ is at most $$$500\,000$$$.
For each query, print a single line containing an integer, denoting the maximum possible reduced taxi fare.
1 3 4 1 5 7 5 1 6 2 3 9 1 5 2 2 4 3 10 10
6 4 5 9
You are given a matrix $$$\textbf{M}$$$ with $$$n$$$ rows and $$$n$$$ columns. Let's define $$$f(i,j,k)=[(\textbf{M}^k)_{i,j}\neq 0]$$$. For each pair of $$$(i,j)$$$ ($$$1\leq i,j\leq n$$$), find the smallest positive integer $$$t$$$ such that $$$f(i,j,k)=f(i,j,k+t)$$$ always holds when $$$k$$$ is sufficiently large.
Note that matrix multiplication should be calculated under $$$\mathbb{Z}$$$.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 20$$$), the number of test cases. For each test case:
The first line contains a single integer $$$n$$$ ($$$1\leq n\leq 60$$$).
Each of the next $$$n$$$ lines contains a binary string of length $$$n$$$. The $$$j$$$-th bit of the $$$i$$$-th string denotes $$$\textbf{M}_{i,j}$$$ ($$$1\leq i,j\leq n$$$). In other words, all the values in $$$\textbf{M}$$$ are in $$$\{0,1\}$$$.
For each test case, output $$$n$$$ lines. Output $$$n$$$ integers in the $$$i$$$-th line, the $$$j$$$-th integer of which denoting the smallest positive integer $$$t$$$ such that $$$f(i,j,k)=f(i,j,k+t)$$$ always holds when $$$k$$$ is sufficiently large. Note that if you can not find such $$$t$$$, output "-1" instead.
19010010000001000001000100000010000000000001000000000100000000010000010001000000000
1 3 3 3 4 4 4 4 12 1 3 3 3 1 1 1 1 3 1 3 3 3 1 1 1 1 3 1 3 3 3 1 1 1 1 3 1 1 1 1 4 4 4 4 4 1 1 1 1 4 4 4 4 4 1 1 1 1 4 4 4 4 4 1 1 1 1 4 4 4 4 4 1 1 1 1 1 1 1 1 1
You are given a directed graph with $$$n$$$ vertices and $$$m$$$ edges. There may be multiple edges between same pair of vertices. Self-loops are also allowed in this graph. The vertices are labeled by $$$1,2,\dots,n$$$. The length of the $$$i$$$-th edge $$$u_i\rightarrow v_i$$$ is $$$998\,244\,353^{w_i}$$$.
Your task is to find a cycle in the given graph such that the average length of all edges on the cycle is minimized, or determine there is no cycle.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:
The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1\leq n\leq 1000$$$, $$$1\leq m\leq 2000$$$), denoting the number of vertices and edges.
Each of the following $$$m$$$ lines contains three integers $$$u_i,v_i$$$ and $$$w_i$$$ ($$$1\leq u_i,v_i\leq n$$$, $$$0\leq w_i \lt n$$$), describing an edge.
It is guaranteed that there are at most $$$10$$$ cases such that $$$n \gt 100$$$ or $$$m \gt 100$$$.
For each test case, output a single line containing an integer, denoting the average length of all edges on the cycle that you find.
More precisely, if the answer is $$$\frac{p}{q}$$$, you should output the minimum non-negative integer $$$r$$$ such that $$$q \cdot r \equiv p \pmod{(10^9+7)}$$$. You may safely assume that such $$$r$$$ always exists in all test cases. But if there is no cycle, please print "-1" instead.
63 31 2 02 3 03 1 02 11 2 02 21 2 02 1 13 51 2 01 3 12 3 23 2 02 3 14 51 2 32 3 33 1 32 4 14 1 11 11 1 0
1 -1 499122177 499122177 540815376 1
You got a treasure map that marks $$$n$$$ pirate chests buried in the ground, labeled by $$$1,2,\dots,n$$$. The $$$i$$$-th chest is located at point $$$(i,p_i)$$$ on the plane. When you open the $$$i$$$-th pirate chest, you will get $$$v_i$$$ coins.
You will start at $$$(0,0)$$$, and every time you can choose to move from $$$(x,y)$$$ to $$$(x+1,y)$$$ or $$$(x,y+1)$$$. You can dig up a pirate chest and take all the coins it contains when you are right above it.
Unfortunately, there is a dangerous trap area that is not marked! Through much research, you know that this is a rectangular area with sides parallel to the coordinate axes, and that it can only be one of $$$m$$$ possible areas. Please write a program to calculate how many coins you can take by following the optimal route for each possible case.
Assume that the real trap area is the $$$i$$$-th rectangle $$$[x_1,x_2]\times [y_1,y_2]$$$, then a position $$$(x,y)$$$ is considered to be dangerous if and only if $$$x_1\leq x\leq x_2$$$ and $$$y_1\leq y\leq y_2$$$. You should never pass through any dangerous position.
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\leq n,m\leq 300\,000$$$), denoting the number of pirate chests and the number of possible traps.
Each of the following $$$n$$$ lines contains two integers $$$p_i$$$ and $$$v_i$$$ ($$$1\leq p_i\leq n$$$, $$$1\leq v_i\leq 10^9$$$), describing each pirate chest. It is guaranteed that the values of all $$$p_i$$$ are pairwise distinct.
Each of the following $$$m$$$ lines contains four integers $$$x_1,y_1,x_2$$$ and $$$y_2$$$ ($$$1\leq x_1\leq x_2\leq n$$$, $$$1\leq y_1\leq y_2\leq n$$$), describing each possible rectangular trap area.
It is guaranteed that the sum of all $$$n$$$ is at most $$$1\,500\,000$$$, and the sum of all $$$m$$$ is at most $$$1\,500\,000$$$.
For each test case, output $$$m$$$ lines, the $$$i$$$-th ($$$1\leq i\leq m$$$) of which containing an integer, denoting the maximum number of coins that you can take when the dangerous trap area is the $$$i$$$-th rectangle.
13 52 43 31 51 1 1 11 1 1 32 1 3 31 1 3 21 1 3 3
7 5 4 3 0
You are given an integer sequence $$$a_1,a_2,\dots,a_n$$$, find exactly $$$k$$$ disjoint consecutive subsequences from it such that the length of each consecutive subsequence is prime. Assume the sum of all integers in the $$$i$$$-th consecutive subsequence is $$$s_i$$$, you need to maximize $$$\min\{s_1,s_2,\dots,s_k\}$$$.
Note that the selected consecutive subsequences don't need to cover the entire sequence. Here, "consecutive subsequence" means that it must be contiguous (i.e. a substring).
The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 100$$$), the number of test cases. For each test case:
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2\leq n\leq 200\,000$$$, $$$1\leq k\leq n$$$).
The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$|a_i|\leq 1000$$$).
It is guaranteed that all the values of $$$a_i$$$ are chosen uniformly at random from integers in $$$[-1000,1000]$$$. The randomness condition does not apply to the sample test case, but your solution must pass the sample as well.
It is also guaranteed that the sum of all $$$n$$$ is at most $$$10^6$$$.
For each test case, output a single line containing an integer, denoting the maximum possible value of $$$\min\{s_1,s_2,\dots,s_k\}$$$. If there is no solution, please output "impossible" instead.
46 11 1 4 5 1 46 1-1 -1 -4 -5 -1 -46 3-1 -1 -4 -5 -1 -42 20 0
15 -2 -9 impossible