POJ 1195 Mobile phones

  
    
// 二维数状数组 需要注意的是下标从1开始 lowbit(0) = 0.. 超时陷阱,一定要谨记啊! 还有是c++的输入输出2600ms scanf 547ms
#include < iostream >
#include
< stdio.h >
using namespace std;
const int MAXN = 1025 ;
int tree[MAXN][MAXN];
int p[MAXN];
int n; // 坐标范围 1 ~ 1
inline int lowbit( int t){
return t & ( - t);
}
void add( int x, int y, int value){
for (;x <= n; x += lowbit(x)){
for ( int tempy = y; tempy <= n; tempy += p[tempy]) // lowbit(tempy))
tree[x][tempy] += value;
}
}
int sum( int x, int y){
int ans = 0 ;;
for (; x > 0 ; x -= lowbit(x)){
for ( int tempy = y; tempy > 0 ; tempy -= p[tempy]) // lowbit(tempy))
ans += tree[x][tempy];
}
return ans;
}
int main(){
for ( int i = 1 ;i <= 1024 ; i ++ )
p[i]
= i & ( - i);
int cmd;
// cin >> cmd >> n;
scanf( " %d%d " , & cmd, & n);
while (scanf( " %d " , & cmd) == 1 ){ // cin >> cmd){
switch (cmd)
{
case 1 :
int x, y, value;
// cin >> x >> y >> value;
scanf( " %d%d%d " , & x, & y, & value);
add(x
+ 1 , y + 1 , value);
break ;
case 2 :
int x1, y1, x2, y2;
// cin >> x1 >> y1 >> x2 >> y2;
scanf( " %d%d%d%d " , & x1, & y1, & x2, & y2);
/* cout << sum(x2 + 1, y2 + 1) + sum(x1, y1) - sum(x1, y2 + 1) - sum(x2 + 1, y1) << endl; //下标要加一 这题目做的一波三折 我怎么写成sum(x2 + 1, y2 + 1) - sum(x1, y1)了呢 ? 我靠..脑袋抽筋啊 */
printf(
" %d\n " ,sum(x2 + 1 , y2 + 1 ) + sum(x1, y1) - sum(x1, y2 + 1 ) - sum(x2 + 1 , y1));
break ;
default :
return 0 ;
}
}
return 0 ;
}

你可能感兴趣的:(mobile)