[PKU2398]Toy Storage

基本上同PKU2318。不同的地方在于读入的分割线段是无序的,需要先排序才能二分;统计的对象稍有变动。
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
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

struct Point
{
  int x, y;
  Point(const int& _x = 0, const int& _y = 0): x(_x), y(_y) { }
};
int n, m, c[1002], box[1002];
Point p, UpLeft, DownRight;
pair<int, int> l[1002];
inline bool operator<(const pair<int, int> &lhs, const pair<int, int> &rhs)
{ return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second); }
inline int CrossProduct(const Point &A, const Point &B, const Point &C, const Point &D)
{
  const int x1 = B.x-A.x, y1 = B.y-A.y;
  const int x2 = D.x-C.x, y2 = D.y-C.y;
  return x1*y2-y1*x2;
}
int main()
{
  ios::sync_with_stdio(false);
  while (cin >> n, n)
  {
    memset(c, 0, sizeof(c));
    memset(box, 0, sizeof(box));
    cin >> m >> UpLeft.x >> UpLeft.y >> DownRight.x >> DownRight.y;
    for (int i = 0; i < n; ++i)
      cin >> l[i].first >> l[i].second;
    sort(l, l+n);
    l[n].first = l[n].second = DownRight.x;

    for (int i = 0; i < m; ++i)
    {
      cin >> p.x >> p.y;
      int lef = -1, rig = n;
      while (rig-lef > 1)
      {
        int mid = (lef+rig)>>1;
        Point p1(l[mid].first, UpLeft.y);
        Point p2(l[mid].second, DownRight.y);
        if (CrossProduct(p1, p2, p1, p) > 0)
          lef = mid;
        else
          rig = mid;
      }
      ++c[rig];
    }
    for (int i = 0; i <= n; ++i) ++box[c[i]];
    cout << "Box" << endl;
    for (int i = 1; i <= m; ++i)
      if (box[i])
        cout << i << ": " << box[i] << endl;
  }
}

Comments