#include <vector>
#include <map>
#include <array>
#include <bitset>
#include <string>
#include <cstdio>
#include <iostream>
#include <ostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned char ubyte;
constexpr ld EPS = 1e-9;
constexpr ld PI = 3.141592653589793238462643383279502884L;
constexpr int INF = (int)1e9 + 5;
constexpr ll LINF = (ll)1e18 + 5;
bool eq(ld a, ld b) {
return fabs(a - b) < EPS;
}
template<class T>
T sqr(const T & x) { return x * x; }
template<class T>
ll abs(const T & x) { return x < 0 ? -x : x; }
template<class T>
ll myround(const T & x) { return x < 0 ? x - 0.5 : x + 0.5; }
template<class T>
bool chmin(T & x, const T & y) {
if (y < x) {
x = y;
return true;
}
return false;
}
template<class T>
ostream & operator <<(ostream & os, const vector<T> & v) {
os << "{";
for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
os << *it;
if ((it + 1) != v.end()) os << ", ";
}
os << "}";
return os;
}
template<class P, class Q>
ostream & operator <<(ostream & os, const pair<P, Q> & p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template<class T>
bool chmax(T & x, const T & y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template<class IntegerType = int>
IntegerType nextInt() {
IntegerType x = 0;
bool p = false;
char c;
do {
c = getchar();
} while (c <= 32);
if (c == '-') {
p = true;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return (p ? -x : x);
}
string nextToken() {
static const size_t MAX_LEN = 500500;
static char str[MAX_LEN];
scanf("%s", str);
return str;
}
//template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
//template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
#define all(a) (a).begin(), (a).end()
void no_solution()
{
puts("-1");
exit(0);
}
void testcase()
{
long long n = nextInt<long long>();
int m = nextInt<int>();
long long s = nextInt<long long>();
vector<pair<long long, long long>> p(m);
for ( auto& t : p )
{
t.first = nextInt<long long>();
t.second = nextInt<long long>();
}
sort(p.begin(), p.end());
long long prev = 1;
long long res = -1;
auto best_ans = [&](long long p)
{
if ( res == -1 )
res = p;
else if ( std::abs(p - s) < std::abs(res - s) )
res = p;
else if ( std::abs(p - s) == std::abs(res - s) && p < res )
res = p;
};
for ( auto& t : p )
{
long long l = prev;
long long r = t.first - 1;
if ( l > r )
{
prev = t.second + 1;
continue;
}
best_ans(l);
best_ans(r);
prev = t.second + 1;
}
long long l = prev;
long long r = n;
if ( l <= r )
{
best_ans(l);
best_ans(r);
}
std::cout << res << std::endl;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int t = 1;
while (t--)
testcase();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #include <vector> #include <map> #include <array> #include <bitset> #include <string> #include <cstdio> #include <iostream> #include <ostream> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef unsigned char ubyte; constexpr ld EPS = 1e-9; constexpr ld PI = 3.141592653589793238462643383279502884L; constexpr int INF = (int)1e9 + 5; constexpr ll LINF = (ll)1e18 + 5; bool eq(ld a, ld b) { return fabs(a - b) < EPS; } template<class T> T sqr(const T & x) { return x * x; } template<class T> ll abs(const T & x) { return x < 0 ? -x : x; } template<class T> ll myround(const T & x) { return x < 0 ? x - 0.5 : x + 0.5; } template<class T> bool chmin(T & x, const T & y) { if (y < x) { x = y; return true; } return false; } template<class T> ostream & operator <<(ostream & os, const vector<T> & v) { os << "{"; for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) { os << *it; if ((it + 1) != v.end()) os << ", "; } os << "}"; return os; } template<class P, class Q> ostream & operator <<(ostream & os, const pair<P, Q> & p) { return os << "(" << p.first << ", " << p.second << ")"; } template<class T> bool chmax(T & x, const T & y) { if (x < y) { x = y; return true; } return false; } template<class IntegerType = int> IntegerType nextInt() { IntegerType x = 0; bool p = false; char c; do { c = getchar(); } while (c <= 32); if (c == '-') { p = true; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return (p ? -x : x); } string nextToken() { static const size_t MAX_LEN = 500500; static char str[MAX_LEN]; scanf("%s", str); return str; } //template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; //template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; #define all(a) (a).begin(), (a).end() void no_solution() { puts("-1"); exit(0); } void testcase() { long long n = nextInt<long long>(); int m = nextInt<int>(); long long s = nextInt<long long>(); vector<pair<long long, long long>> p(m); for ( auto& t : p ) { t.first = nextInt<long long>(); t.second = nextInt<long long>(); } sort(p.begin(), p.end()); long long prev = 1; long long res = -1; auto best_ans = [&](long long p) { if ( res == -1 ) res = p; else if ( std::abs(p - s) < std::abs(res - s) ) res = p; else if ( std::abs(p - s) == std::abs(res - s) && p < res ) res = p; }; for ( auto& t : p ) { long long l = prev; long long r = t.first - 1; if ( l > r ) { prev = t.second + 1; continue; } best_ans(l); best_ans(r); prev = t.second + 1; } long long l = prev; long long r = n; if ( l <= r ) { best_ans(l); best_ans(r); } std::cout << res << std::endl; } int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); #endif int t = 1; while (t--) testcase(); } |
English