题目
Calculate the number of toys that land in each bin of a partitioned toy box.
Mom and dad have a problem - their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.
John’s parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.
Input
The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1,y1) and (x2,y2), respectively. The following n lines contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.
Output
The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.
Sample Input
5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0
Sample Output
0: 2
1: 1
2: 1
3: 1
4: 0
5: 1
0: 2
1: 2
2: 2
3: 2
4: 2
Hint
As the example illustrates, toys that fall on the boundary of the box are “in” the box.
解释与代码
抄了一下点和线的模板,然后根据叉积的性质判断在线的哪一边,最后二分即可,其实我写了非二分的版本,但是不知道为什么就是wa,可能是哪里错了吧
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ppb pop_back
#define lbnd lower_bound
#define ubnd upper_bound
#define endl '\n'
#define trav(a, x) for(auto& a : x)
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ini(a) memset(a,0,sizeof(a))
#define case ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x) x&(-x)
#define pr printf
#define sc scanf
#define _ 0
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
(void)(cout << "L" << __LINE__ \
<< ": " << #x << " = " << (x) << '\n')
#define TIE \
cin.tie(0);cout.tie(0);\
ios::sync_with_stdio(false);
//#define long long int
//using namespace __gnu_pbds;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
x *= f;
return;
}
inline void write(long long x) {
if(x<0) putchar('-'), x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
putchar('\n');
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 5005;
const ll N = 5;
int cmp (double x) {
if (fabs(x) < eps) return 0;
if (x > 0) return 1;
return -1;
}
const double pi = acos(-1.0);
inline double sqr (double x) {
return x*x;
}
struct point {
double x, y;
point() {}
point(double a, double b) : x(a), y(b) {}
void input() {
scanf("%lf%lf", &x, &y);
// cin>>x>>y;
}
friend point operator + (const point &a, const point &b) {
return point(a.x + b.x, a.y + b.y);
}
friend point operator - (const point &a, const point &b) {
return point(a.x - b.x, a.y - b.y);
}
friend bool operator == (const point &a, const point &b) {
return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
}
friend point operator * (const point &a, const double &b) {
return point(a.x * b, a.y * b);
}
friend point operator * (const double &a, const point &b) {
return point(a * b.x, a * b.y);
}
friend point operator / (const point &a, const double &b) {
return point(a.x / b, a.y / b);
}
double norm() {//计算长度
return sqrt(sqr(x) + sqr(y));
}
};
//计算两个向量的叉积
double det (const point &a, const point &b) {
return a.x*b.y - a.y*b.x;
}
//计算两个向量的点积
double dot (const point &a, const point &b) {
return a.x*b.x + a.y*b.y;
}
double dist (const point &a, const point &b) {
return (a - b).norm();
}
point rotate_point (const point &p, double A) {
double tx = p.x, ty = p.y;
return point (tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A));
}
struct line {
point a, b;
line() {}
line (point x, point y): a(x), b(y) {};
};
line point_make_line(const point a, const point b) {
return line(a, b);
}
//vector<vector<point> > vvp(5001);
vector<line> vl(maxn);
int ans[maxn];
void solve(){
int n, m;
double x1, y1, x2, y2, u, l;
while (sc("%d", &n) && n!=0) {
ini(ans);
sc("%d%lf%lf%lf%lf",&m, &x1, &y1, &x2, &y2);
for (int o=0; o<n; o++) {
sc("%lf%lf",&u,&l);
vl[o] = point_make_line(point(u, y1), point (l,y2));
// cout<<u<<" "<<y1<<" "<<l<<" "<<y2<<endl;
}
for (int o=0; o<m; o++) {
sc("%lf%lf",&u,&l);
int L = 0, R = n;
while (L < R) {
int mid = (L + R) >> 1;
point p(u-vl[mid].a.x, l-vl[mid].a.y);//计算向量
if (det(p, vl[mid].b - vl[mid].a) < 0) {
L = mid + 1;
} else {
R = mid;
}
}
ans[L]++;
}
for (int i=0; i<=n; i++) {
pr("%d: %d\n",i, ans[i]);
}
cout<<endl;
}
}
int main()
{
// TIE;
#ifndef ONLINE_JUDGE
// freopen ("in.txt" , "r", stdin );
// freopen ("out.txt", "w", stdout);
#else
#endif
solve();
// case{solve();}
// case{cout<<"Case "<<Q<<":"<<endl;solve();}
return ~~(0^_^0);
}