|
| 1 | +// |
| 2 | +// Created by Alon on 02/06/2020. |
| 3 | +// |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <cstdlib> |
| 7 | +#include <cstring> |
| 8 | +#include <fstream> |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | +#include <algorithm> |
| 12 | +#include <set> |
| 13 | +#include <stack> |
| 14 | +#include <cassert> |
| 15 | + |
| 16 | +using namespace std; |
| 17 | +typedef long long ll; |
| 18 | + |
| 19 | +enum edge {END = 0, START = 1}; |
| 20 | +struct pillar { |
| 21 | + int y, x1, x2; |
| 22 | +}; |
| 23 | + |
| 24 | +int main() { |
| 25 | + int N, res = 0; |
| 26 | + cin >> N; |
| 27 | + vector<pillar> pillars(N); // Y X1 X2 |
| 28 | +// vector<pair<int, int>> starts(N); // X1 N |
| 29 | +// vector<pair<int, int>> ends(N); // X2 N |
| 30 | + vector<pair<int, int>> axis; // X2 N |
| 31 | + for (int i = 0; i < N; ++i) { |
| 32 | + cin >> pillars[i].y >> pillars[i].x1 >> pillars[i].x2; |
| 33 | +// starts[i] = {pillars[i].x1, i}; |
| 34 | +// ends[i] = {pillars[i].x2, i}; |
| 35 | + axis.push_back(make_pair(pillars[i].x1, i)); // start |
| 36 | + axis.push_back(make_pair(pillars[i].x2, i)); // end |
| 37 | + } |
| 38 | +// sort(starts.begin(), starts.end()); |
| 39 | +// sort(ends.begin(), ends.end()); |
| 40 | + sort(axis.begin(), axis.end()); |
| 41 | + set<int> current_heights = { 0 }; |
| 42 | + int i = -1; |
| 43 | + while (i + 1 < (int)axis.size()) { |
| 44 | + stack<int> pillar_to_remove; |
| 45 | + vector<int> pillar_to_add; |
| 46 | + do { |
| 47 | + ++i; |
| 48 | + int number_of_pillar = axis[i].second; |
| 49 | + if (axis[i].first == pillars[number_of_pillar].x1) { /// begin pillar |
| 50 | + pillar_to_add.push_back(number_of_pillar); |
| 51 | + } else { /// end pillar |
| 52 | + assert(axis[i].first == pillars[number_of_pillar].x2); |
| 53 | + pillar_to_remove.push(number_of_pillar); |
| 54 | + int len = pillars[number_of_pillar].y - *(--(current_heights.find(pillars[number_of_pillar].y))); // upper_bound is exclusive |
| 55 | + res += len; |
| 56 | + } |
| 57 | + } while (i + 1 < axis.size() && axis[i].first == axis[i+1].first); |
| 58 | + while (!pillar_to_remove.empty()) { |
| 59 | + current_heights.erase(pillars[pillar_to_remove.top()].y); |
| 60 | + pillar_to_remove.pop(); |
| 61 | + } |
| 62 | + for (int p : pillar_to_add) current_heights.insert(pillars[p].y); |
| 63 | + for (int p : pillar_to_add) { |
| 64 | + int len = pillars[p].y - *(--(current_heights.find(pillars[p].y))); // upper_bound is exclusive |
| 65 | + res += len; |
| 66 | + } |
| 67 | + } |
| 68 | + cout << res << endl; |
| 69 | + return 0; |
| 70 | +} |
0 commit comments