90分,最后一个点MLE了,求求dalao们帮忙优化一下qwq
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
struct node {
int xa, ya, xb, yb;
};
vector<int> px, py;
vector<node> add;
const int N = 4010;
int c[N][N], s[N][N];
node idx[N];
signed main() {
cin >> n;
for(int i = 1; i <= n; i++) {
int xa, ya, xb, yb;
cin >> xa >> yb >> xb >> ya;
px.push_back(xa);
py.push_back(ya);
px.push_back(xb);
px.push_back(xb - 1);
py.push_back(yb);
py.push_back(yb - 1);
add.push_back({xa, ya, xb, yb});
}
sort(px.begin(), px.end());
sort(py.begin(), py.end());
px.erase(unique(px.begin(), px.end()), px.end());
py.erase(unique(py.begin(), py.end()), py.end());
for(int i = 1; i <= n; i++) {
idx[i].xa = lower_bound(px.begin(), px.end(), add[i - 1].xa) - px.begin() + 1;
idx[i].ya = lower_bound(py.begin(), py.end(), add[i - 1].ya) - py.begin() + 1;
idx[i].xb = lower_bound(px.begin(), px.end(), add[i - 1].xb) - px.begin() + 1;
idx[i].yb = lower_bound(py.begin(), py.end(), add[i - 1].yb) - py.begin() + 1;
}
for(int i = 1; i <= n; i++) {
c[idx[i].xa][idx[i].ya]++;
c[idx[i].xa][idx[i].yb]--;
c[idx[i].xb][idx[i].ya]--;
c[idx[i].xb][idx[i].yb]++;
}
for(int i = 1; i <= px.size(); i++) {
for(int j = 1; j <= py.size(); j++) {
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + c[i][j];
}
}
long long ans = 0;
for(int i = 1; i <= px.size(); i++) {
for(int j = 1; j <= py.size(); j++) {
if(s[i][j] > 0) {
j++;
for(; s[i][j] > 0 && j <= py.size(); j++) {
if(s[i - 1][j - 1] > 0) {
ans += (py[j - 1] - py[j - 2]) * (px[i - 1] - px[i - 2]);
}
else {
ans += py[j - 1] - py[j - 2];
}
}
if(j <= py.size()) {
if(s[i - 1][j - 1] > 0) {
ans += (py[j - 1] - py[j - 2]) * (px[i - 1] - px[i - 2]);
}
else {
ans += py[j - 1] - py[j - 2];
}
}
}
}
}
cout << ans;
return 0;
}