POJ A Simple Problem with Integers | 线段树基础练习
#include<cstdio>
#include<algorithm>
#include<cstring>
typedef long long ll;
#define N 100010
using namespace std;
struct node
{
ll l,r,lz,sum;
}t[*N];
ll read()
{
ll ret=,neg=;
char j=getchar();
for (;j>'' || j<'';j=getchar())
if (j=='-') neg=-;
for (;j<='' && j>='';j=getchar())
ret=ret*+j-'';
return ret*neg;
}
ll n,q,a[N],l,r,k;
void pushup(ll p)
{
t[p].sum=t[*p].sum+t[*p+].sum;
}
void pushdown(ll p)
{
if (t[p].l==t[p].r || t[p].lz==) return;
ll tmp=t[p].lz,mid=(t[p].r+t[p].l)>>;
t[*p].sum+=(mid-t[p].l+)*tmp;
t[*p+].sum+=(t[p].r-mid)*tmp;
t[*p].lz+=tmp;
t[*p+].lz+=tmp;
t[p].lz=;
}
void build(ll p,ll l,ll r)
{
t[p].l=l,t[p].r=r;
if (l!=r)
{
ll mid=l+r>>;
build(*p,l,mid);
build(*p+,mid+,r);
pushup(p);
}
else
t[p].sum=a[l];
}
void modify(ll p,ll l,ll r,ll k)
{
ll L=t[p].l,R=t[p].r,mid=(L+R)>>;
if (l==L && r==R)
{
t[p].sum+=(r-l+)*k;
t[p].lz+=k;
return ;
}
pushdown(p);
if (r<=mid)
modify(*p,l,r,k);
else if (l>mid)
modify(*p+,l,r,k);
else modify(*p,l,mid,k),modify(*p+,mid+,r,k);
pushup(p);
}
ll query(ll p,ll l,ll r)
{
ll L=t[p].l,R=t[p].r,mid=(L+R)>>;
if (l==L && r==R)
return t[p].sum;
pushdown(p);
if (r<=mid)
return query(*p,l,r);
if (l>mid) return query(*p+,l,r);
return query(*p,l,mid)+query(*p+,mid+,r);
}
char op[N];
int main()
{
n=read(),q=read();
for (int i=;i<=n;i++)
a[i]=read();
build(,,n);
while (q--)
{
scanf("%s",op);
if (op[]=='Q')
l=read(),r=read(),printf("%lld\n",query(,l,r));
else l=read(),r=read(),k=read(),modify(,l,r,k);
}
return ;
}
POJ A Simple Problem with Integers | 线段树基础练习的更多相关文章
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- 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 线段树区间加,区间查询和
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 (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
随机推荐
- MyEclipse的快捷键大全(超级实用,方便)
常用快捷键 1. [ALT+/] 能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ALT+/]快捷键带来的好处吧. 2. [Ctrl+O] 显示类 ...
- 自定义动画函数JQuery实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Percona-Tookit工具包之pt-mext
Preface We are always obliged to analyze many outputs generated by various tools directly ev ...
- sql server几种Join的区别测试方法与union表的合并
/* sql server几种Join的区别测试方法 主要来介绍下Inner Join , Full Out Join , Cross Join , Left Join , Right Join的区别 ...
- Unity基础
unity unity 3大场景 Asset Scene Component Asset :资源导入导出 右击资源,选择导出Unity包 导入可以直接将只有复制到Asset文件夹 创建场景 File- ...
- java 基础面试
1. &和&&的区别 答: &是位运算符,表示按位与运算,&&是逻辑运算符,表示逻辑与(and) 2.int 和 Integer 有什么区别 答: Ja ...
- ES6笔记02-箭头函数
eg1:// ES5 匿名函数 var total = values.reduce(function (a, b) { return a + b; }, 0); // ES6 匿名函数 var tot ...
- 【Python 2 到 3 系列】 此整型非彼整型
v2.2 (2.x)以后,python支持不会溢出的 long 型. v3.0后,确切的讲, int 型(依赖运行环境C编译器中long型的精度)消失了,long型替代 int 型,成为新的.不依赖运 ...
- laravel环境配置的常见问题
从开始下载相关软件到现在,整整一天,终于成功了.不得不说官方的说明文档相当详细,毕竟我都成功了,不是吗,哈哈. 好了,不多说了,直接上干货 官方环境配置文档地址:https://laravel-chi ...
- Swoole 创建服务
1: 创建TCP 服务器 $serv = new swoole_server(‘127.0.0.1’,9501); 2:创建UDP服务器 $serv = new swoole_server('127 ...