首页 > 其他分享 >线段树--区间最大值模板

线段树--区间最大值模板

时间:2023-07-21 11:58:17浏览次数:37  
标签:pr rs -- 线段 mid int ls mx 模板

Smiling & Weeping

            ----你是我绕过的山河错落,才找到的人间烟火

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 题目链接:Problem - 5306 (hdu.edu.cn) 思路:需要引入一个se次最大值,话不多说 上代码ヾ(@^▽^@)ノ
  1 //必须使用C++编译器
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<cmath>
  7 using namespace std;
  8 typedef long long ll;
  9 const int maxn = 1000100;
 10 #define ls(p) p<<1
 11 #define rs(p) p<<1|1
 12 int a[maxn] , n , m;
 13 int mx[maxn<<2] , cnt[maxn<<2] , se[maxn<<2] , add[maxn<<2];
 14 ll sum[maxn<<2];
 15 void push_up(int p)
 16 {
 17     sum[p] = sum[ls(p)] + sum[rs(p)];
 18     if(mx[ls(p)] == mx[rs(p)])
 19     {
 20         se[p] = max(se[ls(p)] , se[rs(p)]);
 21         mx[p] = mx[ls(p)];
 22         cnt[p] = cnt[ls(p)] + cnt[rs(p)];
 23     }
 24     else if(mx[ls(p)] > mx[rs(p)])
 25     {
 26         se[p] = max(se[ls(p)] , mx[rs(p)]);
 27         mx[p] = mx[ls(p)];
 28         cnt[p] = cnt[ls(p)];
 29     }
 30     else
 31     {
 32         se[p] = max(se[rs(p)] , mx[ls(p)]);
 33         mx[p] = mx[rs(p)];
 34         cnt[p] = cnt[rs(p)];
 35     }
 36 }
 37 void push_down(int p)
 38 {
 39     if(add[p] == -1) return ;  // 区间覆盖
 40     int& t = add[p];
 41     if(mx[ls(p)] > t && t > se[ls(p)])
 42     {
 43         sum[ls(p)] -= 1ll*(mx[ls(p)]-t)*cnt[ls(p)];
 44         add[ls(p)] = mx[ls(p)] = t;
 45     }
 46     if(mx[rs(p)] > t && t > se[rs(p)])
 47     {
 48         sum[rs(p)] -= 1ll*(mx[rs(p)]-t)*cnt[rs(p)];
 49         add[rs(p)] = mx[rs(p)] = t;
 50     }
 51     t = -1;
 52 }
 53 void build(int p , int pl , int pr)
 54 {
 55     add[p] = -1;
 56     if(pl == pr)
 57     {
 58         se[p] = -1; cnt[p] = 1;
 59         sum[p] = mx[p] = a[pl];
 60         return ;
 61     }
 62     int mid = pl+pr >> 1;
 63     build(ls(p) , pl , mid);
 64     build(rs(p) , mid+1 , pr);
 65     push_up(p);
 66 }
 67 void update(int L , int R, int p , int pl, int pr , int t)
 68 {
 69     if(mx[p] <= t) return ;
 70     if(L <= pl && pr <= R && se[p] < t)
 71     {
 72         sum[p] -= 1ll*(mx[p]-t)*cnt[p];
 73         mx[p] = add[p] = t;
 74         return ;
 75     }
 76     int mid = pl+pr >> 1;
 77     push_down(p);
 78     if(L <= mid) update(L , R , ls(p) , pl , mid , t);
 79     if(R > mid)  update(L , R , rs(p) , mid+1 , pr , t);
 80     push_up(p);
 81 }
 82 int query_max(int L , int R , int p , int pl , int pr)
 83 {
 84     if(L <= pl && pr <= R) return mx[p];
 85     int mid = pl+pr >> 1;
 86     int ans = 0;
 87     push_down(p);
 88     if(L <= mid) ans = max(ans , query_max(L , R , ls(p) , pl , mid));
 89     if(R > mid)  ans = max(ans , query_max(L , R , rs(p) , mid+1 , pr));
 90     return ans;
 91 }
 92 ll query_sum(int L , int R , int p , int pl , int pr)
 93 {
 94     if(L <= pl && pr <= R) return sum[p];
 95     int mid = pl+pr >> 1;
 96     ll ans = 0;
 97     push_down(p);
 98     if(L <= mid) ans += query_sum(L , R , ls(p) , pl , mid);
 99     if(R > mid)  ans += query_sum(L , R , rs(p) , mid+1 , pr);
100     return ans;
101 }
102 int main()
103 {
104     int t;
105     scanf("%d",&t);
106     while(t--)
107     {
108         scanf("%d%d",&n,&m);
109         for(int i = 1; i <= n; i++)
110             scanf("%d",&a[i]);
111         build(1,1,n);
112         for(int i = 1;i <= m; i++)
113         {
114             int opt , L , R , k;
115             scanf("%d",&opt);
116             if(opt == 0)
117             {
118                 scanf("%d%d%d",&L,&R,&k);
119                 if(L > R) swap(L , R);
120                 update(L , R , 1 , 1 , n , k);
121             }
122             else if(opt == 1)
123             {
124                 scanf("%d%d",&L,&R);
125                 if(L > R) swap(L , R);
126                 printf("%d\n",query_max(L,R,1,1,n));
127             }
128             else if(opt == 2)
129             {
130                 scanf("%d%d",&L,&R);
131                 if(L > R) swap(L , R);
132                 printf("%lld\n",query_sum(L,R,1,1,n));
133             }
134         }
135     }
136     return 0;
137 }

؏؏☝ᖗ乛◡乛ᖘ☝؏؏下次再见

标签:pr,rs,--,线段,mid,int,ls,mx,模板
From: https://www.cnblogs.com/smiling-weeping-zhr/p/17570890.html

相关文章

  • c++入门以及简单顺序结构-习题
    1.c++入门以及简单顺序结构-习题1.计算(a+b)*c的值inta,b,c;cin>>a>>b>>c;cout<<(a+b)*c;2.带余除法inta,b;cin>>a>>b;cout<<a/b<<""<<a%b;//c++中取余结果正负只与%前面的正负有关系 cout<<5%2<<endl;//输出1 c......
  • MIT 6.5840 Raft Implementation(2A, Leader Election)
    Raft实现思路+细节2A任务分解总体来说,2A中主要的任务就是选出领导人,在选出领导人的时候,我们要遵循下图。在2A中,由于并没有出现日志复制,所以我们只需要考察两者的任期是否相等,以及接收者在本轮任期中有没有投票即可。因而我们可以这样地给出2A中的实现内容:完善GetState()......
  • CTS测试
    镜像文件镜像文件与安全补丁securtiypatch的月份对应#CTS-on-GSIadbrebootbootloaderfastbootflashingunlockfastbootflashingunlock_criticalfastbootrebootfastbootfastbootflashsystemsystem.imgfastboot-wfastbootreboot#VTSadbrebootbootloade......
  • B站千峰网安笔记
    01、批处理操作简单的cmd指令:1、TXT文件可以更改后缀名来实现转换(.dat/.html)2、@echooff关闭回显指令(即不显示如何运行)3、>nul是屏蔽屏幕显示2>nul是屏蔽错误提示4、分块指令:15、跳转指令qoto302、服务器服务器系统版本介绍Windows服务器系统:win2000w......
  • 2.分支结构-习题
    2.分支结构-习题1.偶数【题目描述】读入一个正整数a,如果a为偶数输出yes。【输入】一个正整数a【输出】偶数输出yes,否则什么也不输出。【输入样例】12【输出样例】yes#include<iostream>usingnamespacestd;intmain(){ inta; cin>>a; if(a%2==0) { c......
  • UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1023: unexp
     Connectedtopydevdebugger(build213.6461.77)Traceback(mostrecentcalllast): File"PyCharmCommunityEdition2021.3.1\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py",line303,in_on_run   r=r.decode('utf-8&......
  • HTML | 图片标签
    基本使用标签名标签语义常用属性单/双标签img图片src:图片路径(又称:图片地址)——图片的具体位置alt:图片描述width:图片宽度,单位是像素,例如:200px或200height:图片高度,单位是像素,例如:200px或200单总结:像素(px)是一种单位,学到CSS时,我们会详细讲解。尽......
  • C# 使用EPPlus 操作excel The given key '8' was not present in the dictionary.
    使用EPPlus删除excel中某一个sheet中的几列的时候,出现了Thegivenkey'8'wasnotpresentinthedictionary.的报错;最开始的写法,是从前往后删除,出现错误//ExcelWorksheetsheet=package.Workbook.Worksheets[i];//sheet.DeleteCol......
  • 【自动化测试】进行一次AES简单解密
    python3.0后下载Crypto的文件库名是小写的,而它内部引用库名居然是大写的库名。可以参考:最快解决fromCrypto.CipherimportAES报错问题_pittpakk的博客-CSDN博客 协助解决库名的情况。1.导入库名fromCrypto.CipherimportAESfromCrypto.Util.Paddingimportunpad 2.......
  • CloudCanal 数据脱敏实践
    简述本文主要介绍使用CloudCanal做数据迁移同步时如何对特定数据做脱敏处理。技术点自定义代码CloudCanal允许用户上传业务代码到数据任务中,完成数据迁移、同步过程中数据处理的目的。数据同步脱敏也是基于自定义代码实现,具备以下特点:脱敏范围灵活,可选择任何一个或多个......