GSS1 spoj 1043 Can you answer these queries I 最大子段和
今天下午不知道要做什么,那就把gss系列的线段树刷一下吧。
Can you answer these queries I
题目:给出一个数列,询问区间[l,r]的最大子段和
分析:
线段树简单区间操作。
线段树中记录lx,rx,mx,分别表示:最大前驱连续和,最大后继连续和,区间最大子段和。
在合并时时只需要合并两个区间即可,具体可以看代码的Union。
从队友jingo那里学到了这种合并的写法,发现比网上大部分代码简单很多。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 100005; int a[MAXN]; struct segTree{
int l,r,lx,rx,mx,sum;
inline int mid(){
return (l+r)>>1;
}
}tree[MAXN<<2]; inline void Union(segTree& now,segTree l,segTree r){
now.lx = max( l.lx , l.sum+max(0,r.lx) );
now.rx = max( r.rx , r.sum+max(0,l.rx) );
now.mx = max( l.rx+r.lx , max(l.mx,r.mx) );
now.sum = l.sum+r.sum;
} void build(int l,int r,int rt){
tree[rt].l = l;
tree[rt].r = r;
if(l==r){
tree[rt].lx = tree[rt].rx = tree[rt].sum = tree[rt].mx = a[l];
return;
}
int mid = tree[rt].mid();
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
Union(tree[rt],tree[rt<<1],tree[rt<<1|1]);
} segTree ask(int l,int r,int rt){
if(l<=tree[rt].l&&r>=tree[rt].r)
return tree[rt];
int mid = tree[rt].mid();
segTree ans;
if(r<=mid) ans = ask(l,r,rt<<1);
else if(l>mid) ans = ask(l,r,rt<<1|1);
else{
segTree a = ask(l,r,rt<<1);
segTree b = ask(l,r,rt<<1|1);
Union( ans,a,b );
}
Union(tree[rt],tree[rt<<1],tree[rt<<1|1]);
return ans;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int m,n,x,y;
while(~RD(n)){
rep1(i,n)
RD(a[i]);
build(1,n,1);
RD(m);
while(m--){
RD2(x,y);
segTree now = ask(x,y,1);
printf("%d\n",now.mx);
}
} return 0;
}
GSS1 spoj 1043 Can you answer these queries I 最大子段和的更多相关文章
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- GSS3 SPOJ 1716. Can you answer these queries III gss1的变形
gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...
- GSS5 spoj 2916. Can you answer these queries V 线段树
gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...
- SPOJ 1557. Can you answer these queries II 线段树
Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...
- bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树
2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 145 ...
- spoj 4487. Can you answer these queries VI (gss6) splay 常数优化
4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...
- spoj gss2 : Can you answer these queries II 离线&&线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
GSS2 - Can you answer these queries II #tree Being a completist and a simplist, kid Yang Zhe cannot ...
随机推荐
- sql 自连接
这是一个部门表,里面存放了部门及其上级部门,但都放在同一张表中,我们假设现在需要用SQL查询出各部门及其上级部门!就如何做,当然,不用自连接也一样,可以如下: 我们达到预期目的!在这个查询中使用了一个 ...
- Oracle的体系结构
前言 这个章节主要想说的是Oracle的体系结构,这个也是理论强些.还有一些比较理论的知识点(比如表空间啊),就暂时先不写了,下一章节开始进入Oracle的操作阶段,比如表的查询啊.插入以及重点是和S ...
- aix-裸设备文件大小查看
1.使用lsvg 查看有哪些vg 2.使用lsvg myvg VOLUME GROUP: myvg VG IDENTIFIER: 00f7563100004c000000013e5f8a53fa VG ...
- cocos2d-x 使用Lua
转自:http://www.benmutou.com/blog/archives/49 1. Lua的堆栈和全局表 我们来简单解释一下Lua的堆栈和全局表,堆栈大家应该会比较熟悉,它主要是用来让C++ ...
- PE文件数字签名信息读取存储及格式具体解释图之上(历史代码,贴出学习)
// 注意下图PE文件格式具体解释图中的 // IMAGE_NT_HEADERS------->OptionalHeader------>DataDirectory[IMAGE_DIREC ...
- 会吓人的概念证明病毒: Chameleon
近期有这么一条新闻指出,有一对家长发现,黑客入侵了他们为10个月女儿所准备的婴儿监视器(baby monitor).该黑客除了远程操控该监视器的录像角度,还大声对着小孩喊叫.婴儿的爸爸冲进女儿房间后, ...
- BZOJ 1084: [SCOI2005]最大子矩阵 DP
1084: [SCOI2005]最大子矩阵 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1084 Description 这里有一个n* ...
- 安装luinxWEB
Webmin的安装很简单,下面就详细说一下安装步骤. 1.用ssh客户端软件登陆服务器2.切换目录到root下,命令是:cd /root/3.下载Webmin的安装文件,命令是:wget http:/ ...
- boa安装
Boa 下载地址:http://www.boa.org/boa-0.94.13.tar.gz 1.解压生成Makefile tar xzf boa-0.94.13.tar.gz #解压 cd boa- ...
- box-flex等分总结
首先要知道在应用 box-flex 时必须给父容器定义 css 属性 display:box 其子容器才可以进行划分. .box{ display: -webkit-box; display: -mo ...