A. Volleyball

In the process of reading the input, we can maintain two variables $sx,sy$$sx, sy$, which respectively indicate that the first team currently scored $sx$$sx$ points, and the second team currently scored $sy$$sy$ points. After reading a line, update the values of these two variables, and then determine whether the current round has ended

The process of judgment is as follows

1. If $sx\ge 25$$sx\geq 25$ and $sx>sy+1$$sx>sy+1$ , then the first team wins

2. If $sy\ge 25$$sy\geq 25$ and $sy>sx+1$$sy>sx+1$ , then the second team wins

3. Otherwise, the current round is not over yet

If a team wins two games, then exit the loop directly and output which team wins the overall game

B. Majhong

Simply enumerate all possible decompositions.

In this case, it is impossible to form a 'Chow' as there are only $n$$n$ different kinds of tiles. Therefore, only 'Pong' can be used. At this time, a hand of tiles is legal if and only if the number of tiles with each symbol is a multiple of $x$$x$, the number of tiles required by a 'Pong'.

The time complexity of this algorithm is $O\left(n\right)$$O(n)$.

It's easy to see that for the smallest available tile, matching 'Pong' is always better than matching 'Chow'. So the legality of a hand can be checked by enumerating from the first tile to the $n$$n$th tile.

Each time when we visit the $i$$i$th tile, let $m$$m$ be the number of remaining tiles with the $i$$i$th symbol. We first use them to form $⌊\frac{m}{x}⌋$$\lfloor \frac{m}{x} \rfloor$ 'Pongs'. In order to use up the remaining tiles with the $i$$i$th symbol, we have to use $m \text{ mod }x$ tiles of each synbol in range $\left[i,i+y-1\right]$$[i, i+y-1]$ to form 'Chows'. If there are not enough tiles, the current hand must be illegal.

The time complexity of this procedure $O\left({n}^{2}\right)$$O(n^2)$. This algorithm can be further improved to $O\left(n\right)$$O(n)$ by differencing array $a$$a$.

C. The 80/20 Rule

Consider performing a state pressure enumeration for all $n$$n$ individuals, enumerating which people are selected by us, and then calculating $A,B$$A, B$ separately to determine the answer, time complexity $O\left({2}^{n}\right)$$O(2^n)$

First we will sort the data in descending order from largest to smallest. For each prefix, we will calculate the corresponding numbers $A$$A$ and $B$$B$ . Let $sum$$sum$ be the sum of the data, and $pr{e}_{i}$$pre_i$ to represent the sum of the data in the prefix $\left[1,i\right]$$[1,i]$, by enumerating $i$$i$, we can get $A=\frac{i}{n}$$A=\frac{i}{n}$, $B=\frac{pr{e}_{i}}{sum}$$B=\frac{pre_i}{sum}$, then find the maximum value of $B-A$$B-A$ for all $i$$i$. As for sorting, you can use sorting algorithms such as bubble sort, selection sort, and insertion sort to achieve a time complexity of $O\left({n}^{2}\right)$$O(n^2)$

You can use algorithms such as merge sort, or use the built-in $sort$$sort$ to achieve a time complexity of $O\left(n\mathrm{log}n\right)$$O(n\log n)$.

D. Matrix

It is easy to find that the final result has nothing to do with the order of operations. You can enumerate whether each $p$$p$ is reversed, and then judge whether it is legal one by one.

Time complexity $O\left({n}^{2}{2}^{n}\right)$$O(n^22^{n})$.

It is easy to find that ${L}_{i,i}$$L_{i,i}$ must be equal to $0$$0$, and ${L}_{i,j}$$L_{i,j}$ must be equal to ${L}_{j,i}$$L_{j,i}$. Consider coloring each position by the number of operations parity:

Among them, red represents the grid on the diagonal, and the purple box on the diagonal represents $\left({p}_{i},{p}_{i}\right)$$(p_i,p_i)$, blue represents an odd number of operations, and purple represents an even number of operations. It can be seen that the number of the matrix $A$$A$ in the purple area, white area, and red area must be $0$$0$.

That is, if $P=\left\{{p}_{i}\right\}$$P=\{p_i\}$, $Q=\left\{x|x\in \left[1,n\right],x\notin P\right\}$$Q=\{x|x\in[1,n],x\not\in P\}$, then $\forall i,j \ in P , A_{i,j} = 0$ and $\mathrm{\forall }i,j\in Q,{A}_{i,j}=0$$\forall i,j \in Q , A_{i,j} = 0$.

Consider constructing an undirected graph $G=\left(V,E\right)$$G=(V,E)$ with $n$$n$ points, and the edge $\left(i,j\right)\in E$$(i,j)\in E$ if and only if ${A}_{i,j}=1$$A_{i,j}=1$. It is easy to find that there is no edge in the point set corresponding to $P$$P$, and there is no edge in the point set corresponding to $Q$$Q$. That is, $G$$G$ must be a bipartite graph at this time.

It is easy to prove that $G$$G$ is a bipartite graph is also a sufficient condition for a solution.

Assuming that the corresponding point set of $P$$P$ is a black point, then the construction scheme is equivalent to a black-and-white coloring scheme of the given graph, so that the lexicographical order of the black point set is as small as possible under the premise that the number of black points is as small as possible.

It is easy to prove that among the two coloring schemes for each connected block, the scheme with the smallest possible lexicographical order of the set of black points must be better on the premise of greedily taking the number of black points as small as possible.

Finally, all the black dot labels can be sorted and output.

Time complexity $O\left({n}^{2}\right)$$O(n^2)$.

E. Sequence

Output the bitwise OR of all numbers.

We can design a dp algorithm:

Denote ${f}_{i}$$f_i$ as the value and minimum value of splitting the first $i$$i$ into several substrings, obviously ${f}_{i}=min\left\{{f}_{j-1}+S\left(j,i\right)\right\}$$f_i=\min\{f_{j-1}+S(j,i)\}$, where $S\left(l,r\right)$$S(l,r)$ represents the value of the substring $\left[l,r\right]$$[l,r]$.

Brute force can do it in $O\left({n}^{2}\right)$$O(n^2)$.

Then we find a fact: for an $i$$i$, there are only $O\left(\mathrm{log}a\right)$$O(\log a)$ different $S\left(j,i\right)$$S(j,i)$.Because if the bitwise OR's value is changed with $j$$j$ increasing, there must be a bit which is changed from 0 to 1.

Then we can find $k$$k$ intervals $\left[{l}_{1},{r}_{1}\right],...,\left[{l}_{k},{r}_{k}\right]$$[l_1,r_1],...,[l_k,r_k]$ for a fixed $i$$i$, satisfying $1={l}_{1}\le {r}_{1}<{l}_{2}\le {r}_{2}<...<{l}_{k}\le {r}_{k}=i$$1=l_1\leq r_1 , so that for each interval $j\in \left[1,k\right]$$j\in [1,k]$ satisfies $x,y\in \left[{l}_{j},{r}_{j}\right],S\left(x,i\right)=S\left(y,i\right)$$x,y\in [l_j,r_j],S(x,i)=S(y, i)$ , and its $S$$S$ is different for positions in different intervals

At the same time, for each interval, the minimum value of $f$$f$ in the interval can be maintained, then violently process all intervals with different $S\left(j,i\right)$$S(j,i)$ and the minimum value of ${f}_{j-1}$$f_{j-1}$, time complexity $O\left(n\mathrm{log}a\right)$$O(n\log a)$, can pass all data.

F. Tree

Simply enumerate ${2}^{n}$$2^n$ edge cutting schemes, and find the centroid for each connected block.

Complexity $O\left({2}^{n}\right)$$O(2^n)$.

For each node $u$$u$, set $u$$u$ as the root of the whole tree and use a tree DP to calculate the probability that $u$$u$ is the centroid of its component. For each node $v$$v$, let $f\left[v\right]\left[i\right]$$f[v][i]$ be the probability that the subtree of node $u$$u$ when the root is $u$$u$ includes exactly $i$$i$ nodes. Array $f$$f$ can be calculated in $O\left({n}^{2}\right)$$O(n^2)$ time, and then the probability can be calculated by enumerating the number of nodes in the same connected component as node $u$$u$.

The total time complexity is $O\left({n}^{3}\right)$$O(n^3)$.

The point $i$$i$ becomes the centroid if and only if its interval $\left[l,r\right]$$[l,r]$ satisfies $i-l>r-i$$i-l>r-i$ or $r-i>i-l$$r-i>i-l$, the number of solutions can be directly calculated. Complexity $O\left({n}^{2}\right)$$O(n^2)$.

Let us consider the probability that the $u$$u$th node is not a centroid. Node $u$$u$ is not a centroid if and only if there is a node $v$$v$ that (1) the edge between $u$$u$ and $v$$v$ is preserved, and (2) after edge $\left(u,v\right)$$(u,v)$ is deleted, the number of nodes belongs to the same component as node $u$$u$ is strictly smaller than that of node $v$$v$.

Choose an arbitrary node as the root of the whole tree. Let $f\left[u\right]\left[i\right]$$f[u][i]$ be the probability that exactly $i$$i$th node in the subtree of node $u$$u$ belonged to the connected component of node $u$$u$ after cutting off the edges. $f$$f$ can be calculated in $O\left({n}^{2}\right)$$O(n^2)$ by a simple tree DP.

Now, for each node $u$$u$, for each node $v$$v$ that is a neighbor of $u$$u$, and for each integer $k$$k$, we need to calculate the probability that when $u$$u$ is set as the root of the whole tree, (1) $u$$u$ and $v$$v$ belongs to the same connected component, and (2) the number of nodes in the subtree of $v$$v$ that belongs to the connected component of node $v$$v$ is larger than a half that of node $u$$u$.

Such a probability can be calculated from the array $g$$g$ defined as the following. For node $u,v$$u, v$ where $\left(u,v\right)$$(u,v)$ is an edge on the tree and integer $x$$x$, ${g}_{u,v,x}$$g_{u,v,x}$ represents the probability that after setting node $u$$u$ as the root, the number of nodes in the component of $u$$u$ that are not in the subtree of node $v$$v$ is exactly $x$$x$.

Though $g$$g$ can be calculated by a tree DP, a trivial algorithm costs $O\left({n}^{3}\right)$$O(n^3)$ times. An important trick here is that ${g}_{u,v,x}$$g_{u,v,x}$ contributes to the result only when the number of nodes belongs to both the component of $u$$u$ and the subtree of $v$$v$ is strictly larger (or strictly smaller) than $x$$x$, and thus the value of ${g}_{u,v,x}$$g_{u,v,x}$ can be merged to ${g}_{u,v,\mathit{\text{size}}\left(v\right)+1}$$g_{u,v,\textit{size}(v)+1}$ when $x\ge \mathit{\text{size}}\left(v\right)+1$$x \geq \textit{size}(v)+1$.

Using this property, it can be proven that the time complexity of the tree DP is improved to $O\left({n}^{2}\right)$$O(n^2)$.

G. Palinomial

For each query, multiply all the polynomials in the query interval violently, and directly judge whether the obtained polynomial is a palindrome polynomial. The time complexity is $O\left(q×\left(\sum {k}_{i}{\right)}^{2}\right)$$O(q\times (\sum k_i)^2)$.

Consider accelerating the polynomial multiplication process of Subtask 1, then divide and conquer + NTT can be solved in the time complexity of $O\left(q×\left(\sum {k}_{i}\right)×\mathrm{log}\right)$$O(q\times (\sum k_i)\times \log )$.

It can be found that when each position is a linear function, we can find that only the linear function that itself is a palindrome polynomial and a first-order function whose solution is the reciprocal of each other can be a palindrome polynomial. We can consider using this property to maintain the set of each primary function of this interval and the set of each primary function of the reciprocal solution by using the set hash, and check whether they are the same.

There may be two methods of this package. One is to brute force each polynomial to turn it into a function and then use the previous package to do it, which may not be particularly easy to implement.

Or it is realized by a violent method accidentally discovered by the questioner, that is, violent maintenance of several items before and after the multiplication of polynomials. After violent verification, it is correct, and it is left to the reader to prove it by myself.

When a $n$$n$ degree polynomial $F$$F$ satisfies $\left[{x}^{i}\right]F\left(x\right)=\left[{x}^{n-i}\right]F\left(x\right)$$[x^i]F(x)=[x^{n-i}]F(x)$, we can find that $F\left(x\right)={x}^{n}F\left(\frac{1}{x}\right)$$F(x)=x^nF(\frac {1}{x})$ , that is, we actually only need to judge whether $F\left(x\right)$$F(x)$ and ${x}^{n}F\left(\frac{1}{x}\right)$$x^nF(\frac{1}{x})$ are the same.

Quickly determine whether two high-order polynomials are the same. If violence is used, it is related to the degree, and the complexity is unacceptable. We use polynomial hashing here.

Let the hash function $G\left(F\left(x\right)\right)={g}^{n+1}+\left(\sum _{i=0}^{n}\left[{x}^{i}\right]F\left(x\right){g}^{i}\right)\phantom{\rule{1em}{0ex}}\left(\mathrm{mod}\phantom{\rule{0.333em}{0ex}}p\right)\phantom{\rule{0ex}{0ex}}$ This hash function can be easily maintained by using a segment tree, and we consider proving its correctness.

If the hash functions of two polynomials of the same degree $F\left(x\right),G\left(x\right)$$F(x),G(x)$ are the same, it must satisfy $\sum _{i=0}^{n}\left(\left[{x}^{i}\right]F\left(x\right)-\left[{x}^{i}\right]G\left(x\right)\right){g}^{i}=0\phantom{\rule{1em}{0ex}}\left(\mathrm{mod}\phantom{\rule{0.333em}{0ex}}p\right)\phantom{\rule{0ex}{0ex}}$ We consider that when $F\left(x\right)$$F(x)$ and $G\left(x\right)$$G(x)$ are different, the polynomial $H\left(x\right)=\sum _{i=0}^{n}\left(\left[{x}^{i}\right]F\left(x\right)-\left[{x}^{i}\right]G\left(x\right)\right){x}^{i}$$H(x)=\sum_{i=0}^n([x^i]F(x)-[x^ i]G(x))x^i$ is at most a polynomial of degree $n$$n$ when the modulus $p$$p$ is a prime number, with at most $n$$n$ zeros. If $x$$x$ is taken randomly, then the probability that $H\left(x\right)$$H(x)$ is $0$$0$ is $\frac{n}{p}$$\frac{n}{p}$ . The probability that two polynomials are different but the hash function is the same is $\frac{n}{p}$$\frac{n}{p}$ .

So we set $p$$p$ to be a prime number at the level of ${10}^{9}$$10^9$, the probability of a single query success is $1-\frac{n}{p}$$1-\frac{n}{p}$, and the probability of $q$$q$ success is $\left(1-\frac{n}{p}{\right)}^{q}$$(1-\frac {n}{p})^q$ is about $0.00004$$0.00004$ , so a single hash cannot meet our requirements, a double hash is needed, and the probability of successful $q$$q$ times is $\left(1-\left(\frac{n}{p}{\right)}^{2}{\right)}^{q}$$(1-(\frac{n} {p})^2)^q$ , about $0.999$$0.999$ .

So far, the problem can be solved within the complexity of $O\left(q\mathrm{log}n\right)$$O(q\log n)$

H. Virus Experiment

Violence takes out the chain of each query, and then simulates it.

Time complexity $O\left(q{n}^{2}\right)$$O(qn^2)$.

The brute force takes out the chain of each query, and uses the hash to determine whether each prefix is a palindrome.

Time complexity $O\left(qn\right)$$O(qn)$.

This part is mainly divided into various root sign practices and practices with larger constants.

One root approach is to use a tree-move and then use a two-way PAM to solve. Time complexity $O\left(n\sqrt{q}|\mathrm{\Sigma }|\right)$$O(n\sqrt q|\Sigma|)$, where $|\mathrm{\Sigma }|$$|\Sigma|$ represents the character set size.

Consider making all queries off-line, sorted by left endpoint position. PAM(Palindrome tree) is used to maintain the palindrome tree and the depth of each node during the scan from right to left. For each query, use tree multiplication to find the node corresponding to the first palindrome prefix, and its depth on the palindrome tree is the answer.

Time complexity $O\left(q\mathrm{log}n\right)$$O(q\log n)$.

First make all queries off-line, and then divide and conquer. For each node, the center processes all queries that pass through the node in its connected block.

For the query $u,v$$u,v$, let the subtree where $u$$u$ is located be $L$$L$, and the subtree where $v$$v$ is located be $R$$R$, and discuss according to the position of the palindrome:

Case 1

The red line segment is the palindrome prefix position, and the blue line is the palindrome center.

Let ${S}_{u}$$S_u$ denote the string from root to $u$$u$.

The entire palindrome is in $L$$L$, which is equivalent to the number of suffixed palindrome substrings of ${S}_{u}$$S_u$. You can pass dfs and maintain revocable PAM statistics, where the number of suffixed palindrome substrings is the depth of the node in the fail tree of PAM.

The total complexity of this part is $O\left(n|\mathrm{\Sigma }|\mathrm{log}n\right)$$O(n|\Sigma|\log n)$.

Case 2

The palindrome reaches the $R$$R$ subtree passing the root, and the center of the palindrome remains in the $L$$L$ subtree.

According to the properties of the palindrome, the string can be divided into three parts $x,s,{x}^{\prime }$$x,s,x'$. where $x$$x$ is the prefix of ${S}_{u}$$S_u$ and ${x}^{\prime }$$x'$ is the suffix of ${S}_{v}$$S_v$. Satisfy $s$$s$ is a palindrome and $x={x}^{\prime }$$x=x'$.

Lemma 1: If $s$$s$ and $s+t$$s+t$ are both palindromes, then $s+t+t$$s+t+t$ must be a palindrome.

Lemma 2: All palindromic prefixes of a string can be divided into $O\left(\mathrm{log}n\right)$$O(\log n)$ arithmetic progressions.

We use Lemma 2 to decompose ${S}_{u}$$S_u$ into a palindrome prefix. For each arithmetic sequence, it is expressed in the form of $s={s}_{i}+{a}_{i}^{j},j\in \left[0,{b}_{i}\right]$$s=s_i+a_i^j,j\in[0,b_i]$. (${s}^{i}$$s^i$ represents the string formed by repeating the string $s$$s$ by $i$$i$, and $s+t$$s+t$ represents the string formed by splicing the strings $s$$s$ and $t$$t$ before and after.)

Consider the case of $s={s}_{0}+{a}^{j},j\in \left[0,b\right]$$s=s_0+a^j,j\in[0,b]$, at this time ${S}_{u}$$S_u$ is divided into ${s}_{0}+{a}^{j}+y$$s_0+a^j+y$, we might as well decompose ${S}_{v}$$S_v$ into ${a}^{k}+{y}^{\prime }+z$$a^k+y'+z$ form, where $|{y}^{\prime }|=|y|$$|y'|=|y|$:

$x$$x$ must be represented as ${a}^{i}+y,i\le b$$a^i+y,i\leq b$. According to Lemma 1, $a$$a$ cannot be a prefix of $y$$y$ and ${y}^{\prime }$$y'$. At this time if:

• $y$$y$ is not a prefix of $a$$a$. It can be deduced that $x$$x$ cannot be the prefix of ${a}^{b+1}$$a^{b+1}$, so ${x}^{\prime }$$x'$ must be suffixed with ${y}^{\prime }$$y'$. In other words, there is at most one feasible string of $s$$s$ at this time, and you can check whether the string is legal by Hash.
• $y$$y$ is a prefix of $a$$a$. Then $x$$x$ must be the prefix of ${a}^{b+1}$$a^{b+1}$. If $s={s}_{0}+{a}^{k}$$s=s_0+a^k$, then $x={a}^{b-k}$$x=a^{b-k}$, according to Lemma 1, the $s$$s$ must be legal. Therefore, the value range of $k$$k$ is an interval, whose right endpoint is $b$$b$, and the left endpoint of the interval depends on the difference between the powers of $a$$a$ on both sides.

For palindrome decomposition, PAM of Case 1 can be used to find the deepest ancestor that does not satisfy the arithmetic progression for each node. For computing the decomposition of ${S}_{v}$$S_v$, you can maintain a hash of ${a}^{{2}^{i}}$$a^{2^{i}}$ and then multiply the computation.

The total complexity of this part is $O\left(q{\mathrm{log}}^{2}n\right)$$O(q\log^2 n)$.

Case 3

The palindrome is centered in the $R$$R$ subtree. Similar to Case 2, we decompose the palindrome into $x,s,{x}^{\prime }$$x,s,x'$, at this time $x={x}^{\prime }$$x=x'$.

Similarly, we decompose $s$$s$ into the form of ${s}_{0}+{a}^{i},i\le b$$s_0+a^i,i\leq b$, and decompose ${S}_{u}$$S_u$ into the form of $y+{a}^{i}$$y+a^i$, and down $|yfrom$$|y from$s$|$$|$ takes out ${y}^{\prime }$$y'$.

Similarly, categorize whether $y$$y$ is a prefix of $a$$a$, and if not, check for unique $x$$x$. Otherwise the result is the difference between the feasible powers of the two sides.

The complexity of this part is $O\left(q{\mathrm{log}}^{2}n\right)$$O(q\log^2 n)$.

Total complexity $O\left(n|\mathrm{\Sigma }|\mathrm{log}n+q{\mathrm{log}}^{2}n\right)$$O(n|\Sigma|\log n+q\log^2 n)$.