// File: /workspace/github_cpp_makefile_star2_O1/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 0x38ce
movq %rdi, %r15
pxor %xmm0, %xmm0
leaq 0x30(%rsp), %rdi
movdqa %xmm0, (%rdi)
movq $0x0, 0x10(%rdi)
subq %rax, %rsi
sarq $0x2, %rsi
callq 0x4b9e
movq %r12, 0x10(%rsp)
movq %rbx, 0x80(%rsp)
movq (%r15), %r12
movq 0x8(%r15), %rax
xorl %r13d, %r13d
cmpq %rax, %r12
movq %r15, 0x8(%rsp)
je 0x365c
leaq 0x30(%rsp), %rdx
leaq 0x18(%rsp), %rbx
movq %r12, %rsi
movq (%r15), %rcx
movq %rsi, 0x88(%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 0x4f3e
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 0x4f3e
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 0x88(%rsp), %rsi
addq $0x8, %rsi
cmpq %rax, %r12
movq %r15, %rdx
movq %rcx, %r15
movq %rbp, %rbx
jne 0x35a2
movl %r13d, %r13d
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %rbp
cmpq %rbp, %rbx
je 0x36a6
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 0x50c1
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x5180
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x4c5e
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x3f96
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x50(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0x3f96
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x48(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
leaq 0x90(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0x4cf4
cmpl $0x2, %r14d
jl 0x37c0
movq 0x8(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
cmpq %rcx, %rax
je 0x37c0
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movq 0x90(%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 0x3798
movq %rdx, %r15
movq %rax, %r9
movq %rdx, %rbx
shrq %rbx
leaq (%r9,%rbx,8), %r12
movl (%r12), %ebp
cmpl %r11d, %ebp
jge 0x3784
addq $0x8, %r12
notq %rbx
addq %r15, %rbx
movq %r12, %r9
jmp 0x3790
cmpl %ebp, %r10d
jle 0x3790
cmpl %r8d, 0x4(%r12)
jl 0x3775
movq %rbx, %r15
testq %rbx, %rbx
jg 0x3765
cmpl %r11d, (%r9)
jne 0x37b7
cmpl %r8d, 0x4(%r9)
jne 0x37b7
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 0x3746
movq 0x10(%rsp), %rax
movl $0x0, (%rax)
movq 0x30(%rsp), %r12
cmpq 0x38(%rsp), %r12
je 0x38f2
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 0x3889
movslq %r13d, %r15
movq 0x90(%rsp), %rax
movslq (%rax,%r15,4), %rdx
cmpq $-0x1, %rdx
je 0x3841
movq 0x68(%rsp), %rcx
movl (%rcx,%rdx,4), %edx
cmpl (%rcx,%r15,4), %edx
jl 0x3841
incl %edx
movl %edx, (%rcx,%r15,4)
movl (%rax,%r15,4), %eax
movq 0x50(%rsp), %rcx
movl %eax, (%rcx,%r15,4)
movq 0x68(%rsp), %rax
movl (%rax,%r15,4), %eax
movq %r13, %rcx
shlq $0x20, %rcx
orq %rax, %rcx
movq %rcx, 0x48(%rsp)
movq %rbp, %rdi
leaq 0x48(%rsp), %rdx
callq 0x4ec6
movq 0x68(%rsp), %rax
movl (%rax,%r15,4), %eax
movq 0x10(%rsp), %rcx
cmpl (%rcx), %eax
movq 0x8(%rsp), %r15
jle 0x38bd
movq 0x10(%rsp), %rcx
movl %eax, (%rcx)
movl %r13d, %ebx
jmp 0x38bd
movq %rbp, %rdi
callq 0x4e68
movslq %r13d, %rcx
movq 0x68(%rsp), %rdx
movl %r14d, (%rdx,%rcx,4)
movq 0x50(%rsp), %rsi
movl $0xffffffff, (%rsi,%rcx,4) # imm = 0xFFFFFFFF
testl %eax, %eax
jle 0x38bd
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 0x37e7
jmp 0x38f9
movl $0x0, (%r12)
testq %rbx, %rbx
je 0x396e
movq (%rbx), %rax
cmpq %rax, 0x8(%rbx)
je 0x396e
movq %rax, 0x8(%rbx)
jmp 0x396e
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movq 0x80(%rsp), %r9
testq %r9, %r9
je 0x3920
movq 0x10(%rsp), %rax
movl (%rax), %r8d
leaq 0x50(%rsp), %rdx
movq %r15, %rdi
movl %r14d, %esi
movl %ebx, %ecx
callq 0x3dc8
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x3932
callq 0x2130
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x3941
callq 0x2130
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x3950
callq 0x2130
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x395f
callq 0x2130
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x396e
callq 0x2130
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3997
movq %rax, %rbx
jmp 0x39ac
movq %rax, %rbx
jmp 0x39bb
movq %rax, %rbx
jmp 0x39ca
jmp 0x39dd
jmp 0x39dd
jmp 0x39dd
movq %rax, %rbx
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x39ac
callq 0x2130
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x39bb
callq 0x2130
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x39ca
callq 0x2130
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x39e0
callq 0x2130
jmp 0x39e0
jmp 0x39dd
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x39ef
callq 0x2130
movq %rbx, %rdi
callq 0x21f0