hdu 5306 Gorgeous Sequence
来源:互联网 发布:党章党规党纪网络答题 编辑:程序博客网 时间:2024/06/06 23:57
对线段树进行三种操作:
区间求和。
区间求最大值。
将节点值更新为当前值与给定值中的最小值。
怎么做?代码中有详细注解,看看就懂了。
#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<algorithm>#include<bitset>#include<climits>#include<list>#include<iomanip>#include<stack>#include<set>using namespace std;typedef long long ll;/************Read**********/ char *ch, *ch1, buf[40*1024000+5], buf1[40*1024000+5]; void read(int &x) { for (++ch; *ch <= 32; ++ch); for (x = 0; '0' <= *ch; ch++) x = x * 10 + *ch - '0'; } /**************************/ struct Tree{int l,r,num,mx,flag;//这个结点当前管控num个数,这是用来快速求和用的//mx:这个结点的最大值//flag:这个结点的修改值 ll sum;//sum:这个结点的和 }tree[1000000<<2];void makeflag(int k,int flag){if(tree[k].flag!=0&&tree[k].flag<=flag)return;tree[k].flag=flag;tree[k].sum+=ll(tree[k].r-tree[k].l+1-tree[k].num)*flag;//num发挥求和的作用,若管控不了这个结点的所有数,则会加上修改值 if(tree[k].num<tree[k].r-tree[k].l+1)tree[k].mx=flag;tree[k].num=tree[k].r-tree[k].l+1;//修改完毕后,已经管控了这个结点所有数 }void pushdown(int k)//更新k的儿子 {if(tree[k].flag==0)return;makeflag(k<<1,tree[k].flag);makeflag(k<<1|1,tree[k].flag);}void pushup(int k)//用儿子更新k {tree[k].mx=max(tree[k<<1].mx,tree[k<<1|1].mx);tree[k].num=tree[k<<1].num+tree[k<<1|1].num;tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;}void build(int l,int r,int k){tree[k].l=l;tree[k].r=r;tree[k].flag=0;if(l==r){read(tree[k].flag);tree[k].num=1;tree[k].mx=tree[k].sum=tree[k].flag;return;}int m=l+r>>1;build(l,m,k<<1);build(m+1,r,k<<1|1);pushup(k);}void dfs(int k,int flag){if(tree[k].mx<=flag)return;if(tree[k].flag>=flag)tree[k].flag=0;if(tree[k].l==tree[k].r){tree[k].mx=tree[k].sum=tree[k].flag;tree[k].num=tree[k].flag!=0;return;}pushdown(k);dfs(k<<1,flag);dfs(k<<1|1,flag);pushup(k);}void update(int l,int r,int flag,int k){if(tree[k].mx<=flag)return;if(l==tree[k].l&&r==tree[k].r){dfs(k,flag);//更新这个结点的后代 makeflag(k,flag);//更新这个结点 return;}int m=tree[k].l+tree[k].r>>1;pushdown(k);if(r<=m)update(l,r,flag,k<<1);else if(l>m)update(l,r,flag,k<<1|1);else{update(l,m,flag,k<<1);update(m+1,r,flag,k<<1|1);}pushup(k);}Tree seek(int l,int r,int k){if(l==tree[k].l&&r==tree[k].r)return tree[k];int m=tree[k].l+tree[k].r>>1;pushdown(k);if(r<=m)return seek(l,r,k<<1);if(l>m)return seek(l,r,k<<1|1);Tree t1=seek(l,m,k<<1);Tree t2=seek(m+1,r,k<<1|1);t1.mx=max(t1.mx,t2.mx);t1.sum+=t2.sum;return t1;}int main(){ch = buf - 1; ch1 = buf1 - 1; fread(buf, 1, 1000 * 35 * 1024, stdin); int T;read(T);while(T--){int n,m;read(n);read(m);build(1,n,1);while(m--){int x,l,r;read(x);read(l);read(r);if(x==0){read(x);update(l,r,x,1);}else{if(x==1)printf("%d\n",seek(l,r,1).mx);elseprintf("%I64d\n",seek(l,r,1).sum);}}}}
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 521 Accepted Submission(s): 110
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 integersn and m denoting the length of the sequence and the number of operations.
The second line containsn separated integers a1,…,an (∀1≤i≤n,0≤ai<231 ).
Each of the followingm lines represents one operation (1≤x≤y≤n,0≤t<231 ).
It is guaranteed thatT=100 , ∑n≤1000000, ∑m≤1000000 .
The first line contains two integers
The second line contains
Each of the following
It is guaranteed that
Output
For every operation of type 1 or 2 , print one line containing the answer to the corresponding query.
Sample Input
15 51 2 3 4 51 1 52 1 50 3 5 31 1 52 1 5
Sample Output
515312HintPlease use efficient IO method
Source
2015 Multi-University Training Contest 2
0 0
- hdu 5306 Gorgeous Sequence
- hdu 5306 Gorgeous Sequence
- HDU 5306 Gorgeous Sequence
- hdu 5306 Gorgeous Sequence
- hdu 5306 Gorgeous Sequence(线段树)
- HDU 5306 Gorgeous Sequence(线段树)
- HDU 5306 Gorgeous Sequence 线段树
- HDU 5306 Gorgeous Sequence(线段树)
- [线段树 区间最值操作] HDU 5306 Gorgeous Sequence
- Gorgeous Sequence
- Gorgeous Sequence
- HDOJ 5306 Gorgeous Sequence 线段树
- [HDU] 5306 Gorgeous Sequence [区间取min&求和&求max][线段树]
- hdu5306 Gorgeous Sequence
- [题解]hdu5306 Gorgeous Sequence
- 【Gorgeous】
- hdu5306 Gorgeous Sequence(线段树:复杂区间更新)
- hdu 1711 Number Sequence
- 面向对象的特征
- maven+springMVC+mybatis+junit详细搭建过程
- Android中Intent传递Object和ArrayList<Object>对象---笔记
- Intellij IDEA 14.x 菜单项中Compile、Make和Build的区别
- Java 静态变量的继承
- hdu 5306 Gorgeous Sequence
- UVA 10161 Ant on a Chessboard
- LeetCode 235: Lowest Common Ancestor of a Binary Search Tree
- spring mvc中controller层响应内容包含中文的配置
- 发布iOS应用(Xcode5)到App Store详细解析
- 【Trie】bzoj1954——我终于当了权限狗了!
- 黑马程序员——10,线程
- 使用PHP的CURL模拟POST采集开了viewstate的asp.net网页数据
- POJ 2785 4 Values whose Sum is 0 (对半分解 二分搜索)