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 解题报告-线段树的更多相关文章

  1. POJ 3468.A Simple Problem with Integers 解题报告

    用树状数组和线段树会比较简单,这里用这道题来学习Splay. 第一次写,代码比较丑 /* 初始化添加一个key值足够大的结点 保证每个需要的结点都有后继 */ #include <iostrea ...

  2. POJ 3468 A Simple Problem with Integers(详细题解) 线段树

    这是个线段树题目,做之前必须要有些线段树基础才行不然你是很难理解的. 此题的难点就是在于你加的数要怎么加,加入你一直加到叶子节点的话,复杂度势必会很高的 具体思路 在增加时,如果要加的区间正好覆盖一个 ...

  3. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  4. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...

  5. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

  6. A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 69589   ...

  7. A Simple Problem with Integers(100棵树状数组)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. HDU 4267 A Simple Problem with Integers 多个树状数组

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  9. ACM: Just a Hook 解题报告 -线段树

    E - Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   D ...

随机推荐

  1. 解决Pyqt打包后运行报错:应用程序无法启动 因为程序的并行配置不正确

    做了一个生成二维码的小程序:http://www.cnblogs.com/dcb3688/p/4241048.html 直接运行脚本没问题,用pyinstaller打包后再运行就直接报错了: 应用程序 ...

  2. 开个坑, 写个阿里云开放储存服务(OSS)的C++版SDK以及客户端

    这应该是继我研究手册QQ协议后的第2个稍微正式一点的网络程序, 不只是Scoket套接字编程, 还涉及到更多的HTTP协议知识! 阿里云开放储存服务OSS官方已经提供了不少SDK, 包括PHP/Pyt ...

  3. golang json 包简单分析

    首先上代码: func main() { b := true a1, _ := json.Marshal(b) a2, _ := Marshal(b) fmt.Println(string(a1)) ...

  4. h5 range应用 透明度+RGB

    透明度 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  5. .net Session 详解

    (一) 描述当用户在 Web 应用程序中导航 ASP.NET 页时,ASP.NET 会话状态使您能够存储和检索用户的值.HTTP 是一种无状态协议.这意味着 Web 服务器会将针对页面的每个 HTTP ...

  6. ADT开发AndroidManifest.xml file missing错误

    一个错误“AndroidManifest.xml file missing”但helloworld目录下有此文件,几番google仍没能解决.想起曾经在网络上看到的一个修复project的办法,抱着死 ...

  7. Linux学习笔记(9)Linux常用命令之关机重启命令

    (1)shutdown shutdown命令用于关机重启,其语法格式为: shutdown [选项] 时间 其中,-c选项表示取消前一个设置的shutdown命令,-h命令表示关机,-r命令表示重启 ...

  8. kylin查询出现日期对应不上的情况

    情况: 查询的是2016年1月2日的数据,但返回解析出来的数据确实是2号的,可是时间竟然变成了2016年1月1日. 解决: 是时区问题,修改本地时区 具体代码,主要是看加红加粗的: public st ...

  9. 587A

    #include<iostream> #include<algorithm> #include<stdio.h> #include<stdlib.h> ...

  10. Liferay 6.2 改造系列之三:删除Docbar中的添加内容功能

    在/portal-master/portal-web/docroot/html/portlet/dockbar/add_panel.jsp文件中 将以下内容: if (hasAddContentAnd ...