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 ...
随机推荐
- Jcapta
http://blog.csdn.net/shadowsick/article/details/8575471
- Delphi操作XML简介
参考:http://www.delphifans.com/InfoView/Article_850.html Delphi 7支持对XML文档的操作,可以通过 TXMLDocument类来实现对XML ...
- linux命令**50
1.ls命令 命令格式: ls [选项] [目录名] 命令功能: 列出目标目录中所有的子目录和文件. 常用参数: -a,列出所有文件包括隐藏文件 -l,列出详细信息,文件大小一般以字节大小显示 -h, ...
- POJ2065 SETI(高斯消元 同模方程)
(a1 * 1^0 + a2 * 1^1 + ... an * 1^n - 1) % P = f1 .... (a1 * n^0 + a2 * n^1 + ... an - 1 * ...
- Ubuntu / Win7 安装db2 v10.5
抓紧下载v10.5fp1_linuxx64_expc.tar.gz到~/Downloads/java_softcd java_softtar xf v10.5fp1_linuxx64_expc.tar ...
- 介绍n款计算机视觉库/人脸识别开源库/软件
计算机视觉库 OpenCV OpenCV是Intel®开源计算机视觉库.它由一系列 C 函数和少量 C++ 类构成,实现了图像处理和计算机视觉方面的很多通用算法. OpenCV 拥有包括 300 多个 ...
- Zigzag convert
public static String Convert(String s,int row) { char[] c=s.toCharArray(); int len=s.length(); Strin ...
- FZU Problem 2082 过路费 树链剖分
Problem 2082 过路费 Problem Description 有n座城市,由n-1条路相连通,使得任意两座城市之间可达.每条路有过路费,要交过路费才能通过.每条路的过路费经常会更新, ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- T-SQL 使用WITH高效分页
一.WITH AS 含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候, ...