1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class lus {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		int count = scanner.nextInt();

		for (int i = 0; i < count; i++) {
			int offerCount = scanner.nextInt();

			Dimensions maxDim = getMinDimensions();

			List<Dimensions> exceedingDimensions = new LinkedList<lus.Dimensions>();

			for (int j = 0; j < offerCount; j++) {

				Dimensions dim = new Dimensions(scanner.nextInt(), scanner.nextInt(), scanner.nextInt(),
						scanner.nextInt());

				boolean exceed = maxDim.applyValuesIfExceed(dim);
				if (exceed) {
					exceedingDimensions.add(dim);
				}
			}

			boolean majorantFound = false;

			for (Dimensions suspect : exceedingDimensions) {
				if (suspect.isBiggerOrEqualThan(maxDim)) {
					majorantFound = true;
					break;
				}
			}

			if (majorantFound) {
				System.out.println("TAK");
			} else {
				System.out.println("NIE");
			}
		}
	}

	private static Dimensions getMinDimensions() {
		return new Dimensions(1000000000, 0, 1000000000, 0);
	}

	private static class Dimensions {

		private DimensionRange widthRange;
		private DimensionRange heightRange;

		public Dimensions(int w1, int w2, int h1, int h2) {
			this.widthRange = new DimensionRange(w1, w2);
			this.heightRange = new DimensionRange(h1, h2);
		}

		private boolean applyValuesIfExceed(Dimensions dim) {
			boolean exceed = widthRange.applyValuesIfExceed(dim.widthRange);

			return heightRange.applyValuesIfExceed(dim.heightRange) || exceed;
		}

		private boolean isBiggerOrEqualThan(Dimensions dim) {
			return widthRange.isBiggerOrEqualThan(dim.widthRange) && heightRange.isBiggerOrEqualThan(dim.heightRange);
		}
	}

	private static class DimensionRange {

		private int min;
		private int max;

		public DimensionRange(final int min, final int max) {
			this.min = min;
			this.max = max;
		}

		private boolean applyValuesIfExceed(DimensionRange dimensionRange) {
			boolean exceed = false;
			if (dimensionRange.min < this.min) {
				this.min = dimensionRange.min;
				exceed = true;
			}
			if (dimensionRange.max > this.max) {
				this.max = dimensionRange.max;
				exceed = true;
			}
			return exceed;
		}

		private boolean isBiggerOrEqualThan(DimensionRange dim) {
			return min <= dim.min && max >= dim.max;
		}
	}
}