POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers
这个题就是成段的增减以及区间查询求和操作。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
const int inf=0x3f3f3f3f;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
ll tree[maxn<<],add[maxn<<]; void pushup(int rt)
{
tree[rt]=tree[rt<<]+tree[rt<<|];
}
void pushdown(int rt,int m)
{
if(add[rt]){
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
tree[rt<<]+=(m-(m>>))*add[rt];
tree[rt<<|]+=(m>>)*add[rt];
add[rt]=;
}
}
void build(int l,int r,int rt)
{
add[rt]=;
if(l==r){
scanf("%lld",&tree[rt]);
return ;
} int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
}
void update(int L,int R,ll c,int l,int r,int rt)
{
if(L<=l&&r<=R){
add[rt]+=c;
tree[rt]+=c*(r-l+);
return ;
} pushdown(rt,r-l+);
int m=(l+r)>>;
if(L<=m) update(L,R,c,lson);
if(R> m) update(L,R,c,rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
return tree[rt];
} pushdown(rt,r-l+);
int m=(l+r)>>;
ll ret=;
if(L<=m)ret+=query(L,R,lson);
if(R> m)ret+=query(L,R,rson);
return ret;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
build(,n,);
char s[];int l,r;ll c;
for(int i=;i<m;i++){
scanf("%s",s);
if(s[]=='Q'){
scanf("%d%d",&l,&r);
printf("%lld\n",query(l,r,,n,));
}
else{
scanf("%d%d%lld",&l,&r,&c);
update(l,r,c,,n,);
}
}
return ;
}
POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]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: 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 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 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 ...
随机推荐
- Postman-进阶(2)
Postman-进阶(2) Postman-简单使用 Postman-进阶使用 Postman-CI集成Jenkins 管理请求 保存请求-添加“打开百度首页请求” 设置请求方式为Get,地址为www ...
- Python作业--登录接口
作业需求: 编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 实现思路: 1.从文件获取用户名密码 2.判断是否在黑名单中 3.验证用户名密码 成功:输出认证成功 错误:判断验证次 ...
- [oldboy-django][2深入django]form表单clean_xx, clean完成数据验证+ form错误信息
form后台生成form里面的Input标签,以及设置Input的属性 # 需求 后台生成form里面的input标签,并设置input标签的属性, class RegisterForm(Form): ...
- linux备忘录-bash
知识点 环境变量 HOME PATH MAIL SHELL RANDOM // 0~32767的随机数 declare -i number=$RANDOM*10/32768 //0-9的随机数 HIS ...
- php连接数据库增删改查----多条件查询
关于查询,可以直接写在主页面上 来进行查询 首先,先建立一个表单 <form method="post" action="crud.php"> &l ...
- HDU 4417 Super Mario ( 离线树状数组 )
把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...
- Struts2+DAO层实现实例01——搭建Struts2基本框架
实例内容 利用Strust2实现一个登陆+注册功能的登陆系统. 实现基础流程:
- Android事件分发机制详解(1)----探究View的事件分发
探究View的事件分发 在Activity中,只有一个按钮,注册一个点击事件 [java] view plaincopy button.setOnClickListener(new OnClickLi ...
- PHP路径相关 dirname,realpath,__FILE__
比如:程序根目录在:E:\wamp\www 中 1. __FILE__ 当前文件的绝对路径 如果在index.php中调用 则返回 E:\wamp\www\index.php 下面再看一 ...
- quagga源码学习--BGP协议中的routemap
路由策略的基础知识 定义 路由策略(Routing Policy)作用于路由,主要实现了路由过滤和路由属性设置等功能,它通过改变路由属性(包括可达性)来改变网络流量所经过的路径. 目的 路由器在发布. ...