I did the same thing as mentioned in the editorial first making a map of the position of the fence and color .
Then I followed the same procedure as mentioned in the editorial . Still I was getting TLE . It would be great if someone points out the mistake in the given code.
Hey, instead of using cin/cout, try scanf() and printf(), it did the trick for me
and also I heard somewhere that vectors are marginally slower than arrays, but mainly just try printf(), scanf() hope it thelps
or if u want to use cin and cout, like if ur comfortable with it, then use this line
std::ios::sync_with_stdio(false); under the int main() line of your code.
Note:- After writing this line you can only use cin and cout for i/o. Using scanf and printf after thiss will give you rutime error SIGSEGV.
It is for fast input/output ofcourse.
I tried using scanf and all the other optimisations but still I got TLE .
I would like to point out that most of the codes getting TLE posted here have actually the correct logic and implementation and I have faced the same problem. The performance issue is note due to the modular arithmetic but due to slow I/O and not that here we have up to 10^5 inputs.
cin, cout are much slower than scanf and printf, to make them faster you have to include this line
std::ios::sync_with_stdio(false);
at the beginning. For example take my AC solution and TLE solution. Both being exactly the same, I got a 3x performance by including the line above. Hope this helps in future. Kudos !!
I had tried this actually but then I was getting WA .
It would be great if you once check the code
See that the ans could be greater than 10^9, you have to take make your “ans” variable of the type of long long.
Yes got it . Thankyou