Skip to content

Tushar joshi the prestige#19

Open
tusharjoshi4531 wants to merge 8 commits intomainfrom
TusharJoshi-ThePrestige
Open

Tushar joshi the prestige#19
tusharjoshi4531 wants to merge 8 commits intomainfrom
TusharJoshi-ThePrestige

Conversation

@tusharjoshi4531
Copy link
Contributor

@tusharjoshi4531 tusharjoshi4531 commented Sep 5, 2023

Let a deck of cards $D$ be represented as an array of pair of integers $(v_a, v_b)$ where $v_a$ is the number printed on the up face and $v_b$ is the number printed on the down face.

Let the first deck be $D_1$ and the seconde deck be $D_2$.

Observation: The face up cards of the first deck always form the following sequence -

$\exists k \in [1, n]$ such that $D_1[i] = (-1, 1) \forall i \le k$ and $D_1[i] = (1, -1) \forall i > k$

Our data-structure must support the following operations -
Let $T$ denote transformation on elements

  1. For any $l, r \in [1, n]$ such that $l \le r$, $T(D_2, l, r, 1) = \forall x \in [0, l - r]$ $D_2[l + x] = (D_2[r - x].v_b, D_2[r - x].v_a)$
  2. For any $l, r \in [1, n]$ such that $l \le r$, evaluate - $$F_A(l, r) = \sum_{x=l}^{r} D_2[x].v_a$$
  3. For any $l, r \in [1, n]$ such that $l \le r$, evaluate - $$F_B(l, r) = \sum_{x=l}^{r} D_2[x].v_b$$

These operations can be made by using a splay tree.
It is chosen as:

  • It is binary tree which allows to lazily push updates as long as node information can be calculated without explicitly performing the transformation
  • It supports combing range to [l,r] in one sub tree of the binary tree in $O(log(N))$ amortised

The first update can be made using lazy update based in the following transformation.
$$T(D_2, l, r, 1) = [ T(D_2, mid + 1, r, 1) T(D_2, l, mid, 1) ]$$
$$T(T(D_2, l, r, 1), 1) = T(D_2, l, r, 0) = D_2$$

The second and third query can be made using the following fact.
$$F_A(l, r) = F_A(l, mid) + F_A(mid + 1, r)$$
$$F_B(l, r) = F_B(l, mid) + F_B(mid + 1, r)$$

This follows that

$$F_A(T(l,r)) = F_B(mid+1,r) + F_B(l,mid)$$ $$F_B(T(l,r)) = F_A(mid+1,r) + F_B(l,mid)$$

Which helps us compute values of $F$ in constant time without actually performing the transformations
The actual transformations will just reverse the left and right pointer of child and call transformations on them

@sg60692
Copy link
Contributor

sg60692 commented Sep 5, 2023

I have added a few details to fill the missing points in the description, do have a look.
Please add, the space and time complexity as well.
Overall, seems a very well written and thought out approach
A couple of pointers:

  • Some symbols and nomenclature may be innate to you, or even the reviewer, however it may be not be generic, so providing sufficient details would be better, For eg. T denotes transformation
  • When particular data structures are used, its important to realise which criterion or situation we have established that they are suitable for

@sg60692
Copy link
Contributor

sg60692 commented Sep 5, 2023

Please change the branch the pr is being merged to !!!!


node nodes[N];

void g(int u) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: pull would be a better name

}
}

int find(int root, int l) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if Splay are added into the template first and using that this question is solved as it is highly unlikely to write splay from scratch in live contests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incase you feel we should merge this solution right now and may be add to the template later, would also work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants