HDU 5306 吉司机线段树
思路:



后面nlogn的部分是伪证...
吉老司机翻车了
//By SiriusRen
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=;
int cases,n,m,op,xx,yy,zz;
typedef long long ll;
struct SegTree{int max1,max2,maxnum,lazy;ll sum;}tr[N*];
void push_up(int pos){
tr[pos].max2=;int lson=pos<<,rson=pos<<|;
tr[pos].max1=max(tr[lson].max1,tr[rson].max1);
if(tr[lson].max1==tr[rson].max1)
tr[pos].maxnum=tr[lson].maxnum+tr[rson].maxnum,
tr[pos].max1=tr[lson].max1,
tr[pos].max2=max(tr[lson].max2,tr[rson].max2);
else{
tr[lson].max1>tr[rson].max1?tr[pos]=tr[lson]:tr[pos]=tr[rson];
tr[pos].max2=max(tr[lson].max2,tr[rson].max2);
if(tr[pos].max1==tr[lson].max1)tr[pos].max2=max(tr[pos].max2,tr[rson].max1);
else tr[pos].max2=max(tr[pos].max2,tr[lson].max1);
}tr[pos].sum=tr[lson].sum+tr[rson].sum;
}
void change(int pos,int wei){
tr[pos].sum-=(1ll*tr[pos].max1-wei)*tr[pos].maxnum;
tr[pos].max1=tr[pos].lazy=wei;
}
void push_down(int pos){
int lson=pos<<,rson=pos<<|;
if(tr[lson].max1>tr[pos].lazy)change(lson,tr[pos].lazy),tr[lson].lazy=tr[pos].lazy;
if(tr[rson].max1>tr[pos].lazy)change(rson,tr[pos].lazy),tr[rson].lazy=tr[pos].lazy;
tr[pos].lazy=-;
}
void build(int l,int r,int pos){
tr[pos].lazy=-,tr[pos].max2=;
if(l==r){scanf("%d",&tr[pos].max1),tr[pos].sum=tr[pos].max1,tr[pos].maxnum=;return;}
int mid=(l+r)>>,lson=pos<<,rson=pos<<|;
build(l,mid,lson),build(mid+,r,rson),push_up(pos);
}
void insert(int l,int r,int pos,int L,int R,int wei){
if(~tr[pos].lazy)push_down(pos);
int mid=(l+r)>>,lson=pos<<,rson=pos<<|;
if(l>=L&&r<=R){
if(wei>=tr[pos].max1)return;
else if(wei<tr[pos].max1&&wei>tr[pos].max2){change(pos,wei);return;}
else insert(l,mid,lson,L,R,wei),insert(mid+,r,rson,L,R,wei);
}
if(mid<L)insert(mid+,r,rson,L,R,wei);
else if(mid>=R)insert(l,mid,lson,L,R,wei);
else insert(l,mid,lson,L,R,wei),insert(mid+,r,rson,L,R,wei);
push_up(pos);
}
ll query_sum(int l,int r,int pos,int L,int R){
if(~tr[pos].lazy)push_down(pos);
if(l>=L&&r<=R)return tr[pos].sum;
int mid=(l+r)>>,lson=pos<<,rson=pos<<|;
if(mid<L)return query_sum(mid+,r,rson,L,R);
else if(mid>=R)return query_sum(l,mid,lson,L,R);
else return query_sum(l,mid,lson,L,R)+query_sum(mid+,r,rson,L,R);
}
int query_max(int l,int r,int pos,int L,int R){
if(~tr[pos].lazy)push_down(pos);
if(l>=L&&r<=R)return tr[pos].max1;
int mid=(l+r)>>,lson=pos<<,rson=pos<<|;
if(mid<L)return query_max(mid+,r,rson,L,R);
else if(mid>=R)return query_max(l,mid,lson,L,R);
else return max(query_max(l,mid,lson,L,R),query_max(mid+,r,rson,L,R));
}
int read(){
char p=getchar();int x=;
while(p<''||p>'')p=getchar();
while(p>=''&&p<='')x=x*+p-'',p=getchar();
return x;
}
signed main(){
scanf("%d",&cases);
while(cases--){
n=read(),m=read(),build(,n,);
for(int i=;i<=m;i++){
op=read(),xx=read(),yy=read();
if(!op)zz=read(),insert(,n,,xx,yy,zz);
else if(op==)printf("%d\n",query_max(,n,,xx,yy));
else printf("%lld\n",query_sum(,n,,xx,yy));
}
}
}
HDU 5306 吉司机线段树的更多相关文章
- HDU - 6315 吉司机线段树
题意:给出a,b数组,区间上两种操作,给\(a[L,R]\)+1s,或者求\(\sum_{i=l}^{r}a_i/b_i\) 一看就知道是吉司机乱搞型线段树(低配版),暴力剪枝就好 维护区间a的最大值 ...
- HDU - 5306 Gorgeous Sequence (吉司机线段树)
题目链接 吉司机线段树裸题... #include<bits/stdc++.h> using namespace std; typedef long long ll; ,inf=0x3f3 ...
- UVALive - 4108 SKYLINE (吉司机线段树)
题目链接 题意:在一条直线上依次建造n座建筑物,每座建筑物建造完成后询问它在多长的部分是最高的. 比较好想的方法是用线段树分别维护每个区间的最小值mi和最大值mx,当建造一座高度为x的建筑物时,若mi ...
- BZOJ4355: Play with sequence(吉司机线段树)
题意 题目链接 Sol 传说中的吉司机线段树??感觉和BZOJ冒险那题差不多,就是强行剪枝... 这题最坑的地方在于对于操作1,$C >= 0$, 操作2中需要对0取max,$a[i] > ...
- bzoj4355 Play with sequence(吉司机线段树)题解
题意: 已知\(n\)个数字,进行以下操作: \(1.\)区间\([L,R]\) 赋值为\(x\) \(2.\)区间\([L,R]\) 赋值为\(max(a[i] + x, 0)\) \(3.\)区间 ...
- bzoj5312 冒险(吉司机线段树)题解
题意: 已知\(n\)个数字,进行以下操作: \(1.\)区间\([L,R]\) 按位与\(x\) \(2.\)区间\([L,R]\) 按位或\(x\) \(3.\)区间\([L,R]\) 询问最大值 ...
- bzoj4695 最假女选手(势能线段树/吉司机线段树)题解
题意: 已知\(n\)个数字,进行以下操作: \(1.\)给一个区间\([L,R]\) 加上一个数\(x\) \(2.\)把一个区间\([L,R]\) 里小于\(x\) 的数变成\(x\) \(3.\ ...
- hdu6521 吉司机线段树
http://acm.hdu.edu.cn/showproblem.php?pid=6521 待填 代码 #include<bits/stdc++.h> #define ls o<& ...
- HDU 5306 Gorgeous Sequence[线段树区间最值操作]
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- PAT 1123 Is It a Complete AVL Tree
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...
- 如何相互转换逗号分隔的字符串和List --https://blog.csdn.net/yywusuoweile/article/details/50315377
如何相互转换逗号分隔的字符串和List ---https://blog.csdn.net/yywusuoweile/article/details/50315377 方法 2: 利用Guava的Joi ...
- Leetcode 90.子集
子集 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], ...
- 魔法猪学院(codevs 1835)
题目描述 Description iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世 ...
- 定义Portal显示规则
Defining Portal Display Rules Use You use the Portal Display Rules editor to create and edit rule co ...
- Angularjs中比较实用的DateFormat库
angular.module('newApp') .factory('dateUtil', function() { var symbolMap = { 'MM': function(date) { ...
- Visual Assist X破解版安装(vs2010助手)
从网上下载了Visual Assist X 版本号的破解版,安装文件夹为默认的c://program files/Visual Assist X/,当我把破解的VA_X.dll粘贴到该文件夹下,VC+ ...
- java JDBC 连接数据库查询数据与直接使用sql的疑问
JDBC 封装连接是好的前提: SystemAuthorizingRealm c = new SystemAuthorizingRealm(); conn = c.getConnection(); / ...
- Java Web文件下载
Web文件下载有两种.一种是文件在站点文件夹下.在浏览器中直接输入文件路径就可以下载.如http://www.xxx.com/file.zip.第二种是文件不在站点文件夹下或者文件是动态生成的(导出报 ...
- 【剑指offer】合并两有序单链表
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25739727 九度OJ上AC,採用归并的思想递归实现. 题目描写叙述: 输入两个单调递增的 ...