poj 1195
来源:互联网 发布:ubuntu 安装 kde桌面 编辑:程序博客网 时间:2024/06/01 16:25
题目
一道二维树状数组的裸题。
把更新,求和函数改成二维即可
求和的时候注意要
getsum(x2,y2)-getsum(x1-1,y2)-getsum(x2,y1-1)+getsum(x1-1,y1-1)
画图即可理解。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn= 1050;int a[maxn][maxn];int n;int Lowbit(int x){ return x&(-x);}void update(int x,int y,int val){ int i,j; for(i=x;i<maxn;i+=Lowbit(i)){ for(j=y;j<maxn;j+=Lowbit(j)){ a[i][j]+=val; } }}long long getSum(int x,int y){ int i,j; long long ans=0; for(i=x;i>0;i-=Lowbit(i)){ for(j=y;j>0;j-=Lowbit(j)){ ans+=a[i][j]; } } return ans;}int main(){ int x1,y1,x2,y2; int op; while(scanf("%d",&op)){ if(op==0){ scanf("%d",&n); memset(a,0,sizeof(a)); continue; } else if(op==1){ scanf("%d %d %d",&x1,&y1,&x2); x1++;y1++; update(x1,y1,x2); } else if(op==2){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); x1++,x2++,y1++,y2++; printf("%lld\n",getSum(x2,y2)+getSum(x1-1,y1-1)-getSum(x1-1,y2)-getSum(x2,y1-1)); } else break; } return 0;}
- POJ 1195
- poj 1195
- POJ 1195
- poj 1195
- poj 1195
- poj 1195
- poj 1195
- poj 1195
- poj 1195
- POJ 1195
- poj-1195
- poj 1195
- poj 1195
- POJ 1195
- poj 1195
- 树状数组(poj 1195,poj 3321)
- POJ 1195 Mobile phones
- poj 1195 Mobile phones
- 学生管理系统调试——实时错误(实时错误“3021)
- Response.Write 出来的 弹出窗口 主要是window.open()方法的应用
- JavaScript中的shift()和pop()函数
- 笔记:shell命令
- 机器视觉中常用的数据测试集 计算机视觉数据集
- poj 1195
- 使用rman backup命令创建备份系列之服务器管理的一致备份
- 如何让Hadoop运行在OpenStack之上
- 鸟哥的Linux私房菜
- 效果方法(jQuery)
- Parity
- 数论中的定理
- android滑动
- 谈谈如何学习Linux操作系统