using System.Collections.Generic;
public static long kRectangleIntersection (int[] xl, int[] yl, int[] xh, int[] yh, int K)
for (int i = 0;i < N;i++)
BusyRectangle newRect = new BusyRectangle(xl[i], yl[i], xh[i], yh[i]);
mArea = Math.Max (mArea, newRect.Area ());
Dictionary<BusyRectangle,HashSet<int>> hash = new Dictionary<BusyRectangle,HashSet<int>> ();
for (int i = 0;i < N;i++)
for (int j = i + 1;j < N;j++)
int iXl = Math.Max (xl[i], xl[j]);
int iYl = Math.Max (yl[i], yl[j]);
int iXh = Math.Min (xh[i], xh[j]);
int iYh = Math.Min (yh[i], yh[j]);
if ( iXl < iXh && iYl < iYh )
hash.Add(new BusyRectangle(iXl, iYl, iXh, iYh), new HashSet<int> ());
Dictionary<BusyRectangle,HashSet<int>> hash4 = new Dictionary<BusyRectangle,HashSet<int>> ();
var allKeys = new List<BusyRectangle> (hash.Keys);
for (int i = 0;i < allKeys.Count;i++)
for (int j = i + 1;j < allKeys.Count;j++)
int iXl = Math.Max (hash[allKeys[i]].xl, hash[allKeys[j]].xl);
int iYl = Math.Max (hash[allKeys[i]].yl, hash[allKeys[j]].yl);
int iXh = Math.Min (hash[allKeys[i]].xh, hash[allKeys[j]].xh);
int iYh = Math.Min (hash[allKeys[i]].yh, hash[allKeys[j]].yh);
if ( iXl < iXh && iYl < iYh )
bool[] notIntersect = new bool[N];
for (int k=1;k < K && !flag;k++)
Console.WriteLine ("k: " + k + " Total rectangles: " + hash.Keys.Count);
var allKeys = new List<BusyRectangle> (hash.Keys);
for (int i = 0;i < N;i++)
foreach (var key in allKeys)
if ( hash[key].Contains (i) )
int iXl = Math.Max (xl[i], key.xl);
int iYl = Math.Max (yl[i], key.yl);
int iXh = Math.Min (xh[i], key.xh);
int iYh = Math.Min (yh[i], key.yh);
if ( iXl < iXh && iYl < iYh )
BusyRectangle newRect = new BusyRectangle(iXl, iYl, iXh, iYh);
mArea = Math.Max (mArea, newRect.Area());
if ( hash.ContainsKey (newRect) )
int prevCount = hash[newRect].Count;
hash[newRect].UnionWith (hash[key]);
if ( prevCount < hash[newRect].Count)
hash.Add (newRect, new HashSet<int>(new List<int> () {i}));
hash[newRect].UnionWith (hash[key]);
maxCount = Math.Max (maxCount, hash[key].Count);
if ( !bIntersect || countNew == 0 )
Console.WriteLine ("k: " + k + " i: " + i + " Total rectangles: " + hash.Keys.Count);
all = new List<BusyRectangle> (hash.Keys);
List<BusyRectangle> removeKeys = new List<BusyRectangle> ();
foreach (var key in hash.Keys)
if ( hash[key].Count <= (k + 1) )
Console.WriteLine ("Keys to remove: " + removeKeys.Count);
foreach (var rect in removeKeys)
Console.WriteLine ("2 k: " + k + " Total rectangles: " + hash.Keys.Count);
foreach (var key in hash.Keys)
if ( hash[key].Count >= K )
maxArea = Math.Max (maxArea, key.Area());
public static void Main()
Random rnd = new Random (100);
for (int i = 0;i < N;i++)
xl[i] = rnd.Next(0, maxValue >> 1);
yl[i] = rnd.Next(0, maxValue >> 1);
xh[i] = xl[i] + rnd.Next(0, (maxValue >> 1) - 1);
yh[i] = yl[i] + rnd.Next(0, (maxValue >> 1) - 1);
int K = Math.Min (6, N - 1);
Console.WriteLine ("N: " + N + " K: " + K);
Console.WriteLine(kRectangleIntersection (xl, yl, xh, yh, K));
class BusyRectangle : IComparable
public int xl { get; private set; }
public int yl { get; private set; }
public int xh { get; private set; }
public int yh { get; private set; }
public BusyRectangle (int xl, int yl, int xh, int yh)
return (long)(xh - xl) * (long) (yh - yl);
public override int GetHashCode ()
int hash = (int) 2166136261;
hash = (hash * 16777619) ^ xl;
hash = (hash * 16777619) ^ yl;
hash = (hash * 16777619) ^ xh;
hash = (hash * 16777619) ^ yh;
public int CompareTo (object obj)
BusyRectangle other = (BusyRectangle)obj;
if ( Area () < other.Area () )
else if ( Area () > other.Area () )
public override bool Equals (object obj)
BusyRectangle other = (BusyRectangle)obj;
if ( this.xl == other.xl && this.yl == other.yl && this.xh == other.xh && this.yh == other.yh )
public override string ToString ()
return "[{" + xl + "," + yl + "}" + " {" + xh + "," + yh + "}]";