http://poj.org/problem?id=3468

题目大意:

给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000)

操作分为两种,Q A B 表示输出[A,B]的和   C A B X表示把[A,B]的所有数加上X

思路:

线段树的区间修改。。。。。

昨天晚上改了老半天。

然后关机准备睡觉毕竟今天有实验。。去洗个头。。突然有灵感。。急急忙忙的开电脑改了就对了~哈哈哈

PS:POJ AC 100了~ 因为混迹各个OJ,SO才100

用位运算优化*2  1600+MS,不用2200MS。。。差了挺多

顺便测试了下很久以前想的k<<1和k+k的效率,果然是k<<1的效率高,k+k慢了30多MS

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100000+10;
typedef long long LL;
LL sum[MAXN<<2],add[MAXN<<2];
int a[MAXN];
void build(LL k,LL L,LL R)
{
if(L==R) sum[k]=a[L];
else
{
LL m=(L+R)>>1;
build(k<<1,L,m);
build((k<<1)+1,m+1,R);
sum[k]=sum[k<<1]+sum[(k<<1)+1];
}
} void update(LL k,LL L,LL R,LL a,LL b,LL v)
{
sum[k]+=(min(R,b)-max(a,L)+1)*v;
if(a<=L && R<=b) add[k]+=v;
else
{
LL m=(L+R)>>1;
if(a<=m) update(k<<1,L,m,a,b,v);
if(m<b) update((k<<1)+1,m+1,R,a,b,v);
}
}
LL ans;
void query(LL k,LL L,LL R,LL a,LL b,LL addv)
{
if(a<=L && R<=b) ans+=sum[k]+(R-L+1)*addv;
else
{
LL m=(L+R)>>1;
if(a<=m) query(k<<1,L,m,a,b,addv+add[k]);
if(m<b) query((k<<1)+1,m+1,R,a,b,addv+add[k]); }
} int main()
{
// freopen("e:\\input.txt","r",stdin);
int n,q;
while(~scanf("%d%d",&n,&q))
{
memset(add,0,sizeof(add));
memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++)
scanf("%d",&a[i]); build(1,1,n); for(int i=0;i<q;i++)
{
char cmd[5];
int a,b,v;
scanf("%s",cmd);
if(cmd[0]=='Q')
{
ans=0;
scanf("%d%d",&a,&b);
query(1,1,n,a,b,0);
printf("%lld\n",ans);
}
else
{
scanf("%d%d%d",&a,&b,&v);
update(1,1,n,a,b,v);
}
}
}
return 0;
}

POJ 3468 A Simple Problem with Integers 线段树区间修改的更多相关文章

  1. 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 ...

  2. 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 ...

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

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

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

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

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

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

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

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

  7. (简单) 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 ...

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

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

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

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

随机推荐

  1. CodeVS 1296 营业额统计

    1296 营业额统计2002年  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master   题目描述 Description Tiger最近被公司升任为营业部经理, ...

  2. 【数字图像处理】五.MFC图像点运算之灰度线性变化、灰度非线性变化、阈值化和均衡化处理具体解释

    本文主要讲述基于VC++6.0 MFC图像处理的应用知识,主要结合自己大三所学课程<数字图像处理>及课件进行解说.主要通过MFC单文档视图实现显示BMP图片点运算处理.包含图像灰度线性变换 ...

  3. Js将类数组转化为数组

    说起伪数组,大家可能会想到arguments, 这个我们函数参数的一个类数组,是类数组的代表. 1.拥有length属性,可以使用下标来访问元素,这两点和数组相同. 2.不能使用数组的方法,他们不能使 ...

  4. Android SecurityException

    public boolean checkNetwork() { boolean result = false; try { Context context = this.getApplicationC ...

  5. Sparse Autoencoder(二)

    Gradient checking and advanced optimization In this section, we describe a method for numerically ch ...

  6. Logistic Regression and Newton's Method

    Data For this exercise, suppose that a high school has a dataset representing 40 students who were a ...

  7. buffer--cache 详解

  8. [C#防止反编译].NET 产品版权保护方案 (.NET源码加密保护)

    [C#防止反编译].NET 产品版权保护方案 (.NET源码加密保护) 标签: .net加密产品c#dll工具 2011-03-24 21:06 27009人阅读 评论(13) 收藏 举报 分类: C ...

  9. ActiveMQ学习总结(2)——ActiveMQ入门实例教程

    1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击a ...

  10. java 三次样条插值 画光滑曲线 例子

    java 三次样条插值 画光滑曲线 例子 主要是做数值拟合,根据sin函数采点,取得数据后在java中插值并在swing中画出曲线,下面为截图  不光滑和光滑曲线前后对比:    代码: 执行类: p ...