随笔分类 - 数据结构
摘要:题目抽象:给定最大的覆盖次数k和q条线段对于每条线段判断是否能覆盖在相应区间上使得该区间上最大的覆盖次数<k,输出这些线段的编号有两种做法,一种使用懒惰标记,一种不使用,时间差不多先讲使用懒惰标记的没什么好讲的,在需要更新或查询的时候将信息往下传即可,上代码View Code #include<stdio.h>#include<cstring>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mid (l+r)>>1const int maxn = 1000010;i
阅读全文
摘要:线段树区间染色,判断区间颜色的数目典型的成段更新,懒惰思想View Code #include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 100010;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int col[maxn<<2];//是否被某种颜色完全覆盖 int sum[maxn<<2];//刚开始想只用一个域来表示,但是怕会分不清楚的,所以
阅读全文
摘要:坑哥题写完就跑出了样例,挺顺的结果没注意到开方最多开8次就不用开了 TLE没用__int64 WA没想到x>y继续WA最后还有一个PE人生啊,总是不能一帆风顺,偶尔会在得意时给你当头一棒View Code #include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mid (l+r)>
阅读全文
摘要:以前用RMQ做过,刚好最近在做线段树,就用线段树也做了下,1A~两个域,分别保存当前结点下面的最大值和最小值View Code #include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 55555;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int MIN[maxn<<2];int MAX[maxn<<2];int min(int a,int
阅读全文
摘要:View Code #include<cstdio>#include<cstring>const int maxn = 50010;int c[maxn];int lowbit(int x){ return x&-x;}void update(int x,int d){ for(;x<=maxn;x+=lowbit(x)) c[x]+=d;}int query(int x){ int sum=0; for(;x>0;x-=lowbit(x)) sum+=c[x]; return sum;}int main(){ int t,n,...
阅读全文
摘要:注释在代码中两个树状数组,或者用两次第一次维护的是空位的位置,update,更新管辖范围内空位的个数,getk获得第k个空位置的个数,其实就是第k小数,然后占据该位置,更新相应信息第二次维护的是最长不下降子序列的长度,update:更新长度,getm:获得当前位置之前的最长的LIS长度,再把当前的数加进末尾View Code #include<stdio.h>#include<string.h>#define lowbit(i) (i&(-i))const int MAXN=100005;const int MAXLOG=17;int B[MAXN],pos[M
阅读全文
摘要:www.notonlysuccess.com本小节主要特点:成段更新,懒惰标记我对懒惰标记的解释:如果当前的区间被要更新的区间所覆盖,直接就改变当前结点的信息,不用再往下了,所以,每次在更新的时候,只更新一层,把当前结点的信息往下传递一层,以供下一步使用,如果下一步完成了更新,更新也就结束了,没有完成,继续往下传递一层。。。。以下是一些题目,随时更新。。。。hdu 1698 just a hookView Code #include<cstdio>#include<algorithm>using namespace std;#define lson l,m,rt<
阅读全文
摘要:题意坑爹,应该是求大于a的第k小的数。。线段树思路:更新操作就不说了,和另外一篇的一样http://www.cnblogs.com/wuyiqi/archive/2011/12/26/2301504.html关键是询问操作:问大于a的第k小的数,首先判断有几个数小于a,设为t,则题意就转换为了求整个区间的第k+t小的数求之前先特判一下有没有k+t个数线段树代码如下:View Code #include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn
阅读全文
摘要:树状数组实现好了之后,改用线段树也实现了一下,接下来再用各种树实现,继续学习!!View Code #include<cstdio>#include<cstring>const int maxn = 222222;#define mid (l+r)>>1#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int sum[maxn<<2];//记录有几个点的范围在节点的范围内int a[maxn],p[maxn];int n;/*void pushup(int rt){ sum[r
阅读全文
摘要:来自http://www.cnblogs.com/oa414/archive/2011/07/21/2113234.html的启发,看上述博客如何求第k大的数时,被其第二份代码影响,感觉很巧妙,于是研究了一下,搞懂后顿时神清气爽啊。。。还是看这张经典的图吧,知识在图上就变得形象多了现在假设要求sum[a]的值,一般我们都是从后往前求和,如a=1515-lowbit(15)=14;14-lowbit(14)=12;12-lowbit(12)=8;8-lowbit(b)=0;答案就是sum[15]+sum[14]+sum[12]+sum[8];现在我们可以这样来求,从不超过15的只有一个1的最大二
阅读全文
摘要:分成左右两个部分a1,a2和-a3,-a4,-a5View Code import java.io.*;import java.util.*;import java.math.*;public class Main{ public static void main(String[] args){ Scanner cin = new Scanner (System.in); int a1=cin.nextInt(); int a2=cin.nextInt(); int a3=cin.nextInt(); int a4=c...
阅读全文
摘要:风格:notonlysuccess感觉刚开始就看notonlysuccess可能不太好,建议还是先研究一下网上的其他风格,搞透之后在比较一下,这样就能充分理解并加以运用了下面就从一些题来初识一下线段树hdu 1394 Minimum Inversion Number求逆序数的功能View Code #include<cstdio>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 5010;i
阅读全文
摘要:转自http://www.cppblog.com/baby-fly/archive/2010/08/03/122027.aspx?opt=admin很显然是动态规划。dp[i]表示前i个数有多少个有效的子序列。那么 dp[i]=dp[i-1]+A。 A是前面i-1个数中,与i的差值不超过d的以该数结尾的有效的子序列的个数 的和。我们可以用另外一个数组sub[i]表示以i结尾的有效的子序列的个数。 dp与sub的不同之处是dp中的子序列不一定是以第i个数结尾的。sub[i]= sigma sub[k] ,( abs(numk],num[i])<=d )。 由于求sub的时间复杂度为O(n^
阅读全文
摘要:View Code #include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int MAX = 800000;int a[800000];bool is(int x){ int i=0,a[11]; memset(a,0,sizeof(a)); while(x>0) { int tmp=x%10; a[tmp]++; if(a[tmp]>=2) return false; x/=10; }...
阅读全文
摘要:我决定:以后每一场codeforces我都将进行总结,尽量在赛后把题目AK(*_*),水平有限,只能说尽量。难得有一场CF的比赛在白天开始,于是我准时的守候在电脑前准备打一场CF,哈哈a题b题很快过了,c题卡了一下,原因是在poj做过类似的一道题,http://www.cnblogs.com/wuyiqi/archive/2011/09/20/2182986.html但是这道题目只需要求有几个区间能被其他区间完全覆盖,并不需要求每个区间分别能被几个区间覆盖,这是本质差别对于这道题,只需要排个序(没必要用树状数组)x递增,y递减所以后面的x肯定大于等于前面的x,现在只需要比较y即可,如果y小于m
阅读全文
摘要:求小于n的孪生素数有几对。。。。暴力貌似也可以过,我用了树状数组,预处理了一下,每次输入n直接求sum(n),表示小于n的孪生素数的对数View Code #include<stdio.h>#include<string.h>#define MAX 100000int c[100000];int lowbit(int x){ return x&(-x);}void add(int x,int num){ while(x<=MAX) { c[x]+=num; x+=lowbit(x); }}int sum(int x){ int ans...
阅读全文
摘要:建议先做poj 2481http://poj.org/problem?id=2481我的详细解答http://www.cnblogs.com/wuyiqi/archive/2011/09/20/2182986.html这道题目就是加了点东西,每个区间的数据范围变为32位,不离散化的话树状数组保存不下来所以先进行离散化,按照poj 2481的做法,不通的地方是,只按x排序后对x坐标进行离散化,即依次按x从小到大把把各个排序后的区间标记为1,2,3.。。。就相当于所有的x的坐标都变为了1~n的数,这样做不会影响结果,因为x间的相互关系还是不变的随后再进行一次排序,跟poj2481一样了,不再赘述V
阅读全文
摘要:很多的错误,狂交了几十遍,哈哈~最后总结如下:RE:数组越界WA:字母拼错建trie树之后再加一个并查集,用来查询图是否连通以及很方便的记录每个点的度数View Code //runtime error 的原因:初始化的时候从1到MAX赋值,数组越界,牢记啊!!!//WA原因: Impossible拼错。。囧!#include<stdio.h>#include<string.h>#include<stdlib.h>#include<iostream>using namespace std;const int MAX = 500010;class
阅读全文
摘要:注意路径压缩时的权值合并View Code #include<cstdio>#include<cstring>#include<stdlib.h>const int MAX = 20010;int p[MAX],w[MAX];void init(int n){ for(int i=0;i<=n;i++) { p[i]=i; w[i]=0; }}void unio(int x,int y){ p[x]=y; w[x]=(abs(x-y)%1000);}int find(int x){ if(p[x]==x) return x; ...
阅读全文
摘要:View Code 1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 struct node{ 6 char s[20]; 7 int canshu; 8 int priority; 9 int num;10 bool operator < (const node&b) const{11 if(priority==b.priority)12 return num>b.num;13 ...
阅读全文

浙公网安备 33010602011771号