// File: /workspace/github_cpp_makefile_star2_O3/stoianmihail[P]XY-sorting/algorithm.cpp
// Function: preprocessing_cdf(std::unordered_map<unsigned long, std::pair<unsigned long, bool>, std::hash<unsigned long>, std::equal_to<unsigned long>, std::allocator<std::pair<unsigned long const, std::pair<unsigned long, bool>>>>&, std::vector<unsigned long, std::allocator<unsigned long>>&, std::vector<unsigned long, std::allocator<unsigned long>>&)
std::vector<cdf_coord> preprocessing_cdf(std::unordered_map<uint64_t, hash_coord>& count_, std::vector<uint64_t>& xs, std::vector<uint64_t>& ys)
// Solve with the cdf approximation
{
    // Construct an artificial spline
    assert(xs.size() == ys.size());
    std::vector<cdf_coord> spline(xs.size());
    for (unsigned index = 0; index < xs.size(); ++index)
        spline[index].first = xs[index] + ys[index];
    
    auto forward_pointer = [&spline](uint32_t& knotPtr, uint64_t sum) {
        // Iterate through the spline knots and find the segment of sum
        while ((knotPtr < spline.size()) && (spline[knotPtr].first <= sum))
            ++knotPtr;
        
        // Special case when reaching the last element
        knotPtr -= (knotPtr == spline.size());
    };
    
    // Hash with the frequencies of each sum
    unsigned sums_counter = count_.size();
    for (unsigned index = 0; index < xs.size(); ++index) {
        // Reset the pointer to the splien knot at every new line
        unsigned knotPtr = 0;
        for (unsigned ptr = 0; ptr < ys.size(); ++ptr) {
            // TODO: assuming that the value is still a 32-bit unsigned value
            // This serves only to the theoretical part of the algorithm
            uint64_t sum = xs[index] + ys[ptr];
            
            // Forward the pointer in the spline knots
            forward_pointer(knotPtr, sum);
            
            // First occurence of sum? Then update the "Mars Smen"
            if (count_[sum].second == false) {
                ++spline[knotPtr].second;
                count_[sum].second = true;
            }
        }
    }
    
    // Update the "Mars Smen"
    for (unsigned index = 1; index < spline.size(); ++index)
        spline[index].second += spline[index - 1].second;
    
#if 0
    std::cerr << "Final Spline : " << std::endl;
    for (auto elem: spline)
        std::cerr << elem.first << " " << elem.second << std::endl;
#endif
    
    auto interpolate = [&spline](uint32_t knotPtr, uint64_t sum) {
        // Compute the slope
        double dx = spline[knotPtr].first - spline[knotPtr - 1].first;
        double dy = spline[knotPtr].second - spline[knotPtr - 1].second;

        // Compute offset for the linear function
        double ofs = sum - spline[knotPtr - 1].first;
        return spline[knotPtr - 1].second + ofs * (dy / dx);
    };
    
#if 0
    std::cerr << sums_counter << " out of " << xs.size() * ys.size() << std::endl;
#endif
    
    // Compute the CDF approximation
    // Create the hash table
    uint32_t sums_size = sums_counter + 1;
    adj.resize(sums_size);
    list.resize(sums_size + 1);
    
    sums_counter = 0;
    for (unsigned index = 0; index < xs.size(); ++index) {
        unsigned knotPtr = 0;
        for (unsigned ptr = 0; ptr < ys.size(); ++ptr) {
            // TODO: assuming that the value is stil a 32-bit unsigned value
            // This serves only to the theoretical part of the algorithm
            uint64_t sum = xs[index] + ys[ptr];
            
            // Forward the pointer in the spline knots
            forward_pointer(knotPtr, sum);
            
            // First occurence of sum? (we already used it before, so it changes the meaning)
            if (count_[sum].second == true) {
                double estimation = interpolate(knotPtr, sum);
                uint32_t estimatedIndex = static_cast<uint32_t>(round(estimation));
                
                addToChain(++sums_counter, sum, estimatedIndex);
                
                // Mark the sum as seen
                count_[sum].second = false;
            }
        }
    }
    
    std::cerr << "Chain done" << std::endl;
    
    // Functions to add the values into the final order
    unsigned currBuffer = 0;
    std::vector<cdf_coord> final_(sums_counter);
    
    auto addKeyToFinal = [&final_, &currBuffer](uint64_t key) {
        final_[currBuffer++] = std::make_pair(key, 0);
    };
    
    auto addChain = [addKeyToFinal](std::vector<uint64_t>& values) {
        for (auto elem: values)
            addKeyToFinal(elem);
    };
    
    // Go through the chains from the hash-table
    std::vector<uint64_t> collector_;
    for (unsigned index = 0; index < sums_size; ++index) {
        // TODO: try also with sorting the chain
        collector_ = collectChain(index);
        // And insert each element of the chain
        addChain(collector_);
    }
    std::cerr << "Sums = " << sums_size << std::endl;
#if 0
    std::cerr << currBuffer << " vs " << sums_size << std::endl;
    assert(currBuffer == sums_size);
#endif
    
    std::cerr << "Final done" << std::endl;
    
#if 0
    util::printComputedCdf("final", final_);
#endif
    return final_;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rsi, (%rsp)
movq %rdi, 0x8(%rsp)
movq 0x8(%rdx), %rsi
subq (%rdx), %rsi
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cmpq %rax, %rsi
jne 0x3e7d
movq %rcx, %r14
movq %rdx, %r12
sarq $0x3, %rsi
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x4d46
movq (%r12), %rax
movq 0x8(%r12), %rcx
movq %rcx, %rdx
subq %rax, %rdx
movq %r12, 0x58(%rsp)
je 0x3a25
sarq $0x3, %rdx
movq (%r14), %rsi
movl $0x1, %edi
movq 0x38(%rsp), %r8
xorl %r9d, %r9d
movq (%rsi,%r9), %r10
addq (%rax,%r9), %r10
movq %r10, (%r8,%r9,2)
movl %edi, %r10d
addq $0x8, %r9
incl %edi
cmpq %r10, %rdx
ja 0x390c
movq (%rsp), %rdx
movq 0x18(%rdx), %rbx
cmpq %rax, %rcx
je 0x3a2d
movq %rbx, 0x30(%rsp)
movq (%r14), %rdx
movq 0x8(%r14), %rsi
xorl %r15d, %r15d
cmpq %rdx, %rsi
je 0x3a05
xorl %ebp, %ebp
xorl %ebx, %ebx
movq (%r12), %rax
movq (%rdx,%rbp,8), %rcx
addq (%rax,%r15,8), %rcx
movq %rcx, 0x10(%rsp)
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rax
subq %rsi, %rax
sarq $0x4, %rax
movl %ebx, %edx
cmpq %rdx, %rax
jbe 0x3993
movq %rdx, %rdi
shlq $0x4, %rdi
cmpq %rcx, (%rsi,%rdi)
ja 0x3993
incl %ebx
movq %rbx, %rdx
cmpq %rbx, %rax
ja 0x397c
xorl %r12d, %r12d
cmpq %rdx, %rax
sete %r13b
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x56d2
movb %r13b, %r12b
subl %r12d, %ebx
cmpb $0x0, 0x8(%rax)
jne 0x39d9
movl %ebx, %eax
movq 0x38(%rsp), %rcx
shlq $0x4, %rax
incq 0x8(%rcx,%rax)
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x56d2
movb $0x1, 0x8(%rax)
incl %ebp
movq (%r14), %rdx
movq 0x8(%r14), %rsi
movq %rsi, %rax
subq %rdx, %rax
sarq $0x3, %rax
cmpq %rbp, %rax
movq 0x58(%rsp), %r12
ja 0x3953
movq (%r12), %rax
movq 0x8(%r12), %rcx
jmp 0x3a08
movq %rdx, %rsi
incl %r15d
movq %rcx, %rdi
subq %rax, %rdi
sarq $0x3, %rdi
cmpq %r15, %rdi
ja 0x3946
movq 0x30(%rsp), %rbx
jmp 0x3a2d
movq (%rsp), %rax
movq 0x18(%rax), %rbx
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq $0x2, %rcx
jb 0x3a6b
leaq 0x8(%rax), %rdx
movl $0x1, %edi
movl $0x2, %esi
movq (%rdx), %r8
shlq $0x4, %rdi
addq %r8, 0x8(%rax,%rdi)
movl %esi, %edi
incl %esi
addq $0x10, %rdx
cmpq %rdi, %rcx
ja 0x3a52
leal 0x1(%rbx), %esi
leaq 0x6743(%rip), %rdi      # 0xa1b8
movq %rsi, 0x50(%rsp)
callq 0x4dec
addl $0x2, %ebx
leaq 0x6747(%rip), %rdi      # 0xa1d0
movq %rbx, %rsi
callq 0x4e1e
movq (%r12), %rdx
movq 0x8(%r12), %rsi
xorl %ebp, %ebp
cmpq %rdx, %rsi
je 0x3c88
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq $0x0, 0x30(%rsp)
xorl %r8d, %r8d
cmpq %rax, %rcx
je 0x3c68
movq %r8, 0x60(%rsp)
xorl %ebx, %ebx
xorl %r15d, %r15d
movq (%r12), %rdx
movq (%rax,%rbx,8), %rcx
movq 0x30(%rsp), %rax
addq (%rdx,%rax,8), %rcx
movq %rcx, 0x10(%rsp)
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rax
subq %rsi, %rax
sarq $0x4, %rax
movl %r15d, %edx
cmpq %rdx, %rax
jbe 0x3b12
movq %rdx, %rdi
shlq $0x4, %rdi
cmpq %rcx, (%rsi,%rdi)
ja 0x3b12
incl %r15d
movq %r15, %rdx
cmpq %r15, %rax
ja 0x3afa
xorl %r12d, %r12d
cmpq %rdx, %rax
sete %r13b
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x56d2
movb %r13b, %r12b
subl %r12d, %r15d
cmpb $0x1, 0x8(%rax)
jne 0x3c37
movq 0x10(%rsp), %r12
movq 0x38(%rsp), %rax
movl %r15d, %ecx
shlq $0x4, %rcx
leal -0x1(%r15), %edx
shlq $0x4, %rdx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rcx
movq (%rax,%rdx), %rdi
movq 0x8(%rax,%rdx), %rax
subq %rdi, %rsi
movq %rsi, %xmm0
movq 0x349b(%rip), %xmm3     # 0x7010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x349f(%rip), %xmm4     # 0x7020
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
subq %rax, %rcx
movq %rcx, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
divsd %xmm1, %xmm2
movq %r12, %rcx
subq %rdi, %rcx
movq %rcx, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movq %rax, %xmm2
punpckldq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
subpd %xmm4, %xmm2
movapd %xmm2, %xmm0
unpckhpd %xmm2, %xmm0            # xmm0 = xmm0[1],xmm2[1]
addsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x3090
cvttsd2si %xmm0, %rax
incl %ebp
movl %eax, %eax
movq 0x65ca(%rip), %rcx      # 0xa1d0
movq %rbp, %rdx
shlq $0x4, %rdx
movq %r12, (%rcx,%rdx)
movq 0x65a0(%rip), %rsi      # 0xa1b8
movq (%rsi,%rax,8), %rdi
movq %rdi, 0x8(%rcx,%rdx)
movq %rbp, (%rsi,%rax,8)
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x56d2
movb $0x0, 0x8(%rax)
incl %ebx
movq (%r14), %rax
movq 0x8(%r14), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rbx, %rdx
movq 0x58(%rsp), %r12
ja 0x3acb
movq (%r12), %rdx
movq 0x8(%r12), %rsi
movq 0x60(%rsp), %r8
jmp 0x3c6b
movq %rax, %rcx
incl %r8d
movq %rsi, %rdi
subq %rdx, %rdi
sarq $0x3, %rdi
movq %r8, 0x30(%rsp)
cmpq %r8, %rdi
ja 0x3ab8
movl %ebp, %ebp
movq 0x6359(%rip), %rbx      # 0x9fe8
leaq 0x354e(%rip), %rsi      # 0x71e4
movl $0xa, %edx
movq %rbx, %rdi
callq 0x31e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x31b0
movsbl %al, %esi
movq 0x6327(%rip), %rdi      # 0x9fe8
callq 0x3030
movq %rax, %rdi
callq 0x3110
leaq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x4d46
xorl %r15d, %r15d
cmpq $0x0, 0x50(%rsp)
je 0x3da0
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x3806
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rax
movq %rax, (%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
testq %rbx, %rbx
je 0x3d53
subq %rbx, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x31d0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3d53
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x31d0
cmpq %r14, %r13
je 0x3d86
movq %r13, %rcx
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq (%rcx), %rdx
movl %r15d, %esi
incl %r15d
shlq $0x4, %rsi
movq %rdx, (%rax,%rsi)
movq $0x0, 0x8(%rax,%rsi)
addq $0x8, %rcx
cmpq %r14, %rcx
jne 0x3d63
incq %r12
movq (%rsp), %r14
movq %r14, %rbp
movq %r13, %rbx
cmpq 0x50(%rsp), %r12
jne 0x3cf6
jmp 0x3da6
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x623b(%rip), %rdi      # 0x9fe8
leaq 0x343b(%rip), %rsi      # 0x71ef
movl $0x7, %edx
callq 0x31e0
movq 0x6223(%rip), %rdi      # 0x9fe8
movq 0x50(%rsp), %rsi
callq 0x3100
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x31b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3110
movq 0x61e8(%rip), %rbx      # 0x9fe8
leaq 0x33f0(%rip), %rsi      # 0x71f7
movl $0xa, %edx
movq %rbx, %rdi
callq 0x31e0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x31b0
movsbl %al, %esi
movq 0x61b6(%rip), %rdi      # 0x9fe8
callq 0x3030
movq %rax, %rdi
callq 0x3110
testq %r13, %r13
je 0x3e52
subq %r13, %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x31d0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x3e69
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x31d0
movq 0x8(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3224(%rip), %rdi      # 0x70a8
leaq 0x3234(%rip), %rsi      # 0x70bf
leaq 0x32cb(%rip), %rcx      # 0x715d
movl $0x80, %edx
callq 0x30e0
jmp 0x3ee2
jmp 0x3ee2
movq %r13, %rbx
movq %r14, %rbp
jmp 0x3ea8
movq %rax, %r15
testq %rbx, %rbx
je 0x3ebe
subq %rbx, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x31d0
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3ee5
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x31d0
jmp 0x3ee5
jmp 0x3ee2
jmp 0x3ee2
movq %rax, %r15
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x3efc
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x31d0
movq %r15, %rdi
callq 0x32d0