随笔分类 -  线段树

摘要:这个题目属于线段树的点更新区间查询,而且查的是整个区间,其实不用写query()函数,只需要输出根节点保存的值就可以了。题意: 输入n,m表示有2^n个数和m个更新,每次更新只把p位置的值改成b,然后输出整个序列运算后的值,而这个运算就比较复杂了, 最下面一层两个数字之间或运算得到原来数目一半的数字,然后两个之间异或运算,得到一半,再或再异或………………,一直到得到一个数字,这个数字就是要求的结果。思路: 如果只是一种运算,这就是简单的线段树点更新,区间查询。而现在,我们要确定什么时候用or 什么时候用xor, 想想看,最下面一层是用or, 总共有n层,因为or和xor是交替进行的,我... 阅读全文
posted @ 2013-08-27 11:19 xindoo 阅读(286) 评论(0) 推荐(0)
摘要:题目链接 题目很长,看加猜加谷歌翻译才看懂了题目。每级台阶的宽都是1,但高不同,并且告诉你了,然后给你m个箱子,长和宽都告诉你,把箱子靠左放,求箱子的底部有多高。 因为都是放在最左边的,所以只要和最左边的高度比较,这样就不用更新线段树了。代码://cf 272 C//2013-05-14-20.26#include using namespace std;const int maxn = 100005;struct node{ int l, r, mid; __int64 m;}tree[maxn b) return a; return b;}void bui... 阅读全文
posted @ 2013-05-14 20:28 xindoo 阅读(201) 评论(0) 推荐(0)
摘要:题目链接线段树解法#include #include using namespace std;const int maxn = 100010;struct node{ int l, r, mid, minn;}tree[maxn> 1; tree[o].mid = m; if (l == r) { tree[o].minn = a[l]; return ; } build(l, m, o tree[o].mid) return query(l, r, (o#include using namespace std;co... 阅读全文
posted @ 2013-04-12 13:24 xindoo 阅读(161) 评论(0) 推荐(0)
摘要:题目链接DescriptionN (2 #include #include #define maxn 8005using namespace std;int a[maxn];int ans[maxn];int n;int lowbit(int x){ return x&(-x);}void add(int x, int v){ while (x 0) { s += a[x]; x -= lowbit(x); } return s;}int binarysearch(int l, int r, int x){ if (l == r... 阅读全文
posted @ 2013-04-06 09:57 xindoo 阅读(168) 评论(0) 推荐(0)
摘要:暴力超时,这道题可以用线段树做,因为更新的是单个节点,我们也可以用数组数组来做,我将两种方法的代码都给出 数组数组最适宜的用途就是区间求和和点的更新,但树状数组并不适用于区间的更新问题,也不是做不到,比较麻烦且难理解,有兴趣的可以看看这个http://blog.csdn.net/xindoo/article/details/8748410//树状数组#includeint n,ans[50005],f[50005];int lowbit(int n){ return n&(-n);}void add(int i,int v){ while(i #include #define... 阅读全文
posted @ 2013-04-01 21:31 xindoo 阅读(120) 评论(0) 推荐(0)
摘要:传送门题目意思很简单,有N个数,Q个操作, Q l r 表示查询从l到r 的和,C l r v 表示将从l到r 的值加上v,明显的线段树,不知道线段树的人肯定暴力,肯定超时,哈哈!!解题方法我在代码注释中写的很详细了#include const int maxn = 100005; typedef __int64 ll; //Hint The sums may exceed the range of 32-bit integers.int a[maxn];struct node ... 阅读全文
posted @ 2013-04-01 20:07 xindoo 阅读(122) 评论(0) 推荐(0)
摘要:这两题我都在之前做过,但并未通过,那次做的时候是刚开始接触线段树,现在有了一点点的了解,翻出以前的代码稍作修改就AC了。之前1698错误的原因是没有注意到位运算的优先级。//hdoj 1698#include#include#define maxn 100010struct node{ int l; int r; int mid; int val;}tree[maxn>1; if(l == r) return ; else { buildtree(o tree[o].mid) update((o 0) ... 阅读全文
posted @ 2013-01-08 21:31 xindoo 阅读(177) 评论(0) 推荐(0)