PS: Segment Tree Beats
- [BOJ 17474] 수열과 쿼리 26
- [BOJ 14899] 수열과 쿼리 19
- [BOJ 17473] 수열과 쿼리 25
- [BOJ 17476] 수열과 쿼리 28
- [BOJ 17477] 수열과 쿼리 29
- [BOJ 17475] 수열과 쿼리 27
We use the notation of segment tree beats. Let $N$ be the array size, $M$ the total number of operations, and $h=\max(1,\lceil\log_2N\rceil)$. All potentials below refer to the logical values after pending updates. In every construction, put $C(T,e_\mathcal{S})=e_\mathcal{L}$. When an absorbing state $\bot$ is used, conversion at $\bot$ is undefined.
[BOJ 17474] 수열과 쿼리 26
Task
For a given integer array $A$, perform the following operations on $[l:r]$:
- $A_i\gets\min(A_i,y)$.
- Compute $\max_{l\le i\le r}A_i$.
- Compute $\sum_{i=l}^r A_i$.
Solution
Let’s denote the update by $U_y(a)=\min(a,y)$. For a sequence, store:
\[ \Phi(A)=(n,\Sigma,M_2,M_1,c_M),\qquad n=\abs{A},\quad \Sigma=\sum_i A_i,\quad M_1=\max_i A_i,\quad c_M=\card{\Set{i\mid A_i=M_1}} \]
Here $M_2$ is the strict second maximum; put $M_2=-\infty$ if every value is equal. The empty summary is $e_\mathcal{S}=(0,0,-\infty,-\infty,0)$. For two nonempty summaries $s,t$, their merge is determined by:
\[ \begin{align*} n^\prime&=n_s+n_t,\qquad \Sigma^\prime=\Sigma_s+\Sigma_t,\qquad M_1^\prime=\max(M_{1,s},M_{1,t}), \nl c_M^\prime&=[M_{1,s}=M_1^\prime]c_{M,s}+[M_{1,t}=M_1^\prime]c_{M,t}, \nl M_2^\prime&=\max\left(\{M_{2,s},M_{2,t}\}\cup \Set{M_{1,z}\mid z\in\{s,t\},\ M_{1,z}<M_1^\prime}\right). \end{align*} \]
If $y\ge M_1$, nothing changes. If $M_2<y<M_1$, only the maximum class changes, so:
\[ \widehat{\mathcal{T}}(U_y,s) =\left(n,\Sigma+c_M(y-M_1),M_2,y,c_M\right) \]
Otherwise, we recurse into the children. The inequality $M_2<y$ is strict: when $y=M_2$, the two largest classes merge, and the new maximum count cannot be obtained from $c_M$ alone.
To express this as an endomorphic lazy action, record a coefficient for each allowed input value. For a set $J$ and a coefficient $k$, write:
\[ (J,k):=J\times\{k\} \]
Thus $(J,k)$ records that inputs in $J$ receive coefficient $k$. A tag is the graph of a partial coefficient function; records with the same graph are the same tag. In this problem, the coefficient is the displacement $d$, acting by $a\mapsto a+d$. Put:
\[ \mathcal{S}_0=\Phi(\mathbb{Z}^\ast),\qquad \mathcal{S}=\mathcal{S}_0\sqcup\{\bot\},\qquad \overline{\mathbb{Z}}=\mathbb{Z}\cup\{-\infty,+\infty\},\qquad I_b=\Set{a\in\mathbb{Z}\mid a\le b} \]
The tag set is:
\[ \begin{align*} \mathcal{L} &=\Set{(I_b,0)\mid b\in\overline{\mathbb{Z}}} \nl &\quad{}\cup \Set{(I_b,0)\cup(\{u\},v-u)\mid b\in\overline{\mathbb{Z}},\ u,v\in\mathbb{Z},\ b<v\le u}. \end{align*} \]
The interval $I_b$ remains fixed, while the isolated input $u$, when present, moves to $v$. The condition $b<v\le u$ keeps its output strictly above the fixed interval. The identity and empty-domain tag are:
\[ e_\mathcal{L}=(\mathbb{Z},0),\qquad 0_\mathcal{L}=\emptyset \]
For a tag represented by $(I_b,0)\cup(\{u\},v-u)$ and a nonempty regular summary:
\[ \lambda_l(n,\Sigma,M_2,M_1,c_M)= \begin{cases} (n,\Sigma,M_2,M_1,c_M) &; M_1\le b \nl (n,\Sigma+c_M(v-u),M_2,v,c_M) &; M_1=u,\ M_2\le b \nl \bot &; \text{otherwise} \end{cases} \]
For a tag $(I_b,0)$, omit the isolated-point case. Every tag fixes $e_\mathcal{S}$ and $\bot$, with $\bot$ absorbing under merge. The test accepts exactly when every input belongs to the tag’s domain. The action is then exact, so the absorbing extension gives the endomorphism law.
Composition follows an input through the two coefficient graphs:
\[ l_2\odot l_1 =\Set{(a,d_1+d_2)\mid (a,d_1)\in l_1,\ (a+d_1,d_2)\in l_2} \]
For pieces $(J_1,d_1)$ and $(J_2,d_2)$, this contributes:
\[ \bigl(J_1\cap(J_2-d_1),d_1+d_2\bigr),\qquad J-d:=\Set{a\mid a+d\in J} \]
At most one upper isolated point survives. If the second tag’s isolated input has a preimage in the first tag’s fixed interval, the first tag’s upper point maps above every allowed input of the second tag and is rejected. Thus the product is again in $\mathcal{L}$ and has at most two pieces. Graph composition is associative, and the piece-intersection formula computes it in $O(1)$ time.
Finally, conversion is:
\[ C(U_y,s)= \begin{cases} e_\mathcal{L} &; y\ge M_1 \nl (I_{M_2},0)\cup(\{M_1\},y-M_1) &; M_2<y<M_1 \nl \text{undefined} &; y\le M_2 \end{cases} \]
Every singleton is easy because its second maximum is $-\infty$. The query answers are simply $M_1$ and $\Sigma$.
Complexity
Let $d_v$ be the number of distinct values in the interval of node $v$. Define:
\[ \Psi=\sum_v(d_v-1) \]
At each depth, the sum of node lengths is $N$, so $\Psi_0\le N(h+1)=O(Nh)$. A fully covered chmin cannot increase $d_v$. At a hard node, $y\le M_2<M_1$, so at least the two largest classes merge and $d_v$ decreases by at least one. At a partially covered node, the only possible new value is $y$, so its contribution increases by at most one.
Thus, using $P_t,H_t$ from the algorithm post:
\[ \Delta\Psi_t\le P_t-H_t,\qquad \sum_tH_t\le\Psi_0+\sum_tP_t=O(Nh+Mh) \]
Therefore the total time complexity is $O((N+M)h)$, with $O(h)$ time per query and $O(N)$ memory. The distinct counts are only used in the proof.
[BOJ 14899] 수열과 쿼리 19
Task
For a given integer array $A$, perform the following operations on $[l:r]$:
- $A_i\gets A_i+x$.
- $A_i\gets\lfloor A_i/d\rfloor$, where $d\ge2$.
- Compute $\min_{l\le i\le r}A_i$.
- Compute $\sum_{i=l}^r A_i$.
Division is mathematical floor, including for negative values.
Solution
We store:
\[ \Phi(A)=(n,\Sigma,m_1,M_1) =\left(\abs{A},\sum_i A_i,\min_i A_i,\max_i A_i\right) \]
The summary monoid is $\mathcal{S}=\Phi(\mathbb{Z}^\ast)$, with:
\[ e_\mathcal{S}=(0,0,+\infty,-\infty) \]
\[ (n,\Sigma,m_1,M_1)\oplus(n^\prime,\Sigma^\prime,m_1^\prime,M_1^\prime) =(n+n^\prime,\Sigma+\Sigma^\prime,\min(m_1,m_1^\prime),\max(M_1,M_1^\prime)) \]
Let’s use affine tags $l=(p,q)$, where $p\in\{0,1\}$ and $q\in\mathbb{Z}$. The cases $p=0$ and $p=1$ represent assignment and addition, respectively. Their composition and summary action are:
\[ (p_2,q_2)\odot(p_1,q_1)=(p_2p_1,p_2q_1+q_2),\qquad e_\mathcal{L}=(1,0) \]
\[ \lambda_{(p,q)}(n,\Sigma,m_1,M_1) =(n,p\Sigma+nq,pm_1+q,pM_1+q) \]
The empty summary is fixed separately, so we never evaluate $0\cdot(\pm\infty)$. Since $p\ge0$, the action distributes over minimum and maximum as well as the length-weighted sum. Thus these are total endomorphisms.
Addition is directly converted to $(1,x)$. For division, define:
\[ f_d(a)=\left\lfloor\frac{a}{d}\right\rfloor,\qquad \delta_d(a)=f_d(a)-a \]
If $f_d(m_1)=f_d(M_1)$, every element has the same quotient, so the update is an assignment. Otherwise, if $\delta_d(m_1)=\delta_d(M_1)$, every element changes by the same amount, so the update is an addition. Indeed:
\[ f_d(a+1)-f_d(a)\in\{0,1\} \quad\Longrightarrow\quad \delta_d(a+1)-\delta_d(a)\in\{-1,0\} \]
Thus equal endpoint displacements force $\delta_d$ to be constant on the whole integer interval $[m_1:M_1]$. Consequently:
\[ C(F_d,s)= \begin{cases} (0,f_d(m_1)) &; f_d(m_1)=f_d(M_1) \nl (1,\delta_d(m_1)) &; f_d(m_1)\ne f_d(M_1),\ \delta_d(m_1)=\delta_d(M_1) \nl \text{undefined} &; \text{otherwise} \end{cases} \]
For example, dividing $[3,4]$ by $2$ gives $[1,2]$, which is addition by $-2$. The second case allows us to stop even when the quotients are different. Every singleton satisfies the assignment case.
In C++, signed integer division truncates toward zero. For $d>0$, the required quotient is the C++ quotient minus $[a<0\text{ and }d\nmid a]$.
Complexity
Let $w_v=M_1(v)-m_1(v)$, and let $W_\ast$ be the largest width attained by any node during execution. Define:
\[ B=1+\log_{4/3}(W_\ast+1),\qquad \Psi=\sum_v\log_{4/3}(w_v+1) \]
Then $\Psi_0=O(NB)$. At a hard node, $w\ge2$: width zero admits assignment, and width one admits either assignment or translation. After division:
\[ w^\prime=\left\lfloor\frac{M_1}{d}\right\rfloor -\left\lfloor\frac{m_1}{d}\right\rfloor \le\left\lceil\frac{w}{d}\right\rceil \le\left\lceil\frac{w}{2}\right\rceil \]
For $w=2k\ge2$ and $w=2k+1\ge3$, respectively:
\[ \frac{w^\prime+1}{w+1}\le\frac{k+1}{2k+1}\le\frac34,\qquad \frac{w^\prime+1}{w+1}\le\frac{k+2}{2k+2}\le\frac34 \]
Thus every hard node loses at least one potential unit. Full addition preserves widths, full division cannot increase them, and each boundary node gains at most $B$. Therefore:
\[ \Delta\Psi_t\le BP_t-H_t,\qquad \sum_tH_t=O(NB+MBh) \]
The total time complexity is $O(NB+MBh)$, with $O(h)$ time per query and $O(N)$ memory. If initial absolute values are at most $V$ and additions have magnitude at most $A$, we may use $W_\ast\le2V+2MA$.
[BOJ 17473] 수열과 쿼리 25
Task
For an array of $W$-bit nonnegative integers, perform the following operations on $[l:r]$:
- $A_i\gets A_i\mathbin{\&}x$.
- $A_i\gets A_i\mathbin{\vert}x$.
- Compute $\max_{l\le i\le r}A_i$.
Here $W=20$. Put $\Omega=2^W-1$, and take every bitwise complement within these $W$ bits.
Solution
Applying the bitwise operation to the stored maximum is not generally correct. For example, $\max(1,2)\mathbin{\&}1=0$, but $\max(1\mathbin{\&}1,2\mathbin{\&}1)=1$. We should determine which bits are uniform throughout the node.
Store:
\[ \Phi(A)=(n,M_1,B,O) =\left(\abs{A},\max_i A_i,\bigwedge_i A_i,\bigvee_i A_i\right) \]
Then:
\[ \begin{align*} e_\mathcal{S}&=(0,-\infty,\Omega,0), \nl (n,M_1,B,O)\oplus(n^\prime,M_1^\prime,B^\prime,O^\prime) &=(n+n^\prime,\max(M_1,M_1^\prime),B\mathbin{\&}B^\prime,O\mathbin{\vert}O^\prime). \end{align*} \]
The mask $R=O\mathbin{\operatorname{xor}}B$ contains exactly the varying bits. An OR update forces the bits of $x$ to one; an AND update forces the bits of $\neg x$ to zero. If every forced bit is uniform, every element changes by the same numerical offset. Thus the maximum can also be updated by that offset.
More precisely, use a tag $l=(F,U,V)$ with:
\[ U\mathbin{\&}\neg F=V\mathbin{\&}\neg F=0,\qquad Y_l=\Set{a\in[0:\Omega]\mid a\mathbin{\&}F=U},\qquad f_l(a)=(a\mathbin{\&}\neg F)\mathbin{\vert}V \]
The mask $F$ specifies the constrained bits, $U$ their required inputs, and $V$ their outputs. For every allowed $a$:
\[ f_l(a)=a+V-U \]
Put $\mathcal{S}=\Phi([0:\Omega]^\ast)\sqcup\{\bot\}$. For a nonempty regular summary:
\[ \lambda_l(n,M_1,B,O)= \begin{cases} \bigl(n,M_1+V-U,(B\mathbin{\&}\neg F)\mathbin{\vert}V, (O\mathbin{\&}\neg F)\mathbin{\vert}V\bigr) &; B\mathbin{\&}F=O\mathbin{\&}F=U \nl \bot &; \text{otherwise} \end{cases} \]
As before, $e_\mathcal{S}$ and $\bot$ are fixed. The test accepts exactly when all represented points belong to $Y_l$, so the absorbing extension proves the endomorphism law.
The identity tag is $(0,0,0)$. Include an empty-domain tag $z_\mathcal{L}$, which sends every nonempty regular summary to $\bot$. For $l_1=(F_1,U_1,V_1)$ followed by $l_2=(F_2,U_2,V_2)$, composition fails exactly when:
\[ (V_1\mathbin{\operatorname{xor}}U_2)\mathbin{\&}F_1\mathbin{\&}F_2\ne0 \]
In this case, $l_2\odot l_1=z_\mathcal{L}$. Otherwise:
\[
l_2\odot l_1=
\left(F_1\mathbin{\vert}F_2,
U_1\mathbin{\vert}(U_2\mathbin{\&}\neg F_1),
V_2\mathbin{\vert}(V_1\mathbin{\&}\neg F_2)\right)
\]
This follows by composing the partial transformation of each bit. The empty-domain tag is absorbing for composition. Conversion is:
\[ \begin{align*} C(T_x^\vee,s)&= \begin{cases} (x,B\mathbin{\&}x,x) &; R\mathbin{\&}x=0 \nl \text{undefined} &; \text{otherwise}, \end{cases} \nt C(T_x^\wedge,s)&= \begin{cases} (\neg x,B\mathbin{\&}\neg x,0) &; R\mathbin{\&}\neg x=0 \nl \text{undefined} &; \text{otherwise}. \end{cases} \end{align*} \]
Every leaf has $R=0$, so conversion always succeeds there.
Complexity
Define:
\[ \Psi=\sum_v\operatorname{popcount}(O_v\mathbin{\operatorname{xor}}B_v), \qquad \Psi_0\le(2N-1)W \]
A full AND/OR update cannot create a varying bit. At a hard node, at least one varying bit is forced to a constant, reducing its contribution by at least one. Only boundary nodes may gain potential, by at most $W$ each. Hence:
\[ \Delta\Psi_t\le WP_t-H_t,\qquad \sum_tH_t=O(NW+MWh) \]
The total time complexity is $O(NW+MWh)$, with $O(h)$ time per query and $O(N)$ words of memory. Here all masks fit in one machine word.
[BOJ 17476] 수열과 쿼리 28
Task
For a positive integer array $A$, perform the following operations on $[l:r]$:
- $A_i\gets A_i+x$, where $x>0$.
- $A_i\gets\lfloor\sqrt{A_i}\rfloor$.
- Compute $\sum_{i=l}^r A_i$.
Solution
We can use the same summary $(n,\Sigma,m_1,M_1)$ and affine tags $(p,q)$ as in BOJ 14899. Actual point values lie in $X=\mathbb{Z}_{\ge0}$, but we take the summary monoid over all integer sequences. This makes addition tags with negative $q$ total endomorphisms, even though converted updates preserve nonnegative values.
Put:
\[ r(a)=\lfloor\sqrt a\rfloor,\qquad \delta(a)=r(a)-a \]
Since:
\[ r(a+1)-r(a)\in\{0,1\}, \]
$r$ is nondecreasing and $\delta$ is nonincreasing. Thus equal endpoint values give an assignment, and equal endpoint displacements give an addition:
\[ C(R,s)= \begin{cases} (0,r(m_1)) &; r(m_1)=r(M_1) \nl (1,\delta(m_1)) &; r(m_1)\ne r(M_1),\ \delta(m_1)=\delta(M_1) \nl \text{undefined} &; \text{otherwise} \end{cases} \]
Addition is converted to $(1,x)$. For example, $[8,9]\gets[2,3]$ is addition by $-6$. This is why checking only equal square roots would omit an easy case. Square-root conversion is left undefined on negative algebraic summaries, which never occur during execution.
The integer square root should be computed exactly. If a floating-point square root is used for an initial estimate, correct it with integer comparisons before testing the endpoints.
Complexity
Let $w_v=M_1(v)-m_1(v)$ and let $W_\ast$ be the largest width attained during execution. Define:
\[ B=1+\log_{3/2}(W_\ast+1),\qquad \Psi=\sum_v\log_{3/2}(w_v+1) \]
Consider a hard square-root node, and put $k=r(M_1)-r(m_1)=w^\prime$. If $k=0$, assignment would apply. If $k=1$ and $w=1$, translation would apply. Hence a hard node with $k=1$ has $w\ge2$, giving:
\[ \frac{w^\prime+1}{w+1}\le\frac23 \]
For $k\ge2$, write $t=r(m_1)$. Then:
\[ M_1\ge(t+k)^2,\qquad m_1\le(t+1)^2-1,\qquad w\ge2t(k-1)+k^2\ge k^2 \]
Thus:
\[ \frac{w^\prime+1}{w+1}\le\frac{k+1}{k^2+1}\le\frac23 \]
Every hard node loses at least one potential unit. Full addition preserves width, full square root cannot increase it, and each boundary node gains at most $B$. Therefore:
\[ \Delta\Psi_t\le BP_t-H_t,\qquad \sum_tH_t=O(NB+MBh) \]
The total time complexity is $O(NB+MBh)$, with $O(h)$ time per query and $O(N)$ memory. If initial values are at most $V$ and additions are at most $A$, we may use $W_\ast\le V+MA$.
[BOJ 17477] 수열과 쿼리 29
Task
An integer array $A$ and an array $B$, initially $B_i=0$, are given. Perform the following operations on $[l:r]$:
- $A_i\gets A_i+x$.
- $A_i\gets\max(A_i,y)$.
- $A_i\gets\min(A_i,y)$.
- Compute $\sum_{i=l}^r B_i$.
Whenever an update changes $A_i$, increase $B_i$ by one. An update which leaves $A_i$ unchanged does not increase $B_i$.
Solution
We should retain the number of changes even when several updates cancel. For example, adding $x\ne0$ and then $-x$ restores the current value, but increases the count by two. Thus a lazy tag must store both displacement and change count.
Take $X=\mathbb{Z}\times\mathbb{Z}_{\ge0}$, with point updates:
\[ \begin{align*} A_x(a,b)&=(a+x,b+[x\ne0]), \nl L_y(a,b)&=(\max(a,y),b+[a<y]), \nl U_y(a,b)&=(\min(a,y),b+[a>y]). \end{align*} \]
For the current values, store both first and strict second extrema:
\[ E=(n,m_1,m_2,c_m,M_2,M_1,c_M) \]
Here $c_m,c_M$ are the multiplicities of the minimum and maximum, and missing second extrema are $m_2=+\infty$, $M_2=-\infty$. The full summary is:
\[ \Phi((a_i,b_i)_i)=(E,Q),\qquad Q=\sum_i b_i \]
For two nonempty extrema summaries $E,F$, define $E\boxplus F=(n,u,u_2,c_u,v_2,v,c_v)$ by:
\[ \begin{align*} n&=n(E)+n(F),\qquad u=\min(m_1(E),m_1(F)),\qquad v=\max(M_1(E),M_1(F)), \nl c_u&=\sum_{G\in\{E,F\}}[m_1(G)=u]c_m(G),\qquad c_v=\sum_{G\in\{E,F\}}[M_1(G)=v]c_M(G), \nl u_2&=\min\left(\{m_2(E),m_2(F)\}\cup \Set{m_1(G)\mid G\in\{E,F\},\ m_1(G)>u}\right), \nl v_2&=\max\left(\{M_2(E),M_2(F)\}\cup \Set{M_1(G)\mid G\in\{E,F\},\ M_1(G)<v}\right). \end{align*} \]
The empty extrema summary is:
\[ E_\varepsilon=(0,+\infty,+\infty,0,-\infty,-\infty,0) \]
It is the identity for $\boxplus$. Thus, with $\mathcal{S}_0=\Phi(X^\ast)$:
\[ \mathcal{S}=\mathcal{S}0\sqcup\{\bot\},\qquad e\mathcal{S}=(E_\varepsilon,0),\qquad (E,Q)\oplus(F,R)=(E\boxplus F,Q+R) \]
and $\bot$ is absorbing.
For an effective chmax, we can update the minimum class alone when $m_1<y<m_2$. Similarly, an effective chmin is easy when $M_2<y<M_1$. The counts increase by $c_m$ and $c_M$, respectively. Addition increases $Q$ by $n[x\ne0]$. To compose these actions, we should distinguish the extreme classes by their input values.
Let a coefficient $k=(d,r)\in K=\mathbb{Z}\times\mathbb{Z}_{\ge0}$ mean:
\[ (a,b)\longmapsto(a+d,b+r) \]
Coefficients compose by:
\[ (d_2,r_2)\diamond(d_1,r_1)=(d_1+d_2,r_1+r_2),\qquad 0_K=(0,0) \]
A tag again records coefficients by input value. We first define the common tag form used here and in BOJ 17475. For integer sets, write:
\[ J+d:=\Set{a+d\mid a\in J},\qquad J<J^\prime\iff \forall a\in J\ \forall b\in J^\prime,\ a<b \]
Let $\mathscr{I}$ be the set of all integer intervals, including empty, singleton, and unbounded intervals, and put:
\[ \mathscr{P}=\{\emptyset\}\cup\Set{\{a\}\mid a\in\mathbb{Z}} \]
For a coefficient $k\in K$, let $d(k)$ be its first coordinate. Using the graph notation $(J,k)=J\times\{k\}$, define:
\[ \mathcal{L}(K):= \left\{ \bigcup_{\alpha\in\{-,0,+\}}(J_\alpha,k_\alpha) \;\middle|\; \begin{array}{l} J_0\in\mathscr{I},\quad J_-,J_+\in\mathscr{P},\quad k_\alpha\in K, \nl J_\alpha<J_\beta,\quad J_\alpha+d(k_\alpha)<J_\beta+d(k_\beta) \quad(\alpha<\beta) \end{array} \right\} \]
Here $-<0<+$. Thus the central piece is an interval, and each outer piece is empty or a singleton. The two order conditions make the current-value map $a\mapsto a+d(k_\alpha)$ strictly increasing across pieces. In particular, distinct current-value classes remain distinct at an easy node. For this problem, take:
\[ \mathcal{L}=\mathcal{L}(K),\qquad e_\mathcal{L}=(\mathbb{Z},0K),\qquad 0\mathcal{L}=\emptyset \]
Composition records exactly the inputs admitted at both stages:
\[ l_2\odot l_1 =\Set{(a,k_2\diamond k_1)\mid (a,k_1)\in l_1,\ (a+d(k_1),k_2)\in l_2} \]
Equivalently, if $l_t=\bigcup_\alpha(J_{t,\alpha},k_{t,\alpha})$, then:
\[ l_2\odot l_1 =\bigcup_{\alpha,\beta\in\{-,0,+\}} \left( J_{1,\alpha}\cap(J_{2,\beta}-d(k_{1,\alpha})), k_{2,\beta}\diamond k_{1,\alpha} \right) \]
For $k=(d,r)$, the output coefficient is $(d_1+d_2,r_1+r_2)$. Only nine piece pairs are examined, and at most three pieces survive.
Proof
Write $I_t=J_{t,0}$. Inputs using both central pieces form the interval:
\[ J=I_1\cap(I_2-d(k_{1,0})) \]
Every other nonempty intersection is a singleton, since the current-value map is injective. If $J\ne\emptyset$, two possible inputs below $J$ are the first tag’s lower point and the preimage of the second tag’s lower point in $I_1$. They cannot both survive: when the latter exists, strict increase sends the former below the second tag’s domain. The upper side is symmetric.
If a central interval is empty, injectivity leaves at most two inputs. Otherwise, $J=\emptyset$ means that the translated central intervals are disjoint. At most one input from $I_1$ can then reach a second-tag isolated point. If such an input exists, one first-tag outer point is rejected; otherwise, only the two first-tag outer points may survive. Hence there are at most two isolated points when $J=\emptyset$.
The product preserves strict order, so it belongs to $\mathcal{L}(K)$. Associativity follows from the graph formula and associativity of $\diamond$.
Now we should compute the action from the summary alone. Choose a representation of $l$ with central interval $I$ and isolated-point set $P$. Their counts are:
\[ c(w)= \begin{cases} c_m &; w=m_1 \nl c_M &; w=M_1,\ w\ne m_1 \nl 0 &; \text{otherwise} \end{cases} \qquad(w\in P) \]
The separate cases avoid counting a uniform node twice. After removing the isolated groups, put:
\[ n_I=n-\sum_{w\in P}c(w),\qquad u_I= \begin{cases} m_2 &; m_1\in P \nl m_1 &; m_1\notin P \end{cases}, \qquad v_I= \begin{cases} M_2 &; M_1\in P \nl M_1 &; M_1\notin P. \end{cases} \]
The tag is valid exactly when:
\[ n_I=0 \quad\lor\quad \left(I\ne\emptyset\ \land\ \inf I\le u_I\le v_I\le\sup I\right) \]
Indeed, a present lower isolated point must be the minimum, and a present upper isolated point must be the maximum. Removing an extreme exposes its strict second extreme. Thus the test checks that all remaining values lie in $I$. Conversely, if an allowed isolated point lies strictly between the node’s extrema, one of those extrema is outside the whole allowed set, and the interval test fails.
If invalid, set $\lambda_l(E,Q)=\bot$. Otherwise, let $n_j$ be the count of the occupied group in piece $J_j$: $c(w)$ for an isolated point, and $n_I$ for the interval. Obtain $E^\prime$ by replacing every finite extremum $t\in J_j$ by $t+d_j$, keeping multiplicities and infinite sentinels. Strict increase ensures that the order of distinct values is preserved. Then:
\[ \lambda_l(E,Q)=\left(E^\prime,Q+\sum_{j:n_j>0}n_jr_j\right) \]
Every tag fixes $e_\mathcal{S}$ and $\bot$. The validity test and the exact pointwise action give total endomorphisms by the absorbing-extension argument. The result depends only on the coefficient graph. After discarding empty pieces and merging adjacent equal-coefficient pieces, each tag uses $O(1)$ words. Composition and application take $O(1)$ time.
Finally, addition converts to:
\[ C(A_x,s)=(\mathbb{Z},(x,[x\ne0])) \]
For chmax and chmin:
\[ C(L_y,s)= \begin{cases} e_\mathcal{L} &; y\le m_1 \nl (\{m_1\},(y-m_1,1))\cup([m_2,+\infty)\cap\mathbb{Z},0_K) &; m_1<y<m_2 \nl \text{undefined} &; y\ge m_2, \end{cases} \]
\[ C(U_y,s)= \begin{cases} e_\mathcal{L} &; y\ge M_1 \nl ((-\infty,M_2]\cap\mathbb{Z},0_K)\cup(\{M_1\},(y-M_1,1)) &; M_2<y<M_1 \nl \text{undefined} &; y\le M_2. \end{cases} \]
At a uniform node, an effective clamp gives a singleton-domain tag. The graph refers to actual input values, so the same tag can be pushed to either child.
The query reads $Q$ only on regular summaries. Although $Q$ is additive there, it has no integer-valued additive extension to all of $\mathcal{S}$. For example, a regular $s$ with $Q(s)=1$ would give:
\[ \bot\oplus s=\bot \quad\Longrightarrow\quad Q(\bot)+1=Q(\bot) \]
This is why allowing algebraic states outside $\Phi(X^\ast)$ matters for this construction.
Complexity
The distinct-value potential used for BOJ 17474 is insufficient here: a partial addition can create many distinct values at an ancestor. Instead, let $p(v)$ be the parent of a nonroot node $v$ and $d(v)$ its depth, with root depth zero. Define:
\[ \Psi=\sum_{v\ne\mathrm{root}}d(v) \left([M_1(v)<M_1(p(v))]+[m_1(v)>m_1(p(v))]\right) \]
There are fewer than $2N$ edges, two indicators per edge, and each weight is at most $h$. Thus $0\le\Psi_0<4Nh$.
Inside a fully updated subtree, addition preserves the indicators. A monotone clamp cannot create one: equal parent and child extrema remain equal after applying the same clamp. Only edges incident to partially covered nodes can gain potential. There are at most $3P_t$ such edges, each gaining at most $2h$.
Now consider a hard full chmin node $v$. Choose a leaf with value $M_2(v)$. Along its path from $v$, take the edge whose parent still contains $M_1(v)$ but whose child does not. Since no value lies strictly between $M_2(v)$ and $M_1(v)$, that child’s maximum is $M_2(v)$. After clamping at $y\le M_2(v)$, both extrema become $y$, so the maximum indicator disappears. Hard chmax has the symmetric argument for the minimum indicator.
Several hard nodes may use the same disappearing indicator. However, an edge at depth $d$ has at most $d$ possible hard ancestors, while deleting its indicator releases exactly $d$ potential units. Therefore:
\[ \Delta\Psi_t\le6hP_t-H_t,\qquad \sum_tH_t\le\Psi_0+6h\sum_tP_t=O(Nh+Mh^2) \]
All tag and summary operations take constant time. Thus the total time complexity is $O(Nh+Mh^2)$, with $O(h)$ time per query and $O(N)$ memory.
[BOJ 17475] 수열과 쿼리 27
Task
An integer array $A$ and two arrays $B,C$, initially $B_i=C_i=A_i$, are given. Support range addition, chmax, and chmin on $A$. After every operation:
\[ B_i\gets\min(B_i,A_i),\qquad C_i\gets\max(C_i,A_i) \]
For a range $[l:r]$, answer:
\[ \min_{l\le i\le r}A_i,\qquad \min_{l\le i\le r}B_i,\qquad \max_{l\le i\le r}C_i \]
Thus $B_i,C_i$ are the historical minimum and maximum at position $i$, including its initial value.
Solution
We use the same extrema summary $E$ and the same strict-gap conversion tests as in BOJ 17477. The additional information should retain historical extrema during delayed updates. Take:
\[ X=\Set{(a,b,c)\in\mathbb{Z}^3\mid b\le a\le c} \]
The point updates are:
\[ \begin{align*} A_x(a,b,c)&=(a+x,\min(b,a+x),\max(c,a+x)), \nl L_y(a,b,c)&=(\max(a,y),b,\max(c,y)), \nl U_y(a,b,c)&=(\min(a,y),\min(b,y),c). \end{align*} \]
For example, chmax cannot lower the historical minimum, and chmin cannot raise the historical maximum. Store:
\[ \Phi((a_i,b_i,c_i)_i)=(E,\beta,\gamma),\qquad \beta=\min_i b_i,\qquad \gamma=\max_i c_i \]
Then:
\[ \begin{align*} \mathcal{S}&=\Phi(X^\ast)\sqcup\{\bot\},\qquad e_\mathcal{S}=(E_\varepsilon,+\infty,-\infty), \nl (E,\beta,\gamma)\oplus(F,\beta^\prime,\gamma^\prime) &=(E\boxplus F,\min(\beta,\beta^\prime),\max(\gamma,\gamma^\prime)). \end{align*} \]
As before, $\bot$ is absorbing.
Let’s first consider a group of points which receives the same sequence of displacements. Let $d$ be its final displacement, and $p,q$ the minimum and maximum cumulative displacements, including the initial displacement zero. Then the entire history acts as:
\[ (a,b,c)\longmapsto \left(a+d,\min(b,a+p),\max(c,a+q)\right) \]
Thus we use coefficients:
\[ K=\Set{(d,p,q)\in\mathbb{Z}^3\mid p\le\min(0,d),\ q\ge\max(0,d)}, \qquad 0_K=(0,0,0) \]
Suppose $k_1=(d_1,p_1,q_1)$ is followed by $k_2=(d_2,p_2,q_2)$. The second history starts from $a+d_1$, so its extreme displacements relative to $a$ are $d_1+p_2$ and $d_1+q_2$. Therefore:
\[ k_2\diamond k_1 =\left(d_1+d_2,\min(p_1,d_1+p_2),\max(q_1,d_1+q_2)\right) \]
The product preserves the defining inequalities of $K$, is associative, and has identity $0_K$. For example, adding $5$ and then $-5$ gives $(d,p,q)=(0,0,5)$. The final value is unchanged, but the intermediate maximum is retained.
Take the common coefficient-graph tag set:
\[ \mathcal{L}=\mathcal{L}(K),\qquad e_\mathcal{L}=(\mathbb{Z},(0,0,0)),\qquad 0_\mathcal{L}=\emptyset \]
Use the same graph composition formula, with this history product $\diamond$. Thus a pair of pieces contributes:
\[ \left( J_1\cap(J_2-d_1), \left(d_1+d_2,\min(p_1,d_1+p_2),\max(q_1,d_1+q_2)\right) \right) \]
The closure proof depends only on the current translations. Hence the product still has at most three pieces, while its coefficients retain every intermediate historical extremum.
Apply the same validity test from BOJ 17477. For each occupied piece, let $u_j,v_j$ be its current minimum and maximum: both are $w$ for an isolated value $w$, and they are $u_I,v_I$ for the central interval. If the test fails, return $\bot$. Otherwise, transform the finite extrema of $E$ by the corresponding displacements $d_j$ to get $E^\prime$. Then:
\[ \lambda_l(E,\beta,\gamma)= \left( E^\prime, \min\left(\beta,\min_{j:n_j>0}(u_j+p_j)\right), \max\left(\gamma,\max_{j:n_j>0}(v_j+q_j)\right) \right) \]
No correlation with old histories is needed, because:
\[ \begin{align*} \min_i\min(b_i,a_i+p_{j(i)}) &=\min\left(\beta,\min_j\left(p_j+\min_{i:a_i\in J_j}a_i\right)\right), \nl \max_i\max(c_i,a_i+q_{j(i)}) &=\max\left(\gamma,\max_j\left(q_j+\max_{i:a_i\in J_j}a_i\right)\right), \end{align*} \]
where the inner extrema are taken only over occupied pieces. Every tag fixes the empty summary and $\bot$. The exact action and the same validity test again give total endomorphisms.
For addition, put:
\[ k_x=(x,\min(0,x),\max(0,x)),\qquad C(A_x,s)=(\mathbb{Z},k_x) \]
For an effective easy chmax or chmin, the changed extreme class receives, respectively:
\[ k^-=(y-m_1,0,y-m_1),\qquad k^+=(y-M_1,y-M_1,0) \]
Thus conversion is:
\[ C(L_y,s)= \begin{cases} e_\mathcal{L} &; y\le m_1 \nl (\{m_1\},k^-)\cup([m_2,+\infty)\cap\mathbb{Z},0_K) &; m_1<y<m_2 \nl \text{undefined} &; y\ge m_2, \end{cases} \]
\[ C(U_y,s)= \begin{cases} e_\mathcal{L} &; y\ge M_1 \nl ((-\infty,M_2]\cap\mathbb{Z},0_K)\cup(\{M_1\},k^+) &; M_2<y<M_1 \nl \text{undefined} &; y\le M_2. \end{cases} \]
Every singleton is easy. The three query answers are $m_1,\beta,\gamma$.
Complexity
The current-value updates and hard-node tests are exactly those of BOJ 17477. Historical fields do not affect which nodes are visited. Thus we use the same weighted edge potential:
\[ \Psi=\sum_{v\ne\mathrm{root}}d(v) \left([M_1(v)<M_1(p(v))]+[m_1(v)>m_1(p(v))]\right) \]
The same argument gives $\Psi_0=O(Nh)$ and $\Delta\Psi_t\le6hP_t-H_t$. Updating historical fields and composing history coefficients require only constant work per visit. Therefore, the total time complexity is $O(Nh+Mh^2)$, with $O(h)$ time per query and $O(N)$ memory.