hdu 4419(离散化+扫描线)

题意:有三种颜色的矩形,红R绿G蓝B,然后给出每个矩形左下角和右上角坐标,矩阵重叠会形成新的颜色的矩形,颜色有RG、GR、GB、RGB,一共七种颜色,问所有矩形放好后,每种颜色的面积并是多少。
题解:一开始写挫了,RGB分别用1 2 3代表,然后pushup函数写了很长很长,后来发现如果RBG分别用1 2 4代表就能用位运算来写,代码长度顿时少了很多。3是RB也就是1|2,5是RG也就是1|4,6是BG也就是2|4,7是RBG也就是1|2|4。用线段树维护每种颜色在当前区间内所占长度,然后另一个线段树数组维护当前区间都有什么颜色。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
const int N = 20005;
struct Line {
    int lx, rx, h;
    int v;
    Line(int a, int b, int c, int d): lx(a), rx(b), h(c), v(d) {}
    bool operator < (const Line& a) const { return h < a.h; }
};
int n, tree[N << 2][8], set[N << 2][4];
long long res[8];
vector<int> a;
vector<Line> line;
map<int, int> mp;

void pushup(int k, int left, int right) {
    int flag = 0;
    if (set[k][0] > 0) flag |= 1;
    if (set[k][1] > 0) flag |= 2;
    if (set[k][2] > 0) flag |= 4;
    for (int i = 1; i <= 7; i++)
        tree[k][i] = 0;
    if (flag) {
        tree[k][flag] = tree[k][0];
        for (int i = 1; i <= 7; i++) {
            if (flag != (flag | i)) {
                int temp = 0;
                if (left + 1 != right)
                    temp = tree[k * 2][i] + tree[k * 2 + 1][i];
                tree[k][flag | i] += temp;
                tree[k][flag] -= temp;
            }
        }
    } else if (left + 1 != right) {
        for (int i = 1; i <= 7; i++)
            tree[k][i] = tree[k * 2][i] + tree[k * 2 + 1][i];
    }
}

void build(int k, int left, int right) {
    set[k][0] = set[k][1] = set[k][2] = 0;
    tree[k][0] = a[right] - a[left];
    for (int i = 1; i <= 7; i++)
        tree[k][i] = 0;
    if (left + 1 != right) {
        int mid = (left + right) / 2;
        build(k * 2, left, mid);
        build(k * 2 + 1, mid, right);
    }
}

void modify(int k, int left, int right, int l, int r, int x) {
    if (l <= left && right <= r) {
        int temp;
        if (abs(x) == 1) temp = 0;
        else if (abs(x) == 2) temp = 1;
        else temp = 2;
        set[k][temp] += (x > 0 ? 1 : -1);
        pushup(k, left, right);
        return;
    }
    int mid = (left + right) / 2;
    if (l < mid)
        modify(k * 2, left, mid, l, r, x);
    if (r > mid)
        modify(k * 2 + 1, mid, right, l, r, x);
    pushup(k, left, right);
}

int main() {
    int t, cas = 1;
    scanf("%d", &t);
    while (t--) {
        a.clear(), line.clear(), mp.clear();
        scanf("%d", &n);
        char col[5];
        int x1, y1, x2, y2;
        for (int i = 0; i < n; i++) {
            int temp;
            scanf("%s%d%d%d%d", col, &x1, &y1, &x2, &y2);
            if (col[0] == 'R') temp = 1;
            else if (col[0] == 'G') temp = 2;
            else temp = 4;
            line.push_back(Line(x1, x2, y1, temp));
            line.push_back(Line(x1, x2, y2, -temp));
            a.push_back(x1);
            a.push_back(x2);
        }
        sort(line.begin(), line.end());
        sort(a.begin(), a.end());
        a.erase(unique(a.begin(), a.end()), a.end());
        int sz = a.size(), sz2 = line.size();
        for (int i = 0; i < sz; i++)
            mp[a[i]] = i;
        build(1, 0, sz - 1);
        memset(res, 0, sizeof(res));
        for (int i = 0; i < sz2; i++) {
            if (i != 0) {
                int temp = line[i].h - line[i - 1].h;
                for (int j = 1; j <= 7; j++)
                    res[j] += (long long)tree[1][j] * temp;
            }
            modify(1, 0, sz - 1, mp[line[i].lx], mp[line[i].rx], line[i].v);
        }
        printf("Case %d:\n", cas++);
        printf("%lld\n%lld\n%lld\n%lld\n%lld\n%lld\n%lld\n", res[1], res[2], res[4], res[3], res[5], res[6], res[7]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值