// File: /workspace/github_cpp_makefile_star2_O3/fpavetic[P]lcskpp/lcskpp.cpp
// Function: lcskpp_sparse_fast(std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>> const&, int, int*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>>*)
static void lcskpp_sparse_fast(
    const vector<pair<int, int> >& matches,
    const int k, int* lcskpp_length,
    vector<pair<int, int> >* lcskpp_reconstruction) {
  if (matches.empty()) {
    *lcskpp_length = 0;
    if (lcskpp_reconstruction) lcskpp_reconstruction->clear();
    return;
  }

  vector<tuple<int, int, int> > events;
  events.reserve(2*matches.size());

  int n = 0;
  for (auto it = matches.begin(); it != matches.end(); ++it) {
    int idx = it - matches.begin();
    events.push_back(make_tuple(it->first, it->second, 
				idx+matches.size())); // begin
    events.push_back(make_tuple(it->first+k, it->second+k, idx)); // end
    
    n = max(n, it->first+k);
    n = max(n, it->second+k);
  }
  sort(events.begin(), events.end());

  // Indexed by column, first:dp value, second:index in matches.
  FenwickMax<pair<int, int> > dp_col_max(n);
  vector<int> dp(matches.size());
  vector<int> recon(matches.size());
  vector<int> continues(matches.size(), -1);
  if (k > 1) {
    for (auto curr = matches.begin(); 
         curr != matches.end(); ++curr) {
      auto G = make_pair(curr->first-1, curr->second-1);
      auto prev = lower_bound(matches.begin(), matches.end(), G);
      if (*prev == G) {
	continues[curr-matches.begin()] = prev-matches.begin();
      }
    }
  }

  int best_idx = 0;
  *lcskpp_length = 0;
    
  for (auto event = events.begin(); 
       event != events.end(); ++event) {
    int idx = get<2>(*event) % matches.size();
    bool is_beginning = (get<2>(*event) >= matches.size());
    int i = get<0>(*event);
    int j = get<1>(*event);
    int primary_diagonal = n-1+i-j;

    if (is_beginning) { // begin
      pair<int, int> prev_dp = dp_col_max.get(j);
      dp[idx] = k;
      recon[idx] = -1;

      if (prev_dp.first > 0) {
	dp[idx] = prev_dp.first + k;
	recon[idx] = prev_dp.second;
      }
    } else {
      if (continues[idx] != -1) {
	if (dp[continues[idx]] + 1 > dp[idx]) {
	  dp[idx] = dp[continues[idx]] + 1;
	  recon[idx] = continues[idx];
	}
      }

      dp_col_max.update(j, make_pair(dp[idx], idx));

      if (dp[idx] > *lcskpp_length) {
	*lcskpp_length = dp[idx];
	best_idx = idx;
      }
    }
  }
  
  if (lcskpp_reconstruction) {
    fill_lcskpp_reconstruction(matches, k, recon, best_idx, 
                               *lcskpp_length,
                               lcskpp_reconstruction);
  }    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rcx, %rbx
movq %rdx, %r12
movl %esi, %r14d
movq (%rdi), %rax
movq 0x8(%rdi), %rsi
cmpq %rsi, %rax
je 0x38c4
movq %rdi, %r15
pxor %xmm0, %xmm0
leaq 0x30(%rsp), %rdi
movdqa %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
subq %rax, %rsi
sarq $0x2, %rsi
callq 0x4ba4
movq %r12, 0x10(%rsp)
movq %rbx, 0x68(%rsp)
movq (%r15), %r12
movq 0x8(%r15), %rax
xorl %r13d, %r13d
cmpq %rax, %r12
movq %r15, 0x8(%rsp)
je 0x3657
leaq 0x30(%rsp), %rdx
leaq 0x18(%rsp), %rbx
movq %r12, %rsi
movq (%r15), %rcx
movq %rsi, 0x70(%rsp)
movq %rsi, %r15
subq %rcx, %r15
shrq $0x3, %r15
subq %rcx, %rax
shrq $0x3, %rax
addl %r15d, %eax
movq (%r12), %xmm0
movl %eax, 0x18(%rsp)
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x1c(%rsp)
movq %rdx, %rbp
movq %rdx, %rdi
movq %rbx, %rsi
callq 0x4f2a
movl (%r12), %eax
addl %r14d, %eax
movl 0x4(%r12), %ecx
addl %r14d, %ecx
movl %r15d, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
movl %eax, 0x20(%rsp)
movq %rbp, %r15
movq %rbp, %rdi
movq %rbx, %rbp
movq %rbx, %rsi
callq 0x4f2a
movl (%r12), %eax
addl %r14d, %eax
cmpl %eax, %r13d
cmovgl %r13d, %eax
movl 0x4(%r12), %r13d
addl %r14d, %r13d
cmpl %r13d, %eax
cmovgl %eax, %r13d
addq $0x8, %r12
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x70(%rsp), %rsi
addq $0x8, %rsi
cmpq %rax, %r12
movq %r15, %rdx
movq %rcx, %r15
movq %rbp, %rbx
jne 0x35a3
movl %r13d, %r13d
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %rbp
cmpq %rbp, %rbx
je 0x36a1
movq %rbp, %rax
subq %rbx, %rax
sarq $0x2, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rcx
bsrq %rcx, %rdx
xorl $0x3f, %edx
addl %edx, %edx
xorq $0x7e, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x50ad
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x516c
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x4c64
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x3f90
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x50(%rsp), %rdi
leaq 0x78(%rsp), %rdx
callq 0x3f90
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x48(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x78(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0x4cec
cmpl $0x2, %r14d
jl 0x37b5
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
je 0x37b5
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x78(%rsp), %rsi
movq %rax, %rdi
movl (%rdi), %r10d
movl 0x4(%rdi), %r8d
leal -0x1(%r10), %r11d
decl %r8d
movq %rax, %r9
testq %rdx, %rdx
jle 0x378d
movq %rdx, %r15
movq %rax, %r9
movq %rdx, %rbx
shrq %rbx
leaq (%r9,%rbx,8), %r12
movl (%r12), %ebp
cmpl %r11d, %ebp
jge 0x3779
addq $0x8, %r12
notq %rbx
addq %r15, %rbx
movq %r12, %r9
jmp 0x3785
cmpl %ebp, %r10d
jle 0x3785
cmpl %r8d, 0x4(%r12)
jl 0x376a
movq %rbx, %r15
testq %rbx, %rbx
jg 0x375a
cmpl %r11d, (%r9)
jne 0x37ac
cmpl %r8d, 0x4(%r9)
jne 0x37ac
subq %rax, %r9
shrq $0x3, %r9
movq %rdi, %r8
subq %rax, %r8
sarq %r8
movl %r9d, (%rsi,%r8)
addq $0x8, %rdi
cmpq %rcx, %rdi
jne 0x373b
movq 0x10(%rsp), %rax
movl $0x0, (%rax)
movq 0x30(%rsp), %r12
cmpq 0x38(%rsp), %r12
je 0x38e4
xorl %ebx, %ebx
leaq 0x18(%rsp), %rbp
movq 0x8(%rsp), %r15
movq 0x8(%r15), %rdi
subq (%r15), %rdi
movslq (%r12), %rcx
sarq $0x3, %rdi
movq %rcx, %rax
xorl %edx, %edx
divq %rdi
movq %rdx, %r13
movslq 0x4(%r12), %rsi
cmpq %rcx, %rdi
jbe 0x387c
movslq %r13d, %r15
movq 0x78(%rsp), %rcx
movslq (%rcx,%r15,4), %rdx
movq 0x90(%rsp), %rax
cmpq $-0x1, %rdx
je 0x3836
movl (%rax,%rdx,4), %edx
cmpl (%rax,%r15,4), %edx
jl 0x3836
incl %edx
movl %edx, (%rax,%r15,4)
movl (%rcx,%r15,4), %ecx
movq 0x50(%rsp), %rdx
movl %ecx, (%rdx,%r15,4)
movq %r13, %rcx
shlq $0x20, %rcx
movl (%rax,%r15,4), %eax
orq %rcx, %rax
movq %rax, 0x48(%rsp)
movq %rbp, %rdi
leaq 0x48(%rsp), %rdx
callq 0x4ea8
movq 0x90(%rsp), %rax
movl (%rax,%r15,4), %eax
movq 0x10(%rsp), %rcx
cmpl (%rcx), %eax
movq 0x8(%rsp), %r15
jle 0x38b3
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movl %r13d, %ebx
jmp 0x38b3
movq %rbp, %rdi
callq 0x4e68
movslq %r13d, %rcx
movq 0x90(%rsp), %rdx
movl %r14d, (%rdx,%rcx,4)
movq 0x50(%rsp), %rsi
movl $0xffffffff, (%rsi,%rcx,4) # imm = 0xFFFFFFFF
testl %eax, %eax
jle 0x38b3
movq %rax, %rdi
shrq $0x20, %rdi
addl %r14d, %eax
movl %eax, (%rdx,%rcx,4)
movl %edi, (%rsi,%rcx,4)
addq $0xc, %r12
cmpq 0x38(%rsp), %r12
jne 0x37dc
jmp 0x38eb
movl $0x0, (%r12)
testq %rbx, %rbx
je 0x395d
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x395d
movq %rax, 0x8(%rbx)
jmp 0x395d
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movq 0x68(%rsp), %r9
testq %r9, %r9
je 0x390f
movq 0x10(%rsp), %rax
movl (%rax), %r8d
leaq 0x50(%rsp), %rdx
movq %r15, %rdi
movl %r14d, %esi
movl %ebx, %ecx
callq 0x3dbc
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x391e
callq 0x2130
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x392d
callq 0x2130
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x393f
callq 0x2130
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x394e
callq 0x2130
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x395d
callq 0x2130
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3986
movq %rax, %rbx
jmp 0x3998
movq %rax, %rbx
jmp 0x39a7
movq %rax, %rbx
jmp 0x39b9
jmp 0x39cc
jmp 0x39cc
jmp 0x39cc
movq %rax, %rbx
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x3998
callq 0x2130
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x39a7
callq 0x2130
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x39b9
callq 0x2130
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x39cf
callq 0x2130
jmp 0x39cf
jmp 0x39cc
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x39de
callq 0x2130
movq %rbx, %rdi
callq 0x21f0