Advertisement
yungyao

tioj 1224 矩形覆蓋問題AC

Jan 13th, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. using namespace std;
  2. #include <iostream>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <utility>
  8. #include <bitset>
  9. #include <memory.h>
  10. #include <set>
  11.  
  12. #define pb push_back
  13. #define pii pair<int,int>
  14. #define F first
  15. #define S second
  16. #define LL long long
  17.  
  18. /*
  19. 8e7 so dian
  20. FHVirus so dian
  21. youou so dian
  22. KYW so dian
  23. hubert so dian
  24. jass so dian
  25. tingyu so dian
  26. */
  27.  
  28. //IO
  29. #define theyRSOOOOOOOOODIAN ios_base::sync_with_stdio(false),cin.tie(0);
  30. #define endl '\n'
  31.  
  32. //workspace
  33. #define mid (LB+RB)/2
  34.  
  35. struct BOUND{
  36.     int l,r;
  37.     int b;
  38.     int val;
  39. }boundary[200200];
  40.  
  41. pii seg[4000400];
  42. int lazy[4000400];
  43. void constructTree(int cur,int LB,int RB){
  44.     seg[cur].S = RB - LB + 1;
  45.  
  46.     if (LB == RB)
  47.         return;
  48.  
  49.     constructTree(cur*2,LB,mid);
  50.     constructTree(cur*2+1,mid+1,RB);
  51. }
  52.  
  53. void push(int cur,int LB,int RB){
  54.     if (lazy[cur]){
  55.         if (LB != RB){
  56.             lazy[cur*2] += lazy[cur];
  57.             lazy[cur*2+1] += lazy[cur];
  58.         }
  59.         seg[cur].F += lazy[cur];
  60.         lazy[cur] = 0;
  61.     }
  62. }
  63.  
  64. void modify(int l,int r,int val,int cur,int LB,int RB){
  65.     push(cur,LB,RB);
  66.     //cout << l << ' ' << r << ' ' << LB << ' ' << RB << ' ' << seg[cur].F << '\n';
  67.  
  68.     if (l == LB && r == RB){
  69.         if (LB != RB){
  70.             lazy[cur*2] += val;
  71.             lazy[cur*2+1] += val;
  72.         }
  73.         seg[cur].F += val;
  74.  
  75.         //cout << LB << ' ' << RB << ' ' << seg[cur].F << ' ' << seg[cur].S << '\n';
  76.  
  77.         return;
  78.     }
  79.  
  80.     if (r <= mid){
  81.         modify (l,r,val,cur*2,LB,mid);
  82.     }
  83.     else if (l > mid){
  84.         modify (l,r,val,cur*2+1,mid+1,RB);
  85.     }
  86.     else{
  87.         modify (l,mid,val,cur*2,LB,mid);
  88.         modify (mid+1,r,val,cur*2+1,mid+1,RB);
  89.     }
  90.  
  91.     push(cur*2,LB,mid);
  92.     push(cur*2+1,mid+1,RB);
  93.     if (seg[cur*2].F == seg[cur*2+1].F)
  94.         seg[cur] = {seg[cur*2].F,seg[cur*2].S+seg[cur*2+1].S};
  95.     else
  96.         seg[cur] = seg[cur*2].F < seg[cur*2+1].F ? seg[cur*2] : seg[cur*2+1];
  97.    
  98.     //cout << LB << ' ' << RB << ' ' << seg[cur].F << ' ' << seg[cur].S << '\n';
  99. }
  100.  
  101. int main(){
  102.     //theyRSOOOOOOOOODIAN
  103.     int n;
  104.  
  105.     cin >> n;
  106.     constructTree(1,1,1e6);
  107.  
  108.     for (int i=0;i<n*2;i += 2){
  109.         int l,r,d,u;
  110.  
  111.         cin >> l >> r >> d >> u;
  112.         boundary[i] = {l+1,r,d,1};
  113.         boundary[i+1] = {l+1,r,u,-1};
  114.     }
  115.     sort (boundary,boundary+2*n,[](BOUND A,BOUND B){return A.b < B.b;});
  116.  
  117.     LL area = 0;
  118.  
  119.     for (int i=0,ind=0;i<1000000;++i){
  120.         while (boundary[ind].b == i && ind < n*2){
  121.             modify (boundary[ind].l,boundary[ind].r,boundary[ind].val,1,1,1000000);
  122.             ++ind;
  123.         }
  124.  
  125.         area += (int)(1e6) - (seg[1].F ? 0 : seg[1].S);
  126.     }
  127.  
  128.     cout << area;
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement