// File: /workspace/github_cpp_makefile_star2_O1/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 0x3e35
movq %rcx, %r14
movq %rdx, %r12
sarq $0x3, %rsi
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x4c72
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
je 0x3904
sarq $0x3, %rcx
movq (%r14), %rdx
movl $0x1, %esi
movq 0x38(%rsp), %rdi
xorl %r8d, %r8d
movq (%rdx,%r8), %r9
addq (%rax,%r8), %r9
movq %r9, (%rdi,%r8,2)
movl %esi, %r9d
addq $0x8, %r8
incl %esi
cmpq %r9, %rcx
ja 0x38ea
movq (%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%r12), %rax
cmpq (%r12), %rax
movq %r12, 0x58(%rsp)
je 0x39f6
xorl %r15d, %r15d
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x39dd
xorl %ebp, %ebp
xorl %ebx, %ebx
movq (%r12), %rdx
movq (%rax,%rbp,8), %rcx
addq (%rdx,%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 0x3979
movq %rdx, %rdi
shlq $0x4, %rdi
cmpq %rcx, (%rsi,%rdi)
ja 0x3979
incl %ebx
movq %rbx, %rdx
cmpq %rbx, %rax
ja 0x3962
xorl %r13d, %r13d
cmpq %rdx, %rax
sete %r12b
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x561a
movb %r12b, %r13b
subl %r13d, %ebx
cmpb $0x0, 0x8(%rax)
jne 0x39bf
movl %ebx, %eax
movq 0x38(%rsp), %rcx
shlq $0x4, %rax
incq 0x8(%rcx,%rax)
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x561a
movb $0x1, 0x8(%rax)
incl %ebp
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
movq 0x58(%rsp), %r12
ja 0x3939
incl %r15d
movq 0x8(%r12), %rax
subq (%r12), %rax
sarq $0x3, %rax
cmpq %r15, %rax
ja 0x3928
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
cmpq $0x2, %rcx
jb 0x3a34
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 0x3a1b
movq 0x30(%rsp), %rbx
leal 0x1(%rbx), %esi
leaq 0x6775(%rip), %rdi      # 0xa1b8
movq %rsi, 0x50(%rsp)
callq 0x4d1a
addl $0x2, %ebx
leaq 0x6779(%rip), %rdi      # 0xa1d0
movq %rbx, %rsi
callq 0x4d4c
movq 0x8(%r12), %rax
xorl %ebp, %ebp
cmpq (%r12), %rax
je 0x3c40
movq $0x0, 0x30(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x60(%rsp)
movq (%r14), %rax
cmpq %rax, 0x8(%r14)
je 0x3c1c
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 0x3ad9
movq %rdx, %rdi
shlq $0x4, %rdi
cmpq %rcx, (%rsi,%rdi)
ja 0x3ad9
incl %r15d
movq %r15, %rdx
cmpq %r15, %rax
ja 0x3ac1
xorl %r13d, %r13d
cmpq %rdx, %rax
sete %r12b
movq (%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x561a
movb %r12b, %r13b
subl %r13d, %r15d
cmpb $0x1, 0x8(%rax)
jne 0x3bfe
movq 0x10(%rsp), %r13
movl %r15d, %eax
movq 0x38(%rsp), %rcx
shlq $0x4, %rax
leal -0x1(%r15), %edx
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 0x34d4(%rip), %xmm3     # 0x7010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x34d8(%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 %r13, %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 0x6603(%rip), %rcx      # 0xa1d0
movq %rbp, %rdx
shlq $0x4, %rdx
movq %r13, (%rcx,%rdx)
movq 0x65d9(%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 0x561a
movb $0x0, 0x8(%rax)
incl %ebx
movq (%r14), %rax
movq 0x8(%r14), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbx, %rcx
movq 0x58(%rsp), %r12
ja 0x3a92
movq 0x8(%r12), %rax
subq (%r12), %rax
movq 0x60(%rsp), %rcx
incl %ecx
sarq $0x3, %rax
movq %rcx, 0x30(%rsp)
cmpq %rcx, %rax
ja 0x3a7b
movl %ebp, %ebp
movq 0x63a1(%rip), %rbx      # 0x9fe8
leaq 0x3596(%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 0x636f(%rip), %rdi      # 0x9fe8
callq 0x3030
movq %rax, %rdi
callq 0x3110
leaq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x4c72
xorl %r15d, %r15d
cmpq $0x0, 0x50(%rsp)
je 0x3d58
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x37f4
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 0x3cf4
subq %rbx, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x31d0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3d0b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x31d0
cmpq %r14, %r13
je 0x3d3e
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 0x3d1b
incq %r12
movq (%rsp), %r14
movq %r14, %rbp
movq %r13, %rbx
cmpq 0x50(%rsp), %r12
jne 0x3cae
jmp 0x3d5e
xorl %r13d, %r13d
xorl %r14d, %r14d
movq 0x6283(%rip), %rdi      # 0x9fe8
leaq 0x3483(%rip), %rsi      # 0x71ef
movl $0x7, %edx
callq 0x31e0
movq 0x626b(%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 0x6230(%rip), %rbx      # 0x9fe8
leaq 0x3438(%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 0x61fe(%rip), %rdi      # 0x9fe8
callq 0x3030
movq %rax, %rdi
callq 0x3110
testq %r13, %r13
je 0x3e0a
subq %r13, %r14
movq %r13, %rdi
movq %r14, %rsi
callq 0x31d0
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x3e21
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 0x326c(%rip), %rdi      # 0x70a8
leaq 0x327c(%rip), %rsi      # 0x70bf
leaq 0x3313(%rip), %rcx      # 0x715d
movl $0x80, %edx
callq 0x30e0
jmp 0x3e9a
jmp 0x3e9a
movq %r13, %rbx
movq %r14, %rbp
jmp 0x3e60
movq %rax, %r15
testq %rbx, %rbx
je 0x3e76
subq %rbx, %rbp
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x31d0
movq 0x8(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3e9d
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x31d0
jmp 0x3e9d
jmp 0x3e9a
jmp 0x3e9a
movq %rax, %r15
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x3eb4
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x31d0
movq %r15, %rdi
callq 0x32d0