// Bartłomiej Sitnik
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n;
long long s;
int m;
long long l, r;
cin >> n >> m >> s;
set<pair<long long, long long>> pary;
pary.insert({1ll, n});
for (int i = 0; i < m; i++)
{
cin >> l >> r;
auto it = pary.upper_bound({l, r});
it--;
if (it == pary.end())
continue;
if (it->first == l and it->second == r)
{
pary.erase(it);
continue;
}
if (it->first < l and it->second > r)
{
pair<long long, long long> p1, p2;
p1 = {it->first, l-1};
p2 = {r+1, it->second};
pary.erase(it);
pary.insert(p1);
pary.insert(p2);
continue;
}
if (it->first <= l and it->second > r)
{
pair<long long, long long> p2;
p2 = {r+1, it->second};
pary.erase(it);
pary.insert(p2);
continue;
}
if (it->first < l and it->second >= r)
{
pair<long long, long long> p1;
p1 = {it->first, l-1};
pary.erase(it);
pary.insert(p1);
continue;
}
if (it->first <= l and it->second < r)
{
if (it->first < l)
pary.insert({it->first, l-1});
while (it != pary.end() and it->second <= r)
{
auto it2 = it;
it++;
pary.erase(it2);
}
if (it->second > r and it->first <= r)
{
pary.insert({r+1, it->second});
pary.erase(it);
}
continue;
}
}
auto it = pary.lower_bound({s, s});
it--;
auto it2 = pary.lower_bound({s, s});
if (it2 != pary.end() and s - it->second > it2->first - s)
cout << it2->first;
else
cout << it->second;
return 0;
}
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 | // Bartłomiej Sitnik #include <bits/stdc++.h> using namespace std; int main() { long long n; long long s; int m; long long l, r; cin >> n >> m >> s; set<pair<long long, long long>> pary; pary.insert({1ll, n}); for (int i = 0; i < m; i++) { cin >> l >> r; auto it = pary.upper_bound({l, r}); it--; if (it == pary.end()) continue; if (it->first == l and it->second == r) { pary.erase(it); continue; } if (it->first < l and it->second > r) { pair<long long, long long> p1, p2; p1 = {it->first, l-1}; p2 = {r+1, it->second}; pary.erase(it); pary.insert(p1); pary.insert(p2); continue; } if (it->first <= l and it->second > r) { pair<long long, long long> p2; p2 = {r+1, it->second}; pary.erase(it); pary.insert(p2); continue; } if (it->first < l and it->second >= r) { pair<long long, long long> p1; p1 = {it->first, l-1}; pary.erase(it); pary.insert(p1); continue; } if (it->first <= l and it->second < r) { if (it->first < l) pary.insert({it->first, l-1}); while (it != pary.end() and it->second <= r) { auto it2 = it; it++; pary.erase(it2); } if (it->second > r and it->first <= r) { pary.insert({r+1, it->second}); pary.erase(it); } continue; } } auto it = pary.lower_bound({s, s}); it--; auto it2 = pary.lower_bound({s, s}); if (it2 != pary.end() and s - it->second > it2->first - s) cout << it2->first; else cout << it->second; return 0; } |
English