Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using lli = long long;
- using pL = pair <lli, lli>;
- using pLL = pair <pL, pL>;
- int main(){
- int n;
- scanf("%d", &n);
- vector <pLL> event;
- for(int i=1;i<=n;i++){
- lli x1, x2, y1, y2;
- scanf("%lld%lld%lld%lld", &x1, &x2, &y1, &y2);
- event.push_back({{x1, 2}, {y1, y2}});
- event.push_back({{x2, 1}, {y1, y2}});
- }
- sort(event.begin(), event.end());
- multiset <pL> H;
- lli pre_x = -1, ans = 0;
- for(auto e: event){
- lli x, ev, y1, y2;
- x = e.first.first;
- ev = e.first.second;
- y1 = e.second.first;
- y2 = e.second.second;
- if(pre_x == -1){
- pre_x = x;
- H.insert({y2, 1}); // 1 delete
- H.insert({y1, 2}); // 2 insert
- continue;
- }
- // calculate area
- // find x
- lli sum_x = x - pre_x;
- // find y
- lli sum_y = 0, pre_y = -1, cnt = 0;
- for(auto h: H){
- if(h.second == 2) {
- if(pre_y == -1) pre_y = h.first;
- cnt ++;
- }
- else{
- cnt --;
- if(cnt == 0){
- sum_y += h.first - pre_y;
- pre_y = -1;
- }
- }
- }
- // area = x * y
- ans += (lli) sum_x * sum_y;
- // if ev = 1, delete H // ev = 2, insert H
- if(ev == 1){
- H.erase(H.find({y2, 1})); // 1 delete
- H.erase(H.find({y1, 2})); // 2 insert
- }
- else if(ev == 2){
- H.insert({y2, 1}); // 1 delete
- H.insert({y1, 2}); // 2 insert
- }
- // set variable
- pre_x = x;
- }
- printf("%lld", ans);
- return 0;
- }
RAW Paste Data
Copied