// File: /workspace/github_cpp_makefile_star2_O2/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, -0x60(%rbp)
movq %rdx, -0x38(%rbp)
movq %rsi, %r12
movq %rdi, -0x40(%rbp)
movq 0x8(%rsi), %r15
leaq 0xcfa(%rip), %rsi       # 0x2004
leaq -0xa0(%rbp), %rdi
leaq -0xd0(%rbp), %r13
movq %r13, %rdx
callq 0x168a
leaq -0xc8(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorl %ebx, %ebx
movq -0x98(%rbp), %r14
cmpq %rbx, %r14
jbe 0x1363
movq -0xa0(%rbp), %rsi
addq %rbx, %rsi
movq %r13, %rdi
callq 0x16c6
movl %ebx, (%rax)
incq %rbx
jmp 0x133e
movq %r15, %rbx
movl %ebx, %eax
incl %eax
movslq %eax, %rsi
leaq -0x64(%rbp), %rdx
andl $0x0, (%rdx)
leaq -0x80(%rbp), %rdi
leaq -0x2a(%rbp), %rcx
callq 0x1718
decq %r14
leaq -0x58(%rbp), %rdi
leaq -0x80(%rbp), %rdx
leaq -0x29(%rbp), %rcx
movq %r14, %rsi
callq 0x1778
leaq -0x80(%rbp), %rdi
callq 0x1c44
xorl %r15d, %r15d
testl %ebx, %ebx
cmovgl %ebx, %r15d
incl %r15d
pushq $0x1
popq %rbx
pushq $0x18
popq %r14
leaq -0xd0(%rbp), %r13
cmpq %r15, %rbx
je 0x1421
movq -0x58(%rbp), %rcx
movq -0x50(%rbp), %rax
subq %rcx, %rax
cqto
idivq %r14
subq $0x1, %rax
jb 0x13e6
movq (%rcx), %rdx
movl -0x4(%rdx,%rbx,4), %esi
movl %esi, (%rdx,%rbx,4)
addq $0x18, %rcx
jmp 0x13d0
movq (%r12), %rax
leaq (%rax,%rbx), %rsi
decq %rsi
movq %r13, %rdi
callq 0x16c6
movslq (%rax), %rcx
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rax
subq %rsi, %rax
cqto
idivq %r14
cmpq %rcx, %rax
jbe 0x141c
imulq $0x18, %rcx, %rax
movq (%rsi,%rax), %rax
incl (%rax,%rbx,4)
incq %rbx
jmp 0x13bb
movq -0x38(%rbp), %rbx
movq 0x8(%rbx), %r12
subq (%rbx), %r12
shrq $0x2, %r12
movslq %r12d, %rsi
leaq -0x80(%rbp), %rdx
movq -0x40(%rbp), %r14
movq %r14, %rdi
callq 0x17d8
movq (%rbx), %rcx
movq -0x60(%rbp), %rax
movq (%rax), %rsi
movq -0x98(%rbp), %rdi
movq (%r14), %rax
movq %rax, -0x38(%rbp)
xorl %r9d, %r9d
testl %r12d, %r12d
cmovlel %r9d, %r12d
cmpq %r12, %r9
je 0x14d7
movslq (%rcx,%r9,4), %r11
movslq (%rsi,%r9,4), %rbx
movq -0x58(%rbp), %r15
movq -0x50(%rbp), %rax
subq %r15, %rax
cqto
pushq $0x18
popq %r8
idivq %r8
movq (%r14), %rdx
leaq 0xbe0(%rip), %r13       # 0x2070
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x14ce
cmpq %r8, %rax
jbe 0x14ad
movq (%r15), %r10
movl 0x4(%r10,%rbx,4), %r14d
cmpl (%r10,%r11,4), %r14d
jle 0x14b5
jmp 0x14c2
movl (%r13), %r10d
movl %r10d, (%rdx,%r9,4)
incq %r8
addq $0x4, %r13
addq $0x18, %r15
jmp 0x1493
movl (%r13), %eax
movq -0x38(%rbp), %rdx
movl %eax, (%rdx,%r9,4)
incq %r9
movq -0x40(%rbp), %r14
jmp 0x1465
leaq -0x58(%rbp), %rdi
callq 0x182a
leaq -0xd0(%rbp), %rdi
callq 0x1864
leaq -0xa0(%rbp), %rdi
callq 0x1080
movq %r14, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x151f
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x1c44
jmp 0x1530
jmp 0x152d
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x182a
jmp 0x1530
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x1864
leaq -0xa0(%rbp), %rdi
callq 0x1080
movq %rbx, %rdi
callq 0x1180