A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 74705   Accepted: 22988
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

The sums may exceed the range of 32-bit integers.

Source

field=source&key=POJ+Monthly--2007.11.25">POJ Monthly--2007.11.25, Yang Yi



题目意思:给定Q (1 ≤ Q ≤ 100,000)个数A1,A2 … AQ,, 以及可能多次进行的两个操作:

1)对某个区间Ai … Aj的每一个数都加n(n可变) 2) 求某个区间Ai … Aj的数的和。

就是线段树的更新与查询操作,这里要用到延迟更新。

注意sum和add都要设成int64型,由于在更新过程中。add也可能会由于累加而超出int型的范围。

#include<stdio.h>
#include<string.h>
#define M 100005
struct tree{
int l,r;
__int64 sum,add;
}tree[M<<2];
void pushup(int root)
{
if(tree[root].l==tree[root].r)return;
tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
return;
}
void pushdown(int root)
{
if(tree[root].l==tree[root].r)return;
if(tree[root].add==0)return;
tree[root<<1].add+=tree[root].add;
tree[root<<1|1].add+=tree[root].add;
tree[root<<1].sum=tree[root<<1].sum+(tree[root<<1].r-tree[root<<1].l+1)*tree[root].add;
tree[root<<1|1].sum=tree[root<<1|1].sum+(tree[root<<1|1].r-tree[root<<1|1].l+1)*tree[root].add;
tree[root].add=0;
return;
}
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].sum=0;
tree[root].add=0;
if(l==r){
tree[root].sum=0;
return;
}
int mid=l+r>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
pushup(root);
}
void update(int l,int r,int root,__int64 z)
{
if(tree[root].l==l&&tree[root].r==r)
{ tree[root].sum=tree[root].sum+(r-l+1)*z; tree[root].add=tree[root].add+z; //有可能出现多次的更新操作。所以要将全部的add都累加起来。
return;
}
pushdown(root);
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid)update(l,r,root<<1,z);
else if(l>mid)update(l,r,root<<1|1,z);
else {
update(l,mid,root<<1,z);
update(mid+1,r,root<<1|1,z);
}
pushup(root);
return;
}
__int64 Query(int l,int r,int root)
{
if(l==tree[root].l&&r==tree[root].r){
return tree[root].sum;
}
pushdown(root);
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid)return Query(l,r,root<<1);
else if(l>mid)return Query(l,r,root<<1|1);
else {
return Query(l,mid,root<<1)+Query(mid+1,r,root<<1|1);
}
}
int main()
{
int N,Q,i,j,k,a,b;
__int64 c,d;
char s[20];
while(scanf("%d%d",&N,&Q)!=EOF)
{
build(1,N,1);
for(i=1;i<=N;i++)
{
scanf("%I64d",&d);
update(i,i,1,d);
}
while(Q--)
{
scanf("%s%d%d",s,&a,&b);
if(s[0]=='Q'){
printf("%I64d\n",Query(a,b,1));
}
if(s[0]=='C'){
scanf("%I64d",&c); update(a,b,1,c); }
}
}
return 0;
}

poj 3468 A Simple Problem with Integers(线段树、延迟更新)的更多相关文章

  1. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  2. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  3. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  4. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  5. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  6. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  8. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  9. POJ 3468 A Simple Problem with Integers (线段树多点更新模板)

    题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...

  10. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

随机推荐

  1. 【Hive】explain command throw ClassCastException in 2.3.4

    参考:https://issues.apache.org/jira/browse/HIVE-21489 (一)问题描述: Hive-2.3.4 执行  explain select * from sr ...

  2. MySQL存储过程实现分页及变量的定义

    delimiter是MySQL中的命令,这个命令与存储过程没什么关系. 其实就是告诉mysql解释器,该段命令是否已经结束了,mysql是否可以执行了. 即改变输入结束符. 默认情况下,delimit ...

  3. centos下安装nodejs的三种种方式

    方法一:源码包安装 官网下载 centos下载最新版10.9 https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz mkdir / ...

  4. struct获取不到值的小错误

    struct2 get set 这两个方法一定要用双骆驼命名法:getA() setA(),        而geta() seta()不行 我找了好久的错误,只能说框架这东西快捷方便,找起错误要人命

  5. TWaver可视化编辑器的前世今生(二)3D编辑器

    接着昨天的继续说哈. 作为一款高效.轻量.自带编辑功能小组件,TWaver Java在电信网管界一炮而红,在各大运营商的OSS,BSS,NMS系统中随处可见. 采用了TWaver图形组件的上海世博会监 ...

  6. Mysql 查询多个字段去重复

    今天需要统计信息,但是有过个重复的数据,需要去除,找了如下方法: SELECT *, COUNT(DISTINCT phone ) FROM apply_info GROUP BY phone

  7. 如何用纯 CSS 创作一个渐变色动画边框

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/odpRKX 可交互视频教 ...

  8. systemverilog:task

    1.task declaration 个人喜欢ANSI C格式的声明: task mytask(output int x,input logic y); ...... endtask 注意端口列表的方 ...

  9. svn基本使用详情

    1.增加(Add)先提到变更列表中,再commit到配置库中,选择新增文件,右键SVN菜单执行“Add“操作提交到”变更列表中”,然后右键SVN菜单执行”SVNCommit”提交到版本库中. 2.删除 ...

  10. 【HDU 3336】Count the string(KMP+DP)

    Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...