Gorgeous Sequence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2150    Accepted Submission(s): 594

Problem Description
There is a sequence a of length n. We use ai to denote the i-th element in this sequence. You should do the following three types of operations to this sequence.

0 x y t: For every x≤i≤y, we use min(ai,t) to replace the original ai's value.
1 x y: Print the maximum value of ai that x≤i≤y.
2 x y: Print the sum of ai that x≤i≤y.

 
Input
The first line of the input is a single integer T, indicating the number of testcases.

The first line contains two integers n and m denoting the length of the sequence and the number of operations.

The second line contains n separated integers a1,…,an (∀1≤i≤n,0≤ai<231).

Each of the following m lines represents one operation (1≤x≤y≤n,0≤t<231).

It is guaranteed that T=100, ∑n≤1000000, ∑m≤1000000.

 
Output
For every operation of type 1 or 2, print one line containing the answer to the corresponding query.
 
Sample Input
1
5 5
1 2 3 4 5
1 1 5
2 1 5
0 3 5 3
1 1 5
2 1 5
 
Sample Output
5
15
3
12

Hint

Please use efficient IO method

 
Author
XJZX
 
Source
 

一份代码交了13遍。从TLE->WA->TLE->……QAQ

#include<cstdio>
#include<iostream>
#define lc k<<1
#define rc k<<1|1
#define EF if(ch==EOF) return x;
using namespace std;
typedef long long ll;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;EF;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int N=1e6+;
const int M=N<<;
int n,m,a[N];
ll sum[M];int mx[M],se[M],mc[M];
inline void updata(int k){
sum[k]=sum[lc]+sum[rc];
mx[k]=max(mx[lc],mx[rc]);
se[k]=max(se[lc],se[rc]);mc[k]=;
if(mx[lc]!=mx[rc]) se[k]=max(se[k],min(mx[lc],mx[rc]));
if(mx[k]==mx[lc]) mc[k]+=mc[lc];
if(mx[k]==mx[rc]) mc[k]+=mc[rc];
}
inline void dec_tag(int k,int v){
if(v>=mx[k]) return ;
sum[k]+=1LL*(v-mx[k])*mc[k];mx[k]=v;
}
inline void pushdown(int k){
dec_tag(lc,mx[k]);
dec_tag(rc,mx[k]);
}
void build(int k,int l,int r){
if(l==r){
sum[k]=mx[k]=a[l];mc[k]=;se[k]=-;
return ;
}
int mid=l+r>>;
build(lc,l,mid);
build(rc,mid+,r);
updata(k);
}
void change(int k,int l,int r,int x,int y,int v){
if(v>=mx[k]) return ;
if(l==x&&r==y&&v>se[k]){
dec_tag(k,v);return ;
}
pushdown(k);
int mid=l+r>>;
if(y<=mid) change(lc,l,mid,x,y,v);
else if(x>mid) change(rc,mid+,r,x,y,v);
else change(lc,l,mid,x,mid,v),change(rc,mid+,r,mid+,y,v);
updata(k);
}
int query_max(int k,int l,int r,int x,int y){
if(l==x&&r==y) return mx[k];
pushdown(k);
int mid=l+r>>;
if(y<=mid) return query_max(lc,l,mid,x,y);
else if(x>mid) return query_max(rc,mid+,r,x,y);
else return max(query_max(lc,l,mid,x,mid),query_max(rc,mid+,r,mid+,y));
}
ll query_sum(int k,int l,int r,int x,int y){
if(l==x&&r==y) return sum[k];
pushdown(k);
int mid=l+r>>;
if(y<=mid) return query_sum(lc,l,mid,x,y);
else if(x>mid) return query_sum(rc,mid+,r,x,y);
else return query_sum(lc,l,mid,x,mid)+query_sum(rc,mid+,r,mid+,y);
}
inline void work(){
n=read();m=read();
for(int i=;i<=n;i++) a[i]=read();
build(,,n);
for(int i=,opt,x,y,z;i<=m;i++){
opt=read();x=read();y=read();
if(opt==) z=read(),change(,,n,x,y,z);
if(opt==) printf("%d\n",query_max(,,n,x,y));
if(opt==) printf("%lld\n",query_sum(,,n,x,y));
}
}
int main(){
for(int T=read();T--;) work();
return ;
}

HDU 5306 Gorgeous Sequence[线段树区间最值操作]的更多相关文章

  1. 【hdu5306】Gorgeous Sequence 线段树区间最值操作

    题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...

  2. 【bzoj4355】Play with sequence 线段树区间最值操作

    题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...

  3. HDU - 5306 Gorgeous Sequence 线段树 + 均摊分析

    Code: #include<algorithm> #include<cstdio> #include<cstring> #define ll long long ...

  4. 【bzoj4695】最假女选手 线段树区间最值操作

    题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...

  5. HDOJ 5306 Gorgeous Sequence 线段树

    http://www.shuizilong.com/house/archives/hdu-5306-gorgeous-sequence/ Gorgeous Sequence Time Limit: 6 ...

  6. HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)

    HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...

  7. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  8. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

  9. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

随机推荐

  1. EF Code First 注意事项

    1.异常“实体类型不存在于上下文中” Context类中不包含该实体类型的DbSet,有可能关联关系没有正确设置

  2. C#基础—不安全代码(unsafe code)

    1.为何要有unsafe 也许是为了实现CLR类型安全的目标吧,默认情况下,C#没有提供指针的使用算法,但是有些情况下也可能需要指针这样直接访问内存的东西(虽然目前我还没有用过),但是有时候程序员非常 ...

  3. 去哪网实习总结:递归构建“流程运行顺序”的XML文件(JavaWeb)

    本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发... 只是还是比較认真的做了三个月,老师非常认同我的工作态度和成果.. . 实习立即就要结束了,总结一下几点之前没有注意过的变成习惯和问题.分享 ...

  4. QT类之------QLabel

    QLabel 类代表标签,它是一个用于显示文本或图像的窗口部件. 构造 QLabel 类支持以下构造函数: [plain] view plaincopy QLabel(QWidget *parent  ...

  5. 词法分析器总结--flex&bison

    转自:项目总结之词法分析器 无论是词法分析,还是语法分析,给我的第一感觉就是逻辑要严谨.由于项目有自己一套完整的语言和语法,设计好其对应的词法分析器和语法分析器显得尤为重要. 我们采用flex进行词法 ...

  6. dp之完全背包poj2063

    题意:求投资k年获得最大投资,每年都选最大利息的方案进行投资k年后就可以得到最多的人民币. 注意:每一年收到的利息都可以作为下一年的本金......其实从测试数据来看,是很好看出来的...... 思路 ...

  7. [C++]在什么时候需要“#include string.h“

    相关资料:https://zhidao.baidu.com/question/515578726.html C++中,string头文件基本上已经包含在iostream中了.但是,平时使用的时候建议加 ...

  8. [C++]怎么将.h和.cpp文件分别放在不同的目录

    相关资料: http://blog.csdn.net/onafioo/article/details/8775501 具体操作: 1.找到.h文件目录.2.将所以的.h文件剪切到“include”目录 ...

  9. JS学习笔记(4)--js变量的生命周期

    http://www.cnblogs.com/williamxiao/p/3499973.html 最近看国外经典教材的时候发现JavaScript与熟知的Java,C,C++都不同的特性,其中一个就 ...

  10. Parallelism , Partitioner

    转:spark通过合理设置spark.default.parallelism参数提高执行效率 spark中有partition的概念(和slice是同一个概念,在spark1.2中官网已经做出了说明) ...