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)

Before contest

Codeforces Round #472 (rated, Div. 1, based on VK Cup 2018 Round 2)

05:39:14

Register now »

Codeforces Round #472 (rated, Div. 1, based on VK Cup 2018 Round 2)

05:39:14

Register now »

*has extra registration

Before contest

Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2)

05:39:14

Register now »

Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2)

05:39:14

Register now »

*has extra registration

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

1 | Petr | 3325 |

2 | Um_nik | 3284 |

3 | Syloviaely | 3258 |

4 | tourist | 3206 |

5 | anta | 3106 |

6 | fateice | 3099 |

7 | mnbvmar | 3096 |

8 | OO0OOO00O0OOO0O0…O | 3083 |

9 | Radewoosh | 3076 |

10 | HYPERHYPERHYPERC…R | 3071 |

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

1 | tourist | 183 |

2 | rng_58 | 169 |

3 | csacademy | 162 |

4 | Petr | 158 |

5 | lewin | 152 |

6 | Swistakk | 151 |

7 | matthew99 | 146 |

8 | Errichto | 142 |

9 | BledDest | 141 |

9 | Zlobober | 141 |

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: Mar/24/2018 12:55:46 (d2).

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