HDU 1828 Picture (rectangle perimeter and), hdu1828

Source: Internet
Author: User

HDU 1828 Picture (rectangle perimeter and), hdu1828
HDU 1828 Picture

Question Link

Given n rectangles, the perimeter of the rectangle is output and

Idea: Use the line segment tree for maintenance, scanning from four directions each time. When there are multiple sections, the length of the Section not covered is queried, and the length of the circumference is added, the answer is added to all four directions.

Code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 5005;int n;struct Rec {int x1, y1, x2, y2;void read() {scanf("%d%d%d%d", &x1, &y1, &x2, &y2);x1 += 10000; y1 += 10000; x2 += 10000; y2 += 10000;}} rec[N];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 * 2];bool cmp1(Line a, Line b) {if (a.y == b.y) return a.flag > b.flag;return a.y < b.y;}bool cmp2(Line a, Line b) {if (a.y == b.y) return a.flag > b.flag;return a.y > b.y;}#define lson(x) ((x<<1)+1)#define rson(x) ((x<<1)+2)struct Node {int l, r, add, addv, num;bool cover;void init(int l, int r) {this->l = l; this->r = r;add = addv = num = 0;cover = false;}void gao(int v) {addv += v;if (add == 0) num = r - l + 1;add += v;if (add == 0) num = 0;}} node[4 * 20005];void pushup(int x) {if (node[lson(x)].cover && node[rson(x)].cover && node[lson(x)].add == node[rson(x)].add) {node[x].cover = true;node[x].add = node[lson(x)].add;} else node[x].cover = false;node[x].num = node[lson(x)].num + node[rson(x)].num;}void pushdown(int x) {if (node[x].addv) {node[lson(x)].gao(node[x].addv);node[rson(x)].gao(node[x].addv);node[x].addv = 0;}}void build(int l, int r, int x = 0) {node[x].init(l, r);if (l == r) {node[x].cover = true;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 (node[x].cover && node[x].l >= l && node[x].r <= r) {node[x].gao(v);return;}int mid = (node[x].l + node[x].r) / 2;pushdown(x);if (l <= mid) add(l, r, v, lson(x));if (r > mid) add(l, r, v, rson(x));pushup(x);}int query(int l, int r, int x = 0) {if (node[x].cover && node[x].l >= l && node[x].r <= r)return node[x].num;int mid = (node[x].l + node[x].r) / 2;pushdown(x);int ans = 0;if (l <= mid) ans += query(l, r, lson(x));if (r > mid) ans += query(l, r, rson(x));pushup(x);return ans;}int solve() {build(0, 20000);int ans = 0;for (int i = 0; i < 2 * n; i++) {if (line[i].flag)ans += line[i].r - line[i].l - query(line[i].l, line[i].r - 1);add(line[i].l, line[i].r - 1, line[i].flag);}return ans;}int gao() {int ans = 0;for (int i = 0; i < n; i++) {line[i * 2] = Line(rec[i].x1, rec[i].x2, rec[i].y1, 1);line[i * 2 + 1] = Line(rec[i].x1, rec[i].x2, rec[i].y2, -1);}sort(line, line + 2 * n, cmp1);ans += solve();for (int i = 0; i < n; i++) {line[i * 2] = Line(rec[i].x1, rec[i].x2, rec[i].y2, 1);line[i * 2 + 1] = Line(rec[i].x1, rec[i].x2, rec[i].y1, -1);}sort(line, line + 2 * n, cmp2);ans += solve();return ans;}int main() {while (~scanf("%d", &n)) {for (int i = 0; i < n; i++)rec[i].read();int ans = 0;ans += gao();for (int i = 0; i < n; i++) {swap(rec[i].x1, rec[i].y1);swap(rec[i].x2, rec[i].y2);}ans += gao();printf("%d\n", ans);}return 0;}



Hdu acm 2052 Picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.