POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers
【题目链接】A Simple Problem with Integers
【题目类型】线段树 成段增减+区间求和
&题解:
线段树 成段增减+区间求和 模板题 这种题真的应该理解并且可以流畅的独立码出来了
【时间复杂度】\(O(nlogn)\)
&代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define SIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define red(i,a,b) for(int i=(a);i>=(b);i--)
const ll LINF = 0x3f3f3f3f3f3f3f3f;
#define PU(x) cout<<#x<<endl;
#define PI(A) cout<<(A)<<endl;
#define DG(x) cout<<#x<<"="<<(x)<<endl;
#define DGG(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl;
#define DGGG(x,y,z) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<" "<<#z<<"="<<(z)<<endl;
#define PIar(a,n) rep(i,0,n-1)cout<<a[i]<<" ";PU()
#define PIarr(a,n,m) rep(aa,0,n-1){rep(bb,0,m-1)cout<<a[aa][bb]<<" ";PU()}
typedef pair<int, int> pii;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
const double EPS = 1e-9 ;
/* //////////////////////// C o d i n g S p a c e //////////////////////// */
#define lsn b,m,rt<<1
#define rsn m+1,e,rt<<1|1
const int maxn = (int)1e5 + 9 ;
int n,q,a[maxn];
ll seg[maxn<<2],si[maxn<<2];
void puup(int rt) {
seg[rt]=seg[rt<<1]+seg[rt<<1|1];
}
void pudo(int len,int rt) {
if(si[rt]) {
si[rt<<1]+=si[rt];
si[rt<<1|1]+=si[rt];
seg[rt<<1]+=si[rt]*(len-len/2);
seg[rt<<1|1]+=si[rt]*(len/2);
si[rt]=0;
}
}
void build(int b,int e,int rt) {
if(b==e) {
seg[rt]=a[b];
return ;
}
int m=b+e>>1;
build(lsn);
build(rsn);
puup(rt);
}
ll query(int l,int r,int b,int e,int rt) {
if (l<=b&&e<=r) {
return seg[rt];
}
pudo(e-b+1,rt);
int m=b+e>>1;
ll ans=0;
//小于等于m的全在左儿子 大于m的全在右儿子
//如果需要求的区间[l,r]最左边的那个(也就是l) 小于等于m 那么就一定要往左儿子走
//反之 如果区间[l,r]最右边的那个(也就是r) 大于m 那么就一定要走向右儿子
if (l<=m)
ans+=query(l,r,lsn);
if (m<r)
ans+=query(l,r,rsn);
return ans;
}
void upda(int l,int r,ll xx,int b,int e,int rt) {
if (l<=b&&e<=r) {
si[rt]+=xx;
seg[rt]+=(e-b+1)*xx;
return;
}
pudo(e-b+1,rt);
int m=b+e>>1;
if (l<=m)
upda(l,r,xx,lsn);
if (m<r)
upda(l,r,xx,rsn);
puup(rt);
}
void Solve() {
scanf("%d%d",&n,&q);
rep(i,1,n) scanf("%d",&a[i]);
build(1,n,1);
rep(o,1,q) {
char op[9];
int x,y,z;
scanf("%s",op);
if (op[0]=='Q') {
scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y,1,n,1));
}
else {
scanf("%d%d%d",&x,&y,&z);
upda(x,y,z,1,n,1);
}
}
}
int main() {
//iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// int T;cin>>T;while(T--)
Solve();
return 0;
}
POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- 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 Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
随机推荐
- html默认属性
对于display为block来说width默认是满长的,即父级得100%,而高度是0,除非手动设置为100%或指定高度.
- Hadoop是什么?一句话理解
Hadoop(MapReduce&HDFS) 1.学习目的(前言) 在从业了六年IT生涯里,做个实施顾问.业务顾问.BA需求分析师.项目经理,现在重新定位自己,在新公司做起了开发顾问,虽然经历 ...
- 临时解决系统中大量的TIME_WAIT连接
今天,偶然间发现后台服务与数据库之间有大量的TIME_WAIT的连接: [root@localhost logs]# netstat -an | grep TIME_WAIT tcp a.a.a.a: ...
- HttpClient方式模拟http请求
方式一:HttpClient import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.http.*; im ...
- JS中this的值到底为何?
之前很久的时间,因为研究不深,对于this的值一直模模糊糊,不是很清楚,最近有空做了一些研究,终于彻底弄明白了this到底为何物. 首先, 先抛出一个定论:”在Javascript中,this关键字永 ...
- safari 日期对象新建new Date( timeStr ) 参数TimeStr格式
这是一个浏览器兼容的问题,在此总结一下,别老在这掉坑. 先坐下测试 var timeStrArray = [ '2016-10-04', '2016.10.04', '2016/10/04', '10 ...
- Javascript 自动计算生日
首先调用方法需要注意 //inner_page为外围大层,确保时间可以实时修改 $(".inner_page").mouseover(function() { va ...
- iOS开发 适配iOS10以及Xcode8
iOS10的适配以及Xcode8使用上的一些注意点 一.证书管理 用Xcode8打开工程后,比较明显的就是下图了,这个是苹果的新特性,可以帮助我们自动管理证书.建议大家勾选这个Automaticall ...
- apk下载解决微信扫一扫问题
.btn{display: block;width:100%;padding:10px;border:none;cursor: pointer;outline: none;} .btn-primary ...
- 关于HTML页面布局要注意的问题
1.用百分比设定元素宽度可能造成的错误 很多同学习惯使用百分比来设定页面元素(例如div,以下称作盒子,方便介绍)的宽度,这样做可能造成未知的错误,最常见的就是当页面被缩小,或者屏幕分辨率降低时,由于 ...