POJ 1195- Mobile phones(二维BIT)

题意:

矩阵上的单点更新,范围求和

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 1100
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int bit[N][N],n,m,a[N][N];
int lowbit(int x){
return x&(-x);
}
void add(int x,int y,int d){
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
bit[i][j]+=d;
}
int sum(int x,int y){
int num=;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;j-=lowbit(j))
num+=bit[i][j];
return num;
}
int main()
{
int op,x1,x2,y1,y2,d;
while(~scanf("%d",&op)){
if(op==)break;
else if(op==){
scanf("%d",&n);
memset(bit,,sizeof(bit));
memset(a,,sizeof(a));
}
else if(op==){
scanf("%d%d%d",&x1,&y1,&d);
x1++;
y1++;
if(a[x1][y1]+d<)
d=-a[x1][y1];
add(x1,y1,d);
a[x1][y1]+=d;
}
else{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int tmp=sum(x2+,y2+)-sum(x2+,y1)-sum(x1,y2+)+sum(x1,y1);
printf("%d\n",tmp);
}
}
return ;
}
上一篇:JS入门-慕课网


下一篇:一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架