POJ 3468 线段树+lazy标记
lazy标记
Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u
Submit Status
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C abc" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
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
Hint
The sums may exceed the range of 32-bit integers.
<span style="color:#6633ff;">/********************************************************author : Grant Yuantime : 2014.7.28algorithm : 线段树+lazy标记source : POJ 3468*********************************************************/#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define MAX 100003
#define LL long longusing namespace std;
struct node{
LL l;
LL r;
LL sum;
LL lazy;
};
node tree[3*MAX];
int n,m;
LL input[MAX],ans;void build(LL left,LL right,LL root)
{tree[root].lazy=0;tree[root].l=left;tree[root].r=right;if(left==right){tree[root].sum=input[left];return;}int mid=(left+right)>>1;build(left,mid,root*2);build(mid+1,right,root*2+1);tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
}void upchild(LL root)
{if(tree[root].lazy){tree[root<<1].sum+=(tree[root<<1].r-tree[root<<1].l+1)*tree[root].lazy;tree[(root<<1)|1].sum+=(tree[(root<<1)|1].r-tree[(root<<1)|1].l+1)*tree[root].lazy;tree[root<<1].lazy+=tree[root].lazy;tree[root<<1|1].lazy+=tree[root].lazy;tree[root].lazy=0;}
}void update(LL left,LL right,LL root,LL p)
{if(left<=tree[root].l&&right>=tree[root].r){tree[root].sum+=(LL)(tree[root].r-tree[root].l+1)*p;tree[root].lazy+=(LL)p;return;}upchild(root);int mid=(tree[root].l+tree[root].r)>>1;if(mid<right)update(left,right,root*2+1,p);if(mid>=left)update(left,right,root*2,p);tree[root].sum=tree[root<<1].sum+tree[(root<<1)|1].sum;
}void query(LL left,LL right,LL root)
{if(left<=tree[root].l&&right>=tree[root].r){ ans+=(LL)tree[root].sum;return;}upchild(root);int mid=(tree[root].l+tree[root].r)>>1;if(mid>=left)query(left,right,root<<1);if(mid<right)query(left,right,root*2+1);
}int main()
{ char s;LL a,b,q;while(~scanf("%d%d",&n,&m)){for(int i=1;i<=n;i++)scanf("%lld",&input[i]);build(1,n,1);for(int i=0;i<m;i++){ getchar();scanf("%c",&s);if(s=='Q'){ans=0;scanf("%lld%lld",&a,&b);query(a,b,1);printf("%lld\n",ans);}else if(s=='C'){scanf("%lld%lld%lld",&a,&b,&q);update(a,b,1,q);}}}return 0;
}
</span>
转载于:https://www.cnblogs.com/codeyuan/p/4254477.html
POJ 3468 线段树+lazy标记相关推荐
- POJ 3225 线段树+lazy标记
lazy写崩了--. 查了好久 /* U-> [l,r]–>1 I-> [1,l-1] [r+1,+无穷] –>0 D-> [l,r]–>0 C-> [1,l ...
- HYSBZ - 1798 Seq 维护序列seq 线段树lazy标记
传送门 这道题属实是线段树的道比刷题,又加又乘的,当然还可能会有乘除,阶乘等等可能的情况. 对于这道题,主要的一个就是怎么记录lazy标记,首先的话一个数组是肯定不行的,设乘的为lazy,加的为add ...
- 20.CF817F MEX Queries 线段树(Lazy标记练习)
20.CF817F MEX Queries 离散化+区间覆盖+区间反转线段树 个人Limitの线段树题单题解主目录:Limitの线段树题单 题解目录_HeartFireY的博客-CSDN博客 要求维护 ...
- [hiho 22]线段树-lazy标记的下放
题目描述 之前提到过,线段树之所以更新查询快,是因为区间更新有lazy标记使得不需要每次都操作到叶子节点. 但是如果要操作一个节点时,其父节点上的lazy标记应当被释放,否则该节点无法得到最新的正确结 ...
- poj 3468 线段树
线段树的 建立build(初始化+左右相等+两个递归+别忘了sum)+更新update(递归出口+更新delta+三向递归+修正当前节点的value)+查找query(如果左右相等+更新delta+三 ...
- A Simple Problem with Integers POJ - 3468 (线段树)
思路:线段树,区间更新,区间查找 1 #include<iostream> 2 #include<vector> 3 #include<string> 4 #inc ...
- Hotel POJ - 3667(线段树 + 区间合并
题意: 给定长度为n的区间 ,有2个操作: 操作1: 在区间中靠左放k个元素,输出新放入元素中最左边的位置,如果放不下输出 0: 操作2 : 清空 l 到 l+w-1这一段区间的元素 这里有一个状态转 ...
- POJ 2777 线段树
一道线段树.lazy标记+位运算--(第一次写这个什么lazy标记,抄了一发题解) 我们发现:"或"操作在这里用正合适. 原题请戳这里 // by Sirius_Ren #incl ...
- POJ - 2528 线段树+离散化
其实很早就在白书上的常用技巧上 看到离散化的操作,但是之前一直没遇到过需要离散化的题目(应该是我太菜的缘故),所以一直也没怎么重视,下面说说这道题目的考点,也就是离散化. 什么是离散化呢?请先自行百度 ...
最新文章
- matlab中conv滤波,其中是Matlab(imfilter)和TensorFlow中偶数滤波器(6x6)的中心像素(转速表nn.conv2d)?...
- font awesome java_Android使用Font Awesome显示小图标(一)
- BlockChain:《2016年中国区块链产业概况》听课笔记
- java 数组参数_java中 数组可以作为形式参数传递到调用的方法中吗?要怎么操作?...
- 全程图解:Centos 5.2 下 ORACLE10G 安装配置
- activemq优先级_ActiveMQ消息优先级:工作原理
- HDU - 6333 Harvest of Apples
- JavaScript之阻止事件冒泡。
- linux内核之进程地址空间
- 计算机编辑文档教程,列举Word文档中常用编辑操作
- 数据挖掘算法学习及应用场景
- 【无标题】Windows10安全中心永久关闭教程
- 微信公众号申请注意事项
- Android使用网络打印机打印
- PXE自动安装Linux系统
- 【罗塞塔石碑】—My Lover(One.iso)
- knn鸢尾花数据集java_1.从鸢尾花数据集与KNN说起
- 搭建Android上的服务器
- CSS给元素添加边框(样式、颜色、宽度)
- Windows下在命令行安装postgresql,并注册成window服务;产品内置postgre
热门文章
- MFC的非模态对话框资源管理
- 问题 K: Sequence Problem (III) : Array Practice
- 我的第一本算法书(图解算法)——什么是哈希表
- 网络表示学习算法综述
- 拜登公布价值1.8万亿美元“美国家庭计划”
- Zilliqa联合创始人:ZIP-12提案已成功部署,为未来的治理投票奠定基础
- 数据:灰度比特币信托基金溢价达41%创近一年新高
- SAP License:SAP不便解决的问题之九——客户退货维修
- 机器学习:神经网络的代价函数及反向传播算法
- 项目管理系统、工作台、经营看板、质量管理、合同管理、合同审核、新建合同、分包商管理、立项审批、创建项目、项目模板、项目统计、计划管理、结项申请、审批流程、审批记录、审批状态、参数设置、axure原型