// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movl %esi, %r8d
movq (%rdi), %rax
movq 0x8(%rdi), %rsi
cmpq %rsi, %rax
je 0x361d
movq %rdi, %r15
movq %r8, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rcx, 0x70(%rsp)
leaq 0x40(%rsp), %rdi
andq $0x0, 0x10(%rdi)
pxor %xmm0, %xmm0
movdqa %xmm0, (%rdi)
subq %rax, %rsi
sarq $0x2, %rsi
callq 0x4aa6
movq (%r15), %rdx
movl $0x0, 0x8(%rsp)
leaq 0x40(%rsp), %r14
leaq 0x18(%rsp), %r13
movq %rdx, %r12
movq %r15, 0x78(%rsp)
movq 0x8(%r15), %rax
cmpq %rax, %r12
je 0x33ac
movq (%r15), %rcx
movq %rdx, 0x80(%rsp)
movq %rdx, %rbx
subq %rcx, %rbx
shrq $0x3, %rbx
subq %rcx, %rax
shrq $0x3, %rax
addl %ebx, %eax
movq (%r12), %xmm0
movl %eax, 0x18(%rsp)
pshufd $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3]
movq %xmm0, 0x1c(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x4cf4
movl (%r12), %eax
movq 0x38(%rsp), %r15
addl %r15d, %eax
movl 0x4(%r12), %ecx
addl %r15d, %ecx
movl %ebx, 0x18(%rsp)
movl %ecx, 0x1c(%rsp)
movl %eax, 0x20(%rsp)
movq %r14, %rbx
movq %r14, %rdi
movq %r13, %rbp
movq %r13, %rsi
callq 0x4cf4
movl (%r12), %eax
addl %r15d, %eax
movl 0x8(%rsp), %ecx
cmpl %eax, %ecx
cmovgl %ecx, %eax
movl 0x4(%r12), %ecx
addl %r15d, %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl %ecx, 0x8(%rsp)
addq $0x8, %r12
movq 0x80(%rsp), %rdx
addq $0x8, %rdx
movq %rbx, %r14
movq 0x78(%rsp), %r15
movq %rbp, %r13
jmp 0x32ef
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x4e25
movl 0x8(%rsp), %esi
leaq 0x18(%rsp), %rdi
callq 0x4b3c
movq 0x38(%rsp), %r14
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0xa0(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x3b98
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x58(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x3b98
movq 0x8(%r15), %rsi
subq (%r15), %rsi
sarq $0x3, %rsi
leaq 0x10(%rsp), %rdx
orl $-0x1, (%rdx)
leaq 0x88(%rsp), %rdi
leaq 0x7(%rsp), %rcx
callq 0x4b96
cmpl $0x2, %r14d
jl 0x34ae
movq (%r15), %r12
leaq 0x10(%rsp), %rbx
movq %r12, %r13
movq 0x8(%r15), %rsi
cmpq %rsi, %r13
je 0x34ae
movl (%r13), %eax
movl 0x4(%r13), %ecx
decl %eax
decl %ecx
shlq $0x20, %rcx
orq %rcx, %rax
movq %rax, 0x10(%rsp)
movq (%r15), %rdi
movq %rbx, %rdx
callq 0x5691
movq (%rax), %xmm0
movq 0x10(%rsp), %xmm1
pcmpeqd %xmm0, %xmm1
pshufd $0x50, %xmm1, %xmm0     # xmm0 = xmm1[0,0,1,1]
movmskpd %xmm0, %ecx
cmpl $0x3, %ecx
jne 0x34a4
movq (%r15), %rcx
subq %rcx, %rax
shrq $0x3, %rax
movq %r12, %rdx
subq %rcx, %rdx
movq 0x88(%rsp), %rcx
sarq %rdx
movl %eax, (%rcx,%rdx)
addq $0x8, %r13
addq $0x8, %r12
jmp 0x343e
movq 0x30(%rsp), %r12
andl $0x0, (%r12)
movq $0x0, 0x8(%rsp)
movq 0x40(%rsp), %rbx
leaq 0x18(%rsp), %rbp
cmpq 0x48(%rsp), %rbx
je 0x35b0
movq 0x8(%r15), %rdi
subq (%r15), %rdi
movslq (%rbx), %rcx
sarq $0x3, %rdi
movq %rcx, %rax
xorl %edx, %edx
divq %rdi
movq %rdx, %r13
movslq 0x4(%rbx), %rsi
cmpq %rcx, %rdi
jbe 0x3573
movslq %r13d, %r12
movq 0x88(%rsp), %rcx
movslq (%rcx,%r12,4), %rdx
movq 0xa0(%rsp), %rax
cmpq $-0x1, %rdx
je 0x3531
movl (%rax,%rdx,4), %edx
cmpl (%rax,%r12,4), %edx
jl 0x3531
incl %edx
movl %edx, (%rax,%r12,4)
movl (%rcx,%r12,4), %ecx
movq 0x58(%rsp), %rdx
movl %ecx, (%rdx,%r12,4)
movq %r13, %rcx
shlq $0x20, %rcx
movl (%rax,%r12,4), %eax
orq %rcx, %rax
movq %rax, 0x10(%rsp)
movq %rbp, %rdi
leaq 0x10(%rsp), %rdx
callq 0x4c40
movq 0xa0(%rsp), %rax
movl (%rax,%r12,4), %eax
movq 0x30(%rsp), %r12
cmpl (%r12), %eax
jle 0x35a7
movl %eax, (%r12)
movq %r13, 0x8(%rsp)
jmp 0x35a7
movq %rbp, %rdi
callq 0x4bec
movslq %r13d, %rcx
movq 0xa0(%rsp), %rdx
movl %r14d, (%rdx,%rcx,4)
movq 0x58(%rsp), %rsi
orl $-0x1, (%rsi,%rcx,4)
testl %eax, %eax
jle 0x35a7
movq %rax, %rdi
shrq $0x20, %rdi
addl %r14d, %eax
movl %eax, (%rdx,%rcx,4)
movl %edi, (%rsi,%rcx,4)
addq $0xc, %rbx
jmp 0x34cb
movq 0x70(%rsp), %r9
testq %r9, %r9
je 0x35d3
movl (%r12), %r8d
leaq 0x58(%rsp), %rdx
movq %r15, %rdi
movl %r14d, %esi
movq 0x8(%rsp), %rcx
callq 0x3a30
leaq 0x88(%rsp), %rdi
callq 0x55fc
leaq 0x58(%rsp), %rdi
callq 0x55fc
leaq 0xa0(%rsp), %rdi
callq 0x55fc
leaq 0x18(%rsp), %rdi
callq 0x2912
leaq 0x40(%rsp), %rdi
callq 0x5706
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x0, (%rdx)
testq %rcx, %rcx
je 0x360b
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0x360b
movq %rax, 0x8(%rcx)
jmp 0x360b
jmp 0x364d
movq %rax, %rbx
jmp 0x365d
movq %rax, %rbx
jmp 0x3667
movq %rax, %rbx
jmp 0x3674
jmp 0x3682
jmp 0x3682
jmp 0x3682
jmp 0x364d
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x55fc
leaq 0x58(%rsp), %rdi
callq 0x55fc
leaq 0xa0(%rsp), %rdi
callq 0x55fc
leaq 0x18(%rsp), %rdi
callq 0x2912
jmp 0x3685
jmp 0x3682
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5706
movq %rbx, %rdi
callq 0x21f0