Can this problem be solved by sqrt decomposition?
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Can this problem be solved by sqrt decomposition?
Name |
---|
Yes indeed.This problem can be solved using sqrt decomposition.
This is the solution i have written. Dont know why m i getting TLE 33802921
Does anybody have Java implementation for the same?
check uwi's code 33733698..But it is segment tree implementation
I didnt get the logic
I solved is with sqrt decomp and dsu. My java solution TLEd tho so I just ported it to c++ to get it to pass. I think it's probably solvable with sqrt decomp in java but you might need to do something smarter than I am or perhaps add some more optimizations. JAVA: 33816939 C++ : 33817280
What exactly did u use the dsu for ? Why cant we just use linkedlist ? I didnt get whats the flaw in my logic .. I have given the solution link above
So basically if it said to turn x into y, I had a lookup table for which set was currently x and which set was currently y and I merged both sets. Then I put that no set is currently x. Looking through your solution it does seem to have the same runtime as mine but perhaps the fact that you initialize 100 linked list per bucket is where the main difference is since mine only initializes 1 dsu and 2 arrays.
I'll try using dsu
Here's my AC Solution : 33799911 .
I did get TLE in one of my submission on Test 2, link : 33755328, it was due to way the elments in one bucket were shifted from x to y. Intially, I used merge() to shift element to y and then delete the elements from x. This leads to TLE. After using splice() , if worked fine till test case 114, because of way I've coded the process,made necessary changes in the implementation, and it works in 2047ms.
I did see a submission with sqrt decomposition working in ~970ms. link :33736310
Well, SQRT decompostions works for this problem. It's only the way they have been implemented in the code leads to TLE.
I don't have much knowledge of Java implementation, so can't help with your code.
Here's a link to an interesting question, it asks you to change element x to y in range l to r, and also count element x in range l to r : https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/replace-27c5286c/ , consider it as more advance version of this question. Tester's (Bohdan Pryshchenko) solution is in C++.
Thank you for your reply. For the advanced vesrion of the question that you have posted link for, we will have to use node based pesistant segment tree right ?
Yep, Persistent Segment Tree.