暴力标记
#include <iostream>
using namespace std;
const int N = 110;
bool st[N][N];
int main(){
int n; cin >> n;
for(int i = 0; i < n; i ++ ){
int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
for(int j = x1; j < x2; j ++ ){
for(int k = y1; k < y2; k ++ ) st[j][k] = true;
}
}
int res = 0;
for(int i = 0; i < N; i ++ ){
for(int j = 0; j < N; j ++ ){
if(st[i][j]) res ++ ;
}
}
cout << res << endl;
return 0;
}
差分
#include <iostream>
using namespace std;
const int N = 110;
int graph[N][N], s[N][N];
void insert(int x1, int y1, int x2, int y2){
graph[x1][y1] ++ ;
graph[x1][y2 + 1] -- ;
graph[x2 + 1][y1] -- ;
graph[x2 + 1][y2 + 1] ++;
}
int main(){
int n; cin >> n;
while(n -- ){
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x1 ++ , y1 ++ ;
insert(x1, y1, x2, y2);
}
int cnt = 0;
for(int i = 1; i < N; i ++ ){
for(int j = 1; j < N; j ++ ){
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + graph[i][j];
if(s[i][j]) cnt ++ ;
}
}
cout << cnt << endl;
return 0;
}