本文主要是介绍1228. 油漆面积,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Powered by:NEFU AB-IN
Link
文章目录
- 1228. 油漆面积
- 题意
- 思路
- 代码
1228. 油漆面积
-
题意
求所有矩形的总面积
-
思路
-
代码
''' Author: NEFU AB-IN Date: 2022-03-28 15:57:54 FilePath: \ACM\Acwing\1268.py LastEditTime: 2022-03-28 16:00:44 ''' from bisect import bisect_leftclass Node():def __init__(self, l, r):self.l = lself.r = rself.cnt = 0self.len = 0class Seg():def __init__(self, x, y1, y2, k):self.x = xself.y1 = y1self.y2 = y2self.k = kdef __lt__(self, t):return self.x < t.xN = int(1e5 + 10) tr = [Node(0, 0) for _ in range(N << 2)] xs, seg = [], []def find(x):return bisect_left(xs, x)def pushup(p):if tr[p].cnt:tr[p].len = xs[tr[p].r + 1] - xs[tr[p].l]elif tr[p].l == tr[p].r: #对于老版本的优化,对于叶子节点赋0,也就是没有必要往下开辟空间了tr[p].len = 0else:tr[p].len = tr[p << 1].len + tr[p << 1 | 1].lendef bulid(p, l, r):tr[p] = Node(l, r)if l == r:returnmid = l + r >> 1bulid(p << 1, l, mid)bulid(p << 1 | 1, mid + 1, r)def modify(p, l, r, k):if l <= tr[p].l and tr[p].r <= r:tr[p].cnt += kpushup(p)returnmid = tr[p].l + tr[p].r >> 1if l <= mid: modify(p << 1, l, r, k)if r > mid: modify(p << 1 | 1, l, r, k)pushup(p)n = int(input()) for i in range(n):x1, y1, x2, y2 = map(int, input().split())seg.append(Seg(x1, y1, y2, 1)) #记录每个线段seg.append(Seg(x2, y1, y2, -1))xs.append(y1), xs.append(y2) ans = 0 seg.sort() xs = sorted(list(set(xs))) bulid(1, 0, N) for i in range(2 * n):if i > 0:ans += tr[1].len * (seg[i].x - seg[i - 1].x)modify(1, find(seg[i].y1), find(seg[i].y2) - 1, seg[i].k)print(ans)
这篇关于1228. 油漆面积的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!