-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay5.cpp
More file actions
82 lines (73 loc) · 1.79 KB
/
Day5.cpp
File metadata and controls
82 lines (73 loc) · 1.79 KB
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
#include <bits/stdc++.h>
#define int long long
#define allr(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define ld long double
#define INF (int)(1e18)
#define MOD 1000000007
using namespace std;
void Part1() {
string s;
vector<pair<int, int> > v;
while (cin >> s && s != "-") {
string x = "", y = "";
int ind = s.find("-");
x = s.substr(0, ind);
y = s.substr(ind + 1);
int num1 = stoll(x), num2 = stoll(y);
v.push_back({num1, num2});
}
int num, cnt = 0;
while (cin >> num && num != -1) {
for (auto [l,r]: v) {
if (num >= l && num <= r) {
cnt++;
break;
}
}
}
cout << cnt << "\n";
}
void Part2() {
string s;
vector<pair<int, int> > v;
while (cin >> s && s != "-") {
string x = "", y = "";
int ind = s.find("-");
x = s.substr(0, ind);
y = s.substr(ind + 1);
int num1 = stoll(x), num2 = stoll(y);
v.push_back({num1, num2});
}
int num;
while (cin >> num && num != -1) {
}
sort(v.begin(), v.end());
int ans = 0, l = v[0].first, r = v[0].second;
for (int i = 0; i < v.size(); i++) {
if (v[i].first <= r) {
l = min(l, v[i].first);
r = max(r, v[i].second);
} else {
ans += r - l + 1;
//cout << l << " " << r << "\n";
l = v[i].first;
r = v[i].second;
}
}
ans += r - l + 1;
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("promote.in","r",stdin);
// freopen("promote.out","w",stdout);
int tests = 1;
// cin >> tests;
while (tests--) {
//Part1();
Part2();
}
}