// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x618, %rsp            # imm = 0x618
movq %rsi, %rbx
movl %edi, %r14d
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
leaq -0x340(%rbp), %rdi
callq 0x1110
cmpl $0x2, %r14d
jl 0x3815
movq 0x8(%rbx), %rsi
leaq -0x340(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x10b0
jmp 0x382b
leaq 0x860(%rip), %rsi       # 0x407c
leaq -0x340(%rbp), %rdi
pushq $0x8
popq %rdx
callq 0x10b0
leaq -0xe0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
andl $0x0, -0x48(%rbp)
movb $0x0, (%rax)
andl $0x0, -0x44(%rbp)
leaq -0x340(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x1030
leaq -0x4c0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x1140
leaq -0x4c0(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x10a0
leaq -0x4c0(%rbp), %rdi
leaq -0x44(%rbp), %rsi
callq 0x10a0
xorl %r14d, %r14d
leaq -0xf0(%rbp), %r13
leaq -0x640(%rbp), %rbx
xorl %r15d, %r15d
movl $0x0, -0x40(%rbp)
movl $0x0, -0x3c(%rbp)
xorl %r12d, %r12d
cmpl -0x48(%rbp), %r14d
jge 0x3985
leaq -0x340(%rbp), %rdi
movq %r13, %rsi
callq 0x1030
movq %rbx, %rdi
movq %r13, %rsi
pushq $0x8
popq %rdx
callq 0x1140
movq %rbx, %rdi
leaq -0x38(%rbp), %rsi
callq 0x10a0
movq %rbx, %rdi
leaq -0x30(%rbp), %rsi
callq 0x10a0
movq %rbx, %rdi
leaq -0x34(%rbp), %rsi
callq 0x10a0
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x10a0
pushq $0x14
popq %rdi
callq 0x1100
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %edx
movl -0x30(%rbp), %esi
movl -0x2c(%rbp), %edi
movl %ecx, (%rax)
movl %edx, 0x4(%rax)
movl %esi, 0x8(%rax)
movl %edi, 0xc(%rax)
movb $0x0, 0x10(%rax)
movq %rax, -0xd0(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x1ccc
movl -0x38(%rbp), %eax
movl -0x40(%rbp), %ecx
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, -0x40(%rbp)
movl -0x34(%rbp), %eax
cmpl %r15d, %eax
cmovgl %eax, %r15d
movl -0x30(%rbp), %eax
cmpl %r12d, %eax
cmovll %eax, %r12d
movl -0x2c(%rbp), %eax
movl -0x3c(%rbp), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, -0x3c(%rbp)
movq %rbx, %rdi
callq 0x1070
incl %r14d
jmp 0x38b4
xorps %xmm0, %xmm0
leaq -0xc0(%rbp), %rbx
andq $0x0, 0x10(%rbx)
leaq -0xa0(%rbp), %rax
andq $0x0, 0x10(%rax)
movaps %xmm0, (%rbx)
movaps %xmm0, (%rax)
xorl %r13d, %r13d
xorl %r14d, %r14d
movq -0x70(%rbp), %rsi
movq -0x68(%rbp), %rax
subq %rsi, %rax
sarq $0x3, %rax
cmpq %r14, %rax
jbe 0x39ee
movq (%rsi,%r13), %rax
movl 0x8(%rax), %ecx
movq %rbx, %rdi
cmpl 0xc(%rax), %ecx
je 0x39dd
movl (%rax), %ecx
leaq -0xa0(%rbp), %rdi
cmpl 0x4(%rax), %ecx
jne 0x39e5
addq %r13, %rsi
callq 0x1ccc
incq %r14
addq $0x8, %r13
jmp 0x39ac
pushq $0x8
popq %rdi
callq 0x1100
movq %rax, %rbx
andq $0x0, (%rax)
leaq -0x138(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x1c74
leaq -0x138(%rbp), %rdx
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x367a
movl %r15d, -0x4c(%rbp)
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
callq 0x1dbc
andq $0x0, -0x80(%rbp)
andq $0x0, -0x78(%rbp)
movq -0xb8(%rbp), %rax
cmpq -0xc0(%rbp), %rax
movq %rbx, -0xc8(%rbp)
je 0x3a82
leaq -0x120(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1c74
leaq -0x80(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x1528
leaq -0x120(%rbp), %rdi
callq 0x1dbc
movq -0x98(%rbp), %rax
cmpq -0xa0(%rbp), %rax
je 0x3ac1
leaq -0x108(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x1c74
leaq -0x78(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x180c
leaq -0x108(%rbp), %rdi
callq 0x1dbc
xorl %ebx, %ebx
leaq -0xf0(%rbp), %r14
leaq -0x640(%rbp), %r13
cmpl -0x44(%rbp), %ebx
jge 0x3c51
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x3afd
movq (%rax,%rdx,8), %rdi
movb $0x0, 0x10(%rdi)
incq %rdx
jmp 0x3aeb
leaq -0x340(%rbp), %rdi
movq %r14, %rsi
callq 0x1030
movq %r13, %rdi
movq %r14, %r15
movq %r14, %rsi
pushq $0x8
popq %rdx
callq 0x1140
movq %r13, %rdi
leaq -0x38(%rbp), %rsi
callq 0x10a0
movq %r13, %rdi
leaq -0x30(%rbp), %rsi
callq 0x10a0
movq %r13, %rdi
leaq -0x34(%rbp), %rsi
callq 0x10a0
movq %r13, %rdi
leaq -0x2c(%rbp), %rsi
callq 0x10a0
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x3c3f
movl -0x2c(%rbp), %eax
cmpl %eax, -0x30(%rbp)
je 0x3c3f
movq 0x346c(%rip), %rdi      # 0x6fd8
leaq 0x513(%rip), %rsi       # 0x4086
callq 0x10f0
movl -0x38(%rbp), %esi
movq %rax, %rdi
callq 0x1150
movq %rax, %rdi
leaq 0x481(%rip), %rsi       # 0x400e
callq 0x10f0
movl -0x30(%rbp), %esi
movq %rax, %rdi
callq 0x1150
movq %rax, %rdi
leaq 0x4f5(%rip), %rsi       # 0x409c
callq 0x10f0
movl -0x34(%rbp), %esi
movq %rax, %rdi
callq 0x1150
movq %rax, %rdi
leaq 0x44d(%rip), %rsi       # 0x400e
callq 0x10f0
movl -0x2c(%rbp), %esi
movq %rax, %rdi
callq 0x1150
movq %rax, %rdi
leaq 0x4c5(%rip), %rsi       # 0x40a0
callq 0x10f0
movq %rax, %rdi
callq 0x1080
movl -0x38(%rbp), %esi
movl -0x34(%rbp), %edx
movl -0x30(%rbp), %ecx
movl -0x2c(%rbp), %r8d
movq -0xc8(%rbp), %rdi
callq 0x3460
movl -0x30(%rbp), %ecx
movl -0x2c(%rbp), %r9d
leaq -0x80(%rbp), %rdi
movl -0x4c(%rbp), %esi
movl -0x40(%rbp), %edx
movl %ecx, %r8d
callq 0x1af2
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %r9d
leaq -0x78(%rbp), %rdi
movl -0x3c(%rbp), %esi
movl %r12d, %edx
movl %ecx, %r8d
callq 0x1ba0
movq 0x339e(%rip), %rdi      # 0x6fd8
callq 0x1080
movq %r13, %rdi
callq 0x1070
incl %ebx
movq %r15, %r14
jmp 0x3ad1
leaq -0xa0(%rbp), %rdi
callq 0x1dbc
leaq -0xc0(%rbp), %rdi
callq 0x1dbc
leaq -0x4c0(%rbp), %rdi
callq 0x1070
leaq -0xf0(%rbp), %rdi
callq 0x1060
leaq -0x340(%rbp), %rdi
callq 0x1040
leaq -0x70(%rbp), %rdi
callq 0x1dbc
xorl %eax, %eax
addq $0x618, %rsp            # imm = 0x618
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
jmp 0x3ccc
movq %rax, %rbx
leaq -0x120(%rbp), %rdi
jmp 0x3ccc
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
callq 0x1dbc
jmp 0x3d1a
jmp 0x3cd5
movq %rax, %rbx
jmp 0x3d3e
movq %rax, %rbx
jmp 0x3d4a
movq %rax, %rbx
jmp 0x3d56
jmp 0x3cee
jmp 0x3cee
jmp 0x3cf5
jmp 0x3cee
jmp 0x3cee
movq %rax, %rbx
jmp 0x3d1a
jmp 0x3cf5
movq %rax, %rbx
jmp 0x3d32
movq %rax, %rbx
leaq -0x640(%rbp), %rdi
callq 0x1070
jmp 0x3d32
movq %rax, %rbx
leaq -0x640(%rbp), %rdi
callq 0x1070
leaq -0xa0(%rbp), %rdi
callq 0x1dbc
leaq -0xc0(%rbp), %rdi
callq 0x1dbc
leaq -0x4c0(%rbp), %rdi
callq 0x1070
leaq -0xf0(%rbp), %rdi
callq 0x1060
leaq -0x340(%rbp), %rdi
callq 0x1040
leaq -0x70(%rbp), %rdi
callq 0x1dbc
movq %rbx, %rdi
callq 0x1160