hiho一下 第二十周(线段树模板)

基本上敲的模板

功能:区间更新,区间查询

自己随意开发。。

 1 #include "iostream"
 2 using namespace std;
 3 #define lson l,m,rt<<1
 4 #define rson m+1,r,rt<<1|1
 5 #define bug  cout << "bug!!!" << endl;
 6 const int MAXN = 100010;
 7
 8 int sum[MAXN<<2];
 9 int p[MAXN<<2];
10
11 //更新父亲
12 void PushUp(int rt)
13 {
14     sum[rt] = sum[rt<<1] + sum[rt<<1|1];
15 }
16
17 //下放
18 void PushDown(int rt,int m)
19 {
20     if (p[rt]) {
21         p[rt<<1] = p[rt<<1|1] = p[rt];
22         sum[rt<<1] = (m-(m>>1)) * p[rt];
23         sum[rt<<1|1] = (m >> 1) * p[rt];
24         p[rt] = 0;
25     }
26 }
27
28 void build(int l,int r, int rt)
29 {
30     if (l == r) {
31         cin >> p[rt];
32         sum[rt] = p[rt];
33         return;
34     }
35     int m = (l+r)>>1;
36     build(lson);
37     build(rson);
38     PushUp(rt);
39 }
40
41 //区间更新
42 void update(int L,int R,int c,int l,int r,int rt)
43 {
44     if (L <= l && r <= R) {
45         p[rt] = c;
46         sum[rt] = c*(r-l+1);
47         return;
48     }
49     PushDown(rt,r-l+1);
50     int m = (l+r)>>1;
51     if (L <= m) update(L,R,c,lson);
52     if (R >  m) update(L,R,c,rson);
53     PushUp(rt);
54 }
55
56 //区间查询
57 int query(int L, int R,int l,int r,int rt)
58 {
59     if(L <= l && R >= r) {
60         return sum[rt];
61     }
62     PushDown(rt,r-l+1);//比如rt存的1-4的和 而查询2-5 1-4就需要下放
63     int m = (l + r) >> 1;
64     int re = 0;
65     if (L <= m) re += query(L,R,lson);
66     if (R >  m) re += query(L,R,rson);
67     return re;
68 }
69
70 int main()
71 {
72     int n;
73     cin >> n;
74     build(1,n,1);
75     int t;
76     cin >> t;
77     for (int i = 0;i < t;++ i) {
78         int ok;
79         cin >> ok;
80         if (ok) {
81             int p1,p2,c;
82             cin >> p1 >> p2 >> c;
83             update(p1,p2,c,1,n,1);
84         }
85         else {
86             int p1,p2;
87             cin >> p1 >> p2;
88             cout << query(p1,p2,1,n,1) << endl;
89         }
90     }
91 }

代码君

时间: 2025-01-07 10:25:19

hiho一下 第二十周(线段树模板)的相关文章

hiho一下 第二十一周(线段树 离散化)

知识点1:离散化  对于这些区间来说,其实并不会在乎具体数值是多少,而是在他们的左右端点之间互相进行比较而已.所以你就把这N个区间的左右端点——2N个整数提出来,处理一下呗?你要注意的是,这2N个数是什么其实并不重要,你可以把这2N个数替换成为任何另外2N个数,只要他们之间的相对大小关系不发生改变就可以.” 解决方法: 那么我需要额外做的事情就是在构建线段树之前对区间进行预处理:将区间的左右端点选出来,组成一个集合,然后将这个集合依次对应到正整数集合上,并且利用这个对应将原来的区间的左右端点更换

线段树模板hdu 1754:I Hate It

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 100523    Accepted Submission(s): 37845 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的

线段树模板(结构体)

线段树研究了两天了,总算有了点眉目,今天也把落下的题,补了一下. 贴一份线段树模板 线段树的特点: 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

[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