uvalive 3938 Ray, Pass me the dishes!(线段树)
来源:互联网 发布:药智数据网 编辑:程序博客网 时间:2024/05/16 02:56
题目链接:https://vjudge.net/problem/UVALive-3938
题意:
给一个数组,多次查询,询问区间[l, r]最大连续和。
思路:
线段树。
查询一个区间的最大连续和,可以找一个mid,这个最大连续和的起点和终点,
可能在mid的左边,或者在mid的右边,或者横跨mid。
左区间和右区间的最大值是递归定义的,维护起来也是容易的。
横跨mid的最大值如何计算?等于左区间最大后缀+右区间最大前缀。
如何维护区间最大前缀和区间最大后缀?
设一个区间为[l, r],
设它的左区间最大前缀为[l, idx1],右区间最大前缀为[(l+r)/2+1, idx2]
那么区间最大前缀为比较前缀和在区间[l, idx1]和[l, idx2]中哪一个比较大,
选择大的那个前缀和对应的那个idx。
最大后缀同理。
如何查询最大前缀?
设一个区间[l, r],它的最大前缀为prefix,
如果prefix<=query_r(要查询的区间的右端点值),
满足条件,直接return [l, prefix]
若prefix > query_r,那么最大前缀在当前区间的左区间或者右区间。
当mid = (l+r)/2 >= query_r,那么要查询的最大前缀在区间[l, mid];
当mid < query_r,那么可以得到当前区间的右区间的最大前缀prefix2,
当前区间的最大前缀要在左区间最大前缀prefix1和右区间最大前缀prefix2中选择大的。
最大后缀同理。
注意当区间和相同时,取坐标比较小的那位。
代码:
(写法来源:http://www.cnblogs.com/zyb993963526/p/6547946.html)
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <utility>#include <cstdlib>using namespace std;const int maxn = 1000000 + 5;typedef long long ll;typedef pair<int, int> Interval;int n, m, QL, QR;ll sum[maxn];struct Node{ Interval max_sub; //最大连续和, 一个区间 int prefix; int suffix;}tree[maxn];ll calc(int L, int R){ return sum[R] - sum[L-1];}Interval better(const Interval &a, const Interval &b){ if(calc(a.first, a.second) != calc(b.first, b.second)) // not equal return calc(a.first, a.second) > calc(b.first, b.second)?a:b; return a<b?a:b; // 返回最左边的那个区间}void build(int L, int R, int rt){ if(L==R){ tree[rt].prefix = L; tree[rt].suffix = L; tree[rt].max_sub = make_pair(L, R); } else{ int mid = (L+R)>>1; int lc = rt<<1; int rc = rt<<1|1; build(L, mid, lc); build(mid+1, R, rc); ll x1 = calc(L, tree[lc].prefix); ll x2 = calc(L, tree[rc].prefix); if(x1==x2) tree[rt].prefix = min(tree[lc].prefix, tree[rc].prefix); else tree[rt].prefix = x1>x2?tree[lc].prefix:tree[rc].prefix; x1 = calc(tree[lc].suffix, R); x2 = calc(tree[rc].suffix, R); if(x1==x2) tree[rt].suffix = min(tree[lc].suffix, tree[rc].suffix); else tree[rt].suffix = x1>x2?tree[lc].suffix:tree[rc].suffix; tree[rt].max_sub = better(tree[lc].max_sub, tree[rc].max_sub); tree[rt].max_sub = better(tree[rt].max_sub, make_pair(tree[lc].suffix, tree[rc].prefix)); }}Interval query_prefix(int L, int R, int rt){ if(QR >= tree[rt].prefix) return make_pair(L, tree[rt].prefix); int mid = (L+R)>>1; int lc = rt<<1; int rc = rt<<1|1; if(QR<=mid) return query_prefix(L, mid, lc); Interval x = query_prefix(mid+1, R, rc); x.first = L; return better(x, make_pair(L, tree[lc].prefix));}Interval query_suffix(int L, int R, int rt){ if(QL<=tree[rt].suffix) return make_pair(tree[rt].suffix, R); int mid = (L+R)>>1; int lc = rt<<1; int rc = rt<<1|1; if(QL>mid) return query_suffix(mid+1, R, rc); Interval x = query_suffix(L, mid, lc); x.second = R; return better(x, make_pair(tree[rc].suffix, R));}Interval query(int L, int R, int rt){ if(QL<=L && R<=QR) return tree[rt].max_sub; int mid = (L+R)>>1; int lc = rt<<1; int rc = rt<<1|1; if(QR <= mid) return query(L, mid, lc); if(QL > mid) return query(mid+1, R, rc); Interval x1 = query_suffix(L, mid, lc); Interval x2 = query_prefix(mid+1, R, rc); Interval x3 = better(query(L, mid, lc), query(mid+1, R, rc)); return better(make_pair(x1.first, x2.second), x3);}int main(){ int x, kase=0; ios::sync_with_stdio(false); while(cin>>n>>m){ sum[0] = 0; for(int i=1; i<=n; ++i){ cin>>x; sum[i] = sum[i-1] + x; } build(1, n, 1); cout<<"Case "<<++kase<<":"<<endl; for(int i=0; i<m; ++i){ cin>>QL>>QR; Interval ans = query(1, n, 1); cout<<ans.first<<" "<<ans.second<<endl; } } return 0;}
- UVALive - 3938 "Ray, Pass me the dishes!" (线段树)
- uvalive 3938 Ray, Pass me the dishes!(线段树)
- UVALive - 3938 "Ray, Pass me the dishes!" 线段树
- UVALive 3938 Ray, Pass me the dishes (线段树区间合并)
- UVaLive 3938 "Ray, Pass me the dishes!" (线段树求动态最大连续和)
- UVALive 3938 "Ray, Pass me the dishes!"
- UVALive - 3938 "Ray, Pass me the dishes!"
- UVALive 3938 "Ray, Pass me the dishes!"
- uvalive 3938 - "Ray, Pass me the dishes!" +动态最大连续和+线段树
- UVAlive - 3938 —— Ray, Pass me the dishes! 【线段树】
- LA 3938 - "Ray, Pass me the dishes!"(线段树)
- UVa Ray, Pass me the dishes! (线段树)
- UVA 1400 "Ray, Pass me the dishes!" (线段树)
- [线段树点修改]动态最大连续和(Ray,Pass me the Dishes, LA 3938)
- UVA live 3938 - "Ray, Pass me the dishes!"(线段树)
- UVA1400 "Ray, Pass me the dishes!" 线段树
- UVA 1400 1400 - "Ray, Pass me the dishes!"(线段树)
- uva 1400 "Ray, Pass me the dishes!" 线段树
- ISO-OSI的七层协议经典架构
- 神经网络算法
- VIM备忘
- LinkedList遍历效率
- 数据结构实验之二叉树三:统计叶子数
- uvalive 3938 Ray, Pass me the dishes!(线段树)
- promise原理浅析
- 修改用户信息填写规则
- C++设计模式——Proxy模式
- LIS-最长上升子序列
- 三年PHP面试总结
- 关于RecyclerView的java.lang.IllegalStateException: The specified child already has a parent. You must c
- 未来将更准确!VAR裁判技术引入足球杯
- JS实现滚动监听以及滑动到顶部【转】