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 ...
随机推荐
- 信号量与PV操作
在计算机操作系统中,PV操作是进程管理中的难点.首先应弄清PV操作的含义:PV操作由P操作原语和V操作原语组成(原语是不可中断的过程),对信号量进行操作,具体定义如下: P(S):①将信号量S的 ...
- Amd64 and Va_arg
Made of Bugs Blog Archives Author Amd64 and Va_arg OCT 3RD, 2010 A while back, I was poking around L ...
- Scrollview嵌套Listview运行后最先显示出来的位置不在顶部而是中间问题
问题: Scrollview里面嵌套了一个Listview ,通过设置一个方法设置了Listview的高度,现在的情况就是进到这个界面的时候看到的不是最上面 而是中间 解决办法: 设置ListView ...
- Linux mint 18版本开启SSH服务
linux mint 18版本默认是没有安装ssh server的 需要手动安装 安装ssh server: 此命令需要联网,会自动下载安装 安装之后看是否开始了ssh, 看到ssh-agent 和s ...
- iredmail安装脚本分析(三)---conf/global DISTRO值的来源及操作系统的判断
作者在引入conf/global 文件时,就已经对操作系统的类型进行判断,同时也对DISTRO进行了赋值. 部分代码,如图: 显然文件里的KERNEL_NAME的值就是判断完成的操作系统,具体分析该值 ...
- guess number
crossin的前面几章基本和LPTHW内容重合,因此我直接做了他前面的一个综合练习. 猜数游戏, 即系统随机记录一个数,根据用户猜的记录,如果正确则告知,且退出游戏,如不正确,则提示答案与用户输入的 ...
- PowerDesigner生成Oracle数据库时,表名会带引号问题
使用PowerDesigner生成数据库建表SQL脚本时,尤其是Oracle数据库时,表名一般会带引号.其实加引号是PL/SQL的规范,数据库会严格按照""中的名称建表,如果没有& ...
- CentOS 6.5 升级 GCC 4.9.3
1. GUN官网下载源代码安装包: gcc-4.9.3.tar.gz 2. 解压安装包,并进入解压后的文件夹: tar -zxvf gcc-4.9.3.tar.gz 3. 使用压缩包中的工具下载依赖: ...
- ArrayList集合 、特殊集合
一.ArrayList集合 集合内可以放不同类型的元素 另:object类型为所有数据类型的基类 添加元素:.add(); 清空集合:al.clear(); 克隆集合:.clone(); 判断是否包含 ...
- 兼容PC手机端字体
各平台的主流字体支持情况 各系统的默认字体和常用字体: 系统 默认西文字体 默认中文字体 其他常用西文字体 其他常用中文字体 Windows 宋体 宋体 Tahoma.Arial.Verdana.Ge ...