POJ 3468 A Simple Problem with Integers(线段树)
题意 : 给你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(线段树)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
随机推荐
- db2建立表空间
--缓冲池 CREATE BUFFERPOOL EMP_BUF_POOL IMMEDIATE ALL DBPARTITIONNUMS SIZE 250 NUMBLOCKPAGES 0 PAG ...
- strcpy/strlen/strcat/strcmp面试总结
<strcpy拷贝越界问题> 一. 程序一 #include<stdio.h> #include<string.h> void main() { char s[]= ...
- 掌握这两个技术点,你可以玩转AppCan前端开发
“AppCan的前端开发其实非常简单,只要掌握两方面的技术即可.一方面是会使用栅格布局完成UI的界面排版,另外一方面就是使用AppCan MVVM模型来完成整个页面构造和用户操作逻辑.” 在2016A ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- wifi-sdio接口
1.sdio接口层解析 SDIO总线 SDIO总线和USB总线类似,SDIO也有两端,其中一端是HOST端,另一端是device端.所有的通信都是由HOST端发送命令开始的,Device端只要能解析命 ...
- 使用 RestEasy 和 Apache Tomcat 构建 RESTful Web 服务
第一次,用这个RestEasy框架,用的时候,总是提示,404的错误,郁闷,呵呵,不过经过努力,终于解决问题,特别留个标记. 关于404的错误,上网找了一大堆,也还不行. 我感觉应该是lib下面架包的 ...
- 浅析Objective-C字面量
编写Objective-C程序时,总会用到某几个类,它们属于Foundation框架.虽然从技术上来说,不用Foundation框架也能写出Objective-C代码,但实际上却经常要用到此框架.这几 ...
- java与IOS之间的RSA加解密
很简单的一个需求,ipad端给密码RSA加密,传到java后台,解密.RSA加密算法是基于一个密钥对的,分为公钥和私钥,一般情况公钥加密,私钥解密,但也可私钥加密,公钥解密.还可以验签,就是先用私钥对 ...
- 嗨分享-前端技术-帝国CMS手机站修改列表分页(sysShowListMorePage)
http://bbs.phome.net/showthread-31-318753-0.html 如果你的网站使用的是帝国CMS.PC站和手机站各使用一个模板组,但共同使用一个数据库.那么你的PC站和 ...
- [转]rpcndr.h和wtypes.h冲突Bug的解决方案
[转]rpcndr.h和wtypes.h冲突Bug的解决方案 http://blog.csdn.net/tzwh_86/article/details/9495133 rpcndr.h和wtypes. ...