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 ...
随机推荐
- 22.访问者模式(Vistor Pattern)
using System; using System.Collections; namespace ConsoleApplication5 { /// <summary> /// 访问者模 ...
- Bootstrap简介
接下来的一段时间,想研究一下现有的网页框架,第一个不容错过的就是Bootstrap,Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CS ...
- 【JAVA之泛型】
一.引例. 1.引例. 假设现在有一个ArrayList的容器,如果不使用泛型约束,则可以向容器中加入各种类型的对象,但是如果取出来的时候只是用一种类型的转换则肯定会抛出ClassCastExcept ...
- poj 1003:Hangover(水题,数学模拟)
Hangover Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 99450 Accepted: 48213 Descri ...
- 6-02使用SQL语句向表中插入数据
插入语句的语法: INSERT INTO 表() VALUES(值列表) 注意事项: 1:每次插入一行数据,不能只插入半行或几列数据. 2:每一个数据值的数据类型.精度和小数位数必须与相应的列匹配. ...
- [QCon] Scrum阅读随想
最近从群里面下载到几篇文章,看到QCon出来的相关文章,觉得都写的很不错,都是一些个大公司的非常好的方法 QCon:是为团队领导者.架构师.项目经理和高级软件开发人员量身打造的企业软件开发大会,其 ...
- Windows phone 8.0 本地化遇到的两个问题
基本上来说,按照msdn来讲的,本地化和全球化没有太多的问题,链接如下: http://msdn.microsoft.com/zh-cn/library/windowsphone/develop/ff ...
- csc.rsp Invent by Microshaoft
# This file contains command-line options that the C# # command line compiler (CSC) will process as ...
- Arduino101学习笔记(九)—— 中断函数
1.设置中断函数 //***************************************************************************************** ...
- LayoutInflater(二)
每一个视图的绘制过程都必须经历三个最主要的阶段,即onMeasure().onLayout()和onDraw(),下面我们逐个对这三个阶段展开进行探讨. 一. onMeasure() measure是 ...