HDU 3265 Posters(線段樹),hdu3265

來源:互聯網
上載者:User

HDU 3265 Posters(線段樹),hdu3265
HDU 3265 Posters

題目連結

題意:給定一些矩形海報,中間有孔,求貼海報的之後的海報覆蓋面積並

思路:海報一張可以切割成4個矩形,然後就是普通的矩形面積並了,利用線段樹維護即可

代碼:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int N = 50005;struct Node {int l, r, len, cover;int size() {return r - l + 1;}} node[N * 4];struct Line {int l, r, y, flag;Line() {}Line(int l, int r, int y, int flag) {this->l = l; this->r = r;this->y = y; this->flag = flag;}} line[N * 8];struct Rec {int x1, y1, x2, y2;Rec() {}Rec(int x1, int y1, int x2, int y2) {this->x1 = x1; this->y1 = y1;this->x2 = x2; this->y2 = y2;}} rec[N * 4];bool cmp(Line a, Line b) {return a.y < b.y;}int n;int x[4], y[4];#define lson(x) ((x<<1)+1)#define rson(x) ((x<<1)+2)void pushup(int x) {if (node[x].cover) node[x].len = node[x].size();else if (node[x].l == node[x].r) node[x].len = 0;else node[x].len = node[lson(x)].len + node[rson(x)].len;}void build(int l, int r, int x = 0) {node[x].l = l; node[x].r = r;if (l == r) {node[x].cover = node[x].len = 0;return;}int mid = (l + r) / 2;build(l, mid, lson(x));build(mid + 1, r, rson(x));pushup(x);}void add(int l, int r, int v, int x = 0) {if (l > r) return;if (node[x].l >= l && node[x].r <= r) {node[x].cover += v;pushup(x);return;}int mid = (node[x].l + node[x].r) / 2;if (l <= mid) add(l, r, v, lson(x));if (r > mid) add(l, r, v, rson(x));pushup(x);}int main() {while (~scanf("%d", &n) && n) {build(0, 50000);int rn = 0, ln = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < 4; j++)scanf("%d%d", &x[j], &y[j]);rec[rn++] = Rec(x[0], y[0], x[1], y[2]);rec[rn++] = Rec(x[0], y[2], x[2], y[3]);rec[rn++] = Rec(x[0], y[3], x[1], y[1]);rec[rn++] = Rec(x[3], y[2], x[1], y[3]);}for (int i = 0; i < rn; i++) {line[ln++] = Line(rec[i].x1, rec[i].x2, rec[i].y1, 1);line[ln++] = Line(rec[i].x1, rec[i].x2, rec[i].y2, -1);}n = ln;sort(line, line + n, cmp);ll ans = 0;for (int i = 0; i < n; i++) {if (i) ans += (ll)node[0].len * (line[i].y - line[i - 1].y);add(line[i].l, line[i].r - 1, line[i].flag);}printf("%lld\n", ans);}return 0;}



菜鳥教,hdu3265,給個思路就行,謝了O(∩_∩)O,詳細見下

這個不需要二維線段樹。
用二維線段樹的話可能大材小用了,複雜度也會比較高。
該題只要使用掃描線+一維線段樹就行了。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.