Title Description
The JYY has a rectangle in n plane coordinate systems. The bottom edge of each rectangle is parallel to the x-axis, and the side is parallel to the y-axis. The bottom-left corner coordinates of the I rectangle are (xi,yi), the bottom edge is AI, and the side length is bi. Now Jyy is going to randomly select two different rectangles from these n rectangles and calculate their size. Jyy wants to know what the expectation of the size of the hand is. In other words, in all possible options, the average size of the area of the two rectangular intersection is much larger.
Input
The input line consists of a positive integer n. Next N lines, 4 integers per line, xi,yi,ai,bi2 < = N < = 2*10^5, 0 < = Xi, Yi, Ai, Bi < = 10^6.
Output
The output line contains a real number, which represents the expectation of the size of the rectangle.
Sample input
4
0 0 3 5
2 1 3 5
3 3 3 5
0 5 3 5
Sample output
1.833333333
Exercises
Scan line + Two-dimensional tree array interval modification interval query
Obviously the topic is equivalent to: first give each rectangle within the weight of +1, and then query the weights within each rectangle and that is arbitrarily elected two rectangles (can be the same, there is a sequence) of the intersection area, and then minus the area of each rectangle (self and self) is to choose two different rectangular cross-area of the sum.
Then we divide the difference between the change and the inquiry into 4 endpoints, which is equivalent to the contribution of the bottom-left modification of the split point in each query. Use scan line maintenance.
Notice that we are going to do a rectangle plus, a rectangle summation, you can use the interval of the two-dimensional tree array to modify the interval query method, scan line, one-dimensional tree array method similar.
The answer is then divided by $n (n-1) $.
However, the most disgusting point of the subject: the subject burst long long! Therefore, you can only use long double Dafa. Because the final answer is only the $10^{12}$ level, the precision of a long double is sufficient, and there is no need to worry about accuracy issues.
Time Complexity $O (n\log N) $
It's a pleasure to take a blood ^_^
#include <cstdio> #include <cstring> #include <algorithm> #define N 200010using namespace Std;typedef Long double ld;int k;struct bit{ld f[n << 2];inline void Add (int x, ld a) {int i;for (i = x; I <= k; i + = i & ; -i) f[i] + = A; Inline ld ask (int x) {int i;ld ans = 0;for (i = x; i; i-= i & i) ans + = F[i];return ans;}} A, B, C, d;struct data{ld x;int y, Z, val;bool operator< (const data &a) const {return x < a.x;}} A[n << 1], b[n << 1];ld v[n << 2];inline void Modify (ld x, int y, int a) {A.add (Y, a), B.add (y, X * A), C.add (y, V[y] * a), D.add (y, x * v[y] * a);} Inline LD query (ld x, int y) {return (x + 1) * (V[y] + 1) * A.ask (Y)-(V[y] + 1) * B.ask (Y)-(x + 1) * C.ask (y) + d.ask ( y);} int main () {int n, I, p = 1;ld xi, yi, ai, bi, ans = 0, sum;scanf ("%d", &n), sum = (LD) n * (n-1); for (i = 1; I <= N; i + +) {scanf ("%lf%lf%lf%lf", &xi, &yi, &ai, &bi), ans-= ai * bi;a[i].x = XI, a[I].y = yi, a[i].z = yi + bi, a[i].val = 1;b[i].x = xi-1, b[i].y = yi-1, b[i].z = Yi + bi-1, b[i].val = -1;a[i + n].x = xi + ai, a[i + n].y = yi, a[i + n].z = yi + bi, a[i + n].val = -1;b[i + n].x = xi + ai-1, b[i + n].y = yi- 1, b[i + n].z = yi + bi-1, b[i + n].val = 1;v[++k] = Yi, v[++k] = yi + bi, v[++k] = yi-1, v[++k] = Yi + bi-1;} Sort (v + 1, v + k + 1), k = Unique (v + 1, v + k + 1)-v;for (i = 1; I <= 2 * n; i + +) {a[i].y = Lower_bound (v + 1 , V + k + 1, a[i].y)-v;a[i].z = Lower_bound (v + 1, v + k + 1, a[i].z)-v;b[i].y = Lower_bound (v + 1, v + k + 1, b[ I].Y)-v;b[i].z = Lower_bound (v + 1, v + k + 1, b[i].z)-V;} Sort (A + 1, a + 2 * n + 1), sort (b + 1, B + 2 * n + 1); for (i = 1; I <= 2 * n; i + +) {while (P <= 2 * n && ; a[p].x <= b[i].x) Modify (a[p].x, A[p].y, A[p].val), modify (a[p].x, A[p].z,-a[p].val), p + +; ans + = B[i].val * (q Uery (b[i].x, b[i].z)-Query (b[i].x, B[I].Y));} printf ("%.9lf\n", ans/sum); reTurn 0;}
"bzoj5173" [Jsoi2014] Rectangle and Scan line + two-dimensional tree array interval modified interval query