
树状数组
ehi11
这个作者很懒,什么都没留下…
展开
-
Number sequence 树状数组基础
/*这是一道比较基础的树状数组。注意c下标的最大范围。c的下标是Ai<32768,用memset比较保险。初始化为0,输入的数对应的数组位置++,记为出现的次数。然后计算元素a左端所有<a的元素的个数只需要求sum(a-1)即可。然后再反向扫描一次。只需要将对应位置的左右两边都比该元素小的个数相乘即可。*/#include #include #define maxn 50005i原创 2012-08-01 22:07:25 · 520 阅读 · 0 评论 -
HOJ 1016 Joseph's problem I&&HOJ 2920 Escape
这题有一个很好的数学类解法。推导出母函数来直接求解,很高效。这边介绍一种更为普遍的模拟方法,用树状数组来模拟约瑟夫问题。本题先预处理出要求的素数。然后每次新形成的环里下一个目标的间隔就是相应的素数。在这里用树状数组来处理元素动态删除和在线统计间隔以快速确定下一个元素下标,这里用二分来逼近。动态删除即将单点更新为-1,统计直接取和。#include #include #i原创 2013-02-24 12:37:01 · 1129 阅读 · 0 评论 -
POJ 2985 The k-th Largest Group
数组a[x]的含义是个数为x的组有多少个。利用树状数组更新a,合并,则原先个数所在的集合-1.新形成的几何集合.#include #include const int maxn=200010;int c[maxn+1],f[maxn+1],a[maxn+1];inline int lowbit(int x){ return x&(-x);}void updat原创 2013-02-24 12:34:37 · 888 阅读 · 0 评论 -
HOJ_2430 Counting the algorithms
#include #include const int maxn=2*100001;int c[maxn],a[maxn];bool judge[100001];int f[100001];inline int lowbit(int x){ return x&(-x);}void update(int x,int p){ for(int i=x; i<maxn;原创 2013-02-21 12:24:44 · 819 阅读 · 0 评论 -
HOJ 2686 Magic-Box
#include #include const int maxn=202;int c[maxn][maxn][maxn];inline int lowbit(int x){ return x&(-x);}void update(int x,int y,int z,int p){ for(int i=x; i<maxn; i+=lowbit(i)) f原创 2013-02-21 12:23:33 · 563 阅读 · 0 评论 -
POJ 2155 Matrix
#include #include const int maxn=1001;int c[maxn][maxn];inline int lowbit(int x){ return x&(-x);}int sum(int x,int y){ int ans=0; for(int i=x; i>0; i-=lowbit(i)) for(int j=原创 2013-02-21 12:22:45 · 691 阅读 · 0 评论 -
POJ 1195 Mobile phones
#include #include const int maxn=1050;int c[maxn][maxn];inline int lowbit(int x){ return x&(-x);}void update(int x,int y,int p){ for(int i=x; i<maxn; i+=lowbit(i)) for(int j=y;原创 2013-02-21 12:21:52 · 686 阅读 · 0 评论 -
POJ 2029 Get Many Persimmon Trees
#include #include const int maxn=101;int c[maxn][maxn];inline int lowbit(int x){ return x&(-x);}void update(int x,int y){ for(int i=x; i<=maxn; i+=lowbit(i)) for(int j=y; j<=ma原创 2013-02-21 12:20:23 · 629 阅读 · 0 评论 -
POJ 2481 Cows
#include #include #include using namespace std;const int maxn=100005;int c[maxn+1];struct point{ int s,e,ans,id;} p[maxn+1];inline int lowbit(int x){ return x&(-x);}void update(int原创 2013-02-15 12:06:29 · 1075 阅读 · 1 评论 -
HOJ 2678 Stars
#include #include #include #include using namespace std;const int maxn=1001;struct point{ int x,y,z;} p[15001];int c[maxn][maxn];int s[15001];int cmp(point a,point b){ if(a.z!=b.z)原创 2013-02-15 12:04:46 · 828 阅读 · 0 评论 -
POJ Stars 2352
#include #include int c[32001];int s[15001];inline int lowbit(int x){ return x&(-x);}void update(int p,int d){ for(int i=p; i<=32001; i+=lowbit(i)) c[i]+=d;}int sum(int x){原创 2013-02-15 12:03:36 · 776 阅读 · 0 评论 -
POJ 3067 Japan
#include #include #include using namespace std;const int maxn=1005;long long c[maxn+1];struct point{ int x,y;} p[1000005];inline int lowbit(int x){ return x&(-x);}void update(int x原创 2013-02-15 12:05:41 · 733 阅读 · 0 评论 -
Ultra-QuickSort
#include #include #include #include using namespace std;const int maxn=500001;int c[maxn],n,m;struct NUM{ int id,num;} a[maxn];int cmp1(NUM a,NUM b){ return a.num<b.num;}int cmp2(原创 2013-02-15 12:00:46 · 978 阅读 · 0 评论 -
树状数组 经理的烦恼
/*用一个树状数组tree来保存数组a中对应我只的质数的个数.此时sum(p)表示a[]中1到p里有多少个质数.对tree的初始化,tree[]=lowbit[]*tag.若初始部位质数,则全都为0,若为质数,则按照树状数组的定义,在相应的位置按lowbit进行累加.即c[i]=a[i-lowbit(i)+!]....a[i].*/#include #include #d原创 2012-08-01 12:30:31 · 1196 阅读 · 0 评论 -
树状数组小结(1)
树状数组其统计量的变化,可以动态删除区间,更新区间,更新节点,区间统计,单点求值。大部分题的代码在博客中有。利用对和的二分,可以快速的求解第K大。主要注重问题的转化和应用。1.hoj 2275 Number sequence 利用树状数组来统计个数,左边比他小的元素的个数,顺序将元素的个数更新为1,统计右边比他大的元素个数,可以逆序更新,取和求统计个数是动态过程。2.Ultra-Quic原创 2013-02-14 19:18:38 · 1321 阅读 · 0 评论