Блог пользователя Mike_Mirzayanov_Is_Pussy

Автор Mike_Mirzayanov_Is_Pussy, 3 года назад, По-английски

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 ?

  • Проголосовать: нравится
  • +39
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

can you share the code for 1st problem ?

»
3 года назад, # |
  Проголосовать: нравится +15 Проголосовать: не нравится

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 года назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

how to do 1st and what is harmonic series

»
3 года назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, # |
Rev. 3   Проголосовать: нравится +14 Проголосовать: не нравится

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 года назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    Yes I now realise that it was an overkill :(

    What is better approach ?

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится +10 Проголосовать: не нравится

      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 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        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 года назад, # ^ |
          Rev. 8   Проголосовать: нравится 0 Проголосовать: не нравится

          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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    .

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

Found video editorials for these questions.

»
3 года назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

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

»
3 года назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится

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 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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; } };
»
22 месяца назад, # |
Rev. 2   Проголосовать: нравится +2 Проголосовать: не нравится

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.