Language: C++

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <list>
using namespace std;

void getP(vector<int> &p1, vector<int> &p2, int n) {
	int buf;
	for (int i = 0; i < n/2; i++) {
		scanf("%d", &buf);
		p1[i] = buf;
		scanf("%d", &buf);
		p2[i] = buf;
	}
}

list<pair<int, int>> mergeVector(list<pair<int, int>> &v1, list<pair<int, int>> &v2) {
	int n = v1.size() + v2.size();
	list<pair<int, int>>::iterator it1 = v1.begin();
	list<pair<int, int>>::iterator it2 = v2.begin();
	pair<int, int> buf;

	list<pair<int, int>> result;
	for (int i = 0; i < n; i++) {
		if (it1 != v1.end()) {
			if (it2 != v2.end()) {
				if (*it1 < *it2) {
					buf = *it1;
					result.push_back(make_pair(buf.first, buf.second));
					it1++;
				}
				else {
					buf = *it2;
					result.push_back(make_pair(buf.first, buf.second));
					it2++;
				}
			}
			else {
				buf = *it1;
				result.push_back(make_pair(buf.first, buf.second));
				it1++;
			}
		}
		else {
			buf = *it2;
			result.push_back(make_pair(buf.first, buf.second));
			it2++;
		}
	}
	return result;
}

list<pair<int, int>> getSortedPairs(const vector<int> &p1, const vector<int> &p2, int start, int end) {
	list<pair<int, int>> result;
	if (start + 1 == end) {
		if (start%2 == 0) {
			result.push_back(make_pair(p1[start/2], p2[start/2]));
		}
		else {
			result.push_back(make_pair(p2[start/2], p1[start/2+1]));
		}
		return result;
	}
	else if (start >= end) {
		return result;
	}

	if (start % 2 == 0) {
		int p1_ind = 0;
		int p1_min = 999999;
		for (int i = start / 2; i <= end / 2; i++) {
			if (p1[i] < p1_min) {
				p1_min = p1[i];
				p1_ind = i;
			}
		}
		int p2_ind = 0;
		int p2_min = 999999;
		for (int i = p1_ind; i <= end / 2; i++) {
			if (p2[i] < p2_min) {
				p2_min = p2[i];
				p2_ind = i;
			}
		}
		result = getSortedPairs(p1, p2, start, p1_ind*2-1);
		result = mergeVector(result, getSortedPairs(p1, p2, p1_ind * 2 + 1, p2_ind * 2+ 1 -1));
		result = mergeVector(result, getSortedPairs(p1, p2, p2_ind * 2 + 1 +1, end));
		result.push_front(make_pair(p1_min, p2_min));
	}
	else {
		int p2_ind = 0;
		int p2_min = 999999;
		for (int i = start/2; i < end / 2; i++) {
			if (p2[i] < p2_min) {
				p2_min = p2[i];
				p2_ind = i;
			}
		}
		int p1_ind = 0;
		int p1_min = 999999;
		for (int i = start / 2 +1; i <= end / 2; i++) {
			if (p1[i] < p1_min) {
				p1_min = p1[i];
				p1_ind = i;
			}
		}

		result = getSortedPairs(p1, p2, start, p2_ind * 2 +1 - 1);
		result = mergeVector(result, getSortedPairs(p1, p2, p2_ind * 2 + 1 + 1, p1_ind * 2 - 1));
		result = mergeVector(result, getSortedPairs(p1, p2, p1_ind * 2 + 1, end));
		result.push_front(make_pair(p2_min, p1_min));
	}

	return result;
}

int main(void) {
	int N;
	scanf("%d", &N);
	vector<int> p1 = vector<int>(N/2, 0);
	vector<int> p2 = vector<int>(N/2, 0);
	getP(p1, p2, N);
	auto result = getSortedPairs(p1, p2, 0, N - 1);
	for (auto it = result.begin(); it != result.end(); it++) {
		printf("%d %d ", (*it).first, (*it).second);
	}
	printf("\n");

	return 0;
}

Compiler output:
a.cc: In function 'std::__cxx11::list<std::pair<int, int> > getSortedPairs(const std::vector<int>&, const std::vector<int>&, int, int)':
a.cc:90:60: error: cannot bind non-const lvalue reference of type 'std::__cxx11::list<std::pair<int, int> >&' to an rvalue of type 'std::__cxx11::list<std::pair<int, int> >'
   90 |                 result = mergeVector(result, getSortedPairs(p1, p2, p1_ind * 2 + 1, p2_ind * 2+ 1 -1));
      |                                              ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:21:82: note:   initializing argument 2 of 'std::__cxx11::list<std::pair<int, int> > mergeVector(std::__cxx11::list<std::pair<int, int> >&, std::__cxx11::list<std::pair<int, int> >&)'
   21 | list<pair<int, int>> mergeVector(list<pair<int, int>> &v1, list<pair<int, int>> &v2) {
      |                                                            ~~~~~~~~~~~~~~~~~~~~~~^~
a.cc:91:60: error: cannot bind non-const lvalue reference of type 'std::__cxx11::list<std::pair<int, int> >&' to an rvalue of type 'std::__cxx11::list<std::pair<int, int> >'
   91 |                 result = mergeVector(result, getSortedPairs(p1, p2, p2_ind * 2 + 1 +1, end));
      |                                              ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:21:82: note:   initializing argument 2 of 'std::__cxx11::list<std::pair<int, int> > mergeVector(std::__cxx11::list<std::pair<int, int> >&, std::__cxx11::list<std::pair<int, int> >&)'
   21 | list<pair<int, int>> mergeVector(list<pair<int, int>> &v1, list<pair<int, int>> &v2) {
      |                                                            ~~~~~~~~~~~~~~~~~~~~~~^~
a.cc:113:60: error: cannot bind non-const lvalue reference of type 'std::__cxx11::list<std::pair<int, int> >&' to an rvalue of type 'std::__cxx11::list<std::pair<int, int> >'
  113 |                 result = mergeVector(result, getSortedPairs(p1, p2, p2_ind * 2 + 1 + 1, p1_ind * 2 - 1));
      |                                              ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:21:82: note:   initializing argument 2 of 'std::__cxx11::list<std::pair<int, int> > mergeVector(std::__cxx11::list<std::pair<int, int> >&, std::__cxx11::list<std::pair<int, int> >&)'
   21 | list<pair<int, int>> mergeVector(list<pair<int, int>> &v1, list<pair<int, int>> &v2) {
      |                                                            ~~~~~~~~~~~~~~~~~~~~~~^~
a.cc:114:60: error: cannot bind non-const lvalue reference of type 'std::__cxx11::list<std::pair<int, int> >&' to an rvalue of type 'std::__cxx11::list<std::pair<int, int> >'
  114 |                 result = mergeVector(result, getSortedPairs(p1, p2, p1_ind * 2 + 1, end));
      |                                              ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:21:82: note:   initializing argument 2 of 'std::__cxx11::list<std::pair<int, int> > mergeVector(std::__cxx11::list<std::pair<int, int> >&, std::__cxx11::list<std::pair<int, int> >&)'
   21 | list<pair<int, int>> mergeVector(list<pair<int, int>> &v1, list<pair<int, int>> &v2) {
      |                                                            ~~~~~~~~~~~~~~~~~~~~~~^~