SPOJ GSS1 Can you answer these queries I ——线段树
【题目分析】
线段树裸题。
注意update的操作,写结构体里好方便。
嗯,没了。
【代码】
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib> #include <map>
#include <set>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 500005
#define eps 1e-8
#define db double
#define ll long long
#define inf 0x3f3f3f3f
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i) void Finout()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
} int Getint()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} struct Node{
int lx,rx,mx,sum;
Node operator + (Node x)
{
Node ret;
ret.lx=max(lx,sum+x.lx);
ret.rx=max(x.sum+rx,x.rx);
ret.sum=sum+x.sum;
ret.mx=max(max(mx,x.mx),max(rx+x.lx,max(ret.lx,ret.rx)));
return ret;
}
}t[maxn]; int n,a[maxn],q,L,R; void Build(int o,int l,int r)
{
// printf("Build %d %d\n",l,r);
if (l==r)
{
t[o].lx=t[o].rx=t[o].mx=t[o].sum=a[l];
return ;
}
int mid=l+r>>1;
Build(o<<1,l,mid);
Build(o<<1|1,mid+1,r);
t[o]=t[o<<1]+t[o<<1|1];
} Node Query(int o,int l,int r)
{
// printf("Query %d %d\n",l,r);
if (L<=l&&r<=R) return t[o];
int mid=l+r>>1;
if (L>mid) return Query(o<<1|1,mid+1,r);
else if (R<=mid) return Query(o<<1,l,mid);
else return Query(o<<1,l,mid)+Query(o<<1|1,mid+1,r);
} int main()
{
Finout();
n=Getint(); F(i,1,n) a[i]=Getint();
Build(1,1,n);
q=Getint();
F(i,1,q)
{
L=Getint(); R=Getint();
printf("%d\n",Query(1,1,n).mx);
}
}
SPOJ GSS1 Can you answer these queries I ——线段树的更多相关文章
- SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)
Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...
- SPOJ GSS1 Can you answer these queries I[线段树]
Description You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A q ...
- 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 ...
- 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 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
- SPOJ GSS5 Can you answer these queries V ——线段树
[题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...
随机推荐
- CSS-学习笔记五
1. 权重: 内联:A ID:B Class:C 标签:D 继承:0 2. 文字阴影text-shadow 3. 文字缩进text-index 4. 文本换行 5. 文本溢出 6. 圆角 ...
- python_110_反射
class Dog(object): def __init__(self,name): self.name=name def eat(self): print('%s is eating '%self ...
- C++判断两个double类型双精度浮点数是否同号
看到的一种整数的方法 != y < ) 由此, double x,y; == fabs( ) { } 目前想到的比较合适判断方法. 此外这里还有一种强制转换类型求符号位的方法. /** * Ge ...
- fckeditor的实例
第一步:去官网下载,删除多余的包 删除所有”_”开头的文件和文件夹 删除FCKeditor的目录下: fckeditor.afp fckedit ...
- vuejs 中 select 动态填充数据,后台的数据
selected:"A" 对 selected:A 错. 变量不用引号. 内容一定要引号. https://jsfiddle.net/rgnuaw30/ ...
- ios之UIWebView(1)
UIWebView可以让你创建一个网页浏览器,类似safari,而不是在程序中启动safsri哦.是不是觉得很棒呢?废话少说,切入正题. 一.创建UIWebView [java] view plain ...
- kvm网络虚拟化管理
1. Linux Bridge网桥管理 一个网桥上添加多个虚拟机,虚拟机之间是可以相互通信的的,同时虚拟机也都可以通外网. kvm的网桥管理可以通过brctl命令 [root@localhost ~] ...
- Git学习——删除文件
怎么删除版本库中的文件呢? 首先,删除工作区中的文件:rm <file>,然后你有两个选择.其一是,真正删除版库中的文件: git rm <file> git commit - ...
- 【Gradle】Downloading https://services.gradle.org/distributions/gradle-3.3-bin.zip 失败
提示连接超时 Downloading https://services.gradle.org/distributions/gradle-3.3-bin.zip 失败 这时候需要单独去官网下载包,然后放 ...
- FSHC之MCU接口部分理解
|_____________| |_____| |__ ...