ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers
Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Description
给出了一个序列,你需要处理如下两种询问。 "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。 "Q a b" 询问[a, b]区间中所有值的和。 Input
第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000. 第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。 接下来Q行询问,格式如题目描述。 Output
对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。 Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
//用到懒惰标记法。
//Query查询求和,UpData更新数据。
//AC代码:
#include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
using namespace std;
#define MX 1000000 + 10
#define INF 0
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 long long sum[MX<<]; //数据很大可能会爆int,用long long
long long lazy[MX<<]; void PushUp(int rt) {
sum[rt]=sum[rt<<]+sum[rt<<|];
} void PushDown(int rt,int m) {
if(lazy[rt]) { //如果存在懒惰标记就把标记下移;
lazy[rt<<] +=lazy[rt];
lazy[rt<<|]+=lazy[rt];
sum[rt<<] +=lazy[rt]*(m-(m>>));
sum[rt<<|] +=lazy[rt]*(m>>);
lazy[rt]=INF;
}
} void Build(int l,int r, int rt) {
lazy[rt]=INF; //清空懒惰标记
if(r==l) {
scanf("%I64d",&sum[rt]); //输入每个叶节点的值
return ;
}
int m=(r+l)>>;
Build(lson);
Build(rson);
PushUp(rt);
} void UpData(int L,int R,int c,int l,int r,int rt) {
if (L<=l&&r<=R) {
lazy[rt]+=c; //输要改变的值,记录懒惰标记 。
sum[rt]+=(r-l+)*c; //中间N个数都要进行相同的加减。
return ;
}
PushDown(rt,r-l+); //下移懒惰标记
int m=(r+l)>>;
if(L<=m) UpData(L,R,c,lson);
if(R> m) UpData(L,R,c,rson);
PushUp(rt);
} long long Query(int L,int R,int l,int r,int rt) {
if(L<=l&&r<=R) return sum[rt];
PushDown(rt,r-l+);
//【这步不能少,如果区间没有全部包括,要保证每一个标记都放到目标子节点】
int m=(r+l)>>;
long long ret=;
if(L<=m) ret+=Query(L,R,lson);
if(R> m) ret+=Query(L,R,rson);
return ret;
} int main() {
int n,q;
while(~scanf("%d%d",&n,&q)) {
Build(,n,);
char s[];
int a,b,c;
for(int i=; i<q; i++) {
scanf("%s",s);
if(s[]=='Q') {
scanf("%d%d",&a,&b);
printf("%I64d\n",Query(a,b,,n,));
} else if(s[]=='C') {
scanf("%d%d%d",&a,&b,&c);
UpData(a,b,c,,n,);
}
}
}
return ;
}
ACM: A Simple Problem with Integers 解题报告-线段树的更多相关文章
- POJ 3468.A Simple Problem with Integers 解题报告
用树状数组和线段树会比较简单,这里用这道题来学习Splay. 第一次写,代码比较丑 /* 初始化添加一个key值足够大的结点 保证每个需要的结点都有后继 */ #include <iostrea ...
- POJ 3468 A Simple Problem with Integers(详细题解) 线段树
这是个线段树题目,做之前必须要有些线段树基础才行不然你是很难理解的. 此题的难点就是在于你加的数要怎么加,加入你一直加到叶子节点的话,复杂度势必会很高的 具体思路 在增加时,如果要加的区间正好覆盖一个 ...
- C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)
参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...
- A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询
//add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...
- A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 69589 ...
- A Simple Problem with Integers(100棵树状数组)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4267 A Simple Problem with Integers 多个树状数组
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
- ACM: Just a Hook 解题报告 -线段树
E - Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
随机推荐
- Java系列笔记(3) - Java 内存区域和GC机制
目录 Java垃圾回收概况 Java内存区域 Java对象的访问方式 Java内存分配机制 Java GC机制 垃圾收集器 Java垃圾回收概况 Java GC(Garbage Collection, ...
- SQLServer子查询
in谓词子查询 select * from dbo.Worker where did in (select dID from DepartMent) 比较运算子查询 select * from Wor ...
- 攻城狮在路上(壹) Hibernate(六)--- 通过Hibernate操纵对象(上)
一.Hibernate缓存简介: Session接口是Hibernate向应用程序提供的操纵数据接口的最主要接口,它提供了基本的保存.更新.删除和加载Java对象的方法. Session具有一个缓存, ...
- PHP json_encode中文乱码解决方法
相信很多人在使用Ajax与后台php页面进行交互的时候都碰到过中文乱码的问题.JSON作为一种轻量级的数据交换格式,备受亲睐,但是用PHP作为后台交互,容易出现中文乱码的问题.JSON和js一样,对于 ...
- win7-32 系统 + VS2010 配置 glew
网上下载的程序,运行时报错: C1083: 无法打开包括文件:“gl\glew.h”: No such file or directory. 百度一下,发现需要配置 glew 库. 方法如下: 下载 ...
- 注解:【无连接表的】Hibernate单向N->1关联
Person与Address关联:单向N->1,[无连接表的] Person.java package org.crazyit.app.domain; import javax.persiste ...
- VPS -Digital Ocean -搭建一个最简单的web服务器
简单的也是美的 在一个目录放自己的几个showcase网页方便和别人分享,最简单的方式是什么 创建文件夹,放入自己的网页文件 在目录下执行 $ nohup python -m SimpleHTTPSe ...
- Linux C编程(2) dgb调试
1. 首先编写一个用于调试的测试程序test.c #include <stdio.h> int get_sum(int n) { ,i; ; i <=n ; i++) { sum+= ...
- c++11 正则表达式基本使用
c++ 11 正则表达式 常用的方法 regex_match regex_search regex_replace 等. regex_match 要求正则表达式必须与模式串完全匹配,例如: strin ...
- 从两个平方算法到分治算法-java
先来看看问题的来源,假设有这么一个数组: 1 2 -5 4 -2 3 -3 4 -15 我们要求出其中连续字数组的和的最大值 例如这么可以很明显看出 4+ –2 + 3 + –3 + 4 = 6 所有 ...