Advertisement
Guest User

Untitled

a guest
Sep 21st, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long int
  3. #define double long double
  4. #define endl '\n'
  5. #define all(c) c.begin(),c.end()
  6. #define mp(x,y) make_pair(x,y)
  7. #define eb emplace_back
  8. #define tr(k,st,en) for(int k = st; k <= en ; k++)
  9. #define trb(k,en,st) for(int k = en; k >= st ; k--)
  10. #define test int TN; cin>>TN; tr(T,1,TN)
  11. #define mxe(c) max_element(all(c))
  12. #define mne(c) min_element(all(c))
  13. using namespace std;
  14.  
  15. template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
  16. template < typename T_container, typename T = typename enable_if < !is_same<T_container, string>::value, typename T_container::value_type >::type > ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
  17.  
  18. void dbg_out() { cerr << endl; }
  19. template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
  20.  
  21. #ifdef LOCAL
  22. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  23. #else
  24. #define dbg(...)
  25. #endif
  26.  
  27. typedef pair<int, int> pr;
  28. typedef vector<int> vi;
  29. typedef vector<vi> vvi;
  30. typedef vector<pr> vpr;
  31.  
  32. //const int mod = 1e9+7;
  33. const int inf = 9e18;
  34.  
  35. struct Pt {
  36.     int x, y;
  37.     Pt(int _x, int _y) : x(_x), y(_y) {}
  38.     Pt operator - (Pt b) {
  39.         return Pt(x - b.x, y - b.y);
  40.     }
  41.     int isRight(Pt a, Pt b) {
  42.         Pt p = *this - a;
  43.         Pt q = b - a;
  44.         int l = p.x * q.y - p.y * q.x;
  45.         if (l > 0) return 1;
  46.         if (l < 0) return -1;
  47.         return 0;
  48.     }
  49. };
  50.  
  51. bool Intersect(Pt a, Pt b, Pt c, Pt d) {
  52.     int l1, l2, l3; l1 = l2 = 0; l3 = 1;
  53.     if (a.isRight(c, d) * b.isRight(c, d) <= 0) l1 = 1;
  54.     if (c.isRight(a, b) * d.isRight(a, b) <= 0) l2 = 1;
  55.     if (max(a.x, b.x) < min(c.x, d.x) or max(a.y, b.y) < min(c.y, d.y) or
  56.             max(c.x, d.x) < min(a.x, b.x) or max(c.y, d.y) < min(a.y, b.y) ) l3 = 0;
  57.     return l1 and l2 and l3;
  58. }
  59. typedef pair<pair<Pt, Pt>, int> prt;
  60.  
  61. int solve(vector<prt> in, int k) {
  62.     int n = in.size();
  63.     sort(all(in), [] (prt a, prt b) {
  64.         return a.second < b.second;
  65.     });
  66.     int l = 0, r = 0;
  67.     int ans = inf;
  68.     vi tmp(n + 1, 0);
  69.     int cur = 0;
  70.     while (l < n) {
  71.         while (r < n) {
  72.             if (cur >= k) {
  73.                 break;
  74.             }
  75.             for (int i = l; i < r; i++) {
  76.                 if (Intersect(in[i].first.first, in[i].first.second, in[r].first.first, in[r].first.second)) {
  77.                     tmp[i]++;
  78.                     cur++;
  79.                 }
  80.             }
  81.             r++;
  82.  
  83.         }
  84.         if (cur >= k) {
  85.             ans = min(ans, in[r - 1].second - in[l].second);
  86.         }
  87.         cur -= tmp[l];
  88.         l++;
  89.     }
  90.     return ans == inf ? -1 : ans;
  91. }
  92.  
  93. void func() {
  94.     int t;
  95.     cin >> t;
  96.     tr(i, 1, t) {
  97.         int n, k ;
  98.         cin >> n >> k;
  99.         vector<prt> in;
  100.         tr(i, 1, n) {
  101.             int x1, y1, x2, y2, c ;
  102.             cin >> x1 >> y1 >> x2 >> y2 >> c;
  103.             in.eb(mp(Pt(x1, y1), Pt(x2, y2)), c);
  104.         }
  105.         int s = solve(in, k);
  106.         cout  << s << endl;
  107.     }
  108. }
  109.  
  110. int32_t main() {
  111.     std::ios::sync_with_stdio(false);
  112.     cin.tie(NULL); cout.tie(NULL);
  113. //    cout<<"----START-----"<<endl;
  114.     func();
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement