POJ 3468 线段树 成段更新 懒惰标记
A Simple Problem with Integers
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
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100010
#define M 15
#define mod 1000000007
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,q;
int x[N];
char ch;
int a,b,c; struct node
{
ll mark,sum;
}tree[*N]; void update(int l,int r,int i)
{
if(!tree[i].mark) return;
int mid=(l+r)/;
tree[*i].sum+=tree[i].mark*(ll)(mid-l+);
tree[*i+].sum+=tree[i].mark*(ll)(r-mid);
tree[*i].mark+=tree[i].mark;
tree[*i+].mark+=tree[i].mark;
tree[i].mark=; } ll query(int tl,int tr,int l,int r,int i)
{
if(tl>r || tr<l)
return ;
if(tl<=l && r<=tr)
return tree[i].sum;
update(l,r,i);
int mid=(l+r)/; return query(tl,tr,l,mid,*i)+query(tl,tr,mid+,r,*i+);
} void addd(int tl,int tr,int l,int r,int i,int val)
{
if(tl>r || tr<l)
return;
if(tl<=l && tr>=r){
tree[i].sum+=val*(ll)(r-l+);
tree[i].mark+=val;
return;
}
update(l,r,i);
int mid=(l+r)/;
addd(tl,tr,l,mid,*i,val);
addd(tl,tr,mid+,r,*i+,val);
tree[i].sum=tree[*i].sum+tree[*i+].sum;
} void build_tree(int l,int r,int i)
{
if(l==r){
tree[i].sum=x[l];
return;
}
int mid=(l+r)/;
build_tree(l,mid,*i);
build_tree(mid+,r,*i+);
tree[i].sum=tree[*i].sum+tree[*i+].sum;
} int main()
{
int i;
// freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d",&n,&q)!=EOF)
{
// printf(" %d %d \n",n,q);
for(i=;i<=n;i++) scanf("%d",&x[i]);
// printf(" 1\n");
memset(tree,,sizeof(tree));
build_tree(,n,); // printf(" 2\n");
getchar();
while(q--){
// printf(" 3\n");
scanf("%c",&ch);
// printf(" %c\n",ch);
if(ch=='C'){
//printf(" 31\n");
scanf("%d%d%d",&a,&b,&c);
getchar();
addd(a,b,,n,,c); }
else{
// printf(" 32\n");
scanf("%d%d",&a,&b);
getchar();
ll ans=query(a,b,,n,);
printf("%I64d\n",ans);
}
}
} return ;
}
POJ 3468 线段树 成段更新 懒惰标记的更多相关文章
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- poj 3468 线段树成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 54012 ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
- poj 3468 线段树 成段增减 区间求和
题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...
- poj 3669 线段树成段更新+区间合并
添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- HDU-1698-Just a Hook-区间更新+线段树成段更新
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. T ...
随机推荐
- 51Nod 1007 正整数分组 -简单DP
题意: 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. N<=100 sum<=100 ...
- w3 parse a url
最新链接:https://www.w3.org/TR/html53/ 2.6 URLs — HTML5 li, dd li { margin: 1em 0; } dt, dfn { font-wei ...
- 正则表达式匹配:根据key获取value
需求 url请求html字符串,dytk值写在js里,可以看成是key-value的格式,需要提取dytk值. 解决方法 正则匹配 private string get_dytk(string htm ...
- 许大神- xulinbo xulingbo 分享
1. 写文章投稿-- 总结的动力 可用性 单次点击 整年年度 流量激增 上下线 双网卡,交换机(网络层面) 稳定性 2. 收藏夹- canssendra 和 oceanBase 练手落地 3. 压测: ...
- vsftp配置日志及其启用本地时间
vsftp配置日志及其启用本地时间 1. 启用vsftp日志 xferlog_enable=YES xferlog_std_format=YES xferlog_file=/var/log/xferl ...
- ios之alloc和init
复制代码 SomeObject *obj = [[SomeObject alloc] initWithCenter:centerPoint radius:radius]; 和 复制代码 Som ...
- Fortran学习笔记4(循环语句)
Fortran学习笔记4 Fortran学习笔记4 逻辑运算 循环 Do语句 Do-While循环 循环控制 循环应用实例 逻辑运算 if命令需要和逻辑运算表达式搭配才能起到很好的效果.下面分别列出F ...
- CF-1099 D. Sum in the tree
CF-1099 D. Sum in the tree 题意:结点序号为 1~n 的一个有根树,根序号为1,每个点有一个权值a[i], 然后定义一s[i]表示从根节点到 结点序号为i的结点的路途上所经过 ...
- 【树论 倍增】51nod1709 复杂度分析
倍增与位运算有很多共性:这题做法有一点像「线段树上二分」和「线段树套二分」的关系. 给出一棵n个点的树(以1号点为根),定义dep[i]为点i到根路径上点的个数.众所周知,树上最近公共祖先问题可以用倍 ...
- 关于Linux上的SSH服务无法启动,提示“/var/empty/sshd must be owned by root and not group or world-writable”错误
首先通过物理终端进入到linux上,手工检查ssh发现没运行# /etc/init.d/sshd statussshd is stopped 手动启动服务,发现报告权限错误.# /etc/init.d ...