HDU 3255 Farming

体积并。在某些矩形内撒种子,重复的地方取价值最高的那个,每个点只能存活一个,求总价值。最先想到的是二维线段树,区间修改,然后统计各种植物的个数,再乘以价值,得到答案,感觉挺麻烦的。然后发现,把价值当高度,就变成体积并了。先求一层的面积,再乘以两层之间的高度差,如此累加。每层的初始面积都是0,上一层的不要了。对于一个长方体,能到哪层,就在那层留下它的足迹。用vector来记录每层的线段。注意会超int。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<map>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", s)
#define PC(n) printf("Case %d: ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 1e9+7;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 33333;
const int M = N << 3;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};

struct node
{
    int x1, x2, y, k;
    node(){};
    node(int a, int b, int c, int d){x1 = a, x2 = b, y = c, k = d;};
    bool operator < (const node p) const
    {
        return y < p.y;
    }
}s;
struct price
{
    int v, pos;
    bool operator < (const price x) const
    {
        return v < x.v;
    }
}p[5];

int cnt[M], f[5], X[M], L, R;
LL sum[M];
vector<node> v[5];

void pushup(int l, int r, int rt)
{
    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 c, int l, int r, int rt)
{
    if(L <= l && r <= R)
    {
        cnt[rt] += c;
        pushup(l, r, rt);
        return;
    }
    int m = (l + r) >> 1;
    if(L <= m) update(c, lson);
    if(R > m) update(c, rson);
    pushup(l, r, rt);

}

int main()
{
    //freopen("input.txt", "r", stdin);

    int t, n, m, k, a, b, c, d, e;
    LL ans, tmp;
    RI(t);
    FFF(cas, 1, t)
    {
        RII(n, m);
        REP(i, m)
        {
            RI(p[i].v);
            p[i].pos = i + 1;
        }
        sort(p, p + m);
        REP(i, m)f[p[i].pos] = i + 1;
        REP(i, 5)v[i].clear();
        k = ans = 0;
        REP(i, n)
        {
            RV(a, b, c, d, e);
            X[k++] = a;
            X[k++] = c;
            s = node(a, c, b, 1);
            FFF(j, 1, f[e])v[j].PB(s);
            s = node(a, c, d, -1);
            FFF(j, 1, f[e])v[j].PB(s);
        }
        FFF(i, 1, m)sort(v[i].begin(), v[i].end());
        sort(X, X + k);
        k = unique(X, X + k) - X;
        PC(cas);
        CLR(sum, 0);
        CLR(cnt, 0);
        FFF(i, 1, m)
        {
            e = v[i].size();
            if(!e)continue;
            tmp = 0;
            REP(j, e)
            {
                L = lower_bound(X, X + k, v[i][j].x1) - X;
                R = lower_bound(X, X + k, v[i][j].x2) - X - 1;
                update(v[i][j].k, 0, k - 1, 1);
                if(j < e)tmp += sum[1] * (v[i][j + 1].y - v[i][j].y);
            }
            if(i == 1)ans += tmp * LL(p[i - 1].v);
            else ans += tmp * LL(p[i - 1].v - p[i - 2].v);
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值