树状数组
Hearthougan
这个作者很懒,什么都没留下…
展开
-
poj 3067 Japan
#include #include #include #include using namespace std; const int MAXN = 1010; long long c[MAXN]; struct Node { int x, y; }node[MAXN*MAXN]; int lowbit( int x ) { return x & (-x); }原创 2013-11-16 16:00:57 · 796 阅读 · 0 评论 -
hdu 4031 Attack
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4031 区间更新,点原创 2014-04-13 13:21:42 · 660 阅读 · 0 评论 -
POJ 2299
求逆序数,实际上有多种方法,枚举法,插入排序法,冒泡排序算法,归并排序算法,树状数组等方法都可以求解逆序数,前三种当数据比较多时效率比较低,由于数据比较大因此利用树状数组求解时可以把原来数组离散化! 离散化: 9999999 66666666 5555 777777 ··············(1) 1 2 3 4 ········原创 2014-04-01 14:54:11 · 753 阅读 · 0 评论 -
POJ 1007 DNA Sorting
#include #include #include #include using namespace std; const int MAXN = 110; struct DNA_Node { int num; char str[MAXN]; DNA_Node() { num = 0; memset(str, 0, s原创 2014-04-01 16:38:27 · 689 阅读 · 0 评论 -
1166 敌兵布阵
题目:点击打开链接 树状数组,是一个基础题,但是不知道为什么c++过不了。c++很慢么?! #include #include #define MAXN 50050 int c[MAXN]; int N; int lowbit( int x ) { return x & (-x); } void UFset(int x, int num) { while(x <=原创 2013-11-13 14:31:03 · 829 阅读 · 0 评论 -
HOJ Stars 2678
这个题与poj的思路有点类似 点击打开poj Stars #include #include #include using namespace std; const int MAXN = 1010; int c[MAXN][MAXN]; struct Node { int x, y, z; }stars[15010]; int cmp(Node a, Node b) { i原创 2013-11-14 21:49:42 · 961 阅读 · 0 评论 -
poj Stars 2352
#include #include #include using namespace std; const int MAXN = 32010; int c[MAXN]; int lowbit( int x ) { return x&(-x); } void UFset(int pos, int data) { while(pos < MAXN) {原创 2013-11-14 18:41:55 · 888 阅读 · 0 评论 -
hoj1640Mobile Phone
点击打开题目 这是一个典型的二维树状数组,也是模板题; 二维树状数组可以参考:点击打开链接 #include #include #include using namespace std; const int MAXN = 1024; int c[MAXN + 1][MAXN + 1]; int k, s; int lowbit( int x ) { return x原创 2013-11-13 23:41:27 · 1112 阅读 · 0 评论 -
二维数组小结
树状数组可以扩充到二维。 问题:一个由数字构成的大矩阵,能进行两种操作 1) 对矩阵里的某个数加上一个整数(可正可负) 2) 查询某个子矩阵里所有数字的和,要求对每次查询,输出结果。 一维树状数组很容易扩展到二维,在二维情况下:数组A[][]的树状数组定义为: C[x][y] = ∑ a[i][j], 其中, x-lowbit(x) + 1 y-转载 2013-11-13 21:15:05 · 1431 阅读 · 0 评论 -
hoj 1867 经理的烦恼
点击打开题目 树状数组技术题目,加了一个判素; #include #include using namespace std; const int MAXN = 1000010; int c[MAXN];//树状数组中那个c数组; int arr[MAXN];//arr[i]用来存放商店i中商品的个数 int C, N, M; int lowbit( int x ) { re原创 2013-11-13 15:58:40 · 929 阅读 · 0 评论 -
hoj 2275 Number Sequence
知道两点即可: 一:计算出每个Ai,之前以及之后比他小的个数做乘法运算,然后求和,即是最后结果 二:找出Ai之前比Ai小的数,可以用树状数组,同理求之后的数也一样; #include #include #include #define MAXN 50010 using namespace std; int N; int c[MAXN], a[MAXN], b[MAXN], d[MA原创 2013-11-14 10:57:25 · 992 阅读 · 0 评论 -
HOJ 2686 Magic-Box
三维树状数组,关键是更新时,空间想象比二维复杂些。其原理都是相同的! #include #include #include #include using namespace std; const int MAXN =210; int c[MAXN][MAXN][MAXN]; inline int lowbit(int x) { return x & (-x); } v原创 2013-11-17 15:08:08 · 911 阅读 · 0 评论 -
poj 2029 Get Many Persimmon Trees
本题可以二维树状数组的思想来解决,暴力好像也可以,数据比较小。用二维树状数组,就是枚举指定长宽的矩阵内*号的个数,并求出数目最大的。 #include #include #include #include using namespace std; const int MAXN = 110; int c[MAXN][MAXN]; int lowbit(int x) { r原创 2013-11-17 13:38:20 · 811 阅读 · 0 评论 -
poj 2299 Ultra-QuickSort
本题就是求逆序数,在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。 令t[i]表示逆序对(a[j],i)的个数,即排在i的左边且比i大的数的个数,则逆序数为t[1]+t[2]+···t[n] 例l设n=8,a={3,2,l,5,8,4,6,7},t[1] = 2 ,t[2]原创 2013-11-16 21:04:59 · 786 阅读 · 0 评论 -
poj 2481 Cows
题目大意: 给定你一些区间,去判断每个区间是其他区间真子集的个数。 分析: 本题与Stars有点类似。首先对E从大到小排序,如果Ei = Ej 则让S从小到大排序,然后就是stars类型。就是求cow[i].S前比他小的个数。 #include #include #include #include using namespace std;原创 2013-11-17 11:38:15 · 829 阅读 · 0 评论 -
一维树状数组小结
树状数组是一类树状的解决区间统计问题的数据结构。 那么什么是“区间统计”呢?下面举出一个例子: 现有一个长度为n的数组,数组内存有数据。对于这些数据,有2类操作: 1、修改数据第i个元素。 2、查询数据一个区间(如p至q)内元素的和。 下面我将使用不同的数据结构解决这个问题,帮助理解数据结构的意义以及树状数组的思想。 方法一: 使用数组a[i]存储数据的第i个元素。那么对原创 2013-11-13 21:08:43 · 1144 阅读 · 0 评论