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 ...
随机推荐
- Android提升进入界面的速度
应用除了有内存占用.内存泄露.内存抖动等看不见的性能问题外,还有很多看得见的性能问题,比如进入界面慢.点击反应慢.页面卡顿等等,这些看得见的体验问题会严重影响用户使用APP心情,但用户的情绪又无法通过 ...
- 两段简单的JS代码防止SQL注入
1.URL地址防注入: //过滤URL非法SQL字符var sUrl=location.search.toLowerCase();var sQuery=sUrl.substring(sUrl.inde ...
- cocos2d-x 详解之 CCLayer(触摸事件)
CCLayer继承自CCNode,在CCLayer中可以实现单点触摸.多点触摸和重力感应回调3种不同形式的交互.这部分的难点在于,当存在多个层都要去接收触摸时它的响应机制是如何处理的.了解内部的处理机 ...
- HDU5778 abs
http://acm.hdu.edu.cn/showproblem.php?pid=5778 思路:只有平方质因子的数,也就是这题所说的 y的质因数分解式中每个质因数均恰好出现2次 满足条件的数 ...
- HDU 1536 sg-NIM博弈类
题意:每次可以选择n种操作,玩m次,问谁必胜.c堆,每堆数量告诉. 题意:sg—NIM系列博弈模板题 把每堆看成一个点,求该点的sg值,异或每堆sg值. 将多维转化成一维,性质与原始NIM博弈一样. ...
- The Administration Console(管理员控制台)
当你的应用准备好了首次露面时,你要创建一个管理员用户以及将这个应用安装到App Engine上.你使用你的管理员帐户创建和管理这个应用,查看它的资源利用统计,消息日志以及更多.所有这些基于一个叫做管理 ...
- Spark SQL概念学习系列之Spark SQL 架构分析(四)
Spark SQL 与传统 DBMS 的查询优化器 + 执行器的架构较为类似,只不过其执行器是在分布式环境中实现,并采用的 Spark 作为执行引擎. Spark SQL 的查询优化是Catalyst ...
- vector 的resize与reserve
最近遇到一个坑,简单说来是resize与reserve的功能混淆了. 如下: 如果调用resize的化,编译会出错,如果给Text提供默认构造函数,则可以编译通过,最终输出的结果为10. 如果调用re ...
- Delphi使用TStringHash实现建立类(有点像反射)
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- linux性能问题(CPU,内存,磁盘I/O,网络)
一. CPU性能评估 1.vmstat [-V] [-n] [depay [count]] -V : 打印出版本信息,可选参数 -n : 在周期性循环输出时,头部信息仅显示一次 delay : 两次输 ...