线段树 + 区间更新 + 模板 ---- poj 3468
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 59798 | Accepted: 18237 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
Hint
Source
【题目大意】
一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和。(这里区间指的是数列中连续的若干个数)对每次询问给出结果。
【题目分析】
裸的区间更新线段树。
线段树单点更新和区间更新的区别:
1.每个结点中多了一个add值,代表该结点以下的结点需要增加的值;
2.build函数中,如果在建树的过程中就赋值给num,那么在建完树之后不要忘记pushup,因为此时只是叶子结点有值,上面的值都为空;这个在区间更新中很常用,因为区间更新中如果输入一个值,然后更新一个值,这样会很麻烦,会耗费更多的时间;
3.update函数中,区间更新多了一个upshdown函数,并且更新sum和add值的判断条件是树中结点的l~r和要更新的区间的l~r相等,此时sum加的值是整个区间的长度*要更新的值,然后add值记录后面每个结点需要加上的值,即:c;
4.upshdown函数最后不要忘了将延时标记add清零;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | //Memory Time // K MS #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack> #include<iomanip> #include<string> #include<climits> #include<cmath> #define MAX 110000 #define LL long long using namespace std; LL n,m; LL ans; struct Tree { LL l,r; LL sum,add; }; Tree tree[MAX*3]; void pushup(LL x) { LL tmp=2*x; tree[x].sum=tree[tmp].sum+tree[tmp+1].sum; } void pushdown(LL x) { LL tmp=2*x; tree[tmp].add+=tree[x].add; tree[tmp+1].add+=tree[x].add; tree[tmp].sum+=tree[x].add*(tree[tmp].r-tree[tmp].l+1); tree[tmp+1].sum+=tree[x].add*(tree[tmp+1].r-tree[tmp+1].l+1); tree[x].add=0; } void build( int l, int r, int x) { tree[x].l=l; tree[x].r=r; tree[x].add=0; if (l==r) { scanf ( "%lld" ,&tree[x].sum); return ; } int tmp=x<<1; int mid=(l+r)>>1; build(l,mid,tmp); build(mid+1,r,tmp+1); pushup(x); //如果在建树的过程中给sum赋值,记得后面要pushup } void update(LL l,LL r,LL c,LL x) { if (r<tree[x].l||l>tree[x].r) return ; if (l<=tree[x].l&&r>=tree[x].r) { tree[x].add+=c; tree[x].sum+=c*(tree[x].r-tree[x].l+1); return ; } if (tree[x].add) pushdown(x); LL tmp=x<<1; update(l,r,c,tmp); // !!! update(l,r,c,tmp+1); pushup(x); } void query(LL l,LL r,LL x) { if (r<tree[x].l||l>tree[x].r) //要更新的区间不在该区间上 return ; if (l<=tree[x].l&&r>=tree[x].r) //要更新区间包括了该区间 { ans+=tree[x].sum; return ; } if (tree[x].add) pushdown(x); LL tmp=x<<1; LL mid=(tree[x].l+tree[x].r)>>1; if (r<=mid) query(l,r,tmp); else if (l>mid) query(l,r,tmp+1); else { query(l,mid,tmp); query(mid+1,r,tmp+1); } // pushup(x); } int main() { // freopen("cin.txt","r",stdin); // freopen("cout.txt","w",stdout); while (~ scanf ( "%lld %lld" ,&n,&m)) { build(1,n,1); char str[5]; while (m--) { scanf ( "%s" ,str); if (str[0]== 'Q' ) { LL l,r; scanf ( "%lld %lld" ,&l,&r); ans=0; query(l,r,1); printf ( "%lld\n" ,ans); } else { LL l,r,c; scanf ( "%lld %lld %lld" ,&l,&r,&c); update(l,r,c,1); } } } return 0; } |
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3879043.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?