题目链接

题意 : 给你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. ECMAScript5

    张鑫旭:ECMAScript5介绍 淘宝整理的es5-safe /********* It provides the following methods: Function.prototype.bin ...

  2. andirod

    于adnroid老手来说,SDK环境搭建是很简单的,但是对于我这样的小白来说,,,,,走啦很多弯路..特记下.希望对兄弟们有所帮助 因为我也是参考网上的高手知道,所以有的就直接复制啦,,^_^ 想要开 ...

  3. iOS学习之Object-C语言简单的通讯录管理系统

    用这几天学的OC的知识,写了一个实现简单功能的通讯录管理系统,在这里分享给大家: 通讯录管理系统 *  需求: 1.定义联系人类Contact.实例变量:姓名(拼音,首字母大写).性别.电话号码.住址 ...

  4. 在DNS管理器——用局域网IP指定你所起的域名名称

    在服务器上面,进行以下相关的操作: 第一步:打开DNS管理器; 第二步:在“正向查找区域”添加域名为:icanyin.net; 第三步:添加主机pm,将域名pm.icanyin.net解析为IP地址: ...

  5. Team Homework #3 软件工程在北航——IloveSE

    任务要求: 采访以前上过北航  (计算机系/软件学院) 软件工程课的同学.现在上研/工作的也可以. 采访问题如下:* 平均每周花在这门课上的时间 (包括上课/作业/上机)    * 平均写的代码总行数 ...

  6. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  7. Codeforces Round #350 (Div. 2) D2. Magic Powder - 2

    题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...

  8. BZOJ 4443: [Scoi2015]小凸玩矩阵 二分图最大匹配+二分

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=4443 题解: 二分答案,判断最大匹配是否>=n-k+1: #include< ...

  9. netty 粘包问题处理

    netty 粘包问题处理 key words: netty 粘包 解包 半包 TCP 一般TCP粘包/拆包解决办法 定长消息,例如每个报文长度固定,不够补空格 使用回车换行符分割,在包尾加上分割符,例 ...

  10. poj 2449 Remmarguts' Date K短路+A*

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...