注意本例和窗内的星星这个问题的对比
窗内的星星
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef set<int>::iterator ssii;
#define Cmp(a, b) memcmp(a, b, sizeof(b))
#define Cpy(a, b) memcpy(a, b, sizeof(b))
#define Set(a, v) memset(a, v, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define _forS(i, l, r) for(set<int>::iterator i = (l); i != (r); i++)
#define _rep(i, l, r) for(int i = (l); i <= (r); i++)
#define _for(i, l, r) for(int i = (l); i < (r); i++)
#define _forDown(i, l, r) for(int i = (l); i >= r; i--)
#define debug_(ch, i) printf(#ch"[%d]: %d\n", i, ch[i])
#define debug_m(mp, p) printf(#mp"[%d]: %d\n", p->first, p->second)
#define debugS(str) cout << "dbg: " << str << endl;
#define debugArr(arr, x, y) _for(i, 0, x) { _for(j, 0, y) printf("%c", arr[i][j]); printf("\n"); }
#define _forPlus(i, l, d, r) for(int i = (l); i + d < (r); i++)
#define lowbit(i) (i & (-i))
#define MPR(a, b) make_pair(a, b)
pair<int, int> crack(int n) {
int st = sqrt(n);
int fac = n / st;
while (n % st) {
st += 1;
fac = n / st;
}
return make_pair(st, fac);
}
inline ll qpow(ll a, int n) {
ll ans = 1;
for(; n; n >>= 1) {
if(n & 1) ans *= 1ll * a;
a *= a;
}
return ans;
}
template <class T>
inline bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
for(; b; b >>= 1) {
if (b & 1) ans = (ans + a) % mod;
a = (a * 2) % mod;
}
return ans;
}
ll ksm(ll a, ll b, ll mod) {
ll ans = 1 % mod;
a %= mod;
for(; b; b >>= 1) {
if (b & 1) ans = ksc(ans, a, mod);
a = ksc(a, a, mod);
}
return ans;
}
template <class T>
inline bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
bool _check(int x) {
//
return true;
}
int bsearch1(int l, int r) {
while (l < r) {
int mid = (l + r) >> 1;
if(_check(mid)) r = mid;
else l = mid + 1;
}
return l;
}
int bsearch2(int l, int r) {
while (l < r) {
int mid = (l + r + 1) >> 1;
if(_check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
template<class T>
bool lexSmaller(vector<T> a, vector<T> b) {
int n = a.size(), m = b.size();
int i;
for(i = 0; i < n && i < m; i++) {
if (a[i] < b[i]) return true;
else if (b[i] < a[i]) return false;
}
return (i == n && i < m);
}
// ============================================================== //
const int maxn = 1e4 + 5;
int n, m;
class segTree {
public:
int l, r, cnt;
double len;
void clear() {
l = r = 0;
cnt = 0;
len = 0;
}
} tree[maxn * 8];
class A {
public:
double x, y1, y2;
int k;
A() = default;
A(double x, double y1, double y2, int k) : x(x), y1(y1), y2(y2), k(k) {}
bool operator< (const A &rhs) const {
return x < rhs.x;
}
} a[maxn * 2];
vector<double> B;
map<double, int> ny;
void prework() {
sort(a+1, a+1+m);
sort(B.begin(), B.end());
B.erase(unique(B.begin(), B.end()), B.end());
for (auto x : B) ny[x] = lower_bound(B.begin(), B.end(), x) - B.begin();
}
void build(int p, int l, int r) {
tree[p].clear();
tree[p].l = l; tree[p].r = r;
if (l == r) return;
int mid = (l + r) >> 1;
build(p<<1, l, mid);
build(p<<1 | 1, mid + 1, r);
}
void pushup(int p) {
if (tree[p].cnt) tree[p].len = B[tree[p].r + 1] - B[tree[p].l];
else {
if (tree[p].l == tree[p].r) tree[p].len = 0;
else tree[p].len = tree[p<<1].len + tree[p<<1 | 1].len;
}
}
void change(int p, int l, int r, int v) {
if (l <= tree[p].l && tree[p].r <= r) {
tree[p].cnt += v;
pushup(p);
return;
}
int mid = (tree[p].l + tree[p].r) >> 1;
if (l <= mid) change(p<<1, l, r, v);
if (r > mid) change(p<<1 | 1, l, r, v);
pushup(p);
}
void solve() {
build(1, 0, B.size() - 2);
double ans = 0.0;
_rep(i, 1, m) {
if (i > 1) ans += tree[1].len * (a[i].x - a[i-1].x);
change(1, ny[a[i].y1], ny[a[i].y2] - 1, a[i].k);
}
printf("Total explored area: %.2f\n\n", ans);
}
void init() {
m = 0;
B.clear();
ny.clear();
}
int main() {
freopen("input.txt", "r", stdin);
int kase = 0;
while (scanf("%d", &n) == 1 && n) {
init();
printf("Test case #%d\n", ++kase);
// put data
_rep(i, 1, n) {
double x1, y1, x2, y2;
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
a[++m] = A(x1, y1, y2, 1);
a[++m] = A(x2, y1, y2, -1);
B.push_back(y1);
B.push_back(y2);
}
prework();
solve();
}
}