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 ...
随机推荐
- Jmeter增加压力机方法
windows: 需要别人的机器也安装jmeter 在别人的机器上运行jmeter-server.bat 修改jmeter.properties文件,查找remote_hosts 原始:remote_ ...
- android 滚动的缓冲图片
-----------------------java实现代码------------------------- private Animation mRotate; mRotate = Animat ...
- iOS: How To Make AutoLayout Work On A ScrollView
iOS: How To Make AutoLayout Work On A ScrollView Posted on June 11th, 2014 Ok, I’ll admit. I’ve been ...
- 第一章Android系统移植与驱动开发概述--读书笔记
以前,初步学习过嵌入式Linux驱动开发的基础课程,对于驱动开发可以说是有了一点点微末的基础吧.首先我们要对Android嵌入式系统有一个初步的认识,Android系统发展到今天已经具备了完善的架构. ...
- Swift----函数 、 闭包 、 枚举 、 类和结构体 、 属性
1 数组排序 1.1 问题 本案例实现一个整型数组排序的函数,数组排序的规则由传递的规则函数决定. 1.2 方案 首先定义一个整型数组排序函数sortInts,该函数有一个整型数组类型的参数,该参数必 ...
- 常用 Java 静态代码分析工具的分析与比较
常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基 本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代码分析工具 (Checkstyle,FindBu ...
- tensorflow资料补充(很棒)
http://tensorfly.cn/tfdoc/get_started/introduction.html https://github.com/CreatCodeBuild/TensorFlow ...
- 一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试
Web登录测试是很常见的测试!手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文作者就用python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动 ...
- win10下 homestead 安装
1.安装VirtualBox 和 Vagrant 2.git或者composer安装 homestead git clone https://github.com/laravel/homestead. ...
- This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.
-- :::] This application is modifying the autolayout engine from a background thread, which can lead ...