Problem link: http://www.spoj.com/problems/ADAUNIQ/

My solution: https://ideone.com/srH91b

Verdict: TLE

How do I optimize my solution? Any help is really appreciated.

[Update] Got AC :) I missed out the fact that block sizes are of n^(2/3)

# | User | Rating |
---|---|---|

1 | tourist | 3509 |

2 | OO0OOO00O0OOO0O0…O | 3327 |

3 | Um_nik | 3297 |

4 | Syloviaely | 3274 |

5 | LHiC | 3216 |

6 | Petr | 3161 |

7 | Benq | 3130 |

8 | ko_osaga | 3124 |

9 | Swistakk | 3089 |

10 | dotorya | 3060 |

# | User | Contrib. |
---|---|---|

1 | Radewoosh | 185 |

2 | rng_58 | 161 |

3 | tourist | 158 |

4 | Petr | 152 |

5 | Swistakk | 150 |

5 | Vovuh | 150 |

7 | csacademy | 147 |

8 | PikMike | 146 |

9 | Errichto | 145 |

9 | Um_nik | 145 |

Problem link: http://www.spoj.com/problems/ADAUNIQ/

My solution: https://ideone.com/srH91b

Verdict: TLE

How do I optimize my solution? Any help is really appreciated.

[Update] Got AC :) I missed out the fact that block sizes are of n^(2/3)

↑

↓

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Sep/20/2018 16:42:50 (d1).

Desktop version, switch to mobile version.

User lists

Name |
---|

You're not supposed to compare right ends by buckets, instead, just compare values. So, replace

`if(r1 != r2) return r1 < r2;`

with`if(a.r != b.r) return a.r < b.r;`

In order to experiment, I did that too. Still got TLE :(

Ah, I see. The problem is with time updates, these might take up to

O(nq) (lines 81, 82)The time limit given is 8s. I read about MO's algorithm with updates in a blog (https://rezwanarefin01.github.io/posts/block-decomposition-01) and I got to know that the time complexity of the query is O(n ^(5/3)) which is around 7 seconds for this problem. Besides, the judge's solution is off-line as well. Is there any way to cleverly implement that part?

If you wish to know how to use Mo's algorithm with updates, check out this blog post, but I doubt that it will be fast enough.

Thank You :)

Thanks a ton :) I understood where I was wrong logically. Got AC now :)

Good job! There is also a

onlinesolution.Hintcomes from a 2D range query.

Another hintTry maintaining the set of segments [

u,v] such thata_{u}=a_{v}and there is noysuch thatu<y<vanda_{u}=a_{y}.Though you got AC, I'd like to mention one more thing.

I see you wrote —

This might be the reason of your getting TLE. I got 2-3 times too.

Instead of doing this, precompute block numbers and do —

This will significantly reduce your run time.

Can it be solved using offline BIT in something like

O(nlg^{2}(n)) just like the one without updates (DQUERY)?