// File: 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:
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rdi, 0xd8(%rsp)
movq %rdi, %rax
movq %rax, 0xe0(%rsp)
movq %rdi, 0x1f0(%rsp)
movq %rsi, 0x1e8(%rsp)
movq %rdx, 0x1e0(%rsp)
movq %rcx, 0x1d8(%rsp)
movq 0x1e0(%rsp), %rdi
callq 0x68e0
movq %rax, 0xe8(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x68e0
movq %rax, %rcx
movq 0xe8(%rsp), %rax
cmpq %rcx, %rax
jne 0x3cde
jmp 0x3cfd
leaq 0xc377(%rip), %rdi      # 0x1005c
leaq 0xc387(%rip), %rsi      # 0x10073
movl $0x80, %edx
leaq 0xc419(%rip), %rcx      # 0x10111
callq 0x3120
movq 0x1e0(%rsp), %rdi
callq 0x68e0
movq %rax, 0xc8(%rsp)
leaq 0x1bf(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
callq 0x6950
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
leaq 0x1c0(%rsp), %rdi
callq 0x6960
jmp 0x3d46
leaq 0x1bf(%rsp), %rdi
callq 0x69e0
movl $0x0, 0x1a8(%rsp)
movl 0x1a8(%rsp), %eax
movq %rax, 0xc0(%rsp)
movq 0x1e0(%rsp), %rdi
callq 0x68e0
movq %rax, %rcx
movq 0xc0(%rsp), %rax
cmpq %rcx, %rax
jae 0x3e38
movq 0x1e0(%rsp), %rdi
movl 0x1a8(%rsp), %eax
movl %eax, %esi
callq 0x6910
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0x1d8(%rsp), %rdi
movl 0x1a8(%rsp), %eax
movl %eax, %esi
callq 0x6910
movq %rax, %rcx
movq 0xb0(%rsp), %rax
addq (%rcx), %rax
movq %rax, 0xb8(%rsp)
movl 0x1a8(%rsp), %eax
movl %eax, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x68c0
movq 0xb8(%rsp), %rcx
movq %rcx, (%rax)
movl 0x1a8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1a8(%rsp)
jmp 0x3d5e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x1bf(%rsp), %rdi
callq 0x69e0
jmp 0x455b
leaq 0x1c0(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq 0x1e8(%rsp), %rdi
callq 0x6940
movl %eax, 0x19c(%rsp)
movl $0x0, 0x198(%rsp)
movl 0x198(%rsp), %eax
movq %rax, 0xa8(%rsp)
movq 0x1e0(%rsp), %rdi
callq 0x68e0
movq %rax, %rcx
movq 0xa8(%rsp), %rax
cmpq %rcx, %rax
jae 0x4017
movl $0x0, 0x194(%rsp)
movl $0x0, 0x190(%rsp)
movl 0x190(%rsp), %eax
movq %rax, 0xa0(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x68e0
movq %rax, %rcx
movq 0xa0(%rsp), %rax
cmpq %rcx, %rax
jae 0x3fff
movq 0x1e0(%rsp), %rdi
movl 0x198(%rsp), %eax
movl %eax, %esi
callq 0x6910
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0x1d8(%rsp), %rdi
movl 0x190(%rsp), %eax
movl %eax, %esi
callq 0x6910
movq %rax, %rcx
movq 0x98(%rsp), %rax
movq (%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x188(%rsp)
movq 0x188(%rsp), %rdx
leaq 0x1a0(%rsp), %rdi
leaq 0x194(%rsp), %rsi
callq 0x4570
jmp 0x3f4c
movq 0x1e8(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x6890
movq %rax, 0x90(%rsp)
jmp 0x3f6b
movq 0x90(%rsp), %rax
movb 0x8(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x3fe7
movl 0x194(%rsp), %eax
movl %eax, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x68c0
movq 0x8(%rax), %rcx
incq %rcx
movq %rcx, 0x8(%rax)
movq 0x1e8(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x6890
movq %rax, 0x88(%rsp)
jmp 0x3fc0
movq 0x88(%rsp), %rax
movb $0x1, 0x8(%rax)
jmp 0x3fe7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0x454e
jmp 0x3fe9
movl 0x190(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x190(%rsp)
jmp 0x3ead
jmp 0x4001
movl 0x198(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x198(%rsp)
jmp 0x3e67
movl $0x1, 0x184(%rsp)
movl 0x184(%rsp), %eax
movq %rax, 0x80(%rsp)
leaq 0x1c0(%rsp), %rdi
callq 0x6870
movq %rax, %rcx
movq 0x80(%rsp), %rax
cmpq %rcx, %rax
jae 0x40ab
movl 0x184(%rsp), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x68c0
movq 0x8(%rax), %rax
movq %rax, 0x78(%rsp)
movl 0x184(%rsp), %eax
movl %eax, %esi
leaq 0x1c0(%rsp), %rdi
callq 0x68c0
movq 0x78(%rsp), %rcx
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movl 0x184(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x184(%rsp)
jmp 0x4022
leaq 0x1c0(%rsp), %rax
movq %rax, 0x178(%rsp)
movl 0x19c(%rsp), %eax
incl %eax
movl %eax, 0x174(%rsp)
movl 0x174(%rsp), %eax
movl %eax, %esi
leaq 0x13155(%rip), %rdi     # 0x17230
callq 0x6de0
jmp 0x40e2
movl 0x174(%rsp), %eax
incl %eax
movl %eax, %esi
leaq 0x13154(%rip), %rdi     # 0x17248
callq 0x6e80
jmp 0x40fb
movl $0x0, 0x19c(%rsp)
movl $0x0, 0x170(%rsp)
movl 0x170(%rsp), %eax
movq %rax, 0x70(%rsp)
movq 0x1e0(%rsp), %rdi
callq 0x68e0
movq %rax, %rcx
movq 0x70(%rsp), %rax
cmpq %rcx, %rax
jae 0x42e0
movl $0x0, 0x16c(%rsp)
movl $0x0, 0x168(%rsp)
movl 0x168(%rsp), %eax
movq %rax, 0x68(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x68e0
movq %rax, %rcx
movq 0x68(%rsp), %rax
cmpq %rcx, %rax
jae 0x42c8
movq 0x1e0(%rsp), %rdi
movl 0x170(%rsp), %eax
movl %eax, %esi
callq 0x6910
movq (%rax), %rax
movq %rax, 0x60(%rsp)
movq 0x1d8(%rsp), %rdi
movl 0x168(%rsp), %eax
movl %eax, %esi
callq 0x6910
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq (%rcx), %rcx
addq %rcx, %rax
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rdx
leaq 0x1a0(%rsp), %rdi
leaq 0x16c(%rsp), %rsi
callq 0x4570
jmp 0x41e4
movq 0x1e8(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x6890
movq %rax, 0x58(%rsp)
jmp 0x4200
movq 0x58(%rsp), %rax
movb 0x8(%rax), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x42b0
movl 0x16c(%rsp), %esi
movq 0x160(%rsp), %rdx
leaq 0x178(%rsp), %rdi
callq 0x4630
movsd %xmm0, 0x50(%rsp)
jmp 0x423a
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x158(%rsp)
movsd 0x158(%rsp), %xmm0
callq 0x30c0
cvttsd2si %xmm0, %rax
movl %eax, 0x154(%rsp)
movl 0x19c(%rsp), %eax
incl %eax
movl %eax, 0x19c(%rsp)
movl %eax, %edi
movq 0x160(%rsp), %rsi
movl 0x154(%rsp), %eax
movl %eax, %edx
callq 0x3b00
movq 0x1e8(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x6890
movq %rax, 0x48(%rsp)
jmp 0x42a7
movq 0x48(%rsp), %rax
movb $0x0, 0x8(%rax)
jmp 0x42b2
movl 0x168(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x168(%rsp)
jmp 0x4151
jmp 0x42ca
movl 0x170(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x170(%rsp)
jmp 0x4111
movq 0x12d01(%rip), %rdi     # 0x16fe8
leaq 0xbeaa(%rip), %rsi      # 0x10198
callq 0x31e0
movq %rax, 0x40(%rsp)
jmp 0x42fa
movq 0x40(%rsp), %rdi
movq 0x12c9a(%rip), %rsi     # 0x16fa0
callq 0x3210
jmp 0x430d
movl $0x0, 0x150(%rsp)
movb $0x0, 0x14f(%rsp)
movl 0x19c(%rsp), %eax
movq %rax, 0x30(%rsp)
leaq 0x14e(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x6950
movq 0xd8(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
callq 0x6960
jmp 0x4357
leaq 0x14e(%rsp), %rdi
callq 0x69e0
movq 0xd8(%rsp), %rax
movq %rax, 0x138(%rsp)
leaq 0x150(%rsp), %rax
movq %rax, 0x140(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x130(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x6c70
movl $0x0, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
cmpl 0x174(%rsp), %eax
jae 0x4493
movl 0x10c(%rsp), %eax
movl %eax, %esi
leaq 0xf0(%rsp), %rdi
callq 0x3ba0
jmp 0x43e8
leaq 0x110(%rsp), %rdi
movq %rdi, 0x28(%rsp)
leaq 0xf0(%rsp), %rsi
movq %rsi, 0x20(%rsp)
callq 0x6f20
movq 0x20(%rsp), %rdi
callq 0x6c80
movq 0x28(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x47e0
jmp 0x4425
jmp 0x4427
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x43bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x14e(%rsp), %rdi
callq 0x69e0
jmp 0x454e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x6c80
movq 0xd8(%rsp), %rdi
callq 0x6b20
jmp 0x454e
movq 0x12b4e(%rip), %rdi     # 0x16fe8
leaq 0xbd02(%rip), %rsi      # 0x101a3
callq 0x31e0
movq %rax, 0x18(%rsp)
jmp 0x44ad
movq 0x18(%rsp), %rdi
movl 0x174(%rsp), %esi
callq 0x32a0
movq %rax, 0x10(%rsp)
jmp 0x44c5
movq 0x10(%rsp), %rdi
movq 0x12acf(%rip), %rsi     # 0x16fa0
callq 0x3210
jmp 0x44d8
movq 0x12b09(%rip), %rdi     # 0x16fe8
leaq 0xbcc5(%rip), %rsi      # 0x101ab
callq 0x31e0
movq %rax, 0x8(%rsp)
jmp 0x44f2
movq 0x8(%rsp), %rdi
movq 0x12aa2(%rip), %rsi     # 0x16fa0
callq 0x3210
jmp 0x4505
movb $0x1, 0x14f(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x6c80
testb $0x1, 0x14f(%rsp)
jne 0x4531
movq 0xd8(%rsp), %rdi
callq 0x6b20
leaq 0x1c0(%rsp), %rdi
callq 0x6b20
movq 0xe0(%rsp), %rax
addq $0x1f8, %rsp            # imm = 0x1F8
retq
leaq 0x1c0(%rsp), %rdi
callq 0x6b20
movq 0x1b0(%rsp), %rdi
callq 0x3380
nopl (%rax,%rax)