poj 3468 A Simple Problem with Integers 线段树 成段更新
来源:互联网 发布:东莞大朗淘宝电商培训 编辑:程序博客网 时间:2024/06/08 06:43
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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915线段树加lazy思想lazy 思想:在a~b期间上加c,暂时没必要加到每个点(能偷懒就偷懒嘛),在表示区间的root结构体上增加一个inc域,将要加的值赋给这个inc域,然后就不要再往下了。在求区间和时,将root中的inc值赋给要求的区间,并且将该节点的root置零。
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;struct st{ int l; int r; ll n;//记录区间的加的值 ll sum;//区间的求和值}t[400000];void build(int l,int r,int k){ t[k].l=l; t[k].r=r; t[k].n=0; if(t[k].l==t[k].r) { scanf("%lld",&t[k].sum); return ; } int mid=(t[k].l+t[k].r)>>1; build(l,mid,2*k); build(mid+1,r,2*k+1); t[k].sum=t[k*2].sum+t[k*2+1].sum;}void push(int k,int num){ if(t[k].n) { t[k<<1].n+=t[k].n; t[k<<1|1].n+=t[k].n; t[k<<1].sum+=t[k].n*(num-(num>>1)); t[k<<1|1].sum+=t[k].n*(num>>1); t[k].n=0; }}void insert(int n,int l,int r,int k){ if(t[k].l==l&&t[k].r==r) { t[k].n+=n; t[k].sum+=(ll)n*(r-l+1); return ; } if(t[k].l==t[k].r) return ; push(k,t[k].r-t[k].l+1); int mid=(t[k].l+t[k].r)>>1; if(r<=mid) insert(n,l,r,2*k); else if(l>mid) insert(n,l,r,2*k+1); else { insert(n,l,mid,2*k); insert(n,mid+1,r,2*k+1); } t[k].sum=t[k*2].sum+t[k*2+1].sum;}ll search(int l,int r,int k){ if(t[k].l==l&&t[k].r==r) { return t[k].sum; } push(k,t[k].r-t[k].l+1); ll res=0; int mid=(t[k].l+t[k].r)>>1; if(r<=mid) res+=search(l,r,2*k); else if(l>mid) res+=search(l,r,2*k+1); else { res+=search(l,mid,2*k); res+=search(mid+1,r,2*k+1); } return res;}int main(){ int n,m; int i,temp; int a,b,c; char s[5]; while(~scanf("%d%d",&n,&m)) { build(1,n,1); for(i=1;i<=m;i++) { scanf("%s",s); if(s[0]=='Q') { scanf("%d%d",&a,&b); printf("%lld\n",search(a,b,1)); } else { scanf("%d%d%d",&a,&b,&c); insert(c,a,b,1); } } } return 0;}
- 线段树 POJ 3468 A Simple Problem with Integers 线段树 成段更新入门
- poj 3468 A Simple Problem with Integers(线段树|成段更新,区间查询)
- POJ 3468 A Simple Problem with Integers 线段树 成段更新
- poj 3468 A Simple Problem with Integers(线段树,成段更新)
- 线段树模板(二)——成段更新 + POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers 线段树 (成段更新)
- [ACM] poj 3468 A Simple Problem with Integers(线段树,成段更新,懒惰标记)
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
- 线段树(成段更新) 之 poj 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers (线段树---成段更新)
- POJ 3468-A Simple Problem with Integers(线段树:成段更新,区间求和)
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
- POJ 3468 A Simple Problem with Integers(线段树 成段更新)
- POJ 3468 A Simple Problem with Integers 线段树的成段更新
- POJ - 3468 - A Simple Problem with Integers (线段树 - 成段更新)
- POJ 3468 A Simple Problem with Integers (线段树,成段更新,区间求和)
- POJ 3468 A Simple Problem with Integers 线段树(成段更新)
- poj 3468 A Simple Problem with Integers 线段树 成段更新
- 使用批处理实现telnet自动登陆并执行ping命令
- 着手使用@AspectJ
- 嵌入式学习日记(十四)
- 海思arm平台AAC音频转码cpu占用高、效率低的问题解决
- 西部开源运维学习unit5
- poj 3468 A Simple Problem with Integers 线段树 成段更新
- 重建二叉树
- 客户端浏览器向服务器发起http请求的全过程
- 学生信息管理系统总结
- linux下tar命令解压到指定的目录
- 十大基础排序 · 十 --- 基数排序(稳定)
- PHP连接MySQL数据库的三种方式(mysql、mysqli、pdo)
- 找两个字符串中添加的那个字符
- Android Wi-Fi A/N侧解释