日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[codevs3044][POJ1151]矩形面积求并

發布時間:2025/3/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [codevs3044][POJ1151]矩形面积求并 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[codevs3044][POJ1151]矩形面積求并

試題描述

輸入n個矩形,求他們總共占地面積(也就是求一下面積的并)

輸入

可能有多組數據,讀到n=0為止(不超過15組)

每組數據第一行一個數n,表示矩形個數(n<=100)

接下來n行每行4個實數x1,y1,x2,y1(0 <= x1 < x2 <= 100000;0 <= y1 < y2 <= 100000),表示矩形的左下角坐標和右上角坐標

輸出

每組數據輸出一行表示答案

輸入示例

2 10 10 20 20 15 15 25 25.5 0

輸出示例

180.00

數據規模及約定

見“輸入

題解

掃描線 + 線段樹。

線段樹標記永久化,因為這題每個時刻只需要知道線段樹根節點的信息,而不是每次查詢一段區間,所以很容易實現,具體見代碼,或者黃學長的題解。

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> #include <cmath> using namespace std;int read() {int x = 0, f = 1; char c = getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }return x * f; }#define maxn 110struct Line {int l, r, h, tp;Line() {}Line(int _1, int _2, int _3, int _4): l(_1), r(_2), h(_3), tp(_4) {}bool operator < (const Line& t) const { return h < t.h; } } ls[maxn<<1]; double posx[maxn<<1], posy[maxn<<1], numx[maxn<<1], numy[maxn<<1], ans;int cntv[maxn<<3]; double sumv[maxn<<3]; void maintain(int L, int R, int o) {int lc = o << 1, rc = lc | 1;if(cntv[o]) sumv[o] = numx[R] - numx[L-1];else if(L == R) sumv[o] = 0;else sumv[o] = sumv[lc] + sumv[rc];return ; } void update(int L, int R, int o, int ql, int qr, int v) {if(ql <= L && R <= qr) {cntv[o] += v;return maintain(L, R, o);}int M = L + R >> 1, lc = o << 1, rc = lc | 1;if(ql <= M) update(L, M, lc, ql, qr, v);if(qr > M) update(M+1, R, rc, ql, qr, v);return maintain(L, R, o); }int main() {while(1) {int n = read(), cntx = 0, cnty = 0, cntl = 0;if(!n) break;for(int i = 1; i <= n; i++) {double x1, x2, y1, y2;scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);posx[++cntx] = x1; posx[++cntx] = x2;posy[++cnty] = y1; posy[++cnty] = y2;numx[cntx-1] = posx[cntx-1]; numx[cntx] = posx[cntx];numy[cnty-1] = posy[cnty-1]; numy[cnty] = posy[cnty];}sort(numx + 1, numx + cntx + 1);sort(numy + 1, numy + cnty + 1);for(int i = 1; i <= n; i++) {int x1, x2, y1, y2;x1 = lower_bound(numx + 1, numx + cntx + 1, posx[(i<<1)-1]) - numx;x2 = lower_bound(numx + 1, numx + cntx + 1, posx[i<<1]) - numx;y1 = lower_bound(numy + 1, numy + cnty + 1, posy[(i<<1)-1]) - numy;y2 = lower_bound(numy + 1, numy + cnty + 1, posy[i<<1]) - numy;ls[++cntl] = Line(x1, x2, y1, 1);ls[++cntl] = Line(x1, x2, y2, -1);}sort(ls + 1, ls + cntl + 1);memset(cntv, 0, sizeof(cntv));memset(sumv, 0, sizeof(sumv));ans = 0;double start = numx[1];for(int i = 1; i < cntx; i++) numx[i] = numx[i+1] - start;for(int i = 1; i < cntl; i++) {if(ls[i].l < ls[i].r) update(1, cntx - 1, 1, ls[i].l, ls[i].r - 1, ls[i].tp);ans += (numy[ls[i+1].h] - numy[ls[i].h]) * sumv[1];}printf("%.2lf\n", ans);}return 0; }

注意:POJ 上輸出格式不太一樣,詳見題面。

轉載于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6474671.html

總結

以上是生活随笔為你收集整理的[codevs3044][POJ1151]矩形面积求并的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。