Can this problem be solved by sqrt decomposition?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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.