// File: /workspace/github_cpp_makefile_star2_O1/zengfenfei[P]codility/src/GenomicRangeQuery.cpp
// Function: solution(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&)
vector<int> solution(string &S, vector<int> &P, vector<int> &Q) {
	// Calculate prefix counts
	int n = S.length();
	string types = "ACGT";
	int factors[4] = { 1, 2, 3, 4 }; // Ascending order and corresponds to order of types
	map<char, int> type2factorIdx;
	for (int i = 0; i < types.length(); i++) {
		type2factorIdx[types[i]] = i;
	}
	vector<vector<int> > prefixCounts(types.length() - 1,
			vector<int>(n + 1, 0));
	for (int i = 1; i < n + 1; i++) {
		for (int j = 0; j < prefixCounts.size(); j++) {
			prefixCounts[j][i] = prefixCounts[j][i - 1];
		}
		int facI = type2factorIdx[S[i - 1]];
		if (facI < prefixCounts.size()) {
			prefixCounts[facI][i]++;
		}
	}

	int m = P.size();
	vector<int> result(m);
	for (int i = 0; i < m; i++) {
		int f = P[i], t = Q[i] + 1;
		for (int j = 0; j < types.length(); j++) {
			if (j < prefixCounts.size()) {
				int count = prefixCounts[j][t] - prefixCounts[j][f];
				if (count > 0) {
					result[i] = factors[j];
					break;
				}
			} else {
				result[i] = factors[j];
			}
		}
	}
	return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rsi, %r12
movq %rdi, -0x38(%rbp)
movq 0x8(%rsi), %r15
leaq -0x70(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0xcfe(%rip), %rsi       # 0x2030
leaq 0xcfb(%rip), %rdx       # 0x2034
leaq -0x80(%rbp), %r14
movq %r14, %rdi
callq 0x1aec
leaq -0xc8(%rbp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq 0x8(%r14), %r13
testq %r13, %r13
je 0x1393
xorl %ebx, %ebx
leaq -0xd0(%rbp), %r14
movq -0x80(%rbp), %rsi
addq %rbx, %rsi
movq %r14, %rdi
callq 0x17fe
movl %ebx, (%rax)
incq %rbx
movq -0x78(%rbp), %r13
cmpq %rbx, %r13
ja 0x1376
leal 0x1(%r15), %eax
movslq %eax, %rsi
leaq -0x9c(%rbp), %rdx
movl $0x0, (%rdx)
leaq -0x98(%rbp), %rdi
leaq -0x2a(%rbp), %rcx
callq 0x1866
decq %r13
leaq -0x60(%rbp), %rdi
leaq -0x98(%rbp), %rdx
leaq -0x29(%rbp), %rcx
movq %r13, %rsi
callq 0x18ee
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x13ec
movq -0x88(%rbp), %rsi
subq %rdi, %rsi
callq 0x10f0
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
testl %r15d, %r15d
jle 0x147f
incl %r15d
movl $0x1, %ebx
leaq -0xd0(%rbp), %r13
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
subq %rax, %rcx
je 0x143e
sarq $0x3, %rcx
imulq %r14, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
movq (%rax), %rdx
movl -0x4(%rdx,%rbx,4), %esi
movl %esi, (%rdx,%rbx,4)
addq $0x18, %rax
decq %rcx
jne 0x142b
movq (%r12), %rax
leaq (%rax,%rbx), %rsi
decq %rsi
movq %r13, %rdi
callq 0x17fe
movslq (%rax), %rax
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
imulq %r14, %rdx
cmpq %rax, %rdx
jbe 0x1477
leaq (%rax,%rax,2), %rax
movq (%rcx,%rax,8), %rax
incl (%rax,%rbx,4)
incq %rbx
cmpq %r15, %rbx
jne 0x140e
movq -0x48(%rbp), %rbx
movq 0x8(%rbx), %r15
subq (%rbx), %r15
shrq $0x2, %r15
movslq %r15d, %rsi
leaq -0x98(%rbp), %rdx
movq -0x38(%rbp), %rdi
callq 0x1990
testl %r15d, %r15d
jle 0x154d
movq (%rbx), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq -0x78(%rbp), %rdx
movq -0x60(%rbp), %rsi
movq -0x58(%rbp), %rdi
subq %rsi, %rdi
sarq $0x3, %rdi
imulq %r14, %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x40(%rbp)
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
xorl %r9d, %r9d
testq %rdx, %rdx
je 0x1545
movslq (%rcx,%r9,4), %r11
movq -0x48(%rbp), %rax
movslq (%rax,%r9,4), %rbx
movq -0x38(%rbp), %rax
movq (%rax), %r14
movq %rsi, %r13
leaq 0xb1a(%rip), %r12       # 0x2020
xorl %r8d, %r8d
cmpq %r8, %rdi
jbe 0x151f
movq (%r13), %r10
movl 0x4(%r10,%r11,4), %eax
cmpl (%r10,%rbx,4), %eax
jle 0x1527
jmp 0x1539
movl (%r12), %eax
movl %eax, (%r14,%r9,4)
incq %r8
addq $0x4, %r12
addq $0x18, %r13
cmpq %r8, %rdx
jne 0x1509
jmp 0x1545
movl (%r12), %eax
movq -0x40(%rbp), %r8
movl %eax, (%r8,%r9,4)
incq %r9
cmpq %r15, %r9
jne 0x14e4
leaq -0x60(%rbp), %rdi
callq 0x1a24
leaq -0xd0(%rbp), %rdi
callq 0x1a8e
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x157b
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x10f0
movq -0x38(%rbp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15b5
movq %rax, %rbx
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x15c6
movq -0x88(%rbp), %rsi
subq %rdi, %rsi
callq 0x10f0
jmp 0x15c6
jmp 0x15c3
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x1a24
jmp 0x15c6
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x1a8e
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x15eb
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x10f0
movq %rbx, %rdi
callq 0x1170