karan173's blog

By karan173, 6 years ago, In English,

i am looking to solve the problemORDERSET. I was thinking about using Binary Index Tree but i dont think we'll be able to create such a large array(x<=10^9). Also i suppose the problem can be solved with a self balancing BST but the standard Java implementations dont provide efficient rank functionality. (it is O(n) in TreeSet) So are there some alternate ways to solve this problem besides implementing my own self balancing BST?

 
 
 
 

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

Try to write own self balancing BST. I have done it using treap.

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

There is also offline solution. Compress the numbers, then Segment Tree will solve the problem.

But treap should be easier to implement here.

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

Try #1: I tried to solve this problem by building a segment tree with dynamic memory

Result: Time limit exceeded


Try #2: Switched iostream to stdio

Result: Time limit exceeded


Try #3: I compressed the numbers and then built a static segment tree

Result: Time limit exceeded


Try #4: Switched iostream to stdio

Result: Time limit exceeded


Try #5: I coded an AVL Tree class to see if that would make it run in time

Result: After extensive debugging, Time limit exceeded


Try #6: Last thing to try, again switch from iostream to stdio

Result: Accepted


This problem gave me a thousand headaches and, by the way, the AVL Tree class was the hardest thing I ever had to code in my life.

But I'm glad I did it. Additional knowledge is always a good thing :)

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

    btw what's meant by "compressing" the nos.?

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

      If you are given N numbers, compression is assigning numbers from 1 to N to the numbers given in the input. You can do that in the following way...

      1) Read the input and store the numbers in an array.

      2) Sort the array.

      3) Walk the array in order, assigning to each DIFFERENT element an integer. You can do that with a map that says Compressed[n] = x, and another one that says Original[x] = n, if you ever need to know what the original number of compressed x was.

      Compression is a very useful technique. In the case of this problem, it's useful because you can then build a segment tree with 2^19 nodes using the compressed numbers instead of the original ones.

      Other problems where you can use compression are:

      • USACO Contest December — Bronze Problem 3
      • USACO Contest January — Bronze Problem 1
      • Codeforces Problemset — Colorado Potato Beetle
      • Many others
      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        thanks for the gr8 info!

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

        Can you please post links to the other similar questions if possible? Thanks. :)

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

    please can you copy your implementation to AVL tree, I'm looking for a good implementation to AVL tree in C++

    thank you.

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

      This is the AVL Tree class I coded for this problem. It's the first time I coded an AVL Tree, so I doubt it's good, but it worked. Here it is...

      Link here --> http://pastebin.com/iXejKdKs

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

        you'd better send your code to pastebin and give a link to it in your comment

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

          Yeah, I changed it. Sorry for that...

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

        thank you very much

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

        what was the best runtime for your solution in the end ?!

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

          Runtime was 0.65.

          Now the time limit is 0.300 s. I wonder if that's an error... it doesn't seem correct.

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

            yes :D the same for me. It took 0.60 and i just want to be sure maybe it's something like a new compiler or another stuff

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

    an old but effective trick is to replace "endl" by "'\n'".i did it by segment tree but it wasn't iterative so i think you could(if you didn't) do it faster by using this way.

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

Solved it using Binary Indexed Tree, the coolest data structure in the world. And don't bother for the 10^9 thing. That's just maximum value which just fits in an 32bit int. The catch is, query is at most 200000. So you just need an array of at most size 200000. After that cached the input, updated, deleted etc. Btw, got TLE at first attempt. Then just replaced all long longs with int and got AC. I know it's a bit awkward , but for old systems like in SPOJ(Cluster: Pyramid (Intel Pentium III 733 MHz) it takes more time to deal with long long than with int. Try it.

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

    In fact, It was not BIT that did a great job for you in solving this problem. Instead, coordinates' compression was a vital part... not BIT ; )

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

      Let's just say it was both :D Coordinate compression is frequently used with data structures such as BIT.

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

    Did you do it in Q * logQ * logQ or Q * logQ ?

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

My tiny solution for problem... :)

http://ideone.com/9uklMf

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

as mentioned in the comments (in spoj) try solving this after you're done :D

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

Google "Policy Based Data Structures" . It's new a new thing to grasp and implement.

»
9 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I have solved this problem using BIT in offline.

Here is the approach: 1.compress the numbers 2.then for every query,complete the required task.To insert a number,use its compress value as a index.To find out Kth element,do binary search

Hope,who will serach for this problems hint to solve using BIT,he/she will get a hint here :)

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

    So we have to compress the numbers in the same way as they are suppose 5<10 so in the compressed form also 5 must get smaller value than 10 but how do we make this happen as numbers are dynamic so suppose ques asks you to insert 10 then then you will give it value 1 and suppose it then asks you to insert 5 so you will give it value 2.Please help me :(

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

    Can you share your solution via ideone or some other option ? I find it little difficult in solving the question via BIT. Thank you.