UVALive - 3938 "Ray, Pass me the dishes!"
来源:互联网 发布:2017网络流行语英文 编辑:程序博客网 时间:2024/06/05 07:08
白书上讲的已经很明白了....
#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#include <set>#include <map>#include <utility>#include <algorithm>using namespace std;const int MAXN = 500010;typedef long long ll;ll max_sum[MAXN<<2];int max_prefix[MAXN<<2], max_suffix[MAXN<<2], range_x[MAXN<<2], range_y[MAXN<<2];ll prefix[MAXN], suffix[MAXN], val[MAXN];int n, q;void push_up(int rt, int l, int r){ int x1 = max_prefix[rt<<1], y1 = max_suffix[rt<<1],\ x2 = max_prefix[rt<<1|1], y2 = max_suffix[rt<<1|1]; if(prefix[x1] - prefix[l-1] >= prefix[x2] - prefix[l-1]) max_prefix[rt] = x1; else max_prefix[rt] = x2; if(suffix[y2] - suffix[r+1] > suffix[y1] - suffix[r+1]) max_suffix[rt] = y2; else max_suffix[rt] = y1; if(max_sum[rt<<1] >= max_sum[rt<<1|1]) { max_sum[rt] = max_sum[rt<<1]; range_x[rt] = range_x[rt<<1]; range_y[rt] = range_y[rt<<1]; } else { max_sum[rt] = max_sum[rt<<1|1]; range_x[rt] = range_x[rt<<1|1]; range_y[rt] = range_y[rt<<1|1]; } if((prefix[x2] - prefix[y1-1] > max_sum[rt])) { range_x[rt] = y1; range_y[rt] = x2; max_sum[rt] = prefix[x2] - prefix[y1-1]; } else if((prefix[x2]-prefix[y1-1]) == max_sum[rt]) { if(y1 == range_x[rt] && x2 < range_y[rt]) { range_x[rt] = y1; range_y[rt] = x2; } else if(y1 < range_x[rt]) { range_x[rt] = y1; range_y[rt] = x2; } } return ;}void bulid(int l, int r, int rt){ if(l == r) { max_sum[rt] = val[l]; max_prefix[rt] = l; max_suffix[rt] = l; range_x[rt] = l; range_y[rt] = l; return ; } int m = (l + r)>>1; bulid(l, m, rt<<1); bulid(m + 1, r, rt<<1|1); push_up(rt, l, r); return ;}pair< pair<int,int>,pair<int,int> > query(int L, int R, int l, int r, int rt){ if(L == l && R == r) { return make_pair(make_pair(max_prefix[rt], max_suffix[rt]),make_pair(range_x[rt], range_y[rt])); } int m = (l + r)>>1; if(R <= m) { return query(L, R, l, m, rt<<1); } else if(L > m) { return query(L, R, m + 1, r, rt<<1|1); } else { pair< pair<int,int>,pair<int,int> > tmp1 = query(L, m, l, m, rt<<1); pair< pair<int,int>,pair<int,int> > tmp2 = query(m + 1, R, m + 1, r, rt<<1|1); pair< pair<int,int>,pair<int,int> > tmp3; if(prefix[tmp1.first.first] - prefix[L-1] >= prefix[tmp2.first.first] - prefix[L-1]) { tmp3.first.first = tmp1.first.first; } else { tmp3.first.first = tmp2.first.first; } if(suffix[tmp1.first.second] - suffix[R+1] >= suffix[tmp2.first.second] - suffix[R+1]) { tmp3.first.second = tmp1.first.second; } else { tmp3.first.second = tmp2.first.second; } ll tmp; if(prefix[tmp1.second.second] - prefix[tmp1.second.first-1] >= prefix[tmp2.second.second] - prefix[tmp2.second.first-1]) { tmp = prefix[tmp1.second.second] - prefix[tmp1.second.first-1]; tmp3.second.first = tmp1.second.first; tmp3.second.second = tmp1.second.second; } else { tmp = prefix[tmp2.second.second] - prefix[tmp2.second.first-1]; tmp3.second.first = tmp2.second.first; tmp3.second.second = tmp2.second.second; } if(prefix[tmp2.first.first] - prefix[tmp1.first.second-1] > tmp) { tmp3.second.first = tmp1.first.second; tmp3.second.second = tmp2.first.first; } else if(prefix[tmp2.first.first] - prefix[tmp1.first.second-1] == tmp) { if(tmp1.first.second < tmp3.second.first || (tmp1.first.second == tmp3.second.first && tmp2.first.first < tmp3.second.second)) { tmp3.second.first = tmp1.first.second; tmp3.second.second = tmp2.first.first; } } return tmp3; }}int main(){ //freopen("aa.in", "r", stdin); int kcase = 0, x, y; pair< pair<int,int>,pair<int,int> > ret; while(scanf("%d %d", &n, &q) != EOF) { kcase++; memset(prefix, 0, sizeof(prefix)); memset(suffix, 0, sizeof(suffix)); for(int i = 1; i <= n; ++i) { cin >> val[i]; prefix[i] = prefix[i-1] + val[i]; } for(int i = n; i >= 1; --i) { suffix[i] = suffix[i+1] + val[i]; } bulid(1, n, 1); printf("Case %d:\n", kcase); for(int i = 1; i <= q; ++i) { scanf("%d %d", &x, &y); ret = query(x, y, 1, n, 1); printf("%d %d\n", ret.second.first, ret.second.second); } } 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!(线段树)
- LA 3938 "Ray, Pass me the dishes!"
- UVa 3938 "Ray, Pass me the dishes!"
- 3938-Ray, Pass me the dishes!
- LA 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!"(线段树)
- LA-3938-"Ray, Pass me the dishes!" 解题报告
- UvaLA 3938 "Ray, Pass me the dishes!"
- Live archive 3938 "Ray,Pass me the Dishes"
- 【转】字体图标
- Linux系统IO复用接口(select、poll、epoll)
- OCP-1Z0-053-V12.02-97题
- 远程提交Map/Reduce任务
- Mac系统升级至OS X Mavericks后Genymotion出现的问题及解决方法
- UVALive - 3938 "Ray, Pass me the dishes!"
- linux内核移植(6410)
- Web开发相关收藏
- java邮件开发(二)--构建复杂邮件
- 关于surfaceview画背景格的几个问题
- OCP-1Z0-053-V12.02-102题
- php 8个常用功能
- SAP模块常用增强总结
- 服务器cpu占用率100%的问题