-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKattis Platforme.cpp
70 lines (66 loc) · 2.28 KB
/
Kattis Platforme.cpp
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
//
// Created by Alon on 02/06/2020.
//
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <cassert>
using namespace std;
typedef long long ll;
enum edge {END = 0, START = 1};
struct pillar {
int y, x1, x2;
};
int main() {
int N, res = 0;
cin >> N;
vector<pillar> pillars(N); // Y X1 X2
// vector<pair<int, int>> starts(N); // X1 N
// vector<pair<int, int>> ends(N); // X2 N
vector<pair<int, int>> axis; // X2 N
for (int i = 0; i < N; ++i) {
cin >> pillars[i].y >> pillars[i].x1 >> pillars[i].x2;
// starts[i] = {pillars[i].x1, i};
// ends[i] = {pillars[i].x2, i};
axis.push_back(make_pair(pillars[i].x1, i)); // start
axis.push_back(make_pair(pillars[i].x2, i)); // end
}
// sort(starts.begin(), starts.end());
// sort(ends.begin(), ends.end());
sort(axis.begin(), axis.end());
set<int> current_heights = { 0 };
int i = -1;
while (i + 1 < (int)axis.size()) {
stack<int> pillar_to_remove;
vector<int> pillar_to_add;
do {
++i;
int number_of_pillar = axis[i].second;
if (axis[i].first == pillars[number_of_pillar].x1) { /// begin pillar
pillar_to_add.push_back(number_of_pillar);
} else { /// end pillar
assert(axis[i].first == pillars[number_of_pillar].x2);
pillar_to_remove.push(number_of_pillar);
int len = pillars[number_of_pillar].y - *(--(current_heights.find(pillars[number_of_pillar].y))); // upper_bound is exclusive
res += len;
}
} while (i + 1 < axis.size() && axis[i].first == axis[i+1].first);
while (!pillar_to_remove.empty()) {
current_heights.erase(pillars[pillar_to_remove.top()].y);
pillar_to_remove.pop();
}
for (int p : pillar_to_add) current_heights.insert(pillars[p].y);
for (int p : pillar_to_add) {
int len = pillars[p].y - *(--(current_heights.find(pillars[p].y))); // upper_bound is exclusive
res += len;
}
}
cout << res << endl;
return 0;
}