题意:给出n个矩形,每块矩形种不同价值的种子,矩形会出现相交情况,这时候价值高的种子会把价值低的种子淘汰,问最后得到全部价值。
思路:
如果线段树+扫描线可以求覆盖面积,那么这题就是求体积并,试想把种子的价值当作高,那么按高度升序排序后,分段求体积,选出高度不小于h[i]的矩形插入线段树求当前底面积,每次就相当于求一边覆盖面积(其实就是当前底面积,那么久相当同一块矩形里,最高的高(价值最大的种子)会被当作高去求体积),求得的覆盖面积*(h[i+1]-h[i])。那么把每次求得的分段体积加起来就是总的体积了。
用这么模版写的代码 http://blog.csdn.net/qq3434569/article/details/78261949
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define lch(x) x<<1
#define rch(x) x<<1|1
#define ll long long int
const int maxn = 35000;
int cnt[maxn << 2];//边重复的次数
ll sum[maxn << 2];//1次及以上
int X[maxn<<1], val[5], Z[maxn<<1];
struct Seg {
int h, l, r;
int s;//1为入边,-1为出边
Seg() {}
Seg(int a, int b, int c, int d) : l(a), r(b), h(c), s(d) {}
bool operator < (const Seg &cmp) const {
return h < cmp.h;
}
}ss[maxn<<1];
struct Spot
{
int lx, rx, ly, ry, lz, rz;
}p[maxn<<1];
void PushUp(int rt, int l, int r) {
//每个区间求[l, r-1],算的时候右边加一,这样递归的时候就不用考虑叶子节点了。
if (cnt[rt]) sum[rt] = X[r + 1] - X[l];//这条边存在
else if (l == r) sum[rt] = 0;//边不存在,又是叶子节点
else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void update(int L, int R, int c, int l, int r, int rt) {
if (L <= l && r <= R) {
cnt[rt] += c;
PushUp(rt, l, r);
return;
}
int m = (l + r) >> 1;
if (L <= m) update(L, R, c, lson);
if (m < R) update(L, R, c, rson);
PushUp(rt, l, r);
}
int main() {
int t, cas = 0;
scanf("%d", &t);
while (t--)
{
int n, m1;
scanf("%d%d", &n, &m1);
for (int i = 1; i <= m1; i++) scanf("%d", &val[i]);
int m = 0;
for (int i = 1; i <= n; i++)
{
int temp;
scanf("%d%d%d%d%d", &p[i].lx, &p[i].ly, &p[i].rx, &p[i].ry, &temp);
p[i].lz = 0;
p[i].rz = val[temp];
Z[m++] = p[i].lz;
Z[m++] = p[i].rz;
}//离散化
sort(Z, Z + m);
int cntz = unique(Z, Z + m) - Z;
long long ans = 0;
for (int i = 0; i < cntz - 1; i++)
{
memset(cnt, 0, sizeof(cnt));
memset(sum, 0, sizeof(sum));
m = 0;
for (int j = 1; j <= n; j++)
if (p[j].lz <= Z[i] && p[j].rz > Z[i])
{
X[m] = p[j].lx;
ss[m++] = Seg(p[j].lx, p[j].rx, p[j].ly, 1);
X[m] = p[j].rx;
ss[m++] = Seg(p[j].lx, p[j].rx, p[j].ry, -1);
}
sort(X, X + m);
sort(ss, ss + m);
int cntx = unique(X, X + m) - X;
for (int j = 0; j < m - 1; j++)
{
int l = lower_bound(X, X + cntx, ss[j].l) - X;
int r = lower_bound(X, X + cntx, ss[j].r) - X - 1;
if (l <= r) update(l, r, ss[j].s, 0, cntx - 1, 1);
ans += (long long)sum[1] * (long long)(ss[j + 1].h - ss[j].h) * (long long)(Z[i + 1] - Z[i]);
}
}
printf("Case %d: %lld\n", ++cas, ans);
}
return 0;
}
另外一个版本的。
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define ll long long int
const int maxn = 35000;
int cnt[maxn << 2];//边重复的次数
ll sum[maxn << 2];//1次及以上
int X[maxn << 1], val[5];
struct Seg {
int l, r, h, s, val;
Seg() {}
Seg(int x1, int x2, int y, int v, int d) :l(x1), r(x2), h(y), val(v), s(d) {}
bool operator < (const Seg &cmp) const {
return h < cmp.h;
}
}ss[maxn << 1], tmp[maxn << 1];
void PushUp(int rt, int l, int r) {
//每个区间求[l, r-1],算的时候右边加一,这样递归的时候就不用考虑叶子节点了。
if (cnt[rt]) sum[rt] = X[r + 1] - X[l];//这条边存在
else if (l == r) sum[rt] = 0;//边不存在,又是叶子节点
else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void update(int L, int R, int c, int l, int r, int rt) {
if (L <= l && r <= R) {
cnt[rt] += c;
PushUp(rt, l, r);
return;
}
int m = (l + r) >> 1;
if (L <= m) update(L, R, c, lson);
if (m < R) update(L, R, c, rson);
PushUp(rt, l, r);
}
int main() {
int t, x1, x2, y1, y2, v, cas = 0;
scanf("%d", &t);
while (t--)
{
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) scanf("%d", &val[i]);
int k = 0;
for (int i = 0; i < n; i++) {
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &v);
X[k] = x1;
ss[k++] = Seg(x1, x2, y1, val[v], 1);
X[k] = x2;
ss[k++] = Seg(x1, x2, y2, val[v], -1);
}
val[0] = 0;
sort(X, X + k);
sort(ss, ss + k);
sort(val, val + m + 1);
int cntx = unique(X, X + k) - X;
memset(sum, 0, sizeof(sum));
memset(cnt, 0, sizeof(cnt));
ll ans = 0;
for (int i = 1; i <= m; i++) {
int c = 0;
for (int j = 0; j < k; j++)
if (ss[j].val > val[i - 1])
tmp[c++] = ss[j];//选出高度不小于h[i]的矩形插入线段树求当前底面积
for (int j = 0; j < c; j++) {
int l = lower_bound(X, X + cntx, tmp[j].l) - X;
int r = lower_bound(X, X + cntx, tmp[j].r) - X - 1;
if (l <= r) update(l, r, tmp[j].s, 0, cntx - 1, 1);
ans += sum[1] * (val[i] - val[i - 1])*(tmp[j + 1].h - tmp[j].h);
}
}
printf("Case %d: %lld\n", ++cas, ans);
}
return 0;
}