线段树

hdu 3340 Rain in ACStar 线段树区间等差数列更新

一个人想着一个人 提交于 2020-01-05 03:17:32
Rain in ACStar Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 Description Maybe you have heard of Super Cow AC who is the great general of ACM Empire. However, do you know where he is from? This is one of the ten biggest secrets of this world! And it is time to expose the truth! Yes, Super Cow AC is from ACStar which is ten million light-year away from our earth. No one, even AC himself, knows how AC came to our home. The only memory in his head is the strange rain in ACStar. Because of the special gravity of ACStar, the raindrops in ACStar have many funny

HDU 4348 To the moon (主席树区间更新)

半腔热情 提交于 2020-01-05 03:16:53
题意:首先给你n个数,开始时间为0,最后按照操作输出 给你四种操作: 1. C l r d : 在(l,r)区间都加上d,时间加一 2. Q l r : 询问现在(l,r)的区间和 3. H l r t : 询问在t的时间(l,r)的区间和 4. B t : 直接回到t的时间 题解:首先是区间修改区间查询,可以想到线段树,接着就是询问历史版本与回到历史版本,这样就是主席树了 首先我们知道普通主席树是单点修改,并支持历史版本的区间求和与回到历史版本(就是这删除之后的树),仅仅只是因为它存了多棵线段树 而我们这儿是要进行区间修改,所以第一反应就是模拟线段树的lazy标记,并在查询时再更新再建树,但是这样会卡空间 因此我们需要这样想,模拟lazy标记进行重建树(最多建立2*log2(n)个节点)是必须的,但是查询时就不需要重建树了 这样我们就需要记录两个值:sum代表这一段中被增加区间的与此这一段的区间相交的总和,com代表这一段都需要增加这么多 这时我们查询时就需要每次加上com与待查询的区间相交的值(加上之前更新的),最后再包含的区间里加上sum再减去这儿com与待查询的区间相交的值(重复了) #include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<vector>

线段树总结

我们两清 提交于 2020-01-05 03:16:36
线段树总结 导引 有时候我们经常需要对一组序列进行操作,修改或者查询一段区间的信息,朴素的算法是暴力修改暴力查询,但如果数据范围到 \(10^4\) 以上的话就可能会超时,所以,我们就需要用一些数据结构去维护这个序列,线段树就是其中之一。 思想 对于一个序列,我们用二分的思想,将他分为两个长度相等的区间,对每个区间内的所有信息分别维护,比如区间和,区间最大值,区间积等等,这个我们可以直接很快将两个区间信息合并,就成了整个区间的信息,而如果区间内只有一个元素的时候,它的信息就可以直接通过这个值得出,然后一步步合并,我们就把整个序列维护成了一颗完全二叉树,所以树高是log级别的,并且我们知道完全二叉树的节点编号的是有规律的,一个节点的编号位N,那么左儿子是N×2和右儿子是N×2+1,这样构建就非常方便,而这个构建过程往往使用递归实现。 以区间求和为例: void build(int now,int l,int r){ if(l==r){ tr[now]=a[l]; return; } int mid=(l+r)>>1; build(now*2,l,mid); build(now*2+1,mid+1,r); tr[now]=tr[now*2]+tr[now*2+1]; } 修改 一般题目都不会仅仅只有查询操作,往往都会有修改操作。修改一个区间我们往往不一一修改包括这个区间的节点

线段树入门——区间求和,区间更新(模板)

安稳与你 提交于 2020-01-05 03:16:08
线段树入门 区间求和,区间更新。 题目链接: https://cn.vjudge.net/problem/POJ-3468 #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; const int maxn=100000; const int maxsize=4*maxn; struct Node { ll sum; ll tag; }; Node data[maxsize]; void add(int v,int a,int b,int k,int l,int r) { if(b<=l||a>=r) return; if(a<=l&&b>=r) data[k].tag+=v; else { data[k].sum+=(min(r,b)-max(l,a))*v; add(v,a,b,2*k+1,l,(l+r)/2); add(v,a,b,2*k+2,(l+r)/2,r); } } ll query(int a,int b,int k,int l,int r) { if(b<=l||a>=r) return 0; if(a<=l&&b>=r) return data[k].tag*(r-l)+data[k].sum; ll sum=0; sum+=data[k].tag*

hdu1754(线段树单点替换&区间最值模板)

点点圈 提交于 2020-01-05 03:15:54
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:中文题诶~ 思路:线段树单点替换&区间最大值查询模板 代码: 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define lson l, mid, rt << 1 5 #define rson mid + 1, r, rt << 1 | 1 6 using namespace std; 7 8 const int MAXN = 2e5 + 10; 9 int Max[MAXN << 2];//Max[rt]存储rt对应区间的最大值 10 11 void push_up(int rt){//更新rt的值 12 Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]); 13 } 14 15 //建树 16 void build(int l, int r, int rt){//rt对应区间[l, r] 17 if(l == r){ 18 scanf("%d", &Max[rt]); 19 return; 20 } 21 int mid = (l + r) >> 1; 22 build(lson); 23 build(rson); 24 push_up

hdu 5443 The Water Problem 线段树

荒凉一梦 提交于 2020-01-05 03:15:42
The Water Problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5443 Description In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,an representing the size of the water source. Given a set of queries each containing 2 integers l and r, please find out the biggest water source between al and ar. Input First you are given an integer T(T≤10) indicating the number of test cases. For each test case, there is a number n(0≤n≤1000) on a line representing

poj 3264 Balanced Lineup - 线段树

限于喜欢 提交于 2020-01-05 03:14:25
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height. Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and

POJ 3264 Balanced Lineup(线段树)

被刻印的时光 ゝ 提交于 2020-01-05 03:14:00
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23699 Accepted: 11019 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height. Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1

(线段树 && 字符串的处理)codeforces -- 570C

我是研究僧i 提交于 2020-01-05 03:13:30
链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#problem/J Description Daniel has a string s , consisting of lowercase English letters and period signs (characters ' .'). Let's define the operation of replacement as the following sequence of steps: find a substring " .." (two consecutive periods) in string s , of all occurrences of the substring let's choose the first one, and replace this substring with string " .". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string s contains no two consecutive periods, then

POJ 3667 Hotel(线段树+区间合并)

て烟熏妆下的殇ゞ 提交于 2020-01-05 03:13:17
http://poj.org/problem?id=3667 题意: 有N个房间,M次操作。有两种操作(1)"1a",表示找到连续的长度为a的空房间,如果有多解,优先左边的,即表示入住。(2)"2 b len",把起点为b长度的len的房间清空,即退房。 思路: 区间合并的线段树题。 其实如果单点更新和区间更新做得多了的话,区间合并也就不难了。 对于这道题,我们用线段树维护三个值,从左端开始的连续空房间数,从右边开始的连续空房间数,以及整个区间内最大的连续空房间数。 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6 #include<stack> 7 #include<queue> 8 #include<cmath> 9 #include<map> 10 #include<set> 11 using namespace std; 12 typedef long long ll; 13 const int INF = 0x3f3f3f3f; 14 const int maxn=50000+5; 15 16 int n, m; 17 18 struct node //维护从左端开始的最大连续空房间,从右端开始的最大连续空房间