// File: meghanamreddy[P]Range-trees-and-Interval-Trees/main.cpp
// Function: main
int main(int argc, char* argv[]) {
	// Read in line segment data //
	vector<Line*> line_segments;

	// read the input of line segments
	ifstream infile;
	if (argc > 1)
		infile.open(argv[1]);
	else
		infile.open("input.txt");

	string line;
	int num = 0, i, numquery=0, j;
	getline(infile, line);
    	istringstream iss(line);
	iss >> num; iss >> numquery;
	int x1, x2, y1, y2;
	int maxX=0, minX=0, maxY=0, minY=0;
	Line *temp;


	for (i=0; i<num; i++) {
		getline(infile, line);
    		istringstream iss(line);
		iss >> x1; iss >> y1;
		iss >> x2; iss >> y2;
		temp = new Line(x1, x2, y1, y2);
		line_segments.push_back(temp);

		if (x1 < minX) minX = x1;
		if (x2 > maxX) maxX = x2;
		if (y1 < minY) minY = y1;
		if (y2 > maxY) maxY = y2;
	}

	vector<Line*> lines_x;
	vector<Line*> lines_y;
	for (i=0; i<line_segments.size(); i++) {
		if (line_segments[i]->y1 == line_segments[i]->y2)
			lines_x.push_back(line_segments[i]);
		else if (line_segments[i]->x1 == line_segments[i]->x2)
			lines_y.push_back(line_segments[i]);
	}		

	BinaryRangeTree* rangeTreeEndPoints = new BinaryRangeTree();
	rangeTreeEndPoints = rangeTreeEndPoints->buildRangeTreeEndPoints(rangeTreeEndPoints, line_segments);
	IntervalTree treeX;
	IntervalTree treeY;
	if (lines_x.size() > 0)
		treeX.constructX(lines_x);
	if (lines_y.size() > 0)
		treeY.constructY(lines_y);


	//Read queries //

	for (i=0; i<numquery; i++) {
		// set the flags of all lines back to 0
		for (j=0; j<line_segments.size(); j++)
			line_segments[j]->reported = 0;

		getline(infile, line);
    		istringstream iss(line);
		iss >> x1; iss >> y1;
		iss >> x2; iss >> y2;
		if ((x1 != x2) & (y1 != y2)) {
		cout << " Query rectangle :: (" <<  x1 << " " << y1 << ") (" << x2 << " " << y2 << ") " << endl;
		rangeTreeEndPoints->twodquery(x1, x2, y1, y2);
		treeX.queryX(maxX,minX, y1, y1, y2);
		treeY.queryY(maxY, minY, x1, x1, x2);
		cout << endl;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x840, %rsp            # imm = 0x840
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x37e0
leaq -0x230(%rbp), %rdi
callq 0x2130
jmp 0x8940
cmpl $0x1, -0x8(%rbp)
jle 0x8991
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x230(%rbp), %rdi
movl $0x8, %edx
callq 0x20c0
jmp 0x8961
jmp 0x89ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x92f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x92e8
leaq 0x16e4(%rip), %rsi      # 0xa07c
leaq -0x230(%rbp), %rdi
movl $0x8, %edx
callq 0x20c0
jmp 0x89ab
jmp 0x89ad
leaq -0x260(%rbp), %rdi
movq %rdi, -0x7b0(%rbp)
callq 0x2140
movq -0x7b0(%rbp), %rsi
movl $0x0, -0x264(%rbp)
movl $0x0, -0x26c(%rbp)
leaq -0x230(%rbp), %rdi
callq 0x2030
jmp 0x89e9
leaq -0x3f0(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movl $0x8, %edx
callq 0x2190
jmp 0x8a03
leaq -0x3f0(%rbp), %rdi
leaq -0x264(%rbp), %rsi
callq 0x20a0
jmp 0x8a18
leaq -0x3f0(%rbp), %rdi
leaq -0x26c(%rbp), %rsi
callq 0x20a0
jmp 0x8a2d
movl $0x0, -0x404(%rbp)
movl $0x0, -0x408(%rbp)
movl $0x0, -0x40c(%rbp)
movl $0x0, -0x410(%rbp)
movl $0x0, -0x268(%rbp)
movl -0x268(%rbp), %eax
cmpl -0x264(%rbp), %eax
jge 0x8c4e
leaq -0x230(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x2030
jmp 0x8a86
leaq -0x598(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movl $0x8, %edx
callq 0x2190
jmp 0x8aa0
leaq -0x598(%rbp), %rdi
leaq -0x3f4(%rbp), %rsi
callq 0x20a0
jmp 0x8ab5
leaq -0x598(%rbp), %rdi
leaq -0x3fc(%rbp), %rsi
callq 0x20a0
jmp 0x8aca
leaq -0x598(%rbp), %rdi
leaq -0x3f8(%rbp), %rsi
callq 0x20a0
jmp 0x8adf
leaq -0x598(%rbp), %rdi
leaq -0x400(%rbp), %rsi
callq 0x20a0
jmp 0x8af4
movl $0x14, %edi
callq 0x2110
movq %rax, -0x7b8(%rbp)
jmp 0x8b07
movq -0x7b8(%rbp), %rdi
movl -0x3f4(%rbp), %esi
movl -0x3f8(%rbp), %edx
movl -0x3fc(%rbp), %ecx
movl -0x400(%rbp), %r8d
callq 0x9310
jmp 0x8b2e
movq -0x7b8(%rbp), %rax
movq %rax, -0x418(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x418(%rbp), %rsi
callq 0x3800
jmp 0x8b4e
movl -0x3f4(%rbp), %eax
cmpl -0x408(%rbp), %eax
jge 0x8bcf
movl -0x3f4(%rbp), %eax
movl %eax, -0x408(%rbp)
jmp 0x8bcf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x92dc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x92d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x8c3d
movq -0x7b8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
callq 0x20f0
jmp 0x8c3d
movl -0x3f8(%rbp), %eax
cmpl -0x404(%rbp), %eax
jle 0x8be9
movl -0x3f8(%rbp), %eax
movl %eax, -0x404(%rbp)
movl -0x3fc(%rbp), %eax
cmpl -0x410(%rbp), %eax
jge 0x8c03
movl -0x3fc(%rbp), %eax
movl %eax, -0x410(%rbp)
movl -0x400(%rbp), %eax
cmpl -0x40c(%rbp), %eax
jle 0x8c1d
movl -0x400(%rbp), %eax
movl %eax, -0x40c(%rbp)
leaq -0x598(%rbp), %rdi
callq 0x2080
movl -0x268(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x268(%rbp)
jmp 0x8a5f
leaq -0x598(%rbp), %rdi
callq 0x2080
jmp 0x92d0
leaq -0x5b0(%rbp), %rdi
callq 0x37e0
leaq -0x5c8(%rbp), %rdi
callq 0x37e0
movl $0x0, -0x268(%rbp)
movslq -0x268(%rbp), %rax
movq %rax, -0x7c0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x3440
movq %rax, %rcx
movq -0x7c0(%rbp), %rax
cmpq %rcx, %rax
jae 0x8d88
movslq -0x268(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x34d0
movq (%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x7c4(%rbp)
movslq -0x268(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x34d0
movq %rax, %rcx
movl -0x7c4(%rbp), %eax
movq (%rcx), %rcx
cmpl 0xc(%rcx), %eax
jne 0x8d11
movslq -0x268(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x34d0
movq %rax, %rsi
leaq -0x5b0(%rbp), %rdi
callq 0x3800
jmp 0x8cf8
jmp 0x8d72
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x92b8
movslq -0x268(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x34d0
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x7c8(%rbp)
movslq -0x268(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x34d0
movq %rax, %rcx
movl -0x7c8(%rbp), %eax
movq (%rcx), %rcx
cmpl 0x4(%rcx), %eax
jne 0x8d70
movslq -0x268(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x34d0
movq %rax, %rsi
leaq -0x5c8(%rbp), %rdi
callq 0x3800
jmp 0x8d6e
jmp 0x8d70
jmp 0x8d72
jmp 0x8d74
movl -0x268(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x268(%rbp)
jmp 0x8c70
movl $0x8, %edi
callq 0x2110
movq %rax, -0x7d0(%rbp)
jmp 0x8d9b
movq -0x7d0(%rbp), %rdi
callq 0x3ac0
jmp 0x8da9
movq -0x7d0(%rbp), %rax
movq %rax, -0x5d0(%rbp)
movq -0x5d0(%rbp), %rax
movq %rax, -0x7e0(%rbp)
movq %rax, -0x7d8(%rbp)
leaq -0x5e8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x3600
jmp 0x8dde
movq -0x7e0(%rbp), %rsi
movq -0x7d8(%rbp), %rdi
leaq -0x5e8(%rbp), %rdx
callq 0x85e0
movq %rax, -0x7e8(%rbp)
jmp 0x8e01
movq -0x7e8(%rbp), %rax
movq %rax, -0x5d0(%rbp)
leaq -0x5e8(%rbp), %rdi
callq 0x36f0
leaq -0x5f0(%rbp), %rdi
callq 0x9350
jmp 0x8e29
leaq -0x5f8(%rbp), %rdi
callq 0x9350
jmp 0x8e37
leaq -0x5b0(%rbp), %rdi
callq 0x3440
cmpq $0x0, %rax
jbe 0x8eee
leaq -0x610(%rbp), %rdi
leaq -0x5b0(%rbp), %rsi
callq 0x3600
jmp 0x8e62
leaq -0x5f0(%rbp), %rdi
leaq -0x610(%rbp), %rsi
callq 0x2720
jmp 0x8e77
leaq -0x610(%rbp), %rdi
callq 0x36f0
jmp 0x8eee
movq -0x7d0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
callq 0x20f0
jmp 0x92b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x5e8(%rbp), %rdi
callq 0x36f0
jmp 0x92b8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x610(%rbp), %rdi
callq 0x36f0
jmp 0x92b8
leaq -0x5c8(%rbp), %rdi
callq 0x3440
cmpq $0x0, %rax
jbe 0x8f5b
leaq -0x628(%rbp), %rdi
leaq -0x5c8(%rbp), %rsi
callq 0x3600
jmp 0x8f15
leaq -0x5f8(%rbp), %rdi
leaq -0x628(%rbp), %rsi
callq 0x2c40
jmp 0x8f2a
leaq -0x628(%rbp), %rdi
callq 0x36f0
jmp 0x8f5b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x628(%rbp), %rdi
callq 0x36f0
jmp 0x92b8
movl $0x0, -0x268(%rbp)
movl -0x268(%rbp), %eax
cmpl -0x26c(%rbp), %eax
jge 0x9267
movl $0x0, -0x270(%rbp)
movslq -0x270(%rbp), %rax
movq %rax, -0x7f0(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x3440
movq %rax, %rcx
movq -0x7f0(%rbp), %rax
cmpq %rcx, %rax
jae 0x8fcf
movslq -0x270(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0x34d0
movq (%rax), %rax
movb $0x0, 0x10(%rax)
movl -0x270(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x270(%rbp)
jmp 0x8f81
leaq -0x230(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x2030
jmp 0x8fe4
leaq -0x7a8(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movl $0x8, %edx
callq 0x2190
jmp 0x8ffe
leaq -0x7a8(%rbp), %rdi
leaq -0x3f4(%rbp), %rsi
callq 0x20a0
jmp 0x9013
leaq -0x7a8(%rbp), %rdi
leaq -0x3fc(%rbp), %rsi
callq 0x20a0
jmp 0x9028
leaq -0x7a8(%rbp), %rdi
leaq -0x3f8(%rbp), %rsi
callq 0x20a0
jmp 0x903d
leaq -0x7a8(%rbp), %rdi
leaq -0x400(%rbp), %rsi
callq 0x20a0
jmp 0x9052
movl -0x3f4(%rbp), %eax
cmpl -0x3f8(%rbp), %eax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl -0x3fc(%rbp), %ecx
cmpl -0x400(%rbp), %ecx
setne %cl
andb $0x1, %cl
movzbl %cl, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x9247
movq 0x4f4b(%rip), %rdi      # 0xdfd8
leaq 0xff2(%rip), %rsi       # 0xa086
callq 0x2100
movq %rax, -0x7f8(%rbp)
jmp 0x90a2
movq -0x7f8(%rbp), %rdi
movl -0x3f4(%rbp), %esi
callq 0x21a0
movq %rax, -0x800(%rbp)
jmp 0x90bd
movq -0x800(%rbp), %rdi
leaq 0xf43(%rip), %rsi       # 0xa00e
callq 0x2100
movq %rax, -0x808(%rbp)
jmp 0x90d9
movq -0x808(%rbp), %rdi
movl -0x3fc(%rbp), %esi
callq 0x21a0
movq %rax, -0x810(%rbp)
jmp 0x90f4
movq -0x810(%rbp), %rdi
leaq 0xf9a(%rip), %rsi       # 0xa09c
callq 0x2100
movq %rax, -0x818(%rbp)
jmp 0x9110
movq -0x818(%rbp), %rdi
movl -0x3f8(%rbp), %esi
callq 0x21a0
movq %rax, -0x820(%rbp)
jmp 0x912b
movq -0x820(%rbp), %rdi
leaq 0xed5(%rip), %rsi       # 0xa00e
callq 0x2100
movq %rax, -0x828(%rbp)
jmp 0x9147
movq -0x828(%rbp), %rdi
movl -0x400(%rbp), %esi
callq 0x21a0
movq %rax, -0x830(%rbp)
jmp 0x9162
movq -0x830(%rbp), %rdi
leaq 0xf30(%rip), %rsi       # 0xa0a0
callq 0x2100
movq %rax, -0x838(%rbp)
jmp 0x917e
movq -0x838(%rbp), %rdi
movq 0x4e3c(%rip), %rsi      # 0xdfc8
callq 0x2120
jmp 0x9193
movq -0x5d0(%rbp), %rdi
movl -0x3f4(%rbp), %esi
movl -0x3f8(%rbp), %edx
movl -0x3fc(%rbp), %ecx
movl -0x400(%rbp), %r8d
callq 0x82d0
jmp 0x91ba
movl -0x404(%rbp), %esi
movl -0x408(%rbp), %edx
movl -0x3fc(%rbp), %r8d
movl -0x400(%rbp), %r9d
leaq -0x5f0(%rbp), %rdi
movl %r8d, %ecx
callq 0x3160
jmp 0x91e5
movl -0x40c(%rbp), %esi
movl -0x410(%rbp), %edx
movl -0x3f4(%rbp), %r8d
movl -0x3f8(%rbp), %r9d
leaq -0x5f8(%rbp), %rdi
movl %r8d, %ecx
callq 0x32c0
jmp 0x9210
movq 0x4dc1(%rip), %rdi      # 0xdfd8
movq 0x4daa(%rip), %rsi      # 0xdfc8
callq 0x2120
jmp 0x9225
jmp 0x9247
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x7a8(%rbp), %rdi
callq 0x2080
jmp 0x92b8
leaq -0x7a8(%rbp), %rdi
callq 0x2080
movl -0x268(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x268(%rbp)
jmp 0x8f65
leaq -0x5c8(%rbp), %rdi
callq 0x36f0
leaq -0x5b0(%rbp), %rdi
callq 0x36f0
leaq -0x3f0(%rbp), %rdi
callq 0x2080
leaq -0x260(%rbp), %rdi
callq 0x20b0
leaq -0x230(%rbp), %rdi
callq 0x2040
leaq -0x28(%rbp), %rdi
callq 0x36f0
movl -0x4(%rbp), %eax
addq $0x840, %rsp            # imm = 0x840
popq %rbp
retq
leaq -0x5c8(%rbp), %rdi
callq 0x36f0
leaq -0x5b0(%rbp), %rdi
callq 0x36f0
leaq -0x3f0(%rbp), %rdi
callq 0x2080
leaq -0x260(%rbp), %rdi
callq 0x20b0
leaq -0x230(%rbp), %rdi
callq 0x2040
leaq -0x28(%rbp), %rdi
callq 0x36f0
movq -0x238(%rbp), %rdi
callq 0x21b0
nopl (%rax)