Problem p02661 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <queue>
using namespace std;

using ull = unsigned long long;
using ll = long long;

template <class T> // 自定义读入函数
inline void read(T &a) {
  register T x = 0, flag = 1;
  register char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (x = x * 10 + (ch & 15), isdigit(ch = getchar()))
    ;
  a = x * flag;
}

template <class T> // 自定义写入函数
inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

template <class T> inline bool Chkmax(T &x, const T &y) {
  return x < y ? x = y, true : false;
} // 自定义比较函数
template <class T> inline bool Chkmin(T &x, const T &y) {
  return x > y ? x = y, true : false;
} // 自定义比较函数

#define For(i, a, b) for (int i = (a); i <= (b); ++i) // 从小到大重复
#define Rep(i, a, b) for (int i = (a); i >= (b); --i) // 从大到小重复
#define For_edge(x) for (int i = head[x]; i; i = net[i])
#define mem0(a) memset(a, 0, sizeof a) // 内存复制

#if 0
int main()
{
	priority_queue<ll> minMin, minMax;
	priority_queue<ll, vector<ll>, greater<ll> > maxMin, maxMax;
	int N;
	scanf("%d", &N);
	
	for(int i = 0; i < N; i++)
	{
		ll min, max;
		scanf("%lld %lld", &min, &max);
		if(minMin.size() > maxMin.size())
			maxMin.push(min);
		else
			minMin.push(min);
		if(minMax.size() > maxMax.size())
			maxMax.push(max);
		else
			minMax.push(max);
	}
	ll result = 0;
	if(N == 1)
		result = minMax.top() - minMin.top() + 1;
	else if(N == 0)
		result = 0;
	else if(N & 1)
	{
		ll myMin = min(minMin.top(),maxMin.top()), myMax = min(minMax.top(),maxMax.top());
		result = (myMax - myMin + 1);
	}
	else
		result = (minMax.top() + maxMax.top()) - (minMin.top() + maxMin.top()) + 1;
	cout << result << endl;
	return 0;
}

#endif

#if 1

int main() {
  int n;
  cin >> n;
  vector<long long> small;
  vector<long long> large;
  for (int i = 0; i < n; ++i) {
    long long a, b;
    cin >> a >> b;
    small.push_back(a);
    large.push_back(b);
  }

  sort(small.begin(), small.end());
  sort(large.begin(), large.end());
  if (n % 2 == 1) {
    long long d = large[(n) / 2] - small[(n) / 2] + 1;
    cout << d << endl;
  } else {
    long long a, b;
    a = small[n / 2 - 1] + small[n / 2];

    b = large[n / 2 - 1] + large[n / 2];
    cout << b - a + 1 << endl;
  }
  return 0;
}

#endif

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
using namespace std;

using ull = unsigned long long;
using ll = long long;

template <class T> // 自定义读入函数
inline void read(T &a) {
  register T x = 0, flag = 1;
  register char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (x = x * 10 + (ch & 15), isdigit(ch = getchar()))
    ;
  a = x * flag;
}

template <class T> // 自定义写入函数
inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

template <class T> inline bool Chkmax(T &x, const T &y) {
  return x < y ? x = y, true : false;
} // 自定义比较函数
template <class T> inline bool Chkmin(T &x, const T &y) {
  return x > y ? x = y, true : false;
} // 自定义比较函数

#define For(i, a, b) for (int i = (a); i <= (b); ++i) // 从小到大重复
#define Rep(i, a, b) for (int i = (a); i >= (b); --i) // 从大到小重复
#define For_edge(x) for (int i = head[x]; i; i = net[i])
#define mem0(a) memset(a, 0, sizeof a) // 内存复制

int main() {
  priority_queue<ll> minMin, minMax;
  priority_queue<ll, vector<ll>, greater<ll>> maxMin, maxMax;
  ll N;
  scanf("%d", &N);

  for (int i = 0; i < N; i++) {
    ll min, max;
    scanf("%lld %lld", &min, &max);
    if (minMin.size() > maxMin.size())
      maxMin.push(min);
    else
      minMin.push(min);
    if (minMax.size() > maxMax.size())
      maxMax.push(max);
    else
      minMax.push(max);
  }
  ll result = 0;
  if (N == 1)
    result = minMax.top() - minMin.top() + 1;
  else if (N == 0)
    result = 0;
  else if (N & 1) {
    ll myMin = min(minMin.top(), maxMin.top()),
       myMax = min(minMax.top(), maxMax.top());
    result = (myMax - myMin + 1);
  } else
    result = (minMax.top() + maxMax.top()) - (minMin.top() + maxMin.top()) + 1;
  cout << result << endl;
  return 0;
}