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/problems/GSS2/
Description
Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always failes in contests.
When having a contest, Yang Zhe looks at the score of every problems first. For the problems of the same score, Yang Zhe will do only one of them. If he's lucky enough, he can get all the scores wanted.
Amber is going to hold a contest in SPOJ. She has made a list of N candidate problems, which fit Yang Zhe very well. So Yang Zhe can solve any problem he want. Amber lined up the problems, began to select. She will select a subsequence of the list as the final problems. Being A girl of great compassion, she'd like to select such a subsequence (can be empty) that Yang Zhe will get the maximal score over all the possible subsequences.
Amber found the subsequence easily after a few minutes. To make things harder, Amber decided that, Yang Zhe can take this contest only if Yang Zhe can answer her Q questions. The question is: if the final problems are limited to be a subsequence of list[X..Y] (1 <= X <= Y<= N), what's the maximal possible score Yang Zhe can get?
As we know, Yang Zhe is a bit idiot (so why did he solve the problem with a negative score?), he got Wrong Answer again... Tell him the correct answer!
Input
- Line 1: integer N (1 <= N <= 100000);
- Line 2: N integers denoting the score of each problem, each of them is a integer in range [-100000, 100000];
- Line 3: integer Q (1 <= Q <= 100000);
- Line 3+i (1 <= i <= Q): two integers X and Y denoting the ith question.
Output
- Line i: a single integer, the answer to the ith question.
Sample Input
9
4 -2 -2 3 -1 -4 2 2 -6
3
1 2
1 5
4 9
Sample Output
4
5
3
HINT
题意
给你n个数,查询区间最大连续子段和,并且区间内相同的数只计算一次
题解:
没有修改操作,很明显的离线线段树
假设我们不考虑相同的数只计算一次的规则,我们应该怎么做呢?
对于不断增加的r,我们维护c[i]表示从a[i]-a[r]的和,很显然,我们输出历史中最大的max(c[l],c[l+1],c[l+2]....c[r])就是答案了
想一想感觉挺蠢的。。。
我们怎么维护区间内相同的数只计算一次呢?对于每个数,我们只维护(pre[a[i]]+1,i)这个区间就好了嘛
然后这道题就解决了
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum , lazy, cursum, prelazy;
void updata(SgTreeDataType v)
{
sum += v;
lazy += v;
cursum = max(cursum,sum);
prelazy = max(prelazy,lazy);
}
}; treenode tree[]; inline void push_down(int o)
{
SgTreeDataType Prelazy = tree[o].prelazy;
SgTreeDataType Lazy = tree[o].lazy;
tree[*o].prelazy = max(tree[*o].prelazy,tree[o*].lazy + Prelazy);
tree[*o].cursum = max(tree[*o].cursum,tree[o*].sum + Prelazy);
tree[*o].lazy += Lazy; tree[*o].sum += Lazy;
tree[*o+].prelazy = max(tree[*o+].prelazy,tree[o*+].lazy + Prelazy);
tree[*o+].cursum = max(tree[*o+].cursum,tree[o*+].sum + Prelazy);
tree[*o+].lazy += Lazy; tree[*o+].sum += Lazy;
tree[o].lazy = ,tree[o].prelazy = ;
} inline void push_up(int o)
{
tree[o].sum = max(tree[*o].sum,tree[*o+].sum);
tree[o].cursum = max(tree[*o].cursum,tree[*o+].cursum);
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].sum = tree[o].lazy = tree[o].prelazy = tree[o].cursum = ;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
} inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR)
tree[o].updata(v);
else
{
push_down(o);
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
push_up(o);
}
} inline SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].cursum;
else
{
push_down(o);
int mid = (L+R)>>;
SgTreeDataType res = ;
if (QL <= mid) res =max(res, query(QL,QR,*o));
if (QR > mid) res =max(res,query(QL,QR,*o+));
push_up(o);
return res;
}
} int n,m;
int a[];
struct node
{
int l,r,id;
};
bool cmp(node A,node B)
{
return A.r<B.r;
}
node Query[];
int pos[];
long long ans[];
int main()
{
memset(pos,,sizeof(pos));
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
build_tree(,n,);
for(int i=;i<m;i++)
{
scanf("%d%d",&Query[i].l,&Query[i].r);
Query[i].id = i;
}
sort(Query,Query+m,cmp);
int N = ;
for(int i=,j=;i<=n;i++)
{
updata(pos[a[i]+N]+,i,a[i],);
pos[a[i]+N]=i;
while(j<m&&Query[j].r==i)
{
ans[Query[j].id]=query(Query[j].l,Query[j].r,);
j++;
}
}
for(int i=;i<m;i++)
printf("%lld\n",ans[i]);
}
SPOJ 1557. Can you answer these queries II 线段树的更多相关文章
- Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字
题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...
- 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 GSS2 Can you answer these queries II ——线段树
[题目分析] 线段树,好强! 首先从左往右依次扫描,线段树维护一下f[].f[i]表示从i到当前位置的和的值. 然后询问按照右端点排序,扫到一个位置,就相当于查询区间历史最值. 关于历史最值问题: 标 ...
- 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 ...
- 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树
[BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[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]| ≤ ...
- 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 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- 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 ...
随机推荐
- 【jQuery】鼠标接触按钮后改变图片
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 在PHP中如何获取用户的真实IP
/** * 获得用户的真实IP地址 * * @access public * @return string */ function real_ip() { static $realip = NULL; ...
- android Vibrator 使用
private Vibrator vibrator; 取得震动服务的句柄 vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); 或者 vi ...
- hdu 2594-Simpsons’ Hidden Talents(KMP)
题意: 给你两个串a,b,求既是a的前缀又是b的后缀的最长子串的长度. 分析: 很自然的想到把两个串连接起来,根据KMP的性质求即可 #include <map> #include < ...
- IAR编译信息分析
1.怎么设置可以查看单片的内存(消耗)使用状况? IAR的菜单栏 -->Tools -->IDE Options -->Messages -->Show build messa ...
- Nginx + PHP 缓存详解
Nginx缓存nginx有两种缓存机制:fastcgi_cache和proxy_cache下面我们来说说这两种缓存机制的区别吧proxy_cache作用是缓存后端服务器的内容,可能是任何内容,包括静态 ...
- 【转载】epoll的使用
select,poll,epoll简介 select select本质上是通过设置或者检查存放fd标志位的数据结构来进行下一步处理.这样所带来的缺点是: 1 单个进程可监视的fd数量被限制 2 需要维 ...
- maven学习系列第二课,关于springmvc的pop.xml的依赖的添加
不说废话了,图的书序就是操作顺序 1. 2.
- 对Spring的理解
1.Spring实现了工厂模式的工厂类,这个类名为BeanFactory实际上是一个接口,在程序中通常BeanFactory的子类ApplicationContext.Spring相当于一个大的工厂类 ...
- bzoj 3884 上帝与集合的正确用法(递归,欧拉函数)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3884 [题意] 求2^2^2… mod p [思路] 设p=2^k * q+(1/0) ...