LashaBukhnikashvili's blog

By LashaBukhnikashvili, 10 years ago, In English

here is the link of a problem,I wrote O(n log n + m log^3 n) solution,but I have wa16, here is my code.

please help to find my mistake,also I am interested log n and log^2 n solutions.

Any help will be appreciated very much :)

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

for O((n+m) log n) solution see here

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

    could you explain your solution?

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

      Sort the array keeping track of indexes so I have A[0] is the index of smallest number and A[n-1] is the index of biggest number

      Now if I want K-th smallest number in range [l,r] the naive way is like the code

      int tmp=0;
      for(int i=0;i<n;i++){
      if(l<=A[i] && A[i]<=r)tmp++;
      if(tmp==k){
      cout<<A[i]<<endl; //output the index
      break;
      }
      }
      

      This has complexity O(n) for each operation

      I use sqrt decompstion to make it faster

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

        Can you please comment your code a bit so that it makes it easier to understand? I have no idea what your pre[][] array does and not able to understand it from the link that you've posted.

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

          pre[i][j] means how many indexes between 1 and j exists in ith block

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

        it is hard to understand idea from your code, but when you explain I got it,thanks,great idea ;)