每次更新一个点,然后最后统计一个区间
很常规
AC代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX_S = 1024 + 5;
int sum[MAX_S][MAX_S];
int statu, S;
inline int lowbit( int x ){
return x&-x;
}
int getsum( int x, int y ){
int ans = 0;
for( int i = x; i > 0; i -= lowbit( i ) ){
for( int j = y; j > 0; j -= lowbit( j ) ){
ans += sum[i][j];
}
}
return ans;
}
void updata( int x, int y, int val ){
for( int i = x; i <= S; i += lowbit( i ) ){
for( int j = y; j <= S; j += lowbit( j ) ){
sum[i][j] += val;
}
}
}
int main(){
while( scanf( "%d", &statu ) && statu != 3 ){
if( statu == 0 ){
scanf( "%d", &S );
memset( sum, 0, sizeof( sum ) );
}else if( statu == 1 ){
int temp1, temp2, temp3;
scanf( "%d%d%d", &temp1, &temp2, &temp3 );
updata( temp1 + 1, temp2 + 1, temp3 );
}else{
int temp1, temp2, temp3, temp4;
scanf( "%d%d%d%d", &temp1, &temp2, &temp3, &temp4 );
temp1++;
temp2++;
temp3++;
temp4++;
int ans = getsum( temp3, temp4 );
ans += getsum( temp1 - 1, temp2 - 1 );
ans -= getsum( temp1 - 1, temp4 );
ans -= getsum( temp3, temp2 - 1 );
printf( "%d\n", ans );
}
}
return 0;
}