hihoCoder #1078 : 线段树的区间修改
题目大意及分析:
线段树成段更新裸题。
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std; const int N=100000; int n,m;
int a[N+5];
int tr[N*4+5];
int lazy[N*4+5]; void pushDown(int rt,int l,int r)
{
if(lazy[rt]!=-1){
int mid=l+(r-l)/2;
tr[rt<<1]=lazy[rt]*(mid-l+1);
tr[rt<<1|1]=lazy[rt]*(r-mid);
lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
lazy[rt]=-1;
}
} void makeTree(int rt,int l,int r)
{
if(l==r){
scanf("%d",&tr[rt]);
}else{
int mid=l+(r-l)/2;
makeTree(rt<<1,l,mid);
makeTree(rt<<1|1,mid+1,r);
tr[rt]=tr[rt<<1]+tr[rt<<1|1];
}
} void update(int rt,int l,int r,int L,int R,int x)
{
if(L<=l&&r<=R){
lazy[rt]=x;
tr[rt]=x*(r-l+1);
}else{
pushDown(rt,l,r);
int mid=l+(r-l)/2;
if(L<=mid)
update(rt<<1,l,mid,L,R,x);
if(R>mid)
update(rt<<1|1,mid+1,r,L,R,x);
tr[rt]=tr[rt<<1]+tr[rt<<1|1];
}
} int query(int rt,int l,int r,int L,int R)
{
if(L<=l&&r<=R)
return tr[rt];
pushDown(rt,l,r);
int res=0;
int mid=l+(r-l)/2;
if(L<=mid)
res+=query(rt<<1,l,mid,L,R);
if(R>mid)
res+=query(rt<<1|1,mid+1,r,L,R);
return res;
} int main()
{
while(~scanf("%d",&n))
{
memset(tr,0,sizeof(tr));
memset(lazy,-1,sizeof(lazy));
makeTree(1,1,n);
scanf("%d",&m);
int flag,a,b,c;
while(m--)
{
scanf("%d",&flag);
if(flag){
scanf("%d%d%d",&a,&b,&c);
update(1,1,n,a,b,c);
}else{
scanf("%d%d",&a,&b);
printf("%d\n",query(1,1,n,a,b));
}
}
}
return 0;
}
hihoCoder #1078 : 线段树的区间修改的更多相关文章
- hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)
#1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...
- hihoCode 1078 : 线段树的区间修改
#1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...
- hihoCoder week20 线段树的区间修改
区间修改 区间查询 最后一场比赛前的无可救药的热身 #include <bits/stdc++.h> using namespace std; #define mid ((l+r)/2) ...
- hiho一下20周 线段树的区间修改
线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题改了改,又出给了 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- hiho一下21周 线段树的区间修改 离散化
离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho ...
- UVa 11992 Fast Matrix Operations (线段树,区间修改)
题意:给出一个row*col的全0矩阵,有三种操作 1 x1 y1 x2 y2 v:将x1 <= row <= x2, y1 <= col <= y2里面的点全部增加v: 2 ...
- 培训补坑(day7:线段树的区间修改与运用)(day6是测试,测试题解以后补坑QAQ)
补坑咯~ 今天围绕的是一个神奇的数据结构:线段树.(感觉叫做区间树也挺科学的.) 线段树,顾名思义就是用来查找一段区间内的最大值,最小值,区间和等等元素. 那么这个线段树有什么优势呢? 比如我们要多次 ...
- HDU 1698 【线段树,区间修改 + 维护区间和】
题目链接 HDU 1698 Problem Description: In the game of DotA, Pudge’s meat hook is actually the most horri ...
随机推荐
- Map-Reduce的工作机制
Mapper “Map-Reduce”的思想就是“分而治之” Mapper负责“分”,即把复杂的任务分解为若干个“简单的任务”而执行 “简单的任务”有几个意思:1.数据或计算规模相对于原任务要大大缩小 ...
- 记录一些容易忘记的属性 -- UIGestureRecognize手势
//一个手势只能添加到一个view上面 //设置当前手势需要的点击次数 _tapRec.numberOfTapsRequired = 1;//(默认为1) //设置当前需要几个手指同时点击 ...
- JS 职责链模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- MySQL表的增删改查和列的修改(二)
一.使用Like模糊查找搜索前缀为以“exam_”开头的表名 show tables like 'exam_%' ; 语句结束符号是:也是用\G来表示 二.MySQL表的CRUD 2.1 创建表: C ...
- String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。
// 在字符串中删除指定字符串. String phoneNum="1795112345"; phoneNum = phoneNum.replace("17951&quo ...
- poj1276 多重背包
//Accepted 1100 KB 47 ms //多重背包 #include <cstdio> #include <cstring> #include <iostre ...
- hdu 1950 最长上升子序列
//Accepted 3540 KB 62 ms //dp 最长上升子序列 #include <cstdio> #include <cstring> #include < ...
- android-volley-at-a-glance
http://liubin.org/2013/05/27/android-volley-at-a-glance/ http://www.androidhive.info/2014/05/android ...
- openstack中运行定时任务的两种方法及源代码分析
启动一个进程,如要想要这个进程的某个方法定时得进行执行的话,在openstack有两种方式: 一种是通过继承 periodic_task.PeriodicTasks,另一种是使用loopingcall ...
- 2016-1-6第一个完整APP 私人通讯录的实现 3:添加联系人
一:创建模型对象:contact用于存放数据,也便于读取加载 #import <Foundation/Foundation.h> @interface contact : NSObject ...