Problem:
Boring Commercial

Now it is spring holidays. A lazy student has finally passed all final examination, and he decided to just kick back and just watch TV all day. Oh, his
only source of entertainment is watching TV. And TV commercial, as usual, are
a big nuisance for him. He can watch any thing on television, but cannot bear
even a single second of commercial. So to prevent himself from the boredom of
seeing the boring commercial, he keeps shuffling through the TV channels, so
that he can watch programs on different channels without seeing even a single
commercial.

Given the number of channels, and the duration at which the TV commercials
are showed on each of the channels, you have to write a program which will
print the longest interval for which the lazy student can watch the television by
shuffling between the different channels without ever seeing an TV commercial.

For example, consider the simplified situation where there are only three
television channels, and suppose that he is watching TV from 2100 hrs to 2400
hrs. Suppose that the commercials are displayed at following time on each of
the channels.

Channel 1: 2100 to 2130, 2200 to 2230 and 2300 to 2330

Channel 2: 2130 to 2200, 2330 to 2400

Channel 3: 2100 to 2130, 2330 to 2400

Then in this case, he can watch TV without getting interrupted by commercials for full 3 hours by watching Channel 2 from 2100 to 2130, then Channel 3
from 2130 to 2330, and then Channel 1 from 2330 to 2400.

Input

The input will consist of several cases. In each case, the first line of the input
will be n, the number of channels, which will then be followed by p and q, the
time interval between which he will be watching the TV. It will be followed by
2n lines, giving the time slots for each of the channels. For each channel, the
first line will be k, the number of commercial slots, and it will then be followed
by 2k numbers giving the commercial slots in order.

The input will be terminated by values 0 for each of n, p, q. This case should
not be processed.

Output

For each case, you have to output the maximum duration (in minutes) for which
he can watch television without seeing any commercial.

Sample Input

1 2100 2400
1
2130 2200
3 2100 2400
3
2100 2130 2200 2230 2300 2330
2
2130 2200 2330 2400
2
2100 2130 2330 2400
0 0 0

Output for the Sample Input

120
180

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Text;

class TEST{
	static void Main(){
		while(true){
			var ss=Console.ReadLine().Split(' ');
			int n=int.Parse(ss[0]);
			int p=int.Parse(ss[1]);
			int q=int.Parse(ss[2]);
			if(n==0 && p==0 && q==0)break;
			Sol mySol=new Sol(n,p,q);
			Console.WriteLine(mySol.Ans);
		}
	}
}

class HrsInterval{
	public int Start{get;set;}
	public int End{get;set;}
	public int Range_Minute{
		get{
			int h0=this.Start/100;
			int h1=this.End/100;
			int m0=this.Start%100;
			int m1=this.End%100;
			if(m1<m0){
				h1--;
				m1+=60;
			}
			return (h1-h0)*60+m1-m0;
		}
	}
	public HrsInterval(int start,int end){
		this.Start=start;
		this.End=end;
	}
	
	public static HrsInterval createIntersection(HrsInterval I1,HrsInterval I2){
		//共通部分を生成。ない場合はnullを返す
		HrsInterval Ret=null;
		if(I1.Start<I2.Start){
			if(I1.End<=I2.Start)Ret=null;
			if(I1.End>I2.Start){
				if(I2.End<I1.End)Ret=new HrsInterval(I2.Start,I2.End);
				if(I2.End>=I1.End)Ret=new HrsInterval(I2.Start,I1.End);
			}
		}
		if(I1.Start>=I2.Start){
			if(I2.End<=I1.Start)Ret=null;
			if(I2.End>I1.Start){
				if(I1.End<I2.End)Ret=new HrsInterval(I1.Start,I1.End);
				if(I1.End>=I2.End)Ret=new HrsInterval(I1.Start,I2.End);
			}
		}
		return Ret;
	}
}


class Sol{
	int ans;
	public int Ans{
		get{return ans;}
	}
	int watchstart;
	int watchend;
	
	public Sol(int n,int p,int q){
		
		watchstart=p;
		watchend=q;
		
		List<HrsInterval> L1=new List<HrsInterval>();
		List<HrsInterval> L2=new List<HrsInterval>();
		
		for(int i=0;i<n;i++){
			var d =int.Parse(Console.ReadLine());
			var s =Console.ReadLine().Split(' ');
			var arr =Array.ConvertAll(s,elem=>int.Parse(elem));
			if(i==0){
				for(int j=0;j<d;j++){
					L2.Add(new HrsInterval(arr[2*j],arr[2*j+1]));
				}
			}
			if(i>0){
				L2.Clear();
				for(int j=0;j<d;j++){
					HrsInterval I;
					HrsInterval P=new HrsInterval(arr[2*j],arr[2*j+1]);
					foreach(HrsInterval Q in L1){
						I=HrsInterval.createIntersection(Q,P);
						if(I!=null)L2.Add(I);
					}
				}
			}
			L1.Clear();
			foreach(HrsInterval R in L2){
				L1.Add(R);
			}
		}
		
		//最終的にL2を使う。
		ans=CalAns(L2);
	}
	
	int CalAns(List<HrsInterval> L){
		L.Sort( delegate ( HrsInterval P,HrsInterval Q){if(P.Start-Q.Start!=0)return P.Start-Q.Start;return P.End-Q.End;});
		
		List<HrsInterval> Complement=new List<HrsInterval>();
		if(L.Count==0){
			Complement.Add(new HrsInterval(watchstart,watchend));
		}
		if(L.Count>0){
			Complement.Add(new HrsInterval(watchstart,L[0].Start));
			int k;
			for(k=1;k<L.Count;k++){
				Complement.Add(new HrsInterval(L[k-1].End,L[k].Start));
			}
			Complement.Add(new HrsInterval(L[k-1].End,watchend));
		}
	
		int max=0;
		foreach(HrsInterval S in Complement){
			if(S.Range_Minute >=max)max=S.Range_Minute;
		}
		return max;
	}
	
}