// File: /workspace/github_cpp_makefile_star2_O3/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 0xcd2(%rip), %rsi       # 0x2004
leaq 0xccf(%rip), %rdx       # 0x2008
leaq -0x80(%rbp), %r14
movq %r14, %rdi
callq 0x1ac8
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)
cmpq %rcx, 0x8(%r14)
je 0x1395
xorl %ebx, %ebx
leaq -0xd0(%rbp), %r14
movq -0x80(%rbp), %rsi
addq %rbx, %rsi
movq %r14, %rdi
callq 0x17d2
movl %ebx, (%rax)
incq %rbx
movq -0x78(%rbp), %r13
cmpq %rbx, %r13
ja 0x1373
decq %r13
jmp 0x139c
movq $-0x1, %r13
leal 0x1(%r15), %eax
movslq %eax, %rsi
leaq -0x9c(%rbp), %rdx
movl $0x0, (%rdx)
leaq -0x98(%rbp), %rdi
leaq -0x2a(%rbp), %rcx
callq 0x183a
leaq -0x60(%rbp), %rdi
leaq -0x98(%rbp), %rdx
leaq -0x29(%rbp), %rcx
movq %r13, %rsi
callq 0x18c2
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x13f2
movq -0x88(%rbp), %rsi
subq %rdi, %rsi
callq 0x10f0
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
testl %r15d, %r15d
jle 0x1488
incl %r15d
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movl $0x1, %ebx
leaq -0xd0(%rbp), %r13
subq %rax, %rcx
je 0x1444
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 0x1431
movq (%r12), %rax
leaq (%rax,%rbx), %rsi
decq %rsi
movq %r13, %rdi
callq 0x17d2
movslq (%rax), %rdx
movq -0x60(%rbp), %rax
movq -0x58(%rbp), %rcx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x3, %rsi
imulq %r14, %rsi
cmpq %rdx, %rsi
jbe 0x1480
leaq (%rdx,%rdx,2), %rdx
movq (%rax,%rdx,8), %rdx
incl (%rdx,%rbx,4)
incq %rbx
cmpq %r15, %rbx
jne 0x141c
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 0x1964
testl %r15d, %r15d
jle 0x1556
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 0x154e
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 0xb61(%rip), %r12       # 0x2070
xorl %r8d, %r8d
cmpq %r8, %rdi
jbe 0x1528
movq (%r13), %r10
movl 0x4(%r10,%r11,4), %eax
cmpl (%r10,%rbx,4), %eax
jle 0x1530
jmp 0x1542
movl (%r12), %eax
movl %eax, (%r14,%r9,4)
incq %r8
addq $0x4, %r12
addq $0x18, %r13
cmpq %r8, %rdx
jne 0x1512
jmp 0x154e
movl (%r12), %eax
movq -0x40(%rbp), %r8
movl %eax, (%r8,%r9,4)
incq %r9
cmpq %r15, %r9
jne 0x14ed
leaq -0x60(%rbp), %rdi
callq 0x19fa
leaq -0xd0(%rbp), %rdi
callq 0x1a6a
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x1584
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 0x15be
movq %rax, %rbx
movq -0x98(%rbp), %rdi
testq %rdi, %rdi
je 0x15cf
movq -0x88(%rbp), %rsi
subq %rdi, %rsi
callq 0x10f0
jmp 0x15cf
jmp 0x15cc
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x19fa
jmp 0x15cf
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x1a6a
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x15f4
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x10f0
movq %rbx, %rdi
callq 0x1170