DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Strong Laser

Back to problem

Editorial

It is enough to determine the number of blocks in each row independently. The column lasers are not actually needed.

Suppose a row contains xxx blocks. If we fire a laser of strength kkk from left to right in this row, then the following holds.

  • If x≤kx \le kx≤k, the laser penetrates all blocks and exits the grid.
  • If x>kx > kx>k, the laser stops at the (k+1)(k+1)(k+1)-th block.

Therefore, if the result is n+1n+1n+1, the number of blocks in that row is at most kkk. Otherwise, it is greater than kkk. A single experiment tells us this comparison for every row.

Since 0≤x≤n0 \le x \le n0≤x≤n, we can find the number of blocks in each row by binary search. The key observation is that in one experiment, we may choose different strengths for all rows simultaneously. Thus, we can run the binary searches for all rows in parallel.

For each row iii, maintain the possible range [Li,Ri][L_i, R_i][Li​,Ri​], and set midi=⌊(Li+Ri) as the strength of the row laser. The column laser strengths can be arbitrary, so we set them to .

After the experiment, update as follows.

  • If the laser in row iii exits the grid, then the row has at most midimid_imidi​ blocks, so set Ri=midiR_i = mid_iRi​=.

Since n≤100n \le 100n≤100, each row has one of at most 101101101 possible counts, so at most 777 experiments are enough. This is within the limit of 101010.

When Li=RiL_i = R_iLi​=Ri​ for every row, LiL_iLi​ is the number of blocks in row . The answer is

∑i=1nLi\sum_{i=1}^{n} L_ii=1∑n​Li​

The time complexity is O(nlog⁡n)O(n \log n)O(nlogn).

/2⌋mid_i = \lfloor (L_i + R_i) / 2 \rfloor
midi​=⌊(Li​+Ri​)/2⌋
000
midi​
  • Otherwise, the row has more than midimid_imidi​ blocks, so set Li=midi+1L_i = mid_i + 1Li​=midi​+1.
  • iii