XTU1238Segment Tree(线段树)解题思想必较经典
2015-06-14 08:12
190 查看
Segment Tree | ||
Accepted : 9 | Submit : 123 | |
Time Limit : 9000 MS | Memory Limit : 65536 KB |
Segment TreeProblem Description:A contest is not integrity without problems about data structure.There is an array a[1],a[2],…,a . And q questions of the following 4 types: 1 l r c - Update a[k] with a[k]+c for all l≤k≤r 2 l r c - Update a[k] with min{a[k],c} for all l≤k≤r; 3 l r c - Update a[k] with max{a[k],c} for all l≤k≤r; 4 l r - Ask for min{a[k]:l≤k≤r} and max{a[k]:l≤k≤r}. InputThe first line contains a integer T(no more than 5) which represents the number of test cases.For each test case, the first line contains 2 integers n,q (1≤n,q≤200000). The second line contains n integers a1,a2,…,an which indicates the initial values of the array (|ai|≤10^9). Each of the following q lines contains an integer t which denotes the type of i-th question. If t=1,2,3, 3 integers l,r,c follows. If t=4, 2 integers l,r follows. (1≤ti≤4,1≤li≤ri≤n) If t=1, |ci|≤2000; If t=2,3, |ci|≤10^9. OutputFor each question of type 4, output two integers denote the minimum and the maximum.Sample Input11 1 1 4 1 1 Sample Output1 1SourceXTU OnlineJudge在输入时要注意:用%I64d AC,用%lld WA 解题:op=2 [l , r] c 时则区间[l , r]内的数小于等于 c 则 不用变,大于c 则都变成 c . op=3同理。所以我们只需要记录区间内的最大值和最小值,还有每个数都要加多少add.具体代码中体现。 #include<stdio.h> #define LL long long const int N = 200100; const LL INF = 9999999999999; struct Tree{ LL maxt,mint,add; }node[N*3]; LL max(LL a,LL b){return a>b?a:b;} LL min(LL a,LL b){return a>b?b:a;} void pushUP(int k){ node[k].maxt=max(node[k<<1].maxt,node[k<<1|1].maxt); node[k].mint=min(node[k<<1].mint,node[k<<1|1].mint); } void pushDown(int k){ if(node[k].add){ node[k<<1].add+=node[k].add; node[k<<1].maxt+=node[k].add; node[k<<1].mint+=node[k].add; node[k<<1|1].add+=node[k].add; node[k<<1|1].maxt+=node[k].add; node[k<<1|1].mint+=node[k].add; node[k].add=0; } node[k<<1].maxt=min(node[k<<1].maxt,node[k].maxt); node[k<<1].maxt=max(node[k<<1].maxt,node[k].mint); node[k<<1].mint=max(node[k<<1].mint,node[k].mint); node[k<<1].mint=min(node[k<<1].mint,node[k].maxt); node[k<<1|1].maxt=min(node[k<<1|1].maxt,node[k].maxt); node[k<<1|1].maxt=max(node[k<<1|1].maxt,node[k].mint); node[k<<1|1].mint=max(node[k<<1|1].mint,node[k].mint); node[k<<1|1].mint=min(node[k<<1|1].mint,node[k].maxt); } void builde(int l,int r,int k){ node[k].add=0; if(l==r){ scanf("%I64d",&node[k].maxt); node[k].mint=node[k].maxt; return ; } int m=(l+r)>>1; builde(l,m,k<<1); builde(m+1,r,k<<1|1); pushUP(k); } int L,R; LL C; void updata1(int l,int r,int k){ if(L<=l&&r<=R){ node[k].add+=C; node[k].maxt+=C; node[k].mint+=C; return ; } int m=(l+r)>>1; pushDown(k); if(L<=m) updata1(l,m,k<<1); if(m<R) updata1(m+1,r,k<<1|1); pushUP(k); } void updata2(int l,int r,int k){ if(L<=l&&r<=R){ node[k].maxt=min(node[k].maxt,C); node[k].mint=min(node[k].mint,C); return ; } int m=(l+r)>>1; pushDown(k); if(L<=m) updata2(l,m,k<<1); if(m<R) updata2(m+1,r,k<<1|1); pushUP(k); } void updata3(int l,int r,int k){ if(L<=l&&r<=R){ node[k].maxt=max(node[k].maxt,C); node[k].mint=max(node[k].mint,C); return ; } int m=(l+r)>>1; pushDown(k); if(L<=m) updata3(l,m,k<<1); if(m<R) updata3(m+1,r,k<<1|1); pushUP(k); } LL maxans,minans; void query(int l,int r,int k){ if(L<=l&&r<=R){ maxans=max(maxans,node[k].maxt); minans=min(minans,node[k].mint); return ; } int m=(l+r)>>1; pushDown(k); if(L<=m) query(l,m,k<<1); if(m<R) query(m+1,r,k<<1|1); pushUP(k); } int main(){ int T,n,q,op; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&q); builde(1,n,1); while(q--){ scanf("%d%d%d",&op,&L,&R); if(op!=4){ scanf("%I64d",&C); if(op==1)updata1(1,n,1); else if(op==2)updata2(1,n,1); else if(op==3)updata3(1,n,1); } else{ minans=INF; maxans=-INF; query(1,n,1); printf("%I64d %I64d\n",minans,maxans); } } } } |
相关文章推荐
- 程序bug太多,可以用Facebook的开源AI工具
- 支付宝接口程序、文档及解读(ASP.NET)
- python RSA签名
- Ogre参考手册(二)3.1.1-3.1.2 材质、技术、通道
- C++11特性(01)auto关键字
- position和anchorPoint
- IOS UIKit 框架类之视图动画和坐标系介绍
- 动态圆
- 黑马程序员——基础知识总结_static
- ios加载文件中的数据(plist)
- 使用TCP协议写一个可以上传文件的服务器和客户端
- VS2010安装
- Linux网络设备驱动-- skb
- 分支与循环(2)
- Python标准库:内置函数vars([object])
- 安装DB2v9.1Express-C
- Python标准库:内置函数vars([object])
- DevExpress.XtraGrid
- 关于旋转卡壳
- DevExpress.XtraTreeList