M. Window Decoration
time limit per test: 1 second
memory limit per test: 256 megabytes
There is a window with dimensions 100cm×100cm and n square window decorations, each with a diagonal length of 2cm. Establish a coordinate system with the origin at the bottom left corner of the window (0,0) and the top right corner at (100,100). The center of the i-th window decoration is placed at a non-edge integer coordinate point (xi,yi) (1≤xi,yi≤99), and the diagonals of the window decorations are parallel to the coordinate axes.
Find out the total area of the window covered by at least one window decoration.
Input
The first line contains an integer n (1≤n≤10000).
Following are n lines, each containing two integers xi,yi (1≤xi,yi≤99), as described above.
Output
Output a single line with a single real number: area of the window covered by at least one window decoration.
Your answer is considered correct if its absolute or relative error does not exceed . Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if .
Example
input
5
1 1
2 1
3 2
5 5
5 5
output
7.5
Note
Explanation for the first example is shown as follows:
【思路分析】
思维+数学。用map去重,找出相邻xi和yi即可。
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <cstring>
#include <iomanip>
#define i64 long long
using namespace std;
i64 n;
bool searchFirst(const vector<pair<i64,i64>>& arr,i64 val,i64 se){
for (auto & i : arr) {
if (i.first == val&&i.second == se) return true;
}
return false;
}
bool searchSecond(vector<pair<i64,i64>> arr,i64 val,i64 fi){
for (auto & i : arr) {
if (i.second == val&&i.first == fi) return true;
}
return false;
}
void solve() {
cin>>n;
i64 res = 0;
vector<pair<i64,i64>> arr(n);
for (int i = 0; i < n; ++i) cin>>arr[i].first>>arr[i].second;
sort(arr.begin(), arr.end());
for (int i = 1; i < arr.size(); ++i) {
if (arr[i].first==arr[i-1].first&&arr[i].second==arr[i-1].second) {
arr.erase(arr.begin()+i);
--i;
}
}
for (int i = 0; i < arr.size(); ++i) {
if (searchFirst(arr,arr[i].first-1,arr[i].second)) --res;
if (searchSecond(arr,arr[i].second-1,arr[i].first)) --res;
}
res+=4*arr.size();
cout<<res*0.5<<endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}