http://www.codechef.com/problems/MSTICK in this qstn i am getting WA

plz point the mistake in my algorithm that i approched

1)created a segment tree which give minimum value in given range

2)similarly created a segment tree which give maximum value in given range

3)Now let the given L and R given input for query(here i am takeing single query i.e. q=1)

a=min(L,R)//a is minimum value in range L and R(search_segment_min(0,n-1,L,R,0))

a1=max(L,R)//a1 is maximum value in range L and R(search_segment_max(0,n-1,L,R,0))

b=max(0,L-1)//b is maximum value in range L and R(search_segment_max(0,n-1,0,L-1,0))

c=max(R+1,n-1)//c is maximum value in range L and R(search_segment_max(0,n-1,R+1,n-1,0))

b1=maximum(b,c)//give maximum value btwn b,c

if(b1>(a1-a)/2)

print(b1+a);

else

print((a1+a)/2);