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 | 3434 |

2 | Petr | 3353 |

3 | OO0OOO00O0OOO0O0…O | 3314 |

4 | fateice | 3306 |

5 | Um_nik | 3286 |

6 | Syloviaely | 3274 |

7 | dotorya | 3145 |

8 | LHiC | 3114 |

9 | Radewoosh | 3098 |

10 | mnbvmar | 3096 |

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

1 | tourist | 178 |

2 | rng_58 | 166 |

3 | Petr | 155 |

3 | csacademy | 155 |

5 | Swistakk | 149 |

5 | lewin | 149 |

7 | Um_nik | 142 |

8 | Errichto | 140 |

9 | matthew99 | 138 |

10 | Vovuh | 137 |

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: Jun/22/2018 02:58:52 (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)?