a codeforces contest.

A. Grid L

two necessary conditions:

  1. total # of edges = 𝑛(π‘š+1)+π‘š(𝑛+1)=𝑝+2π‘ž
  2. because each L-shape removes one horizontal and one vertical edge, it must hold that min(horizontal,vertical)=min(𝑛(π‘š+1),π‘š(𝑛+1))β‰₯π‘ž

WLOG, let 𝑛<π‘š (rectangle more wide than tall). note that we can always tile an 𝑛 by 𝑛 square like so: and that this can be extended to the right like so for any π‘š: (both of these images are from the official editorial)

every vertical segment is covered by an L-piece, so this proves the sufficiency of the second condition. from the first condition, only βˆšπ‘+2π‘ž pairs (𝑛,π‘š) are viable, so we can just check all by brute force.

B. Unique Values

note that in an array in which every element occurs either once or twice, the parity of the number of single elements is always the same as the parity of the entire array. therefore, when we query, the only time the result does not have the same parity as π‘˜ is when 3 of the same value are contained. therefore, our query becomes the following:

select π‘˜ indices, and check whether any element occurs three times along those indices.

from here, we can binary search to find each of the three occurrences in log𝑛 queries, solving the entire problem in 3log𝑛 queries.

https://codeforces.com/contest/2219/submission/371073987

C. Coloring a Red Black Tree

First, note that there’s definitely no point of operating on a red node. Thus, all nodes will toggle only from black to red, the choice that we have to make is just the order in which the nodes become red.

Formally, let’s direct edge (𝑒,𝑣) from 𝑒→𝑣 if 𝑒 turns red before 𝑣. If a node is initially red, it must have 0 in-degree, and any other node must have positive in-degree (otherwise it will never be able to turn red). Then, the total time taken by this configuration is just βˆ‘degreein-degree over all nodes. We can minimize this quantity with a tree DP, in which dp[i][0/1] denotes the minimum time to turn the entire subtree of i red, and the 0/1 denotes whether the edge from 𝑖 to its parent is directed toward 𝑖 or not.

https://codeforces.com/contest/2219/submission/371075504