Advertisement
Guest User

Red Blue Line Segments

a guest
Jul 21st, 2016
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp> // Common file
  3. #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
  4. #include <ext/pb_ds/detail/standard_policies.hpp>
  5.  
  6. #define INF 2139062143
  7. #define EPS 0.0000001
  8. #define debug(x) cout << #x << " = " << x << endl
  9. #define fori(i, ini, lim) for(int i= int(ini); i<(int)lim; ++i)
  10. #define ford(i, ini, lim) for(int i= int(ini); i>=(int)lim; --i)
  11. using namespace std;
  12. using namespace __gnu_pbds;
  13. typedef long long ll;
  14. typedef long double ld;
  15. typedef pair<double, double> ii;
  16. typedef pair<double, int> id;
  17. typedef pair<id, ii> event;
  18. typedef tree<
  19.     pair<double, int>,
  20.     null_type,
  21.     less<pair<double, int> >,
  22.     rb_tree_tag,
  23.     tree_order_statistics_node_update>
  24. ordered_set;
  25. int cnt = 0;
  26.  
  27. int main(){
  28.     int n;
  29.     while(scanf("%d ", &n) == 1){
  30.         ordered_set active;
  31.         vector<event> v;
  32.         fori(i,0,n){
  33.             double x1, x2, y; scanf("%lf %lf %lf ", &x1,&x2,&y);
  34.             v.push_back(make_pair(ii(x1,0),ii(y,x2)));
  35.             v.push_back(make_pair(ii(x2,2),ii(y,x1)));
  36.         }
  37.         fori(i,0,n){
  38.             double y1, y2, x; scanf("%lf %lf %lf ",&y1,&y2,&x);
  39.             if(y1 > y2) swap(y1, y2);
  40.             v.push_back(make_pair(ii(x,1),ii(y1,y2)));
  41.         }
  42.         sort(v.begin(), v.end());
  43.         ll ans = 0;
  44.         fori(i,0,v.size()){
  45.             event cur = v[i];
  46.             int type = cur.first.second;
  47.             if(type == 0){
  48.                 active.insert(id(cur.second.first, cnt++));
  49.             }
  50.             if(type == 2){
  51.                 active.erase(active.lower_bound(id(cur.second.first,0)));
  52.             }
  53.             if(type == 1){
  54.                 int start = active.order_of_key(id(cur.second.first,INF));
  55.                 int end = active.order_of_key(id(cur.second.second+EPS,INF));
  56.                 ans += end - start;
  57.             }
  58.         }
  59.         printf("%lld\n", ans);
  60.     }
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement