HDU 3890
来源:互联网 发布:数据有效性的使用方法 编辑:程序博客网 时间:2024/06/17 13:50
题意:一个二维平面上有N个点,每个点都对应一个实数。有M次查询,每次查询一个矩形区域内的点的数量以及点所对应的实数之和。N<=60000,M<=20000。
解法:数据量很大,一定要把每次查询操作时间复杂度压缩到O(logn)以内,否则肯定会超时。开始想用二维线段树,或者对一维哈希,另一维暴力。但是点的数量太多,分布可能非常离散,二维线段树或者暴力哈希什么的肯定都是行不通的。那如何解呢?假设某次查询范围是 lx,ly,ux,uy (lx<=ux, ly<=uy)。如果我们统计出y范围为ly-uy,x范围为ux以下(包括ux)的数据d1 以及 y范围同上,x范围为lx以下(不包括lx)的数据d2。那么查询的结果就是 d1-d2。查询y的一段区间我们可以使用树状数组,但是x轴不好解决,假设我们每次查询都先把lx以下的点加入树状数组,然后再把ux以下的点加入树状数组,那么时间复杂度非常高。好在我们可以使用离线查询,先输入所有的查询。然后对查询的lx和ux排序,这样可以发现只要把点入两次树状数组就能完成所有的查询。
注意:输出结果时加上eps,否则可能会出现-0.00的结果。
#include <iostream>#include <algorithm>using namespace std;const double eps = 1e-8;const int maxn = 60005;const int maxm = 20005;struct Point { int x, y; double mag; bool operator < (const Point &oth) const { return x < oth.x; }} pt[maxn];struct Query { int lx, ly, ux, uy; int id, sc; double mc;} qu[maxm];bool lcmp(const Query &q1, const Query &q2) { return q1.lx < q2.lx;}bool ucmp(const Query &q1, const Query &q2) { return q1.ux < q2.ux;}bool idcmp(const Query &q1, const Query &q2) { return q1.id < q2.id;}int yhash[maxn+maxm+maxm], sz;int n, m;int cntC[maxn+maxm+maxm];double magC[maxn+maxm+maxm];int Find(int y) { int l = 0, r = sz - 1, m; while (l < r) { m = (l + r) >> 1; if (yhash[m] > y) r = m - 1; else if (yhash[m] < y) l = m + 1; else return m; } return l;}template<class T>void Modify(int pos, const T &num, T C[]) { for ( ; pos <= sz; pos += (pos & (-pos))) { C[pos] += num; }}template<class T>T GetSum(int pos, const T C[]) { T res = 0; for ( ; pos > 0; pos -= (pos & (-pos))) { res += C[pos]; } return res;}int main() { int i, j, id, id1, id2; while(scanf("%d %d", &n, &m) != EOF) { sz = 0; for (i = 0; i < n; i++) { scanf("%d %d %lf", &pt[i].x, &pt[i].y, &pt[i].mag); yhash[sz++] = pt[i].y; } for (i = 0; i < m; i++) { scanf("%d %d %d %d", &qu[i].lx, &qu[i].ly, &qu[i].ux, &qu[i].uy); qu[i].id = i; qu[i].sc = 0; qu[i].mc = 0.0; yhash[sz++] = qu[i].ly; yhash[sz++] = qu[i].uy; } sort(yhash, yhash + sz); j = 1; for (i = 1; i < sz; i++) { if (yhash[i] != yhash[i-1]) yhash[j++] = yhash[i]; } sz = j; sort(pt, pt + n); sort(qu, qu + m, ucmp); memset(cntC + 1, 0, sz * 4); memset(magC + 1, 0, sz * 8); for (i = j = 0; i < m; i++) { while (j < n && pt[j].x <= qu[i].ux) { id = Find(pt[j].y); Modify(id + 1, 1, cntC); Modify(id + 1, pt[j].mag, magC); j++; } id1 = Find(qu[i].ly); id2 = Find(qu[i].uy); qu[i].sc += GetSum(id2 + 1, cntC) - GetSum(id1, cntC); qu[i].mc += GetSum(id2 + 1, magC) - GetSum(id1, magC); } sort(qu, qu + m, lcmp); memset(cntC + 1, 0, sz * 4); memset(magC + 1, 0, sz * 8); for (i = j = 0; i < m; i++) { while (j < n && pt[j].x < qu[i].lx) { id = Find(pt[j].y); Modify(id + 1, 1, cntC); Modify(id + 1, pt[j].mag, magC); j++; } id1 = Find(qu[i].ly); id2 = Find(qu[i].uy); qu[i].sc -= GetSum(id2 + 1, cntC) - GetSum(id1, cntC); qu[i].mc -= GetSum(id2 + 1, magC) - GetSum(id1, magC); } sort(qu, qu + m, idcmp); for (i = 0; i < m; i++) { printf("%.2lf/%d\n", qu[i].mc + eps, qu[i].sc); } } return 0;}