IOI EGOI SG
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Mexican TST Editorial ## Xor Tree ### Subtask 1 Just code the brute force ### Subtask 2 Let $p[v]$ be the xor-sum of all the edges on the path from vertex $v$ to the root of the tree. If $u$ is an ancestor of $v$, the weight of the path $u \leftrightarrow v$ is $p[u] \oplus p[v]$. Generally, if $u$ and $v$ do not have an ancestor-descendant relationship, then the weight of the path $u \leftrightarrow v$ is $(p[u] \oplus p[lca(u,v)]) \oplus (p[v] \oplus p[lca(u,v)]) = p[u] \oplus p[v]$. That is, for any path $u \leftrightarrow v$, the weight is $p[u]\oplus p[v]$. Now, given the array $p$, our job is to find $\sum\limits_{1\leq u<v\leq n} p[u] \oplus p[v]$. In this subtask, $p[u]$ is $0$ or $1$. So our answer is just $(\text{number of 0s}) \times (\text{number of 1s})$ ### Subtask 3 In this subtask you don't need the LCA observation, since the tree is a line, just take the prefix xor and do the same thing. However, we need to deal with the larger values. A common trick to tackle such that bitwise summations is to consider each bit position seperately, cause different bit positions do not affect each other. As a concrete example, $abc_2 \oplus def_2 = (a00_2 \oplus d00_2) + (b0_2 \oplus e0_2) + (c_2 \oplus f_2)$. If we consider only the least significant bit, we will ask our self this: If $p$ had only $0$ and $1$ as elements, what is $\sum\limits_{1\leq u<v\leq n} p[u] \oplus p[v]$? It is $(\text{number of 0s}) \times (\text{number of 1s})$. So you just use this formula for each bit position. ### Subtask 4 For the full solution, you combine the above two subtasks. ## JP's Students ### Subtask 1 Brute force search ### Subtask 2 Notice that the parity of the value depends on the parity of the position. If $\frac{N}{2}$ is odd, then $a_i$ and $a_{i+\frac{N}{2}}$ have different parities and is definitely not possible. ### Subtask 3 The sequence looks something like `0 1 2 3 4 3 2 1`, and then we can binary search for the maximum point. ### Subtask 4 Since we are looking at two opposite points which are the same, we can consider $b_i = a_i - a_{i+\frac{N}{2}}$, and analyze what to do with $b$. Anyways, notice $b_0 = -b_{\frac{N}{2}}$, and also that $b_{i+1} - b_i$ is either $2$,$0$ or $-2$. Since the value goes from positive to negative, and changes only in steps of 2, we will surely find some $b_i$ along $b_0$ and $b_{\frac{N}{2}}$ that is $0$. (Discrete Intermediate Value Theorem). How to find it quickly? Binary search on $b_i$. Suppose we know that $b_i > 0$ and $b_j < 0$, then there must exist a $b$ in between $i$ and $j$ that is exactly $0$. If we query the midpoint $m = \frac{i+j}{2}$, if $b_m > 0$, then we can query the $m$ to $j$ half. otherwise, we can query the $i$ to $m$ half. Since obtaining a single $b_i$ needs $2$ queries, we will use $2 \log N$ queries. Tangentially related https://www.youtube.com/shorts/mHA-emoi2PI. ## The name of this problem makes no sense. ### Subtask 1 For every $l$, try every $r$, you will need to maintain a set of occurences, and then you can check if the largest two elements are the same or not. The most straightforward way is as follows: - use a count array to store the number of occurences of each element - a multiset to store the set of occurences Then check the multiset largest 2 elements. ### Subtask 2 Make a $1$ be $1$ and a $2$ be $-1$. Then we're looking for a subarray with sum of $0$. Use prefix sums + a map for this. ### Full sollution Let $x$ be the element in the entire array that appears the maximal number of times. If $x$ is not unique, the answer is $n$. So assume that $x$ is unique. Observation 1: if $x$ does not have the maximum occurance in our answer subarray $[l,r]$, our answer is not optimal. Proof: - define $A$: occurences of $x$ - define $B$: maximum occurences of any other element than $x$ When we remove some element from the subarray, $A$ and $B$ will either decreasing by one or stay the same respectively. Now, when the subarray is $[1,n]$, we have $A>B$, however, when the subarray is $[l,r]$ we have $A < B$. Therefore, if we slowly remove elements from $[1,n]$ to $[l,r]$, there has to be some point in time where $A=B$. Therefore, we have found a more optimal answer. Observation 2: either $l=1$ or $a[l-1]=x$. Similarly $r=n$ or $a[r+1]=x$. Proof: If the optimal range is $[l,r]$, we can achieve this range by shrinking $[1,n]$ while maintaining $A \geq B$. The final shrink which leads to $A=B$ will be when $a[l-1] = x$ or $a[r+1] = x$ (or $l=1$ or $r=n$). Now, let us solve the problem with our observation. Suppose the array only contains elements $x$ and $y$. Let $S$ be an array which is $1$ if there is $x$ and $-1$ if there is $y$. Our goal is to find the longest subarray whose sum is $0$. If we can do this in $O(\text{number of y elements})$. Briefly, we will figure out the relative positions of the elements of $y$ with respect to the elements of $x$. Consider the prefix sum of $S$. We will want to skip elements that appear only once. ![ ](https://i.ibb.co/C714Yw0/image.png) In the above image, we only care about those elements that appear in the red box. It should be clear that if there are very little $y$ elements, the number of elements we care about is very little. Indeed, the number of elements we care about can be bounded by $3 \cdot \text{number of y elements}$. Because for each $y$ element, just account the $x$ elements on the same level that is nearest on the left and on the right. Then, all important $x$ elements would be accounted for. ![ ](https://i.ibb.co/9WCNFZJ/image.png) So just code the brute force smartly and it should pass. ## Niccoriˆˆ Survey Team https://artofproblemsolving.com/community/c6h2522661p21410015 See CSJL's solution. Note that the proof that the size of $N_a$ is at least $\lfloor \frac{k-1}{2} \rfloor+1$ that given by CSJL is wrong, that there is a vertex distance $0,1,\ldots,\lfloor\frac{k-1}{2}\rfloor$ away from vertex $a$ (consider a star graph). But his condition is correct if we assume that $N_a$ does not cover all vertices in it's connected component in $G$. Also, the bound of $\frac{2n}{k}$ distinct vertices is tight. Let $k=2q$, then we can construct a star graph with $p+1$ vertices, where we replace each leaf with a path of length $q$ to obtain a new graph of size $pq+1$. We can choose all the leaf vertices to get $p$ leaf vertices fulfilling the property. And $\frac{2n}{k} = \frac{2pq+2}{2q} = p + \frac{1}{q}$. <hr> However, this is IO and not MO. There is no way the judge can force us into the worst case. What is the average size of $|S|$ if we randomize? To make $S$ as large as possible when $G$ is connected, $G$ must be a tree. Claim: $E(|S|)$ is maximized when $G$ is a line. Proof: idk, it seems legit When $G$ is a line, $E(|S|) \approx \frac{n}{2k-1}$, by linearity of expectation. Note that I never analyzed the variance. But for $100$ attempts of $n=10^4$ and $k=50$, the maximum $|S|$ was $110$, so the variance is not super big. Is there a solution that can also recovery a certification for a disconnected pair? ## The N Lives of Raichu https://qoj.ac/contest/760/problem/4912 The problem basically needs us to create a counter that counts up to $n = 2^{l}$, and we can only update and query at most 6 bits in one iteration. Let's consider the sequence of all $l$ bit numbers in binary. ``` 000 001 010 011 100 101 110 111 ``` Let $S$ be an integer written, and $S$ needs to change according to the above sequence. Now, we want to make the transitions between them has as few bits changes as possible. Hence, we use the following algo: - start from $ps=0$ and if $S[ps]==1$, set to $S[ps]=0$ and increment $ps$. - else, set to $S[ps]=1$ and reset $ps$ back to 0 The way $S$ changes will look like: ``` 000* 001* 000 010* 011* 010 000 100* 101* 100 110* 111* 110 100 000 ``` The asteriks are the key points where things are set to 1, and $ps$ is returned back to $0$. The sequence has length $2^{l}-1$. If we observe the index which is changed at each step, it looks like this: `(0) (0 1) (0) (0 1 2) (0) (0 1) (0) (0 1 2 3)` The idea is we use $5$ bits to store $ps$. On iteration $i$, retrieve the current value of $ps$. We also store $S$ in a series of bits after the first $5$ bits. In each iteration we 1. retrieve the value of $ps$ 2. Check the value of $S[ps]$ a. If it's 1, set $S[ps]$ to 0, and increment $ps$ b. If it's 0, set $S[ps]$ to 1, and set $ps$ to 0 3. Write the new value of $ps$ This will work for the first $2^{l}-1$ steps, and the last step can be handled seperately with an additional statement (in the code below, it is the `ps!=l-1` part). ```c++ int l=__builtin_ctz(n),ps=0; for (int i=1;i<=5;++i) ps=(ps<<1)|query(i); if (l==ps) return 1; if (!query(6+ps)&&ps!=l-1) modify(6+ps,1),ps=0; else modify(6+ps,0),ps++; for (int i=1;i<=5;++i) modify(i,(ps>>(5-i))&1); return 0; ``` Actually this is just the main body of the code lol

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully