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 线段树的更多相关文章

  1. Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字

    题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...

  2. 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 ...

  3. SPOJ GSS2 Can you answer these queries II ——线段树

    [题目分析] 线段树,好强! 首先从左往右依次扫描,线段树维护一下f[].f[i]表示从i到当前位置的和的值. 然后询问按照右端点排序,扫到一个位置,就相当于查询区间历史最值. 关于历史最值问题: 标 ...

  4. 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 ...

  5. 【BZOJ2482】[Spoj1557] Can you answer these queries II 线段树

    [BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和( ...

  6. 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]| ≤ ...

  7. 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[ ...

  8. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  9. 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 ...

随机推荐

  1. Oracle中HWM与数据库性能的探讨

    Oracle中HWM与数据库性能的探讨 一.什么是高水位 HWM(high water mark),高水标记,这个概念在segment的存储内容中是比较重要的.简单来说,HWM就是一个segment中 ...

  2. MySQL基础之第2章 Windows平台下安装与配置MySQL

    2.1.msi安装包 2.1.1.安装 特别要注意的是,安装前要删除原来的my.ini和原来的data目录,改名也行,不然在最后一步会“apply security settings”报个1045错误 ...

  3. HDU 5119 Happy Matt Friends

    Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...

  4. LXD 2.0 系列(二):安装与配置

    导读 简单来说,LXD是一个守护进程,为LXC容器的管理提供一组REST API.主要目标是提供一种类虚拟机的用户体验,是一种第三方的容器管理工具.下面呢,我们来介绍LXD 2.0 的安装与配置 安装 ...

  5. MyBatis association的两种形式——MyBatis学习笔记之四

    一.嵌套的resultMap 这 种方法本质上就是上篇博文介绍的方法,只是把教师实体映射从association元素中提取出来,用一个resultMap元素表示.然后 association元素再引用 ...

  6. ASIHTTPREQUEST 文档

    http://blog.csdn.net/ysysbaobei/article/details/17026577 Please note that I am no longer working on ...

  7. LoadRunner学习记录--Flights打开空白页的问题

    从网上查了一下,原因是PERL5LIB这个环境变量的原因. 担心修改环境变量会影响ORACLE的运行 在WebTour中修改run.bat   增加 set PERL5LIB=D:\oracle\pr ...

  8. 第2课 讲解主流三大web服务器之Apache服务器(httpd服务器) - 大型网站高并发架构与自动化运维实战(六)

    基本配置 配置固定IP地址 打开默认的网卡配置文件 cd /etc/sysconfig/network-script/ cp ifcfg-eth0 ifcfg-eth1 vim ifcfg-eth0 ...

  9. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程:简介及目录》(附上完整工程文件)

    介绍:讲述如何使用Genesis-3D来制作一个横版格斗游戏,涉及如何制作连招系统,如何使用包围盒实现碰撞检测,软键盘的制作,场景切换,技能读表,简单怪物AI等等,并为您提供这个框架的全套资源,源码以 ...

  10. 自己实现Single LinkedList

    My_Single_LinkedList 分4个部分实现(CRUD - 增删改查). 首先要有一个Node(节点类) class Node { public int val; public Node ...