Hello all,
I am now attempting to solve the problem FLIPCOIN in the practice section, medium difficulty level.
I am aware that a naive solution gets TLE… However, I also know that some sort of segment tree data structure can and needs to be used to process queries fast.
Below is my naive and wrong (from the execution time point of view) code, that gives me a TLE:
#include <stdio.h>
int main()
{
int N,Q;
int i;
scanf("%d %d", &N, &Q);
char* array;
array = (char *)malloc(N*sizeof(char));
for(i = 0; i < N; i++)
{
array[i] = 'T';
}
int q;
for(q = 0; q < Q;q++)
{
int control,low,high;
scanf("%d %d %d", &control,&low,&high);
if(control == 0)
{
for(i=low;i <= high; i++)
{
if(array[i] == 'T')
array[i] = 'H';
else
array[i] = 'T';
}
}
else
{
int auxiliar = 0;
for(i=low;i <= high; i++)
{
if(array[i] == 'H')
auxiliar++;
}
printf("%d\n", auxiliar);
}
}
return 0;
}
What I would like to ask, if that is possible is either a very good and explanatory reference with examples to implement segment trees for a similar problem, or, something that would be even better, if someone with more expertise could provide some sort of “editorial” and provide me with tips along the way, so that I could implement a segment tree on my own, I would be very appreciated.
The reason why I ask this is that sometimes, even after reading editorials I have some trouble implementing some new data structures and a more guided and step by step explanation would help me a lot… If I can do simple things, one at a time and end up with a segment tree implemented and understood on my own, i’d be very, very thankful
Thanks in advance,
Bruno