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. HOJ——T 1867 经理的烦恼

    http://acm.hit.edu.cn/hoj/problem/view?id=1867 Source : HCPC 2005 Spring   Time limit : 2 sec   Memo ...

  2. 洛谷——P1518 两只塔姆沃斯牛 The Tamworth Two

    https://www.luogu.org/problem/show?pid=1518 题目背景 题目描述 两只牛逃跑到了森林里.农夫John开始用他的专家技术追捕这两头牛.你的任务是模拟他们的行为( ...

  3. java架构解密——实时动态aop

    在上篇博客中个.咱们一起组建了一个容器,里面封装了业务,这样,咱们就将业务和服务的组装放到了client,而client就相当于咱们的开发中使用到的配置文件.大家发现问题了吗?就是我不能动态修改了?业 ...

  4. eclipse部署maven web项目到tomcat服务器时,没有将lib、web.xml复制过去的解决办法

    我这几天在写项目的时候发现自己以前的项目能够访问,隔一段时间写的这个项目却不能够访问,没有发现代码的逻辑错,但是就是访问不了jsp页面,项目一发布就是出现404错误,后来发现原来是发布到tomcat上 ...

  5. offSet和client和scroll

    这三个是是js盒模型属性 client clientWidth 内容宽度加上左右padding clientHeight 内容高度加上上下padding clientTop 上边框的宽度 client ...

  6. type---显示指定命令的类型

    type命令用来显示指定命令的类型,判断给出的指令是内部指令还是外部指令. 命令类型: alias:别名. keyword:关键字,Shell保留字. function:函数,Shell函数. bui ...

  7. 取消cp命令别名

    1. 取消cp命令别名unalias cpcp -rf恢复别名alias cp='cp -i'2.关闭当前用户下的cp别名配置sed -i "s/alias cp='cp -i'/#alia ...

  8. 关于mybatis里面的Executor--转载

    原文地址:http://blog.csdn.net/w_intercool/article/details/7893344 使用mybatis查寻数据,跟踪其执行流程 最开始执行的语句 this.ge ...

  9. div+css制作表格

    html: <div class="table"> <h2 class="table-caption">花名册:</h2> ...

  10. BIND View 加速南北方网络互访

            BIND View 加速南北方网络互访          南北方网络互访的问题一直以来就是广大运维人员的心病,两大网络运营商之间的连接带宽比较有限,跟不上互联网业务发展的速度.如何才能 ...