存一下线段树模板

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<map>
 5 #define ls l,mid,rt<<1
 6 #define rs mid+1,r,rt<<1 |1
 7 using namespace std;
 8 const int mxn=100020;
 9 int data[mxn*4];
10 int tre[mxn],mini[mxn];
11 int add[mxn];//懒标记
12 void pushup(int rt){
13     mini[rt]=min(mini[rt<<1],mini[rt<<1 |1]);
14 }
15 void pushdown(int rt){//向下传递lazy标记
16     if(add[rt]!=0){
17         add[rt<<1]+=add[rt];
18         add[rt<<1 |1]+=add[rt];
19         mini[rt<<1]+=add[rt];
20         mini[rt<<1 |1]+=add[rt];
21         add[rt]=0;//标记传递完毕,清除
22     }
23     return;
24 }
25 void Build(int l,int r,int rt){//建树
26     if(l==r){
27         mini[rt]=data[l];
28         return;
29     }
30     int mid=(l+r)>>1;
31     Build(ls);Build(rs);
32     pushup(rt);
33     return;
34 }
35 void update_point(int p,int x,int l,int r,int rt){//单点更新
36     if(l==r){
37         mini[rt]=x;
38         return;
39     }
40     int mid=(l+r)>>1;
41     if(p<=mid)update_point(p,x,ls);
42     else update_point(p,x,rs);
43     return;
44 }
45 void update_area(int L,int R,int c,int l,int r,int rt){//区域更新
46     if(L<=l && r<=R){
47         add[rt]+=c;
48         mini[rt]+=c;
49     }
50     pushdown(rt);
51     int mid=(l+r)>>1;
52     if(L<=mid)update_area(L,R,c,ls);
53     if(R>mid)update_area(L,R,c,rs);
54     pushup(rt);
55     return;
56 }
57 int query(int L,int R,int l,int r,int rt){//区域查询
58     if(L<=l && r<=R)return mini[rt];
59     int mid=(l+r)>>1;
60     int ans=1000000;
61     if(L<=mid)ans=min(ans,query(L,R,ls));
62     if(mid<R)ans=min(ans,query(L,R,rs));
63     return ans;
64 }
65 int main(){
66
67
68
69 }
时间: 2024-12-17 03:41:14

存一下线段树模板的相关文章

线段树模板(结构体)

线段树研究了两天了,总算有了点眉目,今天也把落下的题,补了一下. 贴一份线段树模板 线段树的特点: 1. 每一层都是区间[a, b]的一个划分,记 L = b - a 2. 一共有log2L层 3. 给定一个点p,从根到叶子p上的所有区间都包含点p,且其他区间都不包含点p. 4. 给定一个区间[l; r],可以把它分解为不超过2log2 L条不相交线段的并. 总结来说:线段树最近本的应用是4点: 1.单点更新:单点替换.单点增减 2.单点询问 3.区间询问:区间之和.区间最值 4.区间更新:区间

[ACM] 线段树模板

#include<iostream> #include<cmath> using namespace std; #define maxn 200005 class Node{ public: int l,r; int add;//附加值 int sum; }node[maxn]; int getRight(int n){//获得满足2^x>=n的最小x[从0层开始,给编号获得层数] return ceil(log10(n*1.0)/log10(2.0)); } void bu

POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 140120   Accepted: 43425 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type o

[POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> using namespace std; int n,q,tot,a[110000]; in

hdu 4819 二维线段树模板

/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int N, Q; struct Nodey { int l, r; int Max, Min; }; int locx[MAXN], l

【HDU 4819】Mosaic 二维线段树模板

二维线段树的模板题,和一维一样的思路,更新的时候注意一下细节. 存模板: /* 二维线段树模板整理 */ #include<cstdio> #include<algorithm> using namespace std; #define lson (pos<<1) #define rson (pos<<1|1) const int maxn = 805; const int INF = (1 << 30); int n; int posX[max

LA 2191电位计(线段树模板题)

线段树模板题,没啥好说的.....注意输出是case之间空一行就行.........之前一直没注意,一直wa 代码如下: #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #includ

洛谷P3372线段树模板1——线段树

题目:https://www.luogu.org/problemnew/show/P3372 线段树模板. 代码如下: #include<iostream> #include<cstdio> using namespace std; long long n,m,a[100005],ct; struct N{ long long lazy,sum; long long ls,rs; }p[200005]; void pushdown(long long cur,long long l

P3373 线段树模板

好,这是一个线段树模板. 1 #include <cstdio> 2 using namespace std; 3 const long long int N=1000010; 4 long long int sum[N],tag1[N],tag2[N],mo; 5 6 void build(int l,int r,int o) 7 { 8 tag1[o]=1; 9 if(l==r) 10 { 11 scanf ("%d",&sum[o]); 12 return;