|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#define F first |
| 3 | +#define S second |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +typedef long long ll; |
| 7 | +typedef pair<int, int> pii; |
| 8 | + |
| 9 | +ll y[4][2010], sz[4]; |
| 10 | + |
| 11 | +ll case1(int l, int r) { |
| 12 | + ll ans = 0; |
| 13 | + int i, j; |
| 14 | + for(i = 1; i <= sz[l]; ++i) { |
| 15 | + ll cur = 0, cnt = 0; |
| 16 | + for(j = 1; j <= sz[r]; ++j) { |
| 17 | + ll z = abs(r - l)*abs(y[r][j] - y[l][i]) + 2*abs(r - l)*min(y[r][j], y[l][i]); |
| 18 | + ans += cnt*z - cur; |
| 19 | + cur += z; |
| 20 | + cnt += 1; |
| 21 | + } |
| 22 | + } |
| 23 | + return ans; |
| 24 | +} |
| 25 | + |
| 26 | +int main() { |
| 27 | + int t, i, j; |
| 28 | + // freopen("in.txt", "r", stdin); |
| 29 | + scanf("%d", &t); |
| 30 | + while(t--) { |
| 31 | + int n, u, v; |
| 32 | + scanf("%d", &n); |
| 33 | + sz[1] = sz[2] = sz[3] = 0; |
| 34 | + for(i = 1; i <= n; ++i) { |
| 35 | + scanf("%d %d", &u, &v); |
| 36 | + sz[u]++; |
| 37 | + y[u][sz[u]] = v; |
| 38 | + } |
| 39 | + for(i = 1; i <= 3; ++i) { |
| 40 | + sort(y[i] + 1, y[i] + sz[i] + 1); |
| 41 | + } |
| 42 | + ll ans = 0; |
| 43 | + //Case 1 and 2 |
| 44 | + ans += case1(1, 2); |
| 45 | + //Case 2 and 1 |
| 46 | + ans += case1(2, 1); |
| 47 | + // Case 2 and 3 |
| 48 | + ans += case1(2, 3); |
| 49 | + // Case 3 and 2 |
| 50 | + ans += case1(3, 2); |
| 51 | + // Case 1 and 3 |
| 52 | + ans += case1(1, 3); |
| 53 | + // Case 3 and 1 |
| 54 | + ans += case1(3, 1); |
| 55 | + // cout << ans << endl; |
| 56 | + // Case 1, 2 and 3 |
| 57 | + for(i = 1; i <= sz[1]; ++i) { |
| 58 | + ll cur = 0, cnt = 0, ptr = 1; |
| 59 | + for(j = 1; j <= sz[2]; ++j) { |
| 60 | + ll z = abs(y[2][j] - y[1][i]) + 2*min(y[2][j], y[1][i]); |
| 61 | + while(ptr <= sz[3] and y[3][ptr] < y[2][j] + y[2][j] - y[1][i]) ptr++; |
| 62 | + ans += (ptr - 1)*z - (sz[3] - ptr + 1)*z; |
| 63 | + // cout << "in " << ptr << endl; |
| 64 | + } |
| 65 | + } |
| 66 | + // cout << ans << endl; |
| 67 | + for(i = 1; i <= sz[3]; ++i) { |
| 68 | + ll cur = 0, cnt = 0, ptr = 1; |
| 69 | + for(j = 1; j <= sz[2]; ++j) { |
| 70 | + ll z = abs(y[2][j] - y[3][i]) + 2*min(y[2][j], y[3][i]); |
| 71 | + while(ptr <= sz[1] and y[1][ptr] < y[2][j] + y[2][j] - y[3][i]) ptr++; |
| 72 | + ans += (ptr - 1)*z - (sz[1] - ptr + 1)*z; |
| 73 | + } |
| 74 | + } |
| 75 | + // cout << ans << endl; |
| 76 | + for(i = 1; i <= sz[1]; ++i) { |
| 77 | + ll cur = 0, cnt = 0, ptr = 1; |
| 78 | + for(j = 1; j <= sz[3]; ++j) { |
| 79 | + ll z = 2*abs(y[3][j] - y[1][i]) + 2*2*min(y[3][j], y[1][i]); |
| 80 | + while(ptr <= sz[2] and y[2][ptr] + y[2][ptr] <= y[1][i] + y[3][j]) ptr++; |
| 81 | + ans += (ptr - 1)*z - (sz[2] - ptr + 1)*z; |
| 82 | + } |
| 83 | + } |
| 84 | + // cout << ans << endl; |
| 85 | + long double fin = (long double)ans/2.0; |
| 86 | + printf("%.3Lf\n", fin); |
| 87 | + } |
| 88 | + return 0; |
| 89 | +} |
0 commit comments