Ahmed-Nawaz's blog

By Ahmed-Nawaz, history, 3 days ago, In English

For context please refer to https://codeforces.me/blog/entry/159248.

So the first problem i am writing about is D. Bitwise Paradox (Div. 1 D, rated 3100)

First of all, I will be honest: I mind-solved this problem while lying in bed. It was time to sleep, but I started thinking about the problem and came up with a solution right there.

For problems like this, the first thing that comes to my mind is parallel binary search, and it turns out that if there were no updates, this problem could have been solved with it. Next I thought about a segment tree, but I could not see an obvious way to combine two ranges. So I thought about something else: how to compare two numbers.

Consider this approach. Go from the highest bit (the 29th) down to the 0th. At each bit:

If the bit is set in v: set it in our current mask, then find the minimum possible maximum over all ranges that contain, for every bit in the current mask, at least one number having that bit. If this was the lowest set bit of v, update the answer. Otherwise, do not update it, since the result so far is not yet guaranteed to be at least v. If the bit is not set in v: set it in our current mask and find the same minimum maximum. Then update the answer, because all the higher bits of v are already covered and this extra bit makes the OR strictly greater than v. After updating the answer, unset this bit again.

It is easy to see that this algorithm is correct. However, there is no obvious way to compute "the minimum maximum of a range that contains at least one number for every bit in the current mask," since there can be O(N) numbers for each bit. Thinking about the segment tree again, I realized this can be done with one.

Each node of the segment tree, covering the range [L, R], stores:

The range maximum. The range answer. For every bit i, the maximum over the range [L, x], where x is the first index in the node's range whose number contains bit i. For every bit i, the maximum over the range [x, R], where x is the last index in the node's range whose number contains bit i.

With this, a node's answer is the minimum of the answers of its left and right children, together with the answer for ranges that cross the middle. For the crossing part, we repeat the exact process described above. Queries can be answered simply because max is monotonic, and we only need to go as far as the last or first position of a bit (depending on whether we are in the left or right child). Overlapping ranges do not matter, since max is idempotent.

With this, the problem is solved. Personally, I found this problem easy, and I do not know why it is rated 3100.

Since my English is not good, this blog was proofread by Claude. The ideas and the solution are my own.I wrote the whole thing on my own first then gave it to Claude.

Any advice about the blog may be shared in comments. Thanks!

Full text and comments »

  • Vote: I like it
  • -2
  • Vote: I do not like it

By Ahmed-Nawaz, history, 3 days ago, In English

Some of you might skip the blog seeing i am cyan but please see my rating based heatmap (still doesnt change the fact that i am cyan lol). So recently i have been solving some red problems so i thought itd be nice to post a blog about my exact way of thinking and how i thought when i first see the problem to eventually solving the full problem.So from now on i will post a blog for every new red problem i solve (and also hard).For those i have previously solved in last few days i might also make a blog of them, share your opinions in comments.Also any recommendations on name of this series, if you have share in comments. (Maybe also for the name of blog). Just to clarify the blogs will not specificly be like the editorial to problems they will be about the sequence of thoughts that came to my mind. BTW if any of you have any advice on how i increase my rating then please share.

Full text and comments »

  • Vote: I like it
  • +15
  • Vote: I do not like it

By Ahmed-Nawaz, history, 6 weeks ago, In English

Its such a shame being an expert i solved only A and C in the contest. I ended up solving E 10 minutes after the end.

after seeing other peoples codes i believe my solution is a alot complex but as per me i think its a very good solution

first we construct the forest from the regular bracket sequence.

its basically a collection of rooted trees.

we make sure children of each node are strictly ordered like the first child must be the left most one as it matters.

now we can see that maximum consecutive ((( is equivalent to any path which starting from a node only goes down to the left most (first) child

an operation is equivalent to removing a leaf and putting it on top of tree

we might compute subtree sizes initially.

now we compute an important term cost[x] be the sum of all subtree sizes of nodes which come before this node in its parent ordered list

basically its the number of times we have to perform operation to make sure that this node is the first child of its parent

from here i used binary lifting define up[i][j] be the 2^jth ancestor of node i and sum[i][j] be the sum of first 2^j ancestors of node i (0th ancestor is i itself)

we can see that if we iterate the downmost node of final optimal path cost[x] is simply the cost of going to its parent

so after fixing first node we can use binary lifting to go as up as we can with some not exceeding k after that

if the final node we took has a parent then cant go up anymore just update the answer

else if we make this the left most node of the whole tree then we will be able to again include the nodes on whcih we perform an operation since they go to very top so we also compute the cost value for all nodes which dont have a prent then remaining value of k be val let current node be x and number of nodes be cnt if cost[x] <= val : we can update cnt = min(N / 2, cnt + k); because all those leaves go up and if we can make this as left most we can also add those k leaves to answer on whcih we performed operation

(pardon my english)

Full text and comments »

  • Vote: I like it
  • +19
  • Vote: I do not like it

By Ahmed-Nawaz, history, 6 months ago, In English

can anybody who uses #define int long long or #define int int64_t or doesnt uses int and only uses 64 bit integers please tell if through out the career due to MLE/TLE they have ever needed to switch to 32 bit and if it made a difference in verdict or not?

Full text and comments »

  • Vote: I like it
  • +17
  • Vote: I do not like it