题目链接

题意 : 给你n个数,进行两种操作,第一种是将a到b上所有元素都加上c,第二种是查询a到b上所有元素之和输出。

思路 : 线段树,以前写过博客,但是现在在重刷,风格改变,,所以重新写一篇。。。。

 #include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
using namespace std ; LL lz[*],p[*] ; void pushup(int rt)
{
p[rt] = p[rt << ] + p[rt << | ] ;
}
void pushdown(int rt,int m)
{
if(lz[rt])
{
lz[rt << ] += lz[rt] ;
lz[rt << | ] += lz[rt] ;
p[rt << ] += (m-m/)*lz[rt] ;
p[rt << | ] += m/ * lz[rt] ;
lz[rt] = ;
}
}
void build(int l,int r,int rt)
{
lz[rt] = ;
if(l == r)
{
scanf("%I64d%*c",&p[rt]) ;
return ;
}
int mid = (l+r) >> ;
build(l,mid,rt << ) ;
build(mid+,r,rt << | ) ;
pushup(rt) ;
}
LL query(int L,int R,int l,int r,int rt)
{
LL sum = ;
if(l >= L && R >= r)
{
return p[rt] ;
}
pushdown(rt,r-l+) ;
int mid = (l+r) >> ;
if(mid >= L)
sum += query(L,R,l,mid,rt << ) ;
if(mid < R)
sum += query(L,R,mid+,r,rt << | ) ;
return sum ;
}
void update(int L,int R, int l,int r,int sc,int rt)
{
if(l >= L && r <= R)
{
lz[rt] += sc ;
p[rt] += (r-l+)*sc ;
return ;
}
pushdown(rt,r-l+) ;
int mid = (l+r) >> ;
if(mid >= L)
update(L,R,l,mid,sc,rt << ) ;
if(mid < R)
update(L,R,mid+,r,sc,rt << | ) ;
pushup(rt) ;
}
int main()
{
int N,Q ,a,b;
LL c ;
char ch[] ;
while(~scanf("%d %d",&N,&Q))
{
build(,N,) ;
for(int i = ; i < Q ; i++)
{
scanf("%s",ch) ;
if(ch[] == 'Q')
{
scanf("%d %d",&a,&b) ;
LL sum = query(a,b,,N,) ;
printf("%I64d\n",sum) ;
}
else
{
scanf("%d %d %I64d",&a,&b,&c) ;
update(a,b,,N,c,) ;
}
}
}
return ;
}

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

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

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

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

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

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  8. poj 3468 A Simple Problem with Integers 线段树加延迟标记

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

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

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

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

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

随机推荐

  1. qtp 设置等待时间

    1.file->settings->run .默认的时间是20 秒 2. browser("browser").Navigate http://www.baidu.co ...

  2. Oracle小技巧

    Oracle纵向变横向,多行变一行 取处方下的药品大类,以 处方    药品大类 a001   中药.西药,中成药的   的格式显示 /*处方与药品大类的对应 周璇球 20130829*/ SELEC ...

  3. 1.1 MySQL 逻辑架构

  4. cnblogs用户体验

    在使用博客园的这段时间内,我们感觉有优点也有缺点,下面谈谈我们的看法: 1.是什么样的用户?有什么样的心理?对cnblogs的期望值是什么? 我们是学生用户,使用cnblogs主要是提交作业记录自己的 ...

  5. 四则运算小程序测试--c++--软件工程课

    一.测试内容: 1.生成题目数是否准确?2.打印方式(列数l.行间距jj)是否准确?3.有无乘除法cc是否准确?4.数的范围fw是否准确?5.除法有无余数c是否准确?6.加减有无负数f是否准确? 二. ...

  6. ios应运程序的五种状态

    ios应运程序的五种状态即转化  从apple的官方文档扣下来的 5状态: Not running The app has not been launched or was running but w ...

  7. MVC5 自定义表单错误信息

    1.

  8. Careercup - Google面试题 - 4699414551592960

    2014-05-06 13:34 题目链接 原题: we have a random list of people. each person knows his own height and the ...

  9. SQL SERVER数据库修改是否区分大小写

    昨天去客户,发现程序无法应用,跟踪错误提示,提示的大致意思是“数据库表名和数据库字段名不存在”.查询后发现是SQL Server数据库设置了区分大小写的缘故(一般安装时,Oracle的正确安装下是默认 ...

  10. SqlServer 全局变量

    1.@@ERROR 返与@@ERROR 近语句错误码局限于DML语句select语句执行现错误则返等于0错误码没错则返0通使用判断语句没执行功 -- Create Schema if not one ...