// File: 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:
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, 0x2a0(%rsp)
movl %esi, 0x29c(%rsp)
movq %rdx, 0x290(%rsp)
movq %rcx, 0x288(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0xc0b0
testb $0x1, %al
jne 0x5709
jmp 0x5734
movq 0x290(%rsp), %rax
movl $0x0, (%rax)
cmpq $0x0, 0x288(%rsp)
je 0x572f
movq 0x288(%rsp), %rdi
callq 0x76e0
jmp 0x6217
leaq 0x270(%rsp), %rdi
movq %rdi, 0xe0(%rsp)
callq 0xc510
movq 0x2a0(%rsp), %rdi
callq 0x7350
movq 0xe0(%rsp), %rdi
movq %rax, %rsi
addq %rsi, %rsi
callq 0xc520
jmp 0x576b
movl $0x0, 0x260(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x7370
movq %rax, 0x258(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x73a0
movq %rax, 0x250(%rsp)
leaq 0x258(%rsp), %rdi
leaq 0x250(%rsp), %rsi
callq 0x73d0
testb $0x1, %al
jne 0x57be
jmp 0x5a02
movq 0x2a0(%rsp), %rdi
callq 0x7370
movq %rax, 0x240(%rsp)
leaq 0x258(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
leaq 0x240(%rsp), %rsi
callq 0xc630
movq 0xc0(%rsp), %rdi
movl %eax, 0x24c(%rsp)
callq 0xc6e0
movq 0xc0(%rsp), %rdi
movq %rax, 0xc8(%rsp)
callq 0xc6e0
addq $0x4, %rax
movq %rax, 0xd0(%rsp)
movslq 0x24c(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x7350
movq 0xc8(%rsp), %rsi
movq 0xd0(%rsp), %rdx
movq %rax, %rcx
movq 0xd8(%rsp), %rax
addq %rcx, %rax
movq %rax, 0x218(%rsp)
leaq 0x220(%rsp), %rdi
leaq 0x218(%rsp), %rcx
callq 0xc6a0
jmp 0x587f
leaq 0x234(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
leaq 0x220(%rsp), %rsi
callq 0xc6f0
movq 0xb8(%rsp), %rsi
leaq 0x270(%rsp), %rdi
callq 0xc670
jmp 0x58b3
leaq 0x258(%rsp), %rdi
movq %rdi, 0xb0(%rsp)
callq 0xc6e0
movq 0xb0(%rsp), %rdi
movl (%rax), %eax
movl 0x29c(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x208(%rsp)
callq 0xc6e0
movl 0x4(%rax), %eax
movl 0x29c(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x204(%rsp)
leaq 0x20c(%rsp), %rdi
leaq 0x208(%rsp), %rsi
leaq 0x204(%rsp), %rdx
leaq 0x24c(%rsp), %rcx
callq 0xc720
jmp 0x5921
leaq 0x270(%rsp), %rdi
leaq 0x20c(%rsp), %rsi
callq 0xc670
jmp 0x5938
leaq 0x258(%rsp), %rdi
callq 0xc6e0
movl (%rax), %eax
movl 0x29c(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x200(%rsp)
leaq 0x260(%rsp), %rdi
leaq 0x200(%rsp), %rsi
callq 0x7640
movq %rax, 0xa8(%rsp)
jmp 0x5976
movq 0xa8(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x260(%rsp)
leaq 0x258(%rsp), %rdi
callq 0xc6e0
movl 0x4(%rax), %eax
movl 0x29c(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x1fc(%rsp)
leaq 0x260(%rsp), %rdi
leaq 0x1fc(%rsp), %rsi
callq 0x7640
movq %rax, 0xa0(%rsp)
jmp 0x59c6
movq 0xa0(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x260(%rsp)
leaq 0x258(%rsp), %rdi
callq 0x7420
jmp 0x578b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
jmp 0x6246
leaq 0x270(%rsp), %rdi
movq %rdi, 0x98(%rsp)
callq 0xc7a0
movq 0x98(%rsp), %rdi
movq %rax, 0x1f0(%rsp)
callq 0xc7d0
movq %rax, 0x1e8(%rsp)
movq 0x1f0(%rsp), %rdi
movq 0x1e8(%rsp), %rsi
callq 0xc760
jmp 0x5a4b
movslq 0x260(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0xc800
jmp 0x5a62
movq 0x2a0(%rsp), %rdi
callq 0x7350
movq %rax, 0x88(%rsp)
leaq 0x1b7(%rsp), %rdi
movq %rdi, 0x90(%rsp)
callq 0x7460
movq 0x88(%rsp), %rsi
movq 0x90(%rsp), %rdx
leaq 0x1b8(%rsp), %rdi
callq 0x7470
jmp 0x5aab
leaq 0x1b7(%rsp), %rdi
callq 0x75f0
movq 0x2a0(%rsp), %rdi
callq 0x7350
movq %rax, 0x78(%rsp)
leaq 0x197(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x7460
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
leaq 0x198(%rsp), %rdi
callq 0x7470
jmp 0x5afb
leaq 0x197(%rsp), %rdi
callq 0x75f0
movq 0x2a0(%rsp), %rdi
callq 0x7350
movq %rax, 0x68(%rsp)
movl $0xffffffff, 0x174(%rsp) # imm = 0xFFFFFFFF
leaq 0x173(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x7460
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rcx
leaq 0x178(%rsp), %rdi
leaq 0x174(%rsp), %rdx
callq 0xc8b0
jmp 0x5b58
leaq 0x173(%rsp), %rdi
callq 0x75f0
cmpl $0x1, 0x29c(%rsp)
jle 0x5dc3
movq 0x2a0(%rsp), %rdi
callq 0x7370
movq %rax, 0x168(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x73a0
movq %rax, 0x160(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x73d0
testb $0x1, %al
jne 0x5bbb
jmp 0x5dc1
leaq 0x168(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0xc6e0
movq 0x58(%rsp), %rdi
movl (%rax), %eax
decl %eax
movl %eax, 0x154(%rsp)
callq 0xc6e0
movl 0x4(%rax), %eax
decl %eax
movl %eax, 0x150(%rsp)
leaq 0x154(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0xc930
movq %rax, 0x60(%rsp)
jmp 0x5c0a
movq 0x60(%rsp), %rax
movq %rax, 0x158(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x7370
movq %rax, 0x140(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x73a0
movq %rax, 0x138(%rsp)
movq 0x140(%rsp), %rdi
movq 0x138(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0xc960
movq %rax, 0x50(%rsp)
jmp 0x5c65
movq 0x50(%rsp), %rax
movq %rax, 0x148(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x7410
movq %rax, %rdi
leaq 0x158(%rsp), %rsi
callq 0xc9c0
movb %al, 0x4f(%rsp)
jmp 0x5c95
movb 0x4f(%rsp), %al
testb $0x1, %al
jne 0x5ca2
jmp 0x5dad
movq 0x2a0(%rsp), %rdi
callq 0x7370
movq %rax, 0x130(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0xc630
movl %eax, 0x48(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x7370
movq %rax, 0x128(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0xc630
movq %rax, %rsi
leaq 0x178(%rsp), %rdi
callq 0x7620
movl 0x48(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x5dad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
leaq 0x1b7(%rsp), %rdi
callq 0x75f0
jmp 0x6239
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
leaq 0x197(%rsp), %rdi
callq 0x75f0
jmp 0x622c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
leaq 0x173(%rsp), %rdi
callq 0x75f0
jmp 0x621f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x7590
jmp 0x621f
jmp 0x5daf
leaq 0x168(%rsp), %rdi
callq 0x7420
jmp 0x5b88
jmp 0x5dc3
movl $0x0, 0x124(%rsp)
movq 0x290(%rsp), %rax
movl $0x0, (%rax)
leaq 0x270(%rsp), %rdi
callq 0xc7a0
movq %rax, 0x118(%rsp)
leaq 0x270(%rsp), %rdi
callq 0xc7d0
movq %rax, 0x110(%rsp)
leaq 0x118(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0xca00
testb $0x1, %al
jne 0x5e24
jmp 0x6191
leaq 0x118(%rsp), %rdi
callq 0xca50
movq %rax, %rdi
callq 0xca40
movslq (%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x7350
movq %rax, %rcx
movq 0x38(%rsp), %rax
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, 0x10c(%rsp)
leaq 0x118(%rsp), %rdi
callq 0xca50
movq %rax, %rdi
callq 0xca40
movslq (%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x2a0(%rsp), %rdi
callq 0x7350
movq %rax, %rcx
movq 0x40(%rsp), %rax
cmpq %rcx, %rax
setae %al
andb $0x1, %al
movb %al, 0x10b(%rsp)
leaq 0x118(%rsp), %rdi
callq 0xca50
movq %rax, %rdi
callq 0xca60
movl (%rax), %eax
movl %eax, 0x104(%rsp)
leaq 0x118(%rsp), %rdi
callq 0xca50
movq %rax, %rdi
callq 0xca70
movl (%rax), %eax
movl %eax, 0x100(%rsp)
movl 0x260(%rsp), %eax
subl $0x1, %eax
addl 0x104(%rsp), %eax
subl 0x100(%rsp), %eax
movl %eax, 0xfc(%rsp)
testb $0x1, 0x10b(%rsp)
je 0x5fda
movslq 0x100(%rsp), %rsi
leaq 0x1d0(%rsp), %rdi
callq 0xca80
movq %rax, 0x30(%rsp)
jmp 0x5f2a
movq 0x30(%rsp), %rax
movq %rax, 0xf4(%rsp)
movl 0x29c(%rsp), %eax
movl %eax, 0x2c(%rsp)
movslq 0x10c(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movl 0x2c(%rsp), %ecx
movl %ecx, (%rax)
movslq 0x10c(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x7620
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
cmpl $0x0, 0xf4(%rsp)
jle 0x5fd5
movl 0xf4(%rsp), %eax
addl 0x29c(%rsp), %eax
movl %eax, 0x24(%rsp)
movslq 0x10c(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movl 0x24(%rsp), %ecx
movl %ecx, (%rax)
movl 0xf8(%rsp), %eax
movl %eax, 0x28(%rsp)
movslq 0x10c(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x7620
movl 0x28(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x617d
movslq 0x10c(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x7620
cmpl $-0x1, (%rax)
je 0x60c7
movslq 0x10c(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x7620
movslq (%rax), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movl (%rax), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
movslq 0x10c(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movq %rax, %rcx
movl 0x20(%rsp), %eax
cmpl (%rcx), %eax
jle 0x60c5
movslq 0x10c(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x7620
movslq (%rax), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movl (%rax), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
movslq 0x10c(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movl 0x18(%rsp), %ecx
movl %ecx, (%rax)
movslq 0x10c(%rsp), %rsi
leaq 0x178(%rsp), %rdi
callq 0x7620
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movslq 0x10c(%rsp), %rsi
leaq 0x198(%rsp), %rdi
callq 0x7620
movl 0x1c(%rsp), %ecx
movl %ecx, (%rax)
jmp 0x60c7
movslq 0x100(%rsp), %rax
movq %rax, 0x8(%rsp)
movslq 0x10c(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movq %rax, %rdi
leaq 0x10c(%rsp), %rsi
callq 0xc3c0
movq %rax, 0x10(%rsp)
jmp 0x6100
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rax
movq %rax, 0xec(%rsp)
leaq 0x1d0(%rsp), %rdi
leaq 0xec(%rsp), %rdx
callq 0xcb10
jmp 0x6129
movslq 0x10c(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movl (%rax), %eax
movq 0x290(%rsp), %rcx
cmpl (%rcx), %eax
jle 0x617b
movslq 0x10c(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x7620
movl (%rax), %ecx
movq 0x290(%rsp), %rax
movl %ecx, (%rax)
movl 0x10c(%rsp), %eax
movl %eax, 0x124(%rsp)
jmp 0x617d
jmp 0x617f
leaq 0x118(%rsp), %rdi
callq 0xcbc0
jmp 0x5df1
cmpq $0x0, 0x288(%rsp)
je 0x61d6
movq 0x2a0(%rsp), %rdi
movl 0x29c(%rsp), %esi
movl 0x124(%rsp), %ecx
movq 0x290(%rsp), %rax
movl (%rax), %r8d
movq 0x288(%rsp), %r9
leaq 0x198(%rsp), %rdx
callq 0x6fa0
jmp 0x61d4
jmp 0x61d6
leaq 0x178(%rsp), %rdi
callq 0x7590
leaq 0x198(%rsp), %rdi
callq 0x7590
leaq 0x1b8(%rsp), %rdi
callq 0x7590
leaq 0x1d0(%rsp), %rdi
callq 0xcbe0
leaq 0x270(%rsp), %rdi
callq 0xcbf0
addq $0x2a8, %rsp            # imm = 0x2A8
retq
leaq 0x198(%rsp), %rdi
callq 0x7590
leaq 0x1b8(%rsp), %rdi
callq 0x7590
leaq 0x1d0(%rsp), %rdi
callq 0xcbe0
leaq 0x270(%rsp), %rdi
callq 0xcbf0
movq 0x268(%rsp), %rdi
callq 0x2270