Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.

Each of the next Q lines represents an operation.

"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.

"Q a b" means querying the sum of AaAa+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.

这题是线段树成段更新,看别人代码想了很久。
思路:开一个结构体,含有左右边界l,r,这段区间线段总和sum,更新标志ans(整段区间内每个数要加的数值)。
每次一个区间更新(即加一个数value)的时候,从第一个线线段开始向下判断,如果更新的区间刚好是这条线段的区间,那么直接加上更新的数值value并返回,否则整段区间的sum值变为sum+(b-a+1)*value,再在子节点中找直到找到区间大小加好符合的时候,ans=ans+value,返回。
每一次询问,从第一个线段开始向下,如果区间刚好符合,那么返回区间的sum否则把这条线段的更新标志往子节点传,同时这条线段的更新标志变为0.这里我采用的是每一次更新就把每根点段的总和都保存在sum中,这样询问的时候就不用麻烦的加上b[i].ans*(b[i].r-b[i].l+1).


#include<stdio.h>
#include<string.h>
#define maxn 100005
#define ll long long
char str[10];
ll a[maxn];
struct node
{
ll l,r,sum,ans;
}b[4*maxn]; void build(ll l,ll r,ll i)
{
ll mid;
b[i].l=l;
b[i].r=r;
b[i].ans=0;
if(b[i].l==b[i].r)
{
b[i].sum=a[l];
return;
}
mid=(l+r)/2;
build(l,mid,2*i);
build(mid+1,r,i*2+1);
b[i].sum=b[i*2].sum+b[i*2+1].sum;
} void pushdown(int i)
{
if(b[i].ans){
b[i*2].ans+=b[i].ans;
b[i*2+1].ans+=b[i].ans;
b[i*2].sum+=b[i].ans*(b[i*2].r-b[i*2].l+1);
b[i*2+1].sum+=b[i].ans*(b[i*2+1].r-b[i*2+1].l+1);
b[i].ans=0;
}
} void add(ll l,ll r,ll value,ll i)
{
ll mid;
if(b[i].l==l && b[i].r==r)
{
b[i].ans=b[i].ans+value;
b[i].sum+=(b[i].r-b[i].l+1)*value;
return;
}
pushdown(i);
b[i].sum+=(r-l+1)*value; //这一句写了,下面第二句就不用写了,是同一个意思
mid=(b[i].l+b[i].r)/2;
if(l>mid)
add(l,r,value,i*2+1);
else if(r<=mid)
add(l,r,value,i*2);
else
{
add(l,mid,value,i*2);
add(mid+1,r,value,i*2+1);
}
//b[i].sum=b[i*2].sum+b[i*2+1].sum; ---2
} ll question(ll l,ll r,ll i)
{
ll mid;
if(b[i].l==l && b[i].r==r)
{
return b[i].sum;
}
pushdown(i);
mid=(b[i].l+b[i].r)/2;
if(l>mid)
return question(l,r,i*2+1);
else if(r<=mid)
return question(l,r,i*2);
else if(l<=mid && r>mid)
return question(l,mid,i*2)+question(mid+1,r,i*2+1); } int main()
{
ll n,m,c,d,e,i,j;
while(scanf("%lld%lld",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,n,1);
while(m--)
{
scanf("%s",str);
if(str[0]=='Q')
{
scanf("%lld%lld",&c,&d);
printf("%lld\n",question(c,d,1));
}
else if(str[0]=='C')
{
scanf("%lld%lld%lld",&c,&d,&e);
add(c,d,e,1);
}
}
}
return 0;
}


poj 3468A Simple Problem with Integers的更多相关文章

  1. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

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

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

  3. POJ - 3468A Simple Problem with Integers (线段树区间更新,区间查询和)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  4. POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新

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

  5. 3468-A Simple Problem with Integers 线段树(区间增减,区间求和)

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

  6. POJ A Simple Problem with Integers | 线段树基础练习

    #include<cstdio> #include<algorithm> #include<cstring> typedef long long ll; #defi ...

  7. POJ 3468_A Simple Problem with Integers(树状数组)

    完全不知道该怎么用,看书稍微懂了点. 题意: 给定序列及操作,求区间和. 分析: 树状数组可以高效的求出连续一段元素之和或更新单个元素的值.但是无法高效的给某一个区间的所有元素同时加个值. 不能直接用 ...

  8. POJ 3468_A Simple Problem with Integers(线段树)

    题意: 给定序列及操作,求区间和. 分析: 线段树,每个节点维护两个数据: 该区间每个元素所加的值 该区间元素和 可以分为"路过"该区间和"完全覆盖"该区间考虑 ...

  9. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

随机推荐

  1. Java虚拟机常用的性能监控工具

    基础故障处理工具 jps: 虚拟机进程状况工具 功能:来处正在运行的虚拟机进程,并显示虚拟机执行主类名称,以及本地虚拟机唯一ID. 它是使用频率最高的命令行工具,因为其他JDK工具大多需要输入他查询到 ...

  2. 详解 TCP的三次握手四次挥手

    本文转载来自https://blog.csdn.net/qzcsu/article/details/72861891 背景描述 通过上一篇中网络模型中的IP层的介绍,我们知道网络层,可以实现两个主机之 ...

  3. 【Oracle】Script to Collect DRM Information (drmdiag.sql) (文档 ID 1492990.1)

    脚本对应如下: The following (drmdiag.sql) is a script to collect information related to DRM (Dyanamic Reso ...

  4. 大文件上传FTP

    需求 将本地大文件通过浏览器上传到FTP服务器. 原有方法 将本地文件整个上传到浏览器,然后发送到node服务器,最后由node发送到FTP服务器. 存在问题 浏览器缓存有限且上传速率受网速影响,当文 ...

  5. 计网Q1:多个方面比较电路交换、报文交换和分组交换的主要优缺点

    网上看到的带佬儿的帖子......膜过来<doge 原文链接: https://blog.csdn.net/njchenyi/article/details/1540657 电路交换: 由于电路 ...

  6. 【源码解读】js原生消息提示插件

    效果如下: 关闭message后前后message的衔接非常丝滑,这部分是我比较感兴趣的.带着这个问题先了解下DOM结构,顺便整理下作者的思路. 从DOM里我们可以看到所有的message都在一个容器 ...

  7. 技术基础 | Apache Cassandra 4.0基准测试

    Apache Cassandra 4.0已经发布了Beta版,这是第一个支持JDK 11及更高JDK版本的Cassandra版本.   时延对于Apache Cassandra用户来说是个显而易见的关 ...

  8. web项目启动链接mysql巨慢

    说明:项目部署到测试服务器上,mysql部署在另一台服务器上,项目第一次启动之后登陆后台很慢,大概30s左右,经查发现第一次访问数据库的时候会通过DNS解析客户端机器域名,mysql还有DNS反向解析 ...

  9. XShell下便捷上载/下载文件到虚拟机

    1.客户机联网后,安装 rz,sz 服务,命令如下: yum install lrzsz 2.XShell连接客户机: 2.1 上传文件:运行rz,在弹窗内选择Windows本地文件上传到客户机当前目 ...

  10. mysql主从复制安装配置

    mysql主从复制安装配置 基础设置准备 #操作系统: centos6.5 #mysql版本: 5.7 #两台虚拟机: node1:192.168.182.111(主) node2:192.168.1 ...