poj 1195 Mobile phones
来源:互联网 发布:mac关闭最小化快捷键 编辑:程序博客网 时间:2024/06/06 01:07
题目看起来就像一个模拟题,对于一个s*s的矩阵有4个操作,输入0时将s*s的矩阵清零,输入1时将坐标为x,y的点增加a,输入2时求从r到l从b到t一个子矩阵的所有项和。题目只有一组数据但是有庞大的操作次数,所以普通的二维数组是要超时的,于是我们为了加快对其中数据和访问的速度,使用树状数组。我最开始做这道题的时候先看了一维的树状数组,然后就用一维的写了一发,4000+ms不过也过了,后来发现有二维树状数组耗时1000ms。
注意:改题个人认为在题面上又坑点,对于输入2以后,之后的那个矩阵范围的输入L,B,R,T其实L,B是左上角的顶点坐标,R,T是右下角顶点坐标(没错,不需要将B和L倒过来)这四个数据中,其中L→R表示的是正常二维数组由上到下,B→T是由左到右。直接领悟L,R,B,T的英文含义是一定会错的!!
下面给出两个代码:
#include <iostream>#include <algorithm>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;int atree[1100][1100];int main(){ int n,ask; while(scanf("%d",&ask),ask!=3) { if(ask==0) { scanf("%d",&n); memset(atree,0,sizeof(atree)); } else if(ask==1) { int x,y,a; scanf("%d%d%d",&x,&y,&a); x++; y++; while(y<=n) { atree[x][y] += a; y += y&-y; } } else if(ask==2) { int l,r,b,t; int sum=0,sum1=0,sum2=0; scanf("%d%d%d%d",&l,&b,&r,&t); l++,b++,r++,t++; for(int i=l;i<=r;i++) { int j=b-1,k=t; sum1=0,sum2=0; while(k) { sum1 += atree[i][k]; k -= k&-k; } while(j) { sum2 += atree[i][j]; j -= j&-j; } sum += sum1-sum2; } printf("%d\n",sum); } } return 0;}
#include <iostream>#include <algorithm>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;int n;int atree[1100][1100];void add(int x,int y,int a){ for(int i=x;i<=n;i+=i&-i) { for(int j=y;j<=n;j+=j&-j) { atree[i][j]+=a; } }}int query(int x,int y){ int sum=0; for(int i=x;i>0;i-=i&-i) { for(int j=y;j>0;j-=j&-j) { sum += atree[i][j]; } } return sum;}int main(){ int ask; while(scanf("%d",&ask),ask!=3) { if(ask==0) { scanf("%d",&n); memset(atree,0,sizeof(atree)); } else if(ask==1) { int x,y,a; scanf("%d%d%d",&x,&y,&a); x++; y++; add(x,y,a); } else if(ask==2) { int l,r,b,t; scanf("%d%d%d%d",&l,&b,&r,&t); l++,b++,r++,t++; printf("%d\n",query(r,t)-query(l-1,t)-query(r,b-1)+query(l-1,b-1)); } } return 0;}
0 0
- POJ 1195 Mobile phones
- poj 1195 Mobile phones
- poj 1195 Mobile phones
- poj 1195 mobile phones
- POJ 1195 Mobile phones
- Poj 1195 Mobile phones
- poj 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- poj 1195 Mobile phones
- poj 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile Phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- MySQL字符串函数substring:字符串截取
- opencv从入门到精通(3)——基本数据类型
- POJ 2391 Ombrophobic Bovines (网络流)
- 虚函数继承的默认缺省参数
- Maven项目下update maven后Eclipse报错:java.lang.ClassNotFoundException: ContextLoaderListener
- poj 1195 Mobile phones
- MySQL详解,第一章
- guava的一些帮助类
- 线段树区间更新的理解 POJ 3468 A Simple Problem with Integers 线段树区间更新 线段树区间更新的理解
- EditText默认不获取焦点的方法
- 报道帖-简单的自我介绍
- 去掉Cell选中效果
- 某公司数据恢复报告书
- 你没钱没男人,却有粗糙的脸和巨大的胃