#include<bits/stdc++.h> using namespace std; struct Interval { Interval(int s, int e) { this->s = s; this->e = e; } int s, e; }; bool mycomp(Interval a, Interval b) { return a.s < b.s; } vector<Interval> mergeIntervals(vector<Interval> arr, int n) { sort(arr.begin(), arr.end(), mycomp); int index = 0; for (int i = 1; i < n; i++) { if (arr[index].e >= arr[i].s) { arr[index].e = max(arr[index].e, arr[i].e); arr[index].s = min(arr[index].s, arr[i].s); } else { index++; arr[index] = arr[i]; } } vector<Interval> tempIntervals; for (int i = 0; i <= index; i++) tempIntervals.push_back(arr[i]); return tempIntervals; } vector<Interval> makeIntersection(vector<Interval> arr1, vector<Interval> arr2) { vector<Interval> tempIntertvals; int i = 0, j = 0; int n = arr1.size(), m = arr2.size(); while (i < n && j < m) { int l = max(arr1[i].s, arr2[j].s); int r = min(arr1[i].e, arr2[j].e); if (l <= r) tempIntertvals.emplace_back(l, r); if (arr1[i].e < arr2[j].e) i++; else j++; } return tempIntertvals; } int main() { std::ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); vector<Interval> arr1; //niebieskie vector<Interval> arr2; //zolte vector<Interval> arr3; //czerwone int n, q, a, s, e; cin >> n >> q; for (int i = 0; i < q; i++) { cin >> s >> e; cin >> a; switch (a) { case 1: arr1.emplace_back(s, e); break; case 2: arr2.emplace_back(s, e); break; case 3: arr3.emplace_back(s, e); break; default: break; } } arr1 = mergeIntervals(arr1, arr1.size()); arr2 = mergeIntervals(arr2, arr2.size()); arr3 = mergeIntervals(arr3, arr3.size()); vector<Interval> resultArray = makeIntersection(arr1, arr2); long long result1 = 0; for(auto & i : resultArray) result1 += i.e - i.s + 1; resultArray = makeIntersection(arr3, resultArray); for(auto & i : resultArray) result1 -= i.e - i.s + 1; cout << result1; }
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 | #include<bits/stdc++.h> using namespace std; struct Interval { Interval(int s, int e) { this->s = s; this->e = e; } int s, e; }; bool mycomp(Interval a, Interval b) { return a.s < b.s; } vector<Interval> mergeIntervals(vector<Interval> arr, int n) { sort(arr.begin(), arr.end(), mycomp); int index = 0; for (int i = 1; i < n; i++) { if (arr[index].e >= arr[i].s) { arr[index].e = max(arr[index].e, arr[i].e); arr[index].s = min(arr[index].s, arr[i].s); } else { index++; arr[index] = arr[i]; } } vector<Interval> tempIntervals; for (int i = 0; i <= index; i++) tempIntervals.push_back(arr[i]); return tempIntervals; } vector<Interval> makeIntersection(vector<Interval> arr1, vector<Interval> arr2) { vector<Interval> tempIntertvals; int i = 0, j = 0; int n = arr1.size(), m = arr2.size(); while (i < n && j < m) { int l = max(arr1[i].s, arr2[j].s); int r = min(arr1[i].e, arr2[j].e); if (l <= r) tempIntertvals.emplace_back(l, r); if (arr1[i].e < arr2[j].e) i++; else j++; } return tempIntertvals; } int main() { std::ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); vector<Interval> arr1; //niebieskie vector<Interval> arr2; //zolte vector<Interval> arr3; //czerwone int n, q, a, s, e; cin >> n >> q; for (int i = 0; i < q; i++) { cin >> s >> e; cin >> a; switch (a) { case 1: arr1.emplace_back(s, e); break; case 2: arr2.emplace_back(s, e); break; case 3: arr3.emplace_back(s, e); break; default: break; } } arr1 = mergeIntervals(arr1, arr1.size()); arr2 = mergeIntervals(arr2, arr2.size()); arr3 = mergeIntervals(arr3, arr3.size()); vector<Interval> resultArray = makeIntersection(arr1, arr2); long long result1 = 0; for(auto & i : resultArray) result1 += i.e - i.s + 1; resultArray = makeIntersection(arr3, resultArray); for(auto & i : resultArray) result1 -= i.e - i.s + 1; cout << result1; } |