SPOJ GSS5 Can you answer these queries V(区间合并)
来源:互联网 发布:淘宝卖家开店 编辑:程序博客网 时间:2024/05/16 09:16
Can you answer these queries V
Description
You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| <= 10000 , 1 <= N <= 10000 ). A query is defined as follows: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 <= j <= y2 and x1 <= x2 , y1 <= y2 }. Given M queries (1 <= M <= 10000), your program must output the results of these queries.
Input
The first line of the input consist of the number of tests cases <= 5. Each case consist of the integer N and the sequence A. Then the integer M. M lines follow, contains 4 numbers x1, y1, x2 y2.
Output
Your program should output the results of the M queries for each test case, one query per line.
Example
Input:26 3 -2 1 -4 5 221 1 2 31 3 2 51 111 1 1 1Output:231
Hint
解题思路:
分为三种情况.
1. x1 y1 x2 y2
这种情况就是 x1 y1 的右最大 + sum【y1 x2】 + x2 y2的做最大
2.x1 x2 y2 y1 其实就是 y1==y2的时候
要么区间在 x2-y2之间
要么区间的头在 x1 x2之间,尾在 x2 y2之间
3. x1 x2 y1 y2。
这种情况和2比较像是 这个时候 x1-y2 区间分成三段。只需要枚举头尾各在区间哪个区间就好了。
需要注意的是:有可能 x2==y1。
所以要注意一下比较的时候的边界。
AC代码:
#include <iostream>#include <cstdio>#define lson id<<1,s,mid#define rson id<<1|1,mid+1,eusing namespace std;const int N = 10005;int tree[N<<2];int lef[N<<2];int rig[N<<2];int sum[N<<2];void pushup(int id){ sum[id] = sum[id<<1]+sum[id<<1|1]; lef[id] = max(lef[id<<1],sum[id<<1]+lef[id<<1|1]); rig[id] = max(rig[id<<1|1],sum[id<<1|1]+rig[id<<1]); tree[id] = max(tree[id<<1],max(tree[id<<1|1],lef[id<<1|1]+rig[id<<1]));}void build(int id,int s,int e){ if(s == e){ scanf("%d",&tree[id]); lef[id] = rig[id] = sum[id] = tree[id]; return; } int mid = (s+e)>>1; build(lson); build(rson); pushup(id);}int Q_sum(int id,int s,int e,int l,int r){ if(l > r) return 0; if(l<=s && r>=e){ return sum[id]; } int mid = (s+e)>>1; if(r <= mid) return Q_sum(lson,l,r); else if(l>mid) return Q_sum(rson,l,r); else{ return Q_sum(lson,l,mid)+Q_sum(rson,mid+1,r); }}int Q_L(int id,int s,int e,int l,int r){ if(l > r) return 0; if(l<=s && r>=e){ return lef[id]; } int mid = (s+e)>>1; if(r <= mid) return Q_L(lson,l,r); else if(l > mid) return Q_L(rson,l,r); else return max(Q_L(lson,l,mid),max(Q_sum(lson,l,mid),Q_sum(lson,l,mid)+Q_L(rson,mid+1,r)));}int Q_R(int id,int s,int e,int l,int r){ if(l > r) return 0; if(l<=s && r>=e){ return rig[id]; } int mid = (s+e)>>1; if(r <= mid) return Q_R(lson,l,r); else if(l > mid) return Q_R(rson,l,r); else return max(Q_R(rson,mid+1,r),max(Q_sum(rson,mid+1,r),Q_sum(rson,mid+1,r)+Q_R(lson,l,mid)));}int query(int id,int s,int e,int l,int r){ if(l > r) return 0; if(l<=s && r>=e){ return tree[id]; } int mid = (s+e)>>1; if(r <= mid) return query(lson,l,r); else if(l > mid) return query(rson,l,r); else{ return max(Q_L(rson,mid+1,r)+Q_R(lson,l,mid),max(query(lson,l,mid),query(rson,mid+1,r))); }}int main(){ int T; scanf("%d",&T); while(T--){ int n; scanf("%d",&n); build(1,1,n); int m; scanf("%d",&m); while(m--){ int x1,x2,y1,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(x2 > y1){ printf("%d\n",Q_R(1,1,n,x1,y1)+Q_sum(1,1,n,y1+1,x2-1)+Q_L(1,1,n,x2,y2)); }else if(y1 == y2){ printf("%d\n",max(query(1,1,n,x2,y2),Q_L(1,1,n,x2,y2)+Q_R(1,1,n,x1,x2-1))); }else{ printf("%d\n",max(Q_R(1,1,n,x1,x2-1)+Q_L(1,1,n,x2,y1),max(Q_R(1,1,n,x1,x2-1)+Q_L(1,1,n,y1+1,y2)+Q_sum(1,1,n,x2,y1),max(Q_R(1,1,n,x2,y1)+Q_L(1,1,n,y1+1,y2),query(1,1,n,x2,y1))))); } } } return 0;}
- SPOJ GSS5 Can you answer these queries V(区间合并)
- SPOJ GSS5 Can you answer these queries V
- [SPOJ GSS5] Can you answer these queries V [线段树]
- SPOJ GSS5 Can you answer these queries V
- SPOJ GSS5 Can you answer these queries V
- GSS5 - Can you answer these queries V
- SPOJ 2916 Can you answer these queries V(GSS5 线段树)
- SPOJ GSS5 Can you answer these queries V (线段树)
- SPOJ Can you answer these queries V
- SPOJ GSS1 Can you answer these queries I(区间合并)
- spoj 2916 Can you answer these queries V
- SPOJ GSS 5. Can you answer these queries V
- spoj 2916. Can you answer these queries V(线段树)
- 【SPOJ】2916 Can you answer these queries V 线段树
- 【线段树】 SPOJ 1043 Can you answer these queries I 区间合并
- SPOJ 1716 GSS3 Can you answer these queries III 线段树区间合并
- spoj Can you answer these queries I(线段树 单点更新 区间查询)
- spoj 6779. Can you answer these queries VII(树链剖分)
- spark+hadoop-2.6配置
- Linux_ mkfifo 命名管道 操作demo
- 【12】Linux-自己动手写驱动
- 在Node.js使用mysql模块时遇到的坑
- C++作业2:分段函数
- SPOJ GSS5 Can you answer these queries V(区间合并)
- bzoj 3262: 陌上花开
- NS3 填写数据包内容
- Java生成验证码并进行验证
- Android-全屏动画
- mysql保留元数据的情况下修改表结构
- 如何利用参数和非参数方法来检测异常值(I)
- 【BZOJ1609】[Usaco2008 Feb]Eating Together麻烦的聚餐【最长不下降子序列】
- AFN、PHP上传图片小