HDU 2642 二维树状数组问题
来源:互联网 发布:数据处理软件pen 编辑:程序博客网 时间:2024/04/30 22:36
简单:Stars
http://acm.hdu.edu.cn/showproblem.php?pid=2642
题目大概意思: 在某个位置可以是 B (明亮的星星) 或者是 D (昏暗的星星) 当 Q x1 x2 y1 y2 时候 求 在 x1 y1 到 x2 y2 中一共有多上个星星。
思路:用二维树状数组进行模拟 其算法的效率为 log(n) java 代码如下:
import java.util.Scanner;/* * 此题用 二维线段树求解 */public class HDU2642 {int c[][];static final int n = 1001;boolean vis[][];public HDU2642(){c = new int[n][n]; vis = new boolean[n][n];}private int lowbit(int n){return n&(-n);}// 修改 给 x1 , x2 位置 增加 data 后的修改public void modify(int x, int y, int data){for(int i =x ; i<=n; i += lowbit(i)){for(int j=y; j<=n; j+= lowbit(j)){c[i][j] += data;}}}// 得到从 (0,0) 到 (x,y) 矩阵的 和public int sum(int x,int y){int result = 0;for(int i=x; i>0; i -= lowbit(i)){for(int j=y; j>0; j -= lowbit(j)){result += c[i][j];}}return result;}// 得到 (x1,y1) 到 (x2, y2) 的 子矩阵的和public int getSubMaxtrix(int x1, int y1, int x2, int y2){// 让 x2>x1 y2>y1;if( x2 < x1){int temp = x1; x1 = x2; x2 = temp;};if( y2 < y1){int temp = y1; y1 = y2; y2 = temp;};return sum(x2,y2) - sum(x1-1,y2) - sum(x2,y1-1) + sum(x1-1,y1-1);}public void solve(){int m ;Scanner sc = new Scanner(System.in);m = sc.nextInt();for(int i=0; i<m; i++){String executeChar ; int x, y;executeChar = sc.next();if(executeChar.equals("B")){// 如果是 Bright x = sc.nextInt(); y = sc.nextInt();x++; y++;if( !vis[x][y]){vis[x][y] = true; modify(x, y, 1);}}else if(executeChar.equals("D")){x = sc.nextInt(); y = sc.nextInt();x++; y++;if(vis[x][y]){vis[x][y] = false; modify(x,y,-1);}}else if(executeChar.equals("Q")){int x1,x2,y1,y2;x1 = sc.nextInt(); x2 = sc.nextInt(); y1 = sc.nextInt(); y2 = sc.nextInt();x1++; y1++; x2++; y2++;System.out.println(getSubMaxtrix(x1, y1, x2, y2));}}}public static void main(String[] args) {new HDU2642().solve();}}
树状数组的知识点:
定义 如下: 有数组 a[1........n] 有 c[] 有 c[n] = a[ n - 2^k+1] + ...............a[n]; k 为 n 化为 2进制后 0 的个数。是在网上学的 。
- HDU 2642 二维树状数组问题
- HDU 2642 二维树状数组
- hdu 2642 二维树状数组
- HDU 2642 二维树状数组
- hdu 2642stars(二维树状数组)
- HDU 2642 stars 二维树状数组
- hdu 2642 stars (二维树状数组)
- HDU 2642 Stars (二维树状数组)
- HDU 2642 Stars(二维树状数组)
- hdu 2642 Stars(二维树状数组)
- hdu- 2642 Stars 二维树状数组
- HDU 2642 Stars 二维树状数组应用
- HDU 2642(树状数组模板二维)
- HDU 2642 Stars 二维树状数组
- HDU 2642 Stars [二维树状数组]【数据结构】
- hdu 2642 star(二维树状数组)
- hdu 2642 (summerIII J) 二维树状数组
- hdu 2642 Stars (二维树状数组)
- 外壳命名空间扩展
- Eclipse下使用Ant
- 跨平台移动开发实战(十二)------HTML5安全
- SurfaceView与多线程
- 让Myeclipse支持jQuery、Ext等主流框架自动完成、调试功能,Myeclipse集成Apatana
- HDU 2642 二维树状数组问题
- Linux命令之pwd
- android与jsp http post方式通信
- SQL SERVER错误代码大全
- IFrame高度设置
- SEO优化(增加搜索爬虫)
- 备份恢复IBM DB2数据库的基本步骤-----文件转自DB2china
- QT国际化机制的相互矛盾的地方[Q_PROPERTY与tr函数]
- 鼠标放到控件上 DIV悬浮提示效果(四种)