uva 1400 "Ray, Pass me the dishes!"
2015-04-02 23:23
489 查看
这题属于数据结构题,用线段树做,不多说,看代码,结构体变量名有注释。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #define maxn 500005 #define mem(a) memset(a, 0, sizeof(a)) using namespace std; struct node { long long vl;//区间左最大值 long long vr;//区间右最大值 long long w;//区间最大值 long long sum;//区间和 int l;//区间结果左坐标 int r;//区间结果右坐标 int yl;//区间左最大值的右坐标 int xr;//区间右最大值的左坐标 }e[maxn * 4]; long long v[maxn]; void build(int l, int r, int n) { if(l == r) { e .sum = e .w = e .vl = e .vr = v[l]; e .l = e .r = e .yl = e .xr = l; return ; } int mid = (l + r) / 2; build(l, mid, 2 * n); build(mid + 1, r, 2 * n + 1); e .sum = e[2 * n].sum + e[2 * n + 1].sum; long long wl = e[2 * n].w, wr = e[2 * n + 1].w, vm = e[2 * n].vr + e[2 * n + 1].vl; if(wl >= wr&&wl >= vm) { e .w = wl; e .l = e[2 * n].l; e .r = e[2 * n].r; } else if(vm >= wr) { e .w = vm; e .l = e[2 * n].xr; e .r = e[2 * n + 1].yl; } else { e .w = wr; e .l = e[2 * n + 1].l; e .r = e[2 * n + 1].r; } if(e[2 * n].vl >= e[2 * n].sum + e[2 * n + 1].vl) { e .vl = e[2 * n].vl; e .yl = e[2 * n].yl; } else { e .vl = e[2 * n].sum + e[2 * n + 1].vl; e .yl = e[2 * n + 1].yl; } if(e[2 * n + 1].vr > e[2 * n].vr + e[2 * n + 1].sum) { e .vr = e[2 * n + 1].vr; e .xr = e[2 * n + 1].xr; } else { e .vr = e[2 * n + 1].sum + e[2 * n].vr; e .xr = e[2 * n].xr; } } node res(int n, int l, int r, int ll, int rr) { if(ll == l&&rr == r) return e ; int mid = (rr + ll) / 2; if(r <= mid) return res(2 * n, l, r, ll, mid); else if(l > mid) return res(2 * n + 1, l, r, mid + 1, rr); else { node sonl, sonr, rst; sonl = res(2 * n, l, mid, ll, mid); sonr = res(2 * n + 1, mid + 1, r, mid + 1, rr); rst.sum = sonl.sum + sonr.sum; if(sonl.w >= sonr.w&&sonl.w >= sonl.vr + sonr.vl) { rst.w = sonl.w; rst.l = sonl.l; rst.r = sonl.r; } else if(sonl.vr + sonr.vl >= sonr.w) { rst.w = sonl.vr + sonr.vl; rst.l = sonl.xr; rst.r = sonr.yl; } else { rst.w = sonr.w; rst.l = sonr.l; rst.r = sonr.r; } if(sonl.vl >= sonl.sum + sonr.vl) { rst.vl = sonl.vl; rst.yl = sonl.yl; } else { rst.vl = sonl.sum + sonr.vl; rst.yl = sonr.yl; } if(sonr.vr > sonl.vr + sonr.sum) { rst.vr = sonr.vr; rst.xr = sonr.xr; } else { rst.vr = sonr.sum + sonl.vr; rst.xr = sonl.xr; } return rst; } } int main() { int num, m, o = 0, i, j, k, x, y, l, r; while(scanf("%d%d",&num, &m) != EOF) { mem(v); mem(e); for(i = 1;i <= num;i++) { scanf("%lld",&v[i]); } build(1, num, 1); node ans; printf("Case %d:\n",++o); for(i = 0;i < m;i++) { scanf("%d%d", &l, &r); ans = res(1, l, r, 1, num); printf("%d %d\n", ans.l, ans.r); } } return 0; }
相关文章推荐
- uva 1400 "Ray, Pass me the dishes!" 线段树
- Uva 1400 "Ray, Pass me the dishes!" (线段树 区间合并)
- UVA1400 - "Ray, Pass me the dishes!"
- UVA 1400 1400 - "Ray, Pass me the dishes!"(线段树)
- UVA 1400 1400 - "Ray, Pass me the dishes!"(线段树)
- UVA 1400 "Ray, Pass me the dishes!"(线段树)
- UVa 3938 "Ray, Pass me the dishes!"
- UVa-1400 - "Ray, Pass me the dishes!"
- UVALive - 3938 "Ray, Pass me the dishes!"
- uva 1400 - "Ray, Pass me the dishes!"
- UVa 1400 (线段树) "Ray, Pass me the dishes!"
- UVA 1400 "Ray, Pass me the dishes!" (线段树)
- UVA-1400-"Ray, Pass me the dishes!"(线段树-区间合并)
- UVA - 1400"Ray, Pass me the dishes!"(线段树)
- UVa 1400 "Ray, Pass me the dishes!"(线段树)
- uva 1400 - "Ray, Pass me the dishes!"(线段树)
- UVALive 3938 "Ray, Pass me the dishes!"
- UVA1400 "Ray, Pass me the dishes!" 线段树
- LA3938 & UVA1400 - Ray, Pass me the dishes!(线段树)
- uvalive 3938 - "Ray, Pass me the dishes!" +动态最大连续和+线段树