1557. Can you answer these queries II

Problem code: GSS2

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.

Example

Input:
9
4 -2 -2 3 -1 -4 2 2 -6
3
1 2
1 5
4 9 Output:
4
5
3   见过变态的线段树,没见过这么变态的。。。。。。
  在我看来,这个线段树lazy标记的传递奇葩之处在于用到了lazy标记传递的一个隐藏的规律:即某个标记被改变意味着它上方不存在其他标记,因此一个标记下放时,其下方标记作用的时间域和此标记是没有交集的。
  我做这道题参考的题解:http://blog.csdn.net/acm_cxlove/article/details/7854526  (注意一下:那个题解的标程没开long long)
  两个lazy标记:lazy:标记代表的那一段时间区间增加的量 lazy2:对应时间区间增加量的最值
  维护两个值:mx:这个区间当前最大值 his:这个区间历史最大值
  其他参见连接题解。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXQ 110000
#define MAXR 200200
#define VAL1 100100
#define MAXN 101000
#define MAXT 410000
#define lch (now<<1)
#define rch (now<<1^1)
#define INFL 0x3f3f3f3f3f3f3f3fLL
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
typedef long long qword;
int n,m;
struct node
{
int l,r;
qword mx;
qword his;
qword lazy;//delta
qword lazy2;//delta_max
}tree[MAXT];
void down(int now)
{
if (tree[now].l==tree[now].r)
{
tree[now].lazy=;
tree[now].lazy2=;
return ;
}
tree[lch].lazy2=max(tree[lch].lazy2,tree[lch].lazy+tree[now].lazy2);
tree[rch].lazy2=max(tree[rch].lazy2,tree[rch].lazy+tree[now].lazy2);
tree[lch].his=max(tree[lch].his,tree[lch].mx+tree[now].lazy2);
tree[rch].his=max(tree[rch].his,tree[rch].mx+tree[now].lazy2);
tree[lch].mx+=tree[now].lazy;
tree[rch].mx+=tree[now].lazy;
tree[lch].lazy+=tree[now].lazy;
tree[rch].lazy+=tree[now].lazy;
tree[now].lazy=;
tree[now].lazy2=;
}
void up(int now)
{
if (tree[now].l==tree[now].r)return ;
tree[now].mx=max(tree[lch].mx,tree[rch].mx);
tree[now].his=max(tree[lch].his,tree[rch].his);
}
void build_tree(int now,int l,int r)
{
tree[now].l=l;
tree[now].r=r;
tree[now].his=;
tree[now].lazy=;
tree[now].lazy2=;
tree[now].mx=;
if (l==r)
{
return ;
}
int mid=(l+r)/;
build_tree(lch,l,mid);
build_tree(rch,mid+,r);
}
void add_val(int now,int l,int r,qword z)
{
if (l==tree[now].l&&r==tree[now].r)
{
tree[now].lazy+=z;
tree[now].lazy2=max(tree[now].lazy2,tree[now].lazy);
tree[now].mx+=z;
tree[now].his=max(tree[now].mx,tree[now].his);
return ;
}
down(now);
int mid=(tree[now].l+tree[now].r)/;
if (r<=mid)
{
add_val(lch,l,r,z);
up(now);
return ;
}
if (mid<l)
{
add_val(rch,l,r,z);
up(now);
return ;
}
add_val(lch,l,mid,z);
add_val(rch,mid+,r,z);
up(now);
}
qword get_max(int now,int l,int r)
{
if (l==tree[now].l&&r==tree[now].r)
{
return tree[now].his;
}
int mid=(tree[now].l+tree[now].r)/;
down(now);
if (r<=mid)
{
return get_max(lch,l,r);
}
if (mid<l)
{
return get_max(rch,l,r);
}
qword t;
t= max(get_max(lch,l,mid),get_max(rch,mid+,r));
up(now);
return t;
}
int num[MAXN];
int rec[MAXR];
int prev[MAXN];
struct qur_t
{
int x,y,id;
qword ans;
}qur[MAXQ];
bool cmp_y(const qur_t &q1,const qur_t &q2)
{
return q1.y<q2.y;
}
bool cmp_id(const qur_t &q1,const qur_t &q2)
{
return q1.id<q2.id;
}
int main()
{
freopen("input.txt","r",stdin);
int i,j,k,x,y,z;
scanf("%d",&n);
memset(rec,-,sizeof(rec));
for (i=;i<n;i++)
{
scanf("%d",&num[i]);
prev[i]=rec[num[i]+VAL1];
rec[num[i]+VAL1]=i;
}
scanf("%d",&m);
for (i=;i<m;i++)
{
scanf("%d%d",&qur[i].x,&qur[i].y);
qur[i].x--;
qur[i].y--;
qur[i].id=i;
}
sort(qur,&qur[m],cmp_y);
int now=-;
build_tree(,,n-);
for (i=;i<m;i++)
{
while (now<qur[i].y)
{
now++;
// cout<<"Add:"<<prev[now]+1<<"~"<<now<<":"<<num[now]<<endl;
add_val(,prev[now]+,now,num[now]);
}
// cout<<"Query:"<<qur[i].x<<"~"<<qur[i].y<<endl;
qur[i].ans=get_max(,qur[i].x,qur[i].y);
}
sort(qur,&qur[m],cmp_id);
// cout<<"a"<<endl;
for(i=;i<m;i++)
{
printf(LL "\n",qur[i].ans);
}
}
 

spoj gss2 : Can you answer these queries II 离线&&线段树的更多相关文章

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

  2. SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)

    都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...

  3. SP1557 GSS2 - Can you answer these queries II(线段树)

    传送门 线段树好题 因为题目中相同的只算一次,我们可以联想到HH的项链,于是考虑离线的做法 先把所有的询问按$r$排序,然后每一次不断将$a[r]$加入线段树 线段树上维护四个值,$sum,hix,s ...

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

  5. SPOJ GSS2 Can you answer these queries II

    Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Being a ...

  6. 【SPOJ - GSS2】Can you answer these queries II(线段树)

    区间连续不重复子段最大值,要维护历史的最大值和当前的最大值,打两个lazy,离线 #include<cstdio> #include<cstring> #include< ...

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

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

  8. Spoj 2713 Can you answer these queries IV 水线段树

    题目链接:点击打开链接 题意: 给定n长的序列 以下2个操作 0 x y 给[x,y]区间每一个数都 sqrt 1 x y 问[x, y] 区间和 #include <stdio.h> # ...

  9. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

随机推荐

  1. Apache CXF 3.0: CDI 1.1 Support as Alternative to Spring--reference

    With Apache CXF 3.0 just being released a couple of weeks ago, the project makes yet another importa ...

  2. android智能天气闹钟应用开发经过

    开发这个应用的初衷是这样产生滴,和我一块租房的同学每天早上都是骑单车上班,所以手机闹钟就会定一个刚好适合骑车的起床时间点.但是呢,有一天早上起床以后发现外面下挺大雨,肯定是不能骑车去上班了,于是就只好 ...

  3. windows 8 解决端口(COM和LPT)问题:Prolific USB-to-Serial Comm Port(COM4)驱动异常的问题

    本来都正常的,一段时间后就不能用了,网上搜索了一圈,现记录下来,供大家参考 操作系统是win8 64位的 参考网址(http://blog.sina.com.cn/s/blog_92942dba010 ...

  4. RedHat7搭建Nginx+Apache+PHP

    Nginx做为前端服务器(本机IP:192.168.136.104),将访问PHP页面的动态请求转发给Apache服务器(只监听本地回环地址172.0.0.1:80) 安装Apache# yum -y ...

  5. python learning_curve函数

    这个函数需要引用sklearn包 import sklearn from sklearn.learning_curve import learning_curve 这个函数的调用格式是: learni ...

  6. Bootstrap--全局CSS样式之排版

    Bootstrap的排版样式大致和html基本元素一样,没什么大的区别,就是对元素加了样式. (1)标题 HTML 中的所有标题标签,<h1> 到 <h6> 均可使用.另外,还 ...

  7. grunt 构建工具(build tool)初体验

    操作环境:win8 系统,建议使用 git bash (window下的命令行工具) 1,安装node.js 官网下载:https://nodejs.org/  直接点击install ,会根据你的操 ...

  8. [DEncrypt] HashEncode--哈希加密帮助类 (转载)

    点击下载 HashEncode.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.HashEncode 得到随机哈希加密字符串2.HashEncode 得到一个随机数值3.HashEncod ...

  9. 调用支付宝接口Android客户端没有支付宝APP的情况下解决无法调用支付宝页面的问题

    这几天一直研究支付宝接口调用,因为当前应用中需要调用支付宝接口作移动支付. 遇到一个问题困扰几天,就是当我们的手机端未安装支付宝APP的时候,需要在自己应用中调用支付宝的登陆网页进行支付.我是Andr ...

  10. SET QUOTED_IDENTIFIER OFF语句的作用

    先看下面几个sql语句  1 SET QUOTED_IDENTIFIER ON 2 SELECT * FROM "USER"    WHERE a='netasp'  3  4 S ...