Westin the sheep is located at the lattice point $$$(x,y)$$$. His private jet can travel to any point inside or on the circle centered at $$$(x,y)$$$ with radius $$$r$$$.
There are two countries. Each country is a parallelogram, and a city is located at every lattice point (integer coordinate) that lies inside the parallelogram. The border of the parallelogram counts as inside.
For each country, Westin can visit the cities whose lattice points lie inside or on his circle. Your task is to compare the number of reachable cities in the two countries.
The vertices of each parallelogram are given in the following order:
It is guaranteed that for every parallelogram, one pair of opposite sides is parallel to the $$$x$$$-axis (equivalently, the top and bottom edges are horizontal, so $$$y_1=y_2$$$ and $$$y_3=y_4$$$).
The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.
Each test case consists of three lines:
Borders count as inside. It is guaranteed that for every parallelogram, one pair of opposite sides is parallel to the $$$x$$$-axis.
Let $$$n$$$ be the vertical span (in lattice rows) of a country: $$$n = y_1 - y_3 + 1$$$ (since $$$(x_1,y_1)$$$ is top-left and $$$(x_3,y_3)$$$ is bottom-left). It is guaranteed that the sum of $$$n$$$ over all countries across all test cases does not exceed $$$10^5$$$.
For each test case, output one integer:
10 0 1-1 1 0 1 0 0 1 0-1 0 0 0 0 -1 1 -1
3
For the first sample, the following figure illustrates the two parallelograms; both have the same amount of cities inside the radius of the circle, thus the output is $$$3$$$:
Westin is building a clock. He may construct any positive number of gears.
Each gear must receive an integer number of teeth, and every gear must have at least $$$2$$$ teeth. If a gear has $$$x$$$ teeth, it returns to its original position every $$$x$$$ seconds.
All gears rotate together. Starting at time $$$0$$$, every gear advances at a constant rate of 1 tooth per second.
Westin wants that at time $$$k$$$, all gears are simultaneously back in their original positions for the first time.
Among all valid constructions, Westin wants to minimize the total number of teeth used, i.e. $$$a_1 + a_2 + \ldots + a_n$$$ where $$$n$$$ is the number of gears used and $$$a_i$$$ is the number of teeth on gear $$$i$$$.
Determine the minimum possible total number of teeth, and one corresponding construction.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
Each test case contains a single integer $$$k$$$ ($$$2 \le k \le 10^5$$$).
For each test case, output:
If multiple optimal constructions exist, print any.
421272210
1224 328 942 3 5 7
The fat sheep and his tiger friend is given a tower of $$$N$$$ distinct Jenga blocks stacked vertically. The blocks are numbered from bottom to top, and the weight of the $$$i$$$-th block from the bottom is $$$w_i$$$. All weights are distinct.
You want to build a second tower by repeatedly removing blocks from the original tower.
A block is considered stable if:
You repeatedly perform the following operation until the original tower becomes empty:
Let the resulting second tower be described from bottom to top by the sequence of weights of the removed blocks in the order they were placed.
Your task is to determine the lexicographically greatest possible second tower (from bottom to top) that can be achieved.
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — the number of test cases.
Each test case consists of two lines:
It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Print $$$N$$$ integers representing the lexicographically greatest possible second tower from bottom to top.
1410 6 9 5
6 5 9 10
433 10 8314 5 161 2 3 4 5 61100
8 10 31 5 146 5 4 3 2 1100
You are given $$$n$$$ elements with distinct power levels $$$p_1,p_2,\dots,p_n$$$.
In one operation, you choose two different elements with power levels $$$a$$$ and $$$b$$$ and combine them into a hybrid. The hybrid's power level is
$$$$$$a \cdot b + 2(a+b).$$$$$$
For example, if you choose elements with power levels $$$6$$$ and $$$7$$$, the hybrid has power level $$$6 \cdot 7 + 2 \cdot (6+7) = 68$$$.
You are given $$$q$$$ independent queries. For each query value $$$x$$$, determine whether there exists a pair of different elements from the list such that combining them in one operation produces a hybrid with power level exactly $$$x$$$.
The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n \le 2 \cdot 10^5,\ 1 \le q \le 2 \cdot 10^5)$$$.
The second line contains $$$n$$$ distinct integers $$$p_1,p_2,\dots,p_n$$$ $$$(1 \le p_i \le 2 \cdot 10^5)$$$.
Each of the next $$$q$$$ lines contains one integer $$$x$$$ $$$(1 \le x \le 10^9)$$$ — the value of a query.
For each query, output YES (case-insensitive) if the query value can be obtained by combining some pair of different elements in one operation, and NO (case-insensitive) otherwise.
5 81 3 4 6 71121263650686920
YES NO YES YES YES YES NO YES
4 62 4 1 38251411168
YES NO YES YES YES YES
You are given $$$t$$$ test cases. For each test case, you are given two integers $$$a$$$ and $$$b$$$ ($$$a \lt b$$$), and $$$n$$$ available operations.
The $$$i$$$-th operation is described by a pair $$$(x_i, y_i)$$$:
Initially, $$$v = a$$$.
You may apply each operation at most once, in any order. You may only apply an operation if after applying it the value does not exceed $$$b$$$ (i.e., you must keep $$$v \le b$$$ at all times).
Your task is to find the minimum total cost needed to reach exactly $$$v = b$$$. If it is impossible, output $$$-1$$$.
For each test case, print one integer: the minimum total cost to reach $$$b$$$, or $$$-1$$$ if it is impossible.
24 3 102 1 1 12 5 1 12 1 52 23 4
4-1
In the first test case, the cheapest way is to use two cheap $$$+1$$$ operations first, then double: $$$3 \to 4 \to 5 \to 10$$$, with total cost $$$1+1+2=4$$$.
In the second test case, only doubling operations are available, so from $$$1$$$ we can reach only $$$2$$$ and $$$4$$$, but never $$$5$$$. Hence the answers are $$$\texttt{4}$$$ and $$$\texttt{-1}$$$.
Volcanoes are erupting over the ovine homeland! Help the Sheep King escape the lava.
There are $$$n$$$ islands connected by $$$m$$$ undirected bridges. Each bridge has a length $$$w$$$: crossing that bridge takes exactly $$$w$$$ days (for both the sheep and the lava).
Each island $$$i$$$ has a volcano that erupts on day $$$a_i$$$. When a volcano erupts, all bridges connected to that island burn down and become unusable.
Lava spreads through the bridge network over time, using the same travel times as the Sheep King (it needs $$$w$$$ days to cross a bridge of length $$$w$$$). If lava reaches an island before its volcano erupts, that island burns at the moment lava arrives.
The Sheep King starts on island $$$1$$$ at day $$$0$$$ and wants to reach island $$$n$$$ to escape. If he arrives at an island exactly when it burns, he is considered safe and may still depart from it at that time.
The first line contains an integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases.
For each test case:
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, print Yes (case-insensitive) if it is possible to travel from island $$$1$$$ to island $$$n$$$. Otherwise, print No (case-insensitive).
13 2100 2 1001 2 22 3 2
YES
25 746 34 5 60 361 3 231 3 291 5 72 5 254 5 84 5 114 5 295 725 53 5 49 71 2 41 4 71 5 232 5 232 5 243 4 124 5 20
YESNO
The Sheep King can travel from island $$$1$$$ to island $$$2$$$ in $$$2$$$ days. He arrives at island $$$2$$$ on day $$$2$$$, exactly when it burns. He is still safe, so he can continue to island $$$3$$$ and arrive there on day $$$4$$$. The answer is Yes.
On the Ovine Island, you are in charge of a herd of big fat sheeps.
To protect them from big bad wolves, you have arranged them into an $$$n \times m$$$ rectangular pen ($$$n,m \le 2000$$$). Each cell in the pen is either empty (weight $$$0$$$) or contains a sheep with weight $$$g_{i,j},$$$ an integer between $$$[1,10^6]$$$.
Your boss is wondering if there are special properties of the weights of your sheep.
You are given $$$q$$$ queries, each describing a rectangle. For each rectangle, consider all non-empty weights inside it. Determine whether you can choose three different cells in the rectangle such that if their weights are $$$a,b,c$$$, then the largest of them is strictly less than half of their sum: $$$$$$\max(a,b,c) \lt \frac{a+b+c}{2}.$$$$$$
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 2000$$$).
The next $$$n$$$ lines each contain $$$m$$$ integers $$$g_{i,j}$$$ ($$$0 \le g_{i,j} \le 10^6$$$), where $$$g_{i,j}=0$$$ means the cell is empty.
The next line contains an integer $$$q$$$ ($$$1 \le q \le 10^5$$$).
Each of the next $$$q$$$ lines contains four integers $$$x_1,y_1,x_2,y_2$$$ ($$$1 \le x_1 \le x_2 \le n$$$, $$$1 \le y_1 \le y_2 \le m$$$), describing a rectangle with corners $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$ (inclusive).
For each query, print YES (case-insensitive) if there exist three different non-empty cells inside the rectangle whose weights satisfy $$$$$$\max(a,b,c) \lt \frac{a+b+c}{2},$$$$$$ and print NO (case-insensitive) otherwise.
3 31 0 02 3 100 4 051 1 3 32 1 2 31 1 2 22 1 3 21 1 1 1
YES NO NO YES NO
Query 1: 1 1 3 3
This rectangle covers the entire grid. The non-empty weights inside are: $$$\{1,2,3,10,4\}$$$.
We need three different cells with weights $$$a,b,c$$$ such that $$$\max(a,b,c) \lt \frac{a+b+c}{2}.$$$
Choose the triple $$$(2,3,4)$$$. We have $$$\max(2,3,4) = 4$$$ and $$$\frac{2+3+4}{2} = \frac{9}{2} = 4.5$$$.
Since $$$4 \lt 4.5,$$$ the condition holds.
Therefore, the answer for Query 1 is $$$\texttt{YES}$$$.
$$$ $$$
Query 2: 2 1 2 3
This rectangle covers row $$$2$$$ and columns $$$1$$$ through $$$3$$$. The non-empty weights inside are: $$$\{2,3,10\}$$$.
The only possible triple is $$$(2,3,10)$$$. We have $$$\max(2,3,10) = 10$$$ and $$$\frac{2+3+10}{2} = \frac{15}{2} = 7.5$$$.
Since $$$10 \not \lt 7.5$$$, the condition does not hold.
Therefore, the answer for Query 2 is $$$\texttt{NO}$$$.
This is the easy version of Alien Attack. The only differences between this and the hard version are the constraints on the value of $$$n$$$ and $$$q$$$.
Westin is in a country with $$$n$$$ cities and $$$n-1$$$ bidirectional roads. The road network forms a tree. Cities are numbered from $$$1$$$ to $$$n$$$.
Each city $$$v$$$ has a power value $$$p_v(t)$$$ that depends on time $$$t$$$. Initially, at time $$$t=0$$$, every city has power $$$0$$$.
During an alien invasion, meteors may crash into some cities. It is guaranteed that each city is targeted by at most one meteor in total.
If a meteor crashes into city $$$x$$$ at time $$$t_0$$$ with initial energy $$$\textit{val}$$$, then for any time $$$t \ge t_0$$$, the power contributed by that meteor at city $$$x$$$ is:
This value can be negative (the meteor keeps losing $$$1$$$ energy per second even after reaching $$$0$$$). Cities that have never been hit by a meteor have power $$$0$$$ at all times.
You are given $$$q$$$ queries of two types:
For each type 2 query, output the total power on the simple path from $$$a$$$ to $$$b$$$ at exactly time $$$t$$$:
The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 2000$$$).
Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$), denoting a road between cities $$$u$$$ and $$$v$$$.
The next line contains an integer $$$q$$$ ($$$1 \leq q \leq 5000$$$).
Each of the next $$$q$$$ lines describes a query in one of the following forms:
If at a time t there are both type 1 and type 2 queries, process the type 1 queries first. It is guaranteed that each city appears in at most one type 1 query.
For each type 2 query, output a single integer — the total power on the path from $$$a$$$ to $$$b$$$ at time $$$t$$$.
31 22 341 1 2 52 2 1 32 4 1 32 4 2 2
4 2 2
Initially, all cities have power $$$0$$$.
Query 1: $$$1\ 1\ 2\ 5$$$
A meteor hits city $$$2$$$ at time $$$t_0=1$$$ with initial energy $$$5$$$. So for $$$t \ge 1$$$,
Query 2: $$$2\ 2\ 1\ 3$$$
At time $$$t=2$$$,
Path $$$1 \to 3$$$ is $$$(1,2,3)$$$, so the total is
Query 3: $$$2\ 4\ 1\ 3$$$
At time $$$t=4$$$,
Query 4: $$$2\ 4\ 2\ 2$$$
The path from $$$2$$$ to $$$2$$$ contains only city $$$2$$$, so the answer is
Therefore the outputs are:
Donkey has a binary string $$$s$$$ of length $$$n$$$ (each $$$s_i \in \{0,1\}$$$). Positions are indexed from $$$0$$$ to $$$n-1$$$. He also has an energy limit $$$E$$$, an integer $$$k$$$, and an integer $$$m$$$.
Donkey starts at position $$$p = 0$$$ and may perform the following actions in any order:
The total energy spent on all actions (including both moves and toggles) must not exceed $$$E$$$.
Your task is to determine the maximum possible number of ones in the array after performing any sequence of actions within the energy limit.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 2000$$$) — the number of test cases.
Each test case consists of:
It is guaranteed that $$$\left\lfloor \frac{n}{k} \right\rfloor \le 50$$$.
The sum of $$$n$$$ over all test cases will not exceed $$$1000$$$.
For each test case, output a single integer — the maximum possible number of ones in the string after operations with total energy at most $$$E$$$.
35 2 3 1000006 3 2 00000005 2 2 110101
465
65 1 10 2010105 2 10 3000005 2 3 5111116 2 100 00101012 1 100 5008 3 7 210100101
445528
For the first test case, one optimal sequence is: toggle at $$$p=0$$$ (cost $$$1$$$), move to $$$p=1$$$ (cost $$$1$$$), then toggle at $$$p=1$$$ (cost $$$1$$$). The array changes
For the second test case, $$$m=0$$$, so toggles are free and only movement matters. We can toggle at $$$p=0,1,2$$$ with total movement cost $$$2$$$, which turns all positions into $$$1$$$, so the answer is $$$6$$$.
For the third test case, move to $$$p=1$$$ (cost $$$1$$$) and toggle there (cost $$$1$$$). This flips $$$a_1$$$ and $$$a_3$$$ from $$$0$$$ to $$$1$$$, so
You have $$$N$$$ friends, numbered $$$1$$$ to $$$N$$$. Each friend chooses exactly one best friend, so the situation can be modeled as a directed graph where every vertex has outdegree $$$1$$$.
For a person $$$v$$$, define $$$\mathrm{indeg}(v)$$$ as the number of people who currently consider $$$v$$$ their best friend (i.e., the indegree of $$$v$$$).
You will process $$$Q$$$ queries of two types:
The MEX (minimum excluded value) of a multiset of nonnegative integers is the smallest nonnegative integer that does not appear in the multiset.
he first line contains two integers $$$N$$$ and $$$Q$$$ ($$$1 \le N \le 5 \cdot 10^4$$$, $$$1 \le Q \le 10^5$$$).
The second line contains $$$N$$$ integers $$$f_1, f_2, \dots, f_N$$$ ($$$1 \le f_i \le N$$$), where $$$f_i$$$ is the current best friend of person $$$i$$$.
Each of the next $$$Q$$$ lines describes a query in one of the following forms:
For every query of type 2, output a single integer — the MEX of the indegrees of all $$$N$$$ people at that moment.
4 53 3 4 221 4 321 2 12
3 2 3
This is the hard version of Alien Attack. The only differences between this and the easy version are the constraints on the value of $$$n$$$ and $$$q$$$.
Westin is in a country with $$$n$$$ cities and $$$n-1$$$ bidirectional roads. The road network forms a tree. Cities are numbered from $$$1$$$ to $$$n$$$.
Each city $$$v$$$ has a power value $$$p_v(t)$$$ that depends on time $$$t$$$. Initially, at time $$$t=0$$$, every city has power $$$0$$$.
During an alien invasion, meteors may crash into some cities. It is guaranteed that each city is targeted by at most one meteor in total.
If a meteor crashes into city $$$x$$$ at time $$$t_0$$$ with initial energy $$$\textit{val}$$$, then for any time $$$t \ge t_0$$$, the power contributed by that meteor at city $$$x$$$ is:
This value can be negative (the meteor keeps losing $$$1$$$ energy per second even after reaching $$$0$$$). Cities that have never been hit by a meteor have power $$$0$$$ at all times.
You are given $$$q$$$ queries of two types:
For each type 2 query, output the total power on the simple path from $$$a$$$ to $$$b$$$ at exactly time $$$t$$$:
The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$).
Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$), denoting a road between cities $$$u$$$ and $$$v$$$.
The next line contains an integer $$$q$$$ ($$$1 \leq q \leq 2 \cdot 10^5$$$).
Each of the next $$$q$$$ lines describes a query in one of the following forms:
If at a time t there are both type 1 and type 2 queries, process the type 1 queries first. It is guaranteed that each city appears in at most one type 1 query.
For each type 2 query, output a single integer — the total power on the path from $$$a$$$ to $$$b$$$ at time $$$t$$$.
31 22 341 1 2 52 2 1 32 4 1 32 4 2 2
4 2 2
Initially, all cities have power $$$0$$$.
Query 1: $$$1\ 1\ 2\ 5$$$
A meteor hits city $$$2$$$ at time $$$t_0=1$$$ with initial energy $$$5$$$. So for $$$t \ge 1$$$,
Query 2: $$$2\ 2\ 1\ 3$$$
At time $$$t=2$$$,
Path $$$1 \to 3$$$ is $$$(1,2,3)$$$, so the total is
Query 3: $$$2\ 4\ 1\ 3$$$
At time $$$t=4$$$,
Query 4: $$$2\ 4\ 2\ 2$$$
The path from $$$2$$$ to $$$2$$$ contains only city $$$2$$$, so the answer is
Therefore the outputs are: