随笔分类 - 数据结构
摘要:线段树做法,成段求和,单点更新View Code #include<cstdio>#include<cstring>#include<map>#include<algorithm>using namespace std;const int maxn = 50010;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1__int64 sum[maxn<<2];struct node{ int l,r,id;}p[200010];map<int,int> ha
        阅读全文
            
摘要:离线处理询问,即先记录下所有询问用map将当前的值映射到一个下标,如果以前已经映射过了,则在树状数组中取消在上一次的位置的记录再在当前位置加入当前值,这样的话对于每个询问便可以先更新再查询,离线记录结果View Code #include<cstdio>#include<cstring>#include<map>#include<algorithm>using namespace std;const int maxn = 50010;__int64 c[maxn];struct node{ int l,r,id;}p[200010];map<
        阅读全文
            
摘要:http://www.notonlysuccess.com/index.php/alibaba/做不出来,参考了not。。。。的解释,懂了,敲完发现,与他的代码几乎一样。。。。。题意很简单,成段更新,成段询问,但是更新却和一般的线段树大不一样,每个点虽然接收到相同的信息,但是由于本身不同,最终得到的值也是不同的.用一般的延迟操作就搞不定了.突破点在K,范围很小,只有10,可以考虑每次有人升级的时候,就递归的找下去,将这个人进行升级操作.由于找到某个人只需要logn的复杂度,每个人最多升k次,所以n个人的复杂度是O(nklogn)用了两个辅助数组add[maxn]和MAX[maxk][maxn]
        阅读全文
            
摘要:同poj 2482 不过这里在边界上的点也算在矩形内,边界处理注意一下就好了View Code #include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 2001;int cover[maxn<<2],Max[maxn<<2];struct seg{ double l,r,h; int flag; 
        阅读全文
            
摘要:一个小小的bug搞了将近两天,郁闷shi了平面上有很多点,每个点有一个权值,给定一个矩形,求出用这个矩形去套时所能同时套住的点的权值的和的最大值转换成线段树:以每个点为左下角向右延伸转换成一个矩形,对x轴建树,一个点就相当于两条线段,一条权值为v,另一条权值为-v,线段树维护总的区间内最大的覆盖次数,把每次的Max[1]与ans比较,能更新就更新。具体为什么这样,用笔和纸画画,仔细想想应该能想出来,我开始也是不清楚,结果在纸上把各种情况一模拟,就清楚了View Code #include<cstdio>#include<cstring>#include<algor
        阅读全文
            
摘要:求一段连续的区间,使其在满足 和 对p取余不超过k的 前提 下 和最大 。做法:用线段树维护区间最小值,最小的下标,即最左边,因为加入sum[i]%p>k就要减去一个sum[id] , sum[id]%p在【sum[i]%p-k,sum[i]%p】之间,sum[i]-sum[id]的值就为以i结尾的满足条件的连续的一段数的最大值,所以id要尽可能小,所以就是求值在【sum[i]%p-k,sum[i]%p】之间的最左边的数的下标,即满足条件的数的下标最小值。每次更新时把进过的结点的下标最小值(如果能更新)都更新一遍View Code #include<cstdio>#inclu
        阅读全文
            
摘要:将线段树从左到右排序,然后从左往右依次添加线段,用一个vector记录能看到当前线段的线段编号,最后暴力统计一个小小的bug查了半天 memset(Hash,-1,sizeof(Hash))写成了memset(hash,-1,sizeof(-1));然后再测数据时一直没有发现初始化的问题,就悲剧了一个晚上,看来以后把memset写在宏定义里也不失为一个防止出错的选择View Code #include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespac
        阅读全文
            
摘要:一个小bug搞了半天,由于没有离散化,线段树的域的空间应该是4*maxn,maxn为x坐标的最大值View Code #include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 50000; //maxn 为 x 的最大值(如果离散化了就是矩形的个数*2)int sum[maxn<<2];int cover[ma
        阅读全文
            
摘要:PostersTime Limit: 5000/2000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2061Accepted Submission(s): 455Problem DescriptionTed has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. 
        阅读全文
            
摘要:View Code //树状数组 + 简单DP//同hdu 3450,dp的求和操作由树状数组来求执行#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 500010;const int mod = 1000000007;__int64 c[maxn],a[maxn],val[maxn],b[maxn];int tot;int lowbit(int x){ return x&-x;}void update(int x,__in
        阅读全文
            
摘要:先用简单线段树记录当前线段的左右两端下来的线段编号再从上往下dp下来即可具体做法:先按高度升序排序,依次插入各条线段,每次先询问,后插入线段树只有一个域id【x】,表示当前区域被哪个线段完全覆盖View Code #include<cstdio>#include<cstring>#include<ctype.h>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 111
        阅读全文
            
摘要:直接把hotel那题的代码拿过来,其他的都用stl来做了View Code #include<cstdio>#include<vector>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 55555;int lsum[maxn<<2],rsum[maxn<<2],msum[maxn<<2],cover[maxn<<2];struc
        阅读全文
            
摘要:需要记录上一次总的区间长度,和这一次的差的绝对值是x轴的增长量,再记录这段区间内共有几段子区间即可算出y轴的增长量同时要增加两个域lbd rbd(布尔型) 分别记录当前区间最左与最右的元素是否被覆盖View Code #include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int maxn = 22222;#define lson l,m,rt<<1#define rson m+1,r,rt<<
        阅读全文
            
摘要:记录三个域,每次更新相关信息之后传递上来。sum【】:当前节点的总共合法区间(被覆盖至少两次)cover【】:当前节点区间被覆盖次数once【】:区间内被覆盖一次的总量其他的独立思考应该能想出来View Code #include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 2222;double sum[maxn<&l
        阅读全文
            
摘要:View Code #include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 1000;double sum[maxn<<2];int cover[maxn<<2];double x[maxn];struct seg{ double l,r,h; int flag; seg(){} seg(doub
        阅读全文
            
摘要:1 a:询问有没有连续长度为a的空位置,插进最左边2 a b :【a,a+b-1】区间清空结点记录的信息:1:区间内最长的连续空位(msum)2:从当前区间的最左端开始的连续空位长度(lsum)3:从当前区间的最右端开始的连续空位长度(往左边数)(rsum)区间的最值就是max(左右子节点最长连续空位的最大值,lsum【rt<<1|1】+rsum[rt<<1])其他就基本上和一般的成段更新,懒惰标记没什么区别了View Code #include<cstdio>#define lson l,m,rt<<1#define rson m+1,r,rt
        阅读全文
            
摘要:题意就不说了有几个值得注意的地方,首先,数据范围太大,要离散化有一个问题就是1 5 5离散化后可能是1 3 2本来应该是1 2 3的,就会出错所以排序的时候加个按关键值排序,值相同时按id递增排序View Code #include<cstdio>#include<algorithm>using namespace std;const int mod=1000000007;const int maxn=100000;struct node{ int num; int id;}a[maxn+10];__int64 dp[maxn+10];int num[maxn+10];
        阅读全文
            
摘要:时间卡的比较紧,险过,成段更新,懒惰标记 发现define max(a,b)比写个函数来的快,不知为何区间记录三个域最大值、最小值、增加的值如果最大值<P,加上2*c如果最小值>=p,加上c;注意每次别忘了把信息pushdown下来就好了最后只需query()一次View Code #include<cstdio>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define max(a,b) (a>b?a:b)#define min(a,b) (a<b?a:b)const int ma
        阅读全文
            
摘要:被攻击的次数等于总的攻击次数减去成功防守的次数这道题目其实主要考如何计算成功防守的次数增加一个辅助数组pos【】,记录上次询问后某个点防守到了第几次进攻(第一次进攻都能防守住)每防守住一次,就过t0-1次进攻,继续判断接下来的第t0次进攻有没有包括(即攻击到)询问的点有的人说复杂度是O(n^2),最坏复杂度确实可能是O(q)*O(q/t)log(n),应该是超时边缘,但就是过了,开个挂后速度还不错。。。可能数据不强View Code #include<cstdio>#include<cstring>const int maxn = 20001;int c[maxn];s
        阅读全文
            
摘要:线段树做法http://www.cnblogs.com/wuyiqi/archive/2011/12/21/2295391.html从后往前操作View Code #include<cstdio>#include<cstring>const int maxn = 80001;int a[maxn],c[maxn],ans[maxn],n;int lowbit(int x){ return x&-x;}void update(int x,int d){ for(;x<maxn;x+=lowbit(x)) c[x]+=d;}int sum(int x){ in
        阅读全文
            
                    
                
浙公网安备 33010602011771号