Create a visited array
int [ ] vis = new int [ MAX ] ; for ( int i = 0 ; i < interval . Length ; ++ i ) { int start = interval [ i ] . first ; int end = interval [ i ] . second ; vis [ start ] ++ ; vis [ end + 1 ] -- ; }