When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

Mike_Mirzayanov_Is_Pussy's blog

By Mike_Mirzayanov_Is_Pussy, 3 years ago, In English

Today I had CodeNation coding round (on-campus):

question1 : Give an array $$$a$$$ of size $$$n$$$ where $$$1<=n<=1e5$$$ and $$$1<=a_i<=2e5$$$, we need to find out for each $$$i$$$ number of other $$$j$$$ such that either $$$a_imoda_j=0$$$ or $$$a_jmoda_i=0$$$.

Example : 4 2 1 3

output : 2 2 3 1

We can solve it in nlogn by using concept of harmonic series.

question2 : given tree of size $$$n$$$, $$$1<=n<=1e5$$$ indexed from 0,rooted at node 0 and each node having distinct value from 0 till n-1. Find mex of each subtree.

Example : n=3 , edges : (0,1),(0,2) and values : v[0]=0, v[1]=1, v[2]=2. Output : mex[0]=3,mex[1]=0,mex[2]=0.

Was able to solve partially in $$$O(n*n)$$$. can someone tell how to solve in better complexity ?

question3 : Given an array $$$a$$$ of size $$$n$$$, beauty from $$$l$$$ to $$$r$$$ is defined as $$$a_l+(-1)*a_{l+1}+a_{l+2}+(-1)*a_{l+3}....$$$repeating till r. $$$1<=n<=1e4$$$. There are $$$Q$$$ queries, $$$1<=Q<=1e5$$$. Either 1 i x, which means update a[i]=x, or 2 L R, which means maximum beauty among all subarrays in index range L to R. Note that $$$-1e9<=x,a[i]<=1e9$$$.

Example :n=2 a: 4 -2 7, two queries 1 2 1e9 and 2 1 3. answer for query is 7.

How to solve 2nd and 3rd one ?

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can you share the code for 1st problem ?

  • »
    »
    3 years ago, # ^ |
    Rev. 5   Vote: I like it -12 Vote: I do not like it

    [DELETED]

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it
    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      can you explain the first one please?

      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 3   Vote: I like it 0 Vote: I do not like it
        • Let f[i] = frequency of value i in given array
        • and cnt[i] = count of value j such that i%j=0 or j%i=0
        • for every i, iterate over its multiples and perform cnt[i] += f[j] (case j%i=0)
        • again, if (i,j) is a pair, (j,i) is also a pair, so cnt[j]+= f[i]
        • when i=j, we did cnt[j]+=f[i] , cnt[i]+=f[j], so we subtract f[i] from each cnt[i] ( counted twice )
        • finally we don't want the pair with itself, so we subtract 1 from the total
        • ans = cnt[i] — f[i] — 1
»
3 years ago, # |
  Vote: I like it +15 Vote: I do not like it

Q2: Just observe that there will be a path from root to the node having value 0, outside this path all the other nodes will have their mex=0. Now for this particular path we can maintain an array starting from the node having value 0 to the root and marking all the values in that particular subtree.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    won't it give TLE?? I mean in worst case time complexity will be o(n^2)

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +14 Vote: I do not like it

      No, for each ancestor of the node with value 0, we'll only mark those nodes in its subtree which haven't already been marked. This is $$$\mathcal{O}(n)$$$.

»
3 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

how to do 1st and what is harmonic series

  • »
    »
    3 years ago, # ^ |
    Rev. 4   Vote: I like it 0 Vote: I do not like it

    Harmonic Series. As a side note, N/1 + N/2 + N/3 + ... + N/N is approximately NlogN. (reference)

    edited to remove incorrect approach.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it

      For input array 2, 4, 8, your output would be 1, 2, 3 but the correct output would be 2, 2, 2.

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thanks for pointing it out! Much appreciated. I've edited the comment to remove that part.

»
3 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

For Q2 use an Euler's tour on tree and store the flattened tree in array, then you can query on subarray for every node to calculate MEX of each node, you can use segment tree for calculating MEX for each query.

Total complexity: $$$O(nlogn)$$$

»
3 years ago, # |
Rev. 3   Vote: I like it +14 Vote: I do not like it

Someone asked me the second problem as a doubt after their round had ended.
The first solution I came up with was $$$N.log^2N$$$ using DSU on trees.

Spoiler
  • »
    »
    3 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Yes I now realise that it was an overkill :(

    What is better approach ?

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      There is a single chain of nodes in which one node will have the value $$$0$$$. For all subtrees which do not contain this node, the answer is simply $$$0$$$. For this chain, we can calculate the answers in linear time.

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        For that nodes which are ancestor of 0, while calculating its mex will we have to look for the nodes values in their subtree(this won't happen linearly) or is there any method by which we can find its mex just by the mex of its children?

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
          Rev. 8   Vote: I like it 0 Vote: I do not like it

          We can maintain an array/vector of size $$$N$$$, where each index has value either $$$1$$$ or $$$0$$$ depending on whether the current subtree contains that number or not.
          Starting from whichever vertex has the value $$$0$$$ (let's call it $$$v_0$$$), perform a depth-first search on all of its unvisited children, mark them as visited and then mark the starting node as visited as well.
          We also maintain a variable which stores the current MEX. While performing our dfs, flip $$$arr[value]$$$ from $$$0$$$ to $$$1$$$ for all vertices that we encounter. If we encounter a vertex which has its value equal to our current MEX, we flip our $$$arr[MEX]$$$ from $$$0$$$ to $$$1$$$, and keep moving forwards in the array until we hit a $$$0$$$ again, which is our new MEX. We can do this for all ancestors of $$$v_0$$$, moving up from $$$v_0$$$ towards the root.
          I hope this made sense. If it did not, please let me know!

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I think the tree question can be done in nlogn using set. Where set will repersent all the values that are not present in the whole substree and the merge the left and right subtree set and ans for i will be *st.begin()

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For Ques 3 you can use Segment Tree. It is a very general ST problem (refer this)

Finding subsegments with the maximal sum -> scroll down to this topic in above link.

It is almost same as given in the above link, the only difference is that you have to create 2 segment trees,

first for(-a[0],a[1],-a[2]...a[n]) and second for (a[0],-a[1],a[2],-a[3].....-a[n]).

Now depending upon the query type you can find the maximal subarray sum accordingly.

Also note that in the update query you will have to update both of your segment trees.

  • »
    »
    3 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    .

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think your solution is wrong because suppose u are querying on subarray [-100,1,2] in one of the query, then your solution will return 101 but actual answer is 2.

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

This is my proposed solution for Q2

Let loc[x] be the location of node with value x , Let LCA(x,y) be the lowest common ancestor of node x and y ,

Then LCA(loc[1],loc[0]) and its ancestors will have mex >=2 , LCA(LCA(loc[0],loc[1]),loc[2]) and its ancestors will have mex >=3 and so on...

Can anyone confirm if this will work ?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This sounds good logically, but how will it help in finding the answer?

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      So if a = 0 , b = LCA(loc[1],loc[0]) ,c = LCA(LCA(loc[0],loc[1]),loc[2])

      Then all nodes in range :

      • [a,b) will have mex = 1

      • [b,c) will have mex = 2

      • and so on..

      All the other nodes will have mex = 0

»
3 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Found video editorials for these questions.

»
3 years ago, # |
  Vote: I like it +2 Vote: I do not like it

One can try GSS3 on spoj. It's similar to 2nd problem.

»
3 years ago, # |
  Vote: I like it +7 Vote: I do not like it

How will we do the question 2 if the nodes don't have distinct values and also the values ranges from 0 to 1e9?

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Question 2

Observation 1: If there is No zero ans will be 0 for all

Observation 2: The nodes which occur in between from root to node which has zero value they will have MEX > 0

Approach: Find node with 0 value (say NODE) and do dfs mark all values which are in the subtree of that node,

use a variable (say mex = 0) that will store ans for this node. while mex is marked true as visited increase it and find first missing positive. now move NODE to parent[NODE]

Do this while NODE != -1


class Solution { public: vector<vector<int>> adj; vector<int> vis; void dfs(int curr, vector<int>&nums){ vis[nums[curr]] = 1; for(int v : adj[curr]){ if(!vis[nums[v]]) dfs(v, nums); } } vector<int> MexSubTree(vector<int>& parent, vector<int>& nums) { int n = parent.size(); int idx = -1; for(int i = 0; i < nums.size(); i++){ if(nums[i] == 0){ idx = i; break; } } vector<int> ans(n, 0); if(idx == -1)return ans; adj.resize(n); vis.resize(n+2, false); int MEX = 0; for(int i = 1 ; i < n ; i++){ adj[parent[i]].push_back(i); } while(idx != -1){ dfs(idx, nums); while(vis[MEX]) MEX++; ans[idx] = MEX; idx = parent[idx]; } return ans; } };
»
21 month(s) ago, # |
Rev. 2   Vote: I like it +2 Vote: I do not like it

Easy to understand and simple solutions,

Alternating queries: https://ideone.com/LFGNGG
Find mex in tree: https://ideone.com/tfUIEn

Hats off to the authors, who created these. Truly mind-blowing problems.