// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rsi, %r12
movq %rdi, 0x28(%rsp)
movq 0x8(%rdx), %rsi
subq (%rdx), %rsi
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cmpq %rax, %rsi
jne 0x3bad
movq %rcx, %r14
movq %rdx, %r15
sarq $0x3, %rsi
leaq 0x58(%rsp), %rbx
movq %rsp, %rdx
movq %rbx, %rdi
callq 0x4576
movq (%r15), %rdx
movq 0x8(%r15), %rcx
movq %rcx, %rsi
subq %rdx, %rsi
sarq $0x3, %rsi
movq (%r14), %rax
xorl %r8d, %r8d
movq (%rbx), %rdi
movl %r8d, %r8d
cmpq %r8, %rsi
jbe 0x380c
movq (%rax,%r8,8), %r9
addq (%rdx,%r8,8), %r9
movq %r8, %r10
shlq $0x4, %r10
movq %r9, (%rdi,%r10)
incl %r8d
jmp 0x37ec
leaq 0x38(%rsp), %rsi
movq %rbx, (%rsi)
movq 0x18(%r12), %rsi
movq %rsi, 0x70(%rsp)
xorl %r13d, %r13d
leaq 0x40(%rsp), %rbp
movq %rsp, %rbx
movq %r15, 0x20(%rsp)
subq %rdx, %rcx
movl %r13d, %r13d
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x38cb
andl $0x0, 0x40(%rsp)
xorl %r15d, %r15d
movl %r15d, %r15d
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
jbe 0x38b7
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq (%rax,%r15,8), %rdx
addq (%rcx,%r13,8), %rdx
movq %rdx, (%rsp)
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x3c02
movq %r12, %rdi
movq %rbx, %rsi
callq 0x50b8
cmpb $0x0, 0x8(%rax)
jne 0x38af
movl 0x40(%rsp), %eax
movq 0x58(%rsp), %rcx
shlq $0x4, %rax
incq 0x8(%rcx,%rax)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x50b8
movb $0x1, 0x8(%rax)
incl %r15d
movq (%r14), %rax
jmp 0x3849
incl %r13d
movq 0x20(%rsp), %r15
movq (%r15), %rdx
movq 0x8(%r15), %rcx
jmp 0x382e
movq 0x58(%rsp), %rax
movq 0x60(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
leaq 0x8(%rax), %rdx
pushq $0x1
popq %rsi
movq 0x70(%rsp), %rbx
movl %esi, %esi
cmpq %rsi, %rcx
jbe 0x3906
movq (%rdx), %rdi
movq %rsi, %r8
shlq $0x4, %r8
addq %rdi, 0x8(%rax,%r8)
incl %esi
addq $0x10, %rdx
jmp 0x38e8
leal 0x1(%rbx), %esi
leaq 0x78a0(%rip), %rdi      # 0xb1b0
movq %rsi, 0x30(%rsp)
callq 0x45f4
addl $0x2, %ebx
leaq 0x78a4(%rip), %rdi      # 0xb1c8
movq %rbx, %rsi
callq 0x4626
xorl %ebx, %ebx
xorl %ebp, %ebp
movl %ebx, %ebx
movq 0x8(%r15), %rax
subq (%r15), %rax
sarq $0x3, %rax
cmpq %rbx, %rax
jbe 0x3aa8
andl $0x0, 0x40(%rsp)
xorl %r13d, %r13d
movl %r13d, %r13d
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x3aa1
movq (%r15), %rcx
movq (%rax,%r13,8), %rdx
addq (%rcx,%rbx,8), %rdx
movq %rdx, (%rsp)
leaq 0x38(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x3c02
movq %r12, %rdi
movq %rsp, %rsi
callq 0x50b8
cmpb $0x1, 0x8(%rax)
jne 0x3a99
movl 0x40(%rsp), %eax
movq (%rsp), %r15
movq 0x58(%rsp), %rcx
leal -0x1(%rax), %edx
shlq $0x4, %rax
shlq $0x4, %rdx
movq (%rcx,%rax), %rsi
movq 0x8(%rcx,%rax), %rdi
movq (%rcx,%rdx), %r8
movq 0x8(%rcx,%rdx), %rax
subq %r8, %rsi
movq %rsi, %xmm0
movq 0x363b(%rip), %xmm3     # 0x7010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x363f(%rip), %xmm4     # 0x7020
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
subq %rax, %rdi
movq %rdi, %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 %r15, %rcx
subq %r8, %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 0x7762(%rip), %rcx      # 0xb1c8
movq %rbp, %rdx
shlq $0x4, %rdx
movq %r15, (%rcx,%rdx)
movq 0x7738(%rip), %rsi      # 0xb1b0
movq (%rsi,%rax,8), %rdi
movq %rdi, 0x8(%rcx,%rdx)
movq %rbp, (%rsi,%rax,8)
movq %r12, %rdi
movq %rsp, %rsi
callq 0x50b8
movb $0x0, 0x8(%rax)
movq 0x20(%rsp), %r15
incl %r13d
jmp 0x394e
incl %ebx
jmp 0x3930
movq 0x7539(%rip), %rdi      # 0xafe8
leaq 0x36fe(%rip), %rsi      # 0x71b4
callq 0x31c0
movq %rax, %rdi
callq 0x30c0
movl %ebp, %esi
movq %rsp, %rdx
movq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0x4576
xorpd %xmm0, %xmm0
movq %rsp, %rbx
andq $0x0, 0x10(%rbx)
movapd %xmm0, (%rbx)
xorl %r14d, %r14d
leaq 0x40(%rsp), %r15
xorl %ebp, %ebp
cmpq 0x30(%rsp), %r14
je 0x3b46
movq %r15, %rdi
movq %r14, %rsi
callq 0x3721
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5930
movq %r15, %rdi
callq 0x509a
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
movq (%r12), %rdx
cmpq %rcx, %rax
je 0x3b41
movq (%rax), %rsi
movl %ebp, %edi
incl %ebp
shlq $0x4, %rdi
movq %rsi, (%rdx,%rdi)
andq $0x0, 0x8(%rdx,%rdi)
addq $0x8, %rax
jmp 0x3b21
incq %r14
jmp 0x3aef
movq 0x749b(%rip), %rdi      # 0xafe8
leaq 0x366b(%rip), %rsi      # 0x71bf
callq 0x31c0
movq %rax, %rdi
movq 0x30(%rsp), %rsi
callq 0x3120
movq %rax, %rdi
callq 0x30c0
movq 0x7473(%rip), %rdi      # 0xafe8
leaq 0x364b(%rip), %rsi      # 0x71c7
callq 0x31c0
movq %rax, %rdi
callq 0x30c0
movq %rsp, %rdi
callq 0x509a
leaq 0x58(%rsp), %rdi
callq 0x5454
movq %r12, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x34c4(%rip), %rdi      # 0x7078
leaq 0x34d4(%rip), %rsi      # 0x708f
leaq 0x356b(%rip), %rcx      # 0x712d
movl $0x80, %edx
callq 0x3100
jmp 0x3bed
jmp 0x3bed
jmp 0x3bd2
movq %rax, %rbx
movq %rsp, %rdi
callq 0x509a
movq 0x28(%rsp), %rdi
callq 0x5454
jmp 0x3bf0
jmp 0x3bed
jmp 0x3bed
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x5454
movq %rbx, %rdi
callq 0x32c0