#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线段树(成段)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  2. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  3. 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. 这题 ...

  4. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  5. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  6. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  7. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  8. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  9. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

随机推荐

  1. php中method_exists()和is_callable()如何进行语句判断

    method_exists()和is_callable()方法进行判断.那么两则区别是什么呢? 已知类文件如下: class Student{private $alias=null;private $ ...

  2. yii2通过foreach循环遍历在一个用户组中取出id去另一表里查寻信息并且带着信息合并原数组信息---案例

    yii2通过foreach循环遍历在一个用户组中取出id去另一表里查寻信息并且带着信息合并元数组信息---案例 public function actionRandomLists(){ //查询到了所 ...

  3. 深入理解GCD(一)

    虽然 GCD 已经出现过一段时间了,但不是每个人都明了其主要内容.这是可以理解的:并发一直很棘手,而 GCD 是基于 C 的 API ,它们就像一组尖锐的棱角戳进 Objective-C 的平滑世界. ...

  4. HDU 1520:Anniversary party(树形DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1520 Anniversary party Problem Description   There i ...

  5. pandas安装过程中提示unable to find vcvarsall.bat的解决方法

    转载自:http://blog.csdn.net/qq_21144699/article/details/46849561 为这位老兄点赞 原帖参考:http://stackoverflow.com/ ...

  6. 基于@AspectJ和schema的aop(一)

    在前面我们使用Pointcut和Advice描述切点和增强, 并使用Advisor整合两者描述切面.@AspectJ使用注解来描述切点和增强.两者使用的方式不同, 但是在本质上都是一样的. 我们还是用 ...

  7. zoj Gao The Sequence

    Gao The Sequence Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a sequence of integ ...

  8. CentOS 配置solr中文分词器

    第一步:使用IK-Analyzer.把分析器的文件夹上传到服务器. 第二步:需要把分析器的jar包添加到solr工程中. [root@bogon IK Analyzer 2012FF_hf1]# cp ...

  9. Codeforces Round #373 (Div. 2) B

    Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...

  10. qbxt十一系列三

    [题目分析] 这完全是个数学题啊,有些崩溃,上午考试写了两个小时,20分,于是乎 下午改啊改啊 改到10分....如果第二个圆的圆心在第一个圆.....呃 说不清楚 像下面这个图这样,两圆重叠部分中C ...