线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<cstdlib>
- #include<cstring>
- #include<string>
- using namespace std;
- const int N = ;
- typedef long long int LL;
- LL sum[N << ];
- LL add[N << ];
- struct node
- {
- int l, r;
- int mid()
- {
- return (l + r) >> ;
- }
- }tree[N << ];
- void PushUp(int rt)
- {
- sum[rt] = sum[rt << ] + sum[rt << | ];
- }
- void PushDown(int rt, int m)
- {
- if (add[rt]) {
- add[rt << ] += add[rt];
- add[rt << | ] += add[rt];
- sum[rt << ] += add[rt] * (m - (m >> ));
- sum[rt << | ] += add[rt] * (m >> );
- add[rt] = ;//更新后要还原,因为递归可能会多次用到这个
- }
- }
- void build(int l,int r,int rt)
- {
- tree[rt].l = l;
- tree[rt].r = r;
- add[rt] = ;
- if (l == r) {
- scanf("%lld", &sum[rt]);
- return;
- }
- int m = tree[rt].mid();
- build(l, m, rt << );
- build(m + , r, rt << | );
- PushUp(rt);
- }
- void updata(int c, int l, int r, int rt)
- {
- if (tree[rt].l == l && r == tree[rt].r) {
- add[rt] += c;
- sum[rt] += (LL)(r - l + )*c;
- return;//这里没有进行子区间更新,用到lazy标记
- }
- if (tree[rt].l == tree[rt].r) return;
- PushDown(rt, tree[rt].r - tree[rt].l + );
- int m = tree[rt].mid();
- if (r <= m) updata(c,l, r, rt << );
- else if (l > m) updata(c,l, r, rt << | );
- else {
- updata(c, l, m, rt << );
- updata(c, m + , r, rt << | );
- }
- PushUp(rt);
- }
- LL query(int l, int r, int rt)
- {
- if (l == tree[rt].l&&r == tree[rt].r) {
- return sum[rt];
- }
- PushDown(rt, tree[rt].r - tree[rt].l + );//标记的特点,用时才进行更新
- int m = tree[rt].mid();
- LL res = ;
- if (r <= m) res += query(l, r, rt << );
- else if (l > m) res += query(l, r, rt << | );
- else {
- res += query(l, m, rt << );
- res += query(m + , r, rt << | );
- }
- return res;
- }
- int main()
- {
- int n, m;
- while (scanf("%d %d", &n, &m) == ) {
- memset(sum, , sizeof(sum));
- memset(add, , sizeof(add));
- build(, n, );
- while (m--) {
- char ch[];
- scanf("%s", ch);
- if (ch[] == 'Q') {
- int a, b;
- scanf("%d %d", &a, &b);
- printf("%lld\n", query(a, b, ));
- }
- else {
- int a, b, c;
- scanf("%d %d %d", &a, &b, &c);
- updata(c, a, b, );
- }
- }
- }
- return ;
- }
线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468的更多相关文章
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- codevs 1690 开关灯 线段树区间更新 区间查询Lazy
题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...
- 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: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
- POJ-3468(线段树+区间更新+区间查询)
A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...
- HDU1698 线段树(区间更新区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- CDOJ 1057 秋实大哥与花 线段树 区间更新+区间查询
链接: I - 秋实大哥与花 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
随机推荐
- 最终类object 和内部类
Object 类 性质:[1]是所有类的根类. [2]如果一个类没有显示继承另外一个类,那么该类一定继承于Object toString() 返回对象的字符串表示形式 特殊:[ ...
- python2.7 加密模块 解决各种坑
1 Python27 安装crypto Windows安装 在Windows上安装的时候直接 pip install pycrypto会报错,参考:http://blog.csdn.net/teloy ...
- Ubuntu 12.04 the system is running in low-graphics mode
1.出现问题如图所示: 2.解决方案: Ctrl + Alt + F1 df -h 输入密码,到了这一步,也是可以使用terminal,那么没有图形界面也是可以的 cd /etc/X11 sudo c ...
- C#操作Word,写数据,插入图片
本篇介绍的是如何在C#中往word里面写入数据. 如何在线的操作文档: c#在线操作文档 关于Aspose.Word控件的介绍,请戳→ 介绍 首先需要去下载这个dll文件,然后引用到你的项目当中.地 ...
- vscode + leetcode +github 同步
1.用VScode打开本地leetcode文件夹 C:\Users\Administrator\.leetcode 2.上传到本地git库 3.打开github桌面,上传到远程库
- filebeat的安装及配置
概述:Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasti ...
- css样式 body的font-size 为什么用625%
浏览器的默认高度?一般为16px. 为什么用62.5%作为body的默认样式?16px62.5%=10px.* 那么为什么一般多是 16px *625% = 100px; <响应式Web设计实 ...
- MySQL创建民族表的SQL语句
MySQL创建民族表的SQL语句 CREATE TABLE `nation` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `nation` ) NOT NUL ...
- php-5.6.26源代码 - opcode处理器,“函数调用opcode”处理器,如何调用扩展模块的函数
// opcode处理器 --- ZEND_DO_FCALL_SPEC_CONST_HANDLER实现在 php-5.6.26\Zend\zend_vm_execute.h static int ZE ...
- 易语言制作的QQ聊天中常用的GIF图片【带源码下载】
该软件调用网页实现表情包制作,使用了精益模块. 最近比较火的王境泽.张学友.切格瓦拉.为所欲为.今天星期五.黑人问号脸.偷电瓶车.诸葛孔明.金坷垃等都可以通过此软件在线制作属于你的表情包. 太困了懒得 ...