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)

Please subscribe to the official Codeforces channel in Telegram via the link: https://t.me/codeforces_official.
×

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

1 | tourist | 3469 |

2 | Radewoosh | 3357 |

3 | OO0OOO00O0OOO0O0…O | 3264 |

4 | LHiC | 3227 |

5 | ksun48 | 3184 |

6 | scott_wu | 3168 |

7 | Um_nik | 3166 |

8 | CongLingDanPaiSh…5 | 3157 |

9 | Petr | 3139 |

10 | V--o_o--V | 3135 |

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

1 | Radewoosh | 195 |

2 | Errichto | 175 |

3 | rng_58 | 158 |

4 | Ashishgup | 157 |

4 | neal | 157 |

6 | tourist | 154 |

7 | PikMike | 152 |

8 | Petr | 151 |

9 | Um_nik | 149 |

10 | kostka | 148 |

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: Dec/17/2018 23:33:37 (d3).

Desktop version, switch to mobile version.

Supported by

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)?