POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
题意分析
注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大坑。
代码总览
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 200000
using namespace std;
struct Tree{
int l,r;
long long val;
long long lazy;
int mid(){
return (l+r)>>1;
}
};
Tree tree[nmax<<2];
long long num[nmax];
void PushUp(int rt)
{
tree[rt].val = tree[rt<<1].val + tree[rt<<1|1].val;
}
void PushDown(int rt)
{
if(tree[rt].lazy){
tree[rt<<1].lazy += tree[rt].lazy;
tree[rt<<1|1].lazy += tree[rt].lazy;
tree[rt<<1].val += tree[rt].lazy * (tree[rt<<1].r - tree[rt<<1].l +1) ;
tree[rt<<1|1].val +=tree[rt].lazy * (tree[rt<<1|1].r - tree[rt<<1|1].l +1);
tree[rt].lazy = 0;
}
}
void Build(int l, int r, int rt)
{
tree[rt].l = l; tree[rt].r = r;
tree[rt].val = tree[rt].lazy = 0;
if(l == r){
tree[rt].val = num[l];
return;
}
Build(l,tree[rt].mid(),rt<<1);
Build(tree[rt].mid()+1,r,rt<<1|1);
PushUp(rt);
}
void UpdatePoint(long long val, int pos, int rt)
{
if(tree[rt].l == tree[rt].r){
tree[rt].val+=val;
return;
}
PushDown(rt);
if(pos<= tree[rt].mid()) UpdatePoint(val,pos,rt<<1);
else UpdatePoint(val,pos,rt<<1|1);
PushUp(rt);
}
void UpdateInterval(long long val, int l, int r, int rt)
{
if(tree[rt].l >r || tree[rt].r < l) return;
if(tree[rt].l >= l && tree[rt].r <= r){
tree[rt].val += val * (tree[rt].r - tree[rt].l + 1);
tree[rt].lazy += val;
return;
}
PushDown(rt);
UpdateInterval(val,l,r,rt<<1) ;
UpdateInterval(val,l,r,rt<<1|1);
PushUp(rt);
}
long long Query(int l,int r,int rt)
{
if(l>tree[rt].r || r<tree[rt].l) return 0;
PushDown(rt);
if(l <= tree[rt].l && tree[rt].r <= r) return tree[rt].val;
else return Query(l,r,rt<<1) + Query(l,r,rt<<1|1);
}
int N,Q,a,b;
long long c;
char op;
int main()
{
//freopen("in3468.txt","r",stdin);
while(scanf("%d %d",&N,&Q) != EOF){
for(int i = 1;i<=N;++i) scanf("%I64d",&num[i]);
Build(1,N,1);
for(int i = 0;i<Q;++i){
scanf(" %c %d %d",&op,&a,&b);
if(op == 'C'){
scanf("%I64d",&c);
UpdateInterval(c,a,b,1);
}else{
printf("%I64d\n",Query(a,b,1));
}
}
}
return 0;
}
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)的更多相关文章
- 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 , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- [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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- 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 ...
随机推荐
- python装饰器简单使用
装饰器和闭包关联很大,要先明白闭包是什么 原始代码: def foo(): print('fcc') 增加装饰器 from time import ctime,sleep def w(fcc): de ...
- katalon系列十三:5.10新增跳过用例&命令行赋值全局变量
Katalon Studio升级到5.10版本了,这次新增了2个很实用的功能:一.跳过用例在Listener中新增了跳过用例方法,Listener类似于JUnit4的annotation中的@Befo ...
- localhost/127.0.0.1/本机IP的区别以及端口号
端口号: http请求默认的端口是:80 PHPstudy中的端口号: Apache服务器的端口是:80 MySQL数据库的端口是:3306 PHP项目端口是:9000 禅道中的端口号: Apache ...
- Scrapy模拟登录GitHub
d: 进入D盘 scrapy startproject GitHub 创建项目 scrapy genspider github github.com 创建爬虫 编辑github.py: # -*- c ...
- MySQL5.6.14从安装到启动全过程
1.下载 地址:http://dev.mysql.com/downloads/mysql/ 这里选择的是Linux-Generic平台,下载了MySQL-5.6.14-1.linux_glibc2.5 ...
- TPO 02 - Early Cinema
TPO 02 - Early Cinema NOTE: 主要意思(大概就是主谓宾)用粗体标出:重要的其它用斜体: []中的是大致意思,可能与原文有关也可能无关,但不会离题 目的为训练句子/段落总结能力 ...
- Scrum立会报告+燃尽图(十月十五日总第六次):视频上传及选题介绍工作
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2196 Scrum立会master:田良 一.小组介绍 组长:付佳 组员: ...
- 第七周C语言代码
#ifndef NMN_LIST_H #define NMN_LIST_H #include <stdio.h> struct list_head { struct lis ...
- 关于String和StringBuffer的原理
public class Foo {2. public static void main (String [] args) {3. StringBuffer a = new Strin ...
- lintcode-203-线段树的修改
203-线段树的修改 对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值. 设计一个 modify 的方法,接受三个参数 root. index 和 val ...