这个题目是Segment-Tree-beats的论文的第一题。

首先我们考虑下这个问题的不同之处在于,有一个区间对x取max的操作。

那么如何维护这个操作呢?

就是对于线段树的区间,维护一个最大值标记,最大值出现次数,以及严格次大值。

接下来考虑处理操作。

首先如果x>maxv[o]证明已经是无所谓的,所以应该直接放弃。

如果v处于semx[o]<x<maxv[o],证明只有最大值需要被修改。

其他的情况就继续向下递进就可以了。

那么我们证明一下为什么这么做复杂度是对的。

首先,如同论文中所说的,对于每个maxv可以看作是一个标记,把相同的maxv合并到第一个点,

可以发现semx的含义就是子树中最大的maxv。(因为最大值已经被缩到了这个点上)

每次暴力进入这些节点的时候,实际上就是将标记进行合并。

(也就是文章中所说的标记回收的理论)

这个题的难点就是为什么我这么暴力的回收依然可以是一个log的。

在每一次操作后会产生一类新的标记。

那么每次标记回收实际上就是有一类标记的权值-=1

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=1e6+;
int a[N],n,m;
struct Segment_Tree{
#define lson (o<<1)
#define rson (o<<1|1)
int maxv[N<<],cntv[N<<],semx[N<<];
ll sumv[N<<];
inline void pushup(int o){
sumv[o]=sumv[lson]+sumv[rson];
maxv[o]=max(maxv[lson],maxv[rson]);
semx[o]=max(semx[lson],semx[rson]);cntv[o]=;
if(maxv[lson]!=maxv[rson])semx[o]=max(semx[o],min(maxv[lson],maxv[rson]));
if(maxv[o]==maxv[lson])cntv[o]+=cntv[lson];
if(maxv[o]==maxv[rson])cntv[o]+=cntv[rson];
}
inline void puttag(int o,int v){
if(v>=maxv[o])return;
sumv[o]+=1LL*(v-maxv[o])*cntv[o];maxv[o]=v;
}
inline void pushdown(int o){puttag(lson,maxv[o]);puttag(rson,maxv[o]);}
inline void build(int o,int l,int r){
if(l==r){
sumv[o]=maxv[o]=a[l];cntv[o]=;semx[o]=-;
return;
}
int mid=(l+r)>>;
build(lson,l,mid);build(rson,mid+,r);
pushup(o);
}
inline void optmax(int o,int l,int r,int ql,int qr,int v){
if(v>=maxv[o])return;
if(ql<=l&&r<=qr&&v>semx[o]){puttag(o,v);return;}
int mid=(l+r)>>;pushdown(o);
if(ql<=mid)optmax(lson,l,mid,ql,qr,v);
if(qr>mid)optmax(rson,mid+,r,ql,qr,v);
pushup(o);
}
inline ll querysum(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr)return sumv[o];
int mid=(l+r)>>;pushdown(o);ll ans=;
if(ql<=mid)ans+=querysum(lson,l,mid,ql,qr);
if(qr>mid)ans+=querysum(rson,mid+,r,ql,qr);
return ans;
}
inline int querymax(int o,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr)return maxv[o];
pushdown(o);int mid=(l+r)>>,ans=-;
if(ql<=mid)ans=max(ans,querymax(lson,l,mid,ql,qr));
if(qr>mid)ans=max(ans,querymax(rson,mid+,r,ql,qr));
return ans;
}
}sgt;
inline int read(){
int f=,x=;char ch;
do{ch=getchar();if(ch=='-')f=-;}while(ch<''||ch>'');
do{x=x*+ch-'';ch=getchar();}while(ch>=''&&ch<='');
return f*x;
}
int main(){
int T=read();
while(T--){
n=read();m=read();
for(int i=;i<=n;i++)a[i]=read();
sgt.build(,,n);
while(m--){
int opt=read(),l=read(),r=read();
if(opt==){
int v=read();
sgt.optmax(,,n,l,r,v);
}
if(opt==)printf("%d\n",sgt.querymax(,,n,l,r));
if(opt==)printf("%lld\n",sgt.querysum(,,n,l,r));
}
}
}

【HDU5306】Gorgeous Sequence的更多相关文章

  1. 【hdu5306】 Gorgeous Sequence

    http://acm.hdu.edu.cn/showproblem.php?pid=5306 (题目链接) 题意 区间取$min$操作,区间求和操作,区间求最值操作. Solution 乱搞一通竟然A ...

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

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

  3. 【HDU5306】【DTOJ2481】Gorgeous Sequence【线段树】

    题目大意:给你一个序列a,你有三个操作,0: x y t将a[x,y]和t取min:1:x y求a[x,y]的最大值:2:x y求a[x,y]的sum 题解:首先很明显就是线段树裸题,那么考虑如何维护 ...

  4. 【arc071f】Infinite Sequence(动态规划)

    [arc071f]Infinite Sequence(动态规划) 题面 atcoder 洛谷 题解 不难发现如果两个不为\(1\)的数连在一起,那么后面所有数都必须相等. 设\(f[i]\)表示\([ ...

  5. 【arc074e】RGB Sequence(动态规划)

    [arc074e]RGB Sequence(动态规划) 题面 atcoder 洛谷 翻译见洛谷 题解 直接考虑暴力\(dp\),设\(f[i][j][k][l]\)表示当前考虑到第\(i\)位,最后一 ...

  6. 【BZOJ1367】[Baltic2004]sequence 左偏树

    [BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sampl ...

  7. 【BZOJ3043】IncDec Sequence 乱搞

    [BZOJ3043]IncDec Sequence Description 给定一个长度为n的数列{a1,a2...an},每次可以选择一个区间[l,r],使这个区间内的数都加一或者都减一.问至少需要 ...

  8. 【POJ2778】DNA Sequence(AC自动机,DP)

    题意: 生物课上我们学到,DNA序列中只有A, C, T和G四种片段. 经科学发现,DNA序列中,包含某些片段会产生不好的基因,如片段"ATC"是不好片段,则"AGATC ...

  9. 【leetcode】 Permutation Sequence (middle)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  2. Kingdom and its Cities - CF613D

    Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in or ...

  3. CentOS 查看系统内核和版本

    1.uname 命令用于查看系统内核与系统版本等信息,格式为“uname [-a]”. [root@bigdata-senior01 ~]# uname -a Linux bigdata-senior ...

  4. [CF622F]The Sum of the k-th Powers

    题目大意:给你$n,k(n\leqslant10^9,k\leqslant10^6)$,求:$$\sum\limits_{i=1}^ni^k\pmod{10^9+7}$$ 题解:可以猜测是一个$k+1 ...

  5. BZOJ2659 [Beijing wc2012]算不出的算式 【数形结合】

    题目链接 BZOJ2659 题解 真没想到,, 观察式子 \[\sum\limits_{k = 1}^{\frac{p - 1}{2}} \lfloor \frac{kq}{p} \rfloor\] ...

  6. rand、randi和randn的区别?

    1,rand 生成均匀分布的伪随机数.分布在(0~1)之间 主要语法:rand(m,n)生成m行n列的均匀分布的伪随机数 rand(m,n,'double')生成指定精度的均匀分布的伪随机数,参数还可 ...

  7. rn初体验

    react-native 需要的工具 .nodejs .rn cli .xcode and as ---------------- 打开终端,切换到根路径(mac中修改npm的默认安装来源) 一.op ...

  8. springboot集成thymeleaf中遇到的问题

    错误:不能返回页面,只返回字符串. 原因:在controller中使用了注解@RestController 修改:修改注解为@Controller @Controller 分析: RestContro ...

  9. 如何更有效使用 Rational AppScan 扫描大型网站,第 2 部分: 案例分析

    使用 AppScan 进行扫描 针对大型网站的扫描,我们按照戴明环 PDCA 的方法论来进行规划和讨论,建议 AppScan 使用步骤:计划(Plan).执行(Do).检查(check).分析(Ana ...

  10. EasyUI Tree递归方式获取JSON

    最近需要用到EASYUI中的TREE功能,以前我是直接拼接成<UL><LI>发现这样拼完之后在更改树后对树的刷新不是很理想,现改用JSON格式,首先分析TREE中JOSN格式如 ...