HIHO线段树(成段)
#include <stdio.h>
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
const int MM = ;
int num[MM<<],lazy[MM<<]; void push_down(int l,int r,int id)
{
int mid=(l+r)>>;
num[id<<]=lazy[id]*(mid-l+);
num[id<<|]=lazy[id]*(r-mid);
lazy[id<<]=lazy[id<<|]=lazy[id];
lazy[id]=;
}
void build_tree(int l,int r,int id)
{
lazy[id]=;
if(l==r)
{
scanf("%d",&num[id]);
return;
}
else
{
int mid=(l+r)>>;
build_tree(lson);
build_tree(rson);
num[id]=num[id<<]+num[id<<|];
}
} void Update(int L,int R,int e,int l,int r,int id)
{
if(L<=l&&r<=R)
{
num[id]=(r-l+)*e;
lazy[id]=e;
return;
}
if(lazy[id])push_down(l,r,id);
int mid=(l+r)>>;
if(L<=mid)
Update(L,R,e,lson);
if(R>mid)
Update(L,R,e,rson);
num[id]=num[id<<]+num[id<<|]; }
int Query(int L,int R,int l,int r,int id)
{
if(L<=l&&r<=R)
{
return num[id];
}
if(lazy[id])push_down(l,r,id);
int mid=(l+r)>>;
int ret=;
if(L<=mid)ret+=Query(L,R,lson);
if(R>mid)ret+=Query(L,R,rson);
num[id]=num[id<<]+num[id<<|];
return ret;
}
int main()
{
int n,m,op,x,y,z;
scanf("%d",&n);
build_tree(,n,);
scanf("%d",&m);
while(m--)
{
scanf("%d",&op);
if(op==)
{
scanf("%d %d %d",&x,&y,&z);
Update(x,y,z,,n,);
}
else
{
scanf("%d %d",&x ,&y);
int ans=Query(x,y,,n,);
printf("%d\n",ans );
}
}
}
HIHO线段树(成段)的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
随机推荐
- java面试每日一题13
题目:有一个分数数列2/1.3/2.5/3.8/5.13/8........求出这个数列的前20项之和.运行结果如下32.660263 public class Page80 { /** * * @p ...
- Djnago的一些零碎知识点
1.TEMPLATE_DIRS relative to the project folder http://stackoverflow.com/questions/9856683/using-pyth ...
- 20160113006 asp.net实现ftp上传代码(解决大文件上传问题)
using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using S ...
- 《python核心编程》读书笔记--第15章 正则表达式
15.1引言与动机 处理文本和数据是一件大事.正则表达式(RE)为高级文本匹配模式,为搜索-替换等功能提供了基础.RE是由一些字符和特殊符号组成的字符串,它们描述了这些字符和字符串的某种重复方式,因此 ...
- PHPExcel的使用与手册说明
1.下载PHPExcel插件 2.解压后提取classes文件夹到工作目录,并重命名为PHPExcel 下载地址:http://phpexcel.codeplex.com/ 3.引入与实例化 ...
- jquery.util.easyui.dialog
(function ($) { var $parent = parent.$; //获取弹出窗口数据集合 function getDialogs() { var dialogs = $parent(& ...
- 2016年11月3日 星期四 --出埃及记 Exodus 19:19
2016年11月3日 星期四 --出埃及记 Exodus 19:19 and the sound of the trumpet grew louder and louder. Then Moses s ...
- 《redis-php中文参考手册》
<redis中文手册>,将示例代码用php来实现,注意php-redis与redis_cli的区别(主要是返回值类型和参数用法). phpredis是redis的php的一个扩展,效率是相 ...
- MVC 中DropDownList 用法
MVC 中DropDownList 用法 后台 Dictionary<string, int> dc = new Dictionary<string, int>(); dc. ...
- Duilib动画按钮实现(转载)
转载:http://blog.csdn.net/cuiguanghui123/article/details/51674218 .h文件 #ifndef __UIBUTTONEX_H__ #defin ...