hdu 4419(离散化+扫描线)
2015-09-16 01:19
337 查看
题意:有三种颜色的矩形,红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。用线段树维护每种颜色在当前区间内所占长度,然后另一个线段树数组维护当前区间都有什么颜色。
题解:一开始写挫了,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; }
相关文章推荐
- PHP 与 UTF-8
- UIWebView学习
- ZOJ 3892 Available Computation Sequence
- Java类加载器总结
- java面试题二 "%"运算符的问题
- 顺序表的实现
- HDU 4373 Mysterious For(Lucas 定理 + 中国剩余定理)
- Android中Intent对象与Intent Filter过滤匹配过程详解
- 面试中如何“投机取巧”
- Rectangle Area
- IOS征途之三 分类与协议
- maven jetty myeclipse 进行debug
- TCP协议流量控制与拥塞避免【转载】
- Activity 通过 Fragment保存大块数据的一种方法。摘自android开发官网 3ff8
- Win10实现窗口AeroGlass化
- word frequency
- android 状态栏一体化实现
- Adapter&&GroupAdapter&&VerifyAdapter&&PicAdapter
- storm-kafka数据流量问题
- MySQL5.6 部署MHA