随笔分类 - 线段树
摘要:http://acm.timus.ru/problem.aspx?space=1&num=1126简单的线段树求区间最值 1 #include 2 #include 3 #include 4 using namespace std; 5 const int N=100002; 6 int a[N],ans ; 7 struct node 8 { 9 int l,r,Max;10 } tree[N*4];11 void build(int t,int l,int r)12 {13 tree[t].l = l;14 tree[t].r = r;15 if (l...
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1754简单线段树题,单点更新+区间求最大值。 1 #include 2 #include 3 #include 4 using namespace std; 5 const int N=200002; 6 int a[N],ans = 0; 7 struct node 8 { 9 int l,r;10 int Max;11 } tree[4*N];12 void build(int root,int l,int r)13 {14 tree[root].l = l;15 t...
阅读全文
摘要:http://poj.org/problem?id=3368题意:给出一个非降序排列的整数数组,对于询问(i,j),输出区间[i,j]中出现最多的值的次数。思路:经典的RMQ,不过我用线段树做的。首先要离散化,因为是非降序的,所以相同的数是连续的,可以将相同的数分在同一个块中,将块中的信息存储起来,然后将其看成一个点,就可以用线段树进行查询了。 1 #include 2 #include 3 #include 4 using namespace std; 5 const int N=1000010; 6 struct node 7 { 8 int l,r; 9 int M...
阅读全文
摘要:http://poj.org/problem?id=2777题意:有一艘长为L的船,初始颜色为1,将船分为L段,用T种颜色去染。有O种操作,C A B C:表示将区间[A,B]段染成C颜色。 P A B:表示询问[A,B]段中有多少不同的颜色。思路:线段树的区间修改。 1 #include 2 #include 3 #include 4 #define lson l,mid,rt>1; 21 build(lson); 22 build(rson); 23 } 24 void pushdow(int rt)//标记传递 25 { 26 if (Tree[rt...
阅读全文
摘要:http://poj.org/problem?id=2828题意:有n个人排队买票,每个人的位置为pos(0~n-1),价值是val,如果位置相同,来的晚的人可以插入他选的位置。最后输出队列中的val。 1 #include 2 #include 3 const int N=200010; 4 int Tree[N*4],seq[N]; 5 int pos[N],val[N]; 6 7 void build(int l,int r,int rt) 8 { 9 Tree[rt]=r-l+1;//初始每个区间的空位数10 if(l==r)11 return;1...
阅读全文
摘要:http://poj.org/problem?id=3468题意:就是区间的查询与更新。。#include #include const int maxn=100010;const int maxm=6600000;#define LL long longstruct node{ int a,b; int l,r; LL sum; LL h;} A[maxm];int Num[maxn],cnt;void build(int root){ int a = A[root].a; int b = A[root].b; int mid = (a+b)>>...
阅读全文