题解:

从没见过这么XXX的线段树啊。。。

T_T

我们考虑离线做,按1-n一个一个插入,并且维护区间【 j,i】(i为当前插入的数)j<i的最优值。

但这个最优值!!!

我们要保存历史的最优值,以及当前的最优值!!!还有lazy!!!也得分历史和现在!!T_T

怎么搞!!!

inline void update(int k,int z1,int z2)
{
t[k].tag[]=max(t[k].tag[],t[k].tag[]+z2);
t[k].tag[]+=z1;
t[k].mx[]=max(t[k].mx[],t[k].mx[]+z2);
t[k].mx[]+=z1;
t[k].mx[]=max(t[k].mx[],t[k].mx[]);
}
inline void pushdown(int k)
{
if(!t[k].tag[]&&!t[k].tag[])return;
update(k<<,t[k].tag[],t[k].tag[]);
update(k<<|,t[k].tag[],t[k].tag[]);
t[k].tag[]=t[k].tag[]=;
}

就是这么鬼畜,然后剩下的套模板就好了。。。

其实也蛮好理解的。

tag[0]表示从上次pushdown到现在加了多少

tag[1]表示从上次pushdown到现在tag[0]最多达到过多少,因为祖先的操作可能累计了很多次,我们只需要向下传一下最大的。

mx[0]和mx[1]与tag类似。

对拍了一组数据发现RE了,调了1h发现我dtmk写错了!!!说多了都是泪啊T_T

不过#2还是蛮开心的 哈哈~

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 200000+5
#define maxm 100000+5
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,v[maxn],last[maxn],ans[maxn];
struct rec{int l,r,id;}a[maxn];
inline bool cmp(rec x,rec y){return x.r<y.r;}
struct seg{int l,r,mx[],tag[];}t[*maxn];
inline void build(int k,int l,int r)
{
t[k].l=l;t[k].r=r;int mid=(l+r)>>;
if(l==r)return;
build(k<<,l,mid);build(k<<|,mid+,r);
}
inline void update(int k,int z1,int z2)
{
t[k].tag[]=max(t[k].tag[],t[k].tag[]+z2);
t[k].tag[]+=z1;
t[k].mx[]=max(t[k].mx[],t[k].mx[]+z2);
t[k].mx[]+=z1;
t[k].mx[]=max(t[k].mx[],t[k].mx[]);
}
inline void pushup(int k)
{
for0(i,)t[k].mx[i]=max(t[k<<].mx[i],t[k<<|].mx[i]);
}
inline void pushdown(int k)
{
if(!t[k].tag[]&&!t[k].tag[])return;
update(k<<,t[k].tag[],t[k].tag[]);
update(k<<|,t[k].tag[],t[k].tag[]);
t[k].tag[]=t[k].tag[]=;
}
inline void add(int k,int x,int y,int z)
{
int l=t[k].l,r=t[k].r,mid=(l+r)>>;
if(l==x&&r==y){update(k,z,);return;}
pushdown(k);
if(y<=mid)add(k<<,x,y,z);
else if(x>mid)add(k<<|,x,y,z);
else add(k<<,x,mid,z),add(k<<|,mid+,y,z);
pushup(k);
}
inline int query(int k,int x,int y)
{
int l=t[k].l,r=t[k].r,mid=(l+r)>>;
if(l==x&&r==y)return t[k].mx[];
pushdown(k);
if(y<=mid)return query(k<<,x,y);
else if(x>mid)return query(k<<|,x,y);
else return max(query(k<<,x,mid),query(k<<|,mid+,y));
}
#define last(i) last[100000+i]
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)v[i]=read();
build(,,n);
m=read();
for1(i,m)a[i].l=read(),a[i].r=read(),a[i].id=i;
sort(a+,a+m+,cmp);
int j=;
for1(i,n)
{
int tmp=last(v[i]);last(v[i])=i;
add(,tmp+,i,v[i]);
while(a[j].r==i)ans[a[j].id]=query(,a[j].l,i),j++;
}
for1(i,m)printf("%d\n",ans[i]);
return ;
}

2482: [Spoj1557] Can you answer these queries II

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 115  Solved: 67
[Submit][Status]

Description

给定n个元素的序列。 
给出m个询问:求l[i]~r[i]的最大子段和(可选空子段)。 
这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次。 
比如:1,2,3,2,2,2出现了3次,但只算一次,于是这个序列的和是1+2+3=6。

Input

第一行一个数n。 
第二行n个数,为给定的序列,这些数的绝对值小于等于100000。 
第三行一个数m。 
接下来m行,每行两个数,l[i],r[i]。

Output

M行,每行一个数,为每个询问的答案。



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

【数据说明】

30%:1 <= n, m <= 100

100%:1 <= n, m <= 100000

Source

BZOJ2482: [Spoj1557] Can you answer these queries II的更多相关文章

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

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

  2. [SPOJ1557] Can you answer these queries II

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2482 [算法] 线段树维护历史最值 时间复杂度 : O(NlogN) [代码] #i ...

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

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

  5. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

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

  7. SPOJ1557 GSS2 Can you answer these queries II 历史最值线段树

    传送门 题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次.所有数字的绝对值$\leq 10^5$ GSS系列中不板子的大火题,单独拿出来写 ...

  8. SPOJ GSS2 Can you answer these queries II

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

  9. GSS2-Can you answer these queries II

    ---恢复内容开始--- 这道题真的是非常恶心,看题解看了半天才弄懂,而且题解上说的相当简略. 此题大意是询问去掉重复元素的最大子区间和,没有修改操作. 没有修改操作,这样就可以离线处理了. 这道题有 ...

随机推荐

  1. android开发 更新升级安装到一半自动闪退

    如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /**     * 安装APK文件     */    private void installApk( ...

  2. 17.2 The DispatcherServlet

    综述: Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed arou ...

  3. sampler2d

    Here is the syntax for a sampler in Direct3D 9. sampler Name = SamplerType{   Texture = <texture_ ...

  4. Maintainable HashCode and Equals Using Apache Commons

    Java hashCode and equals methods can be tricky to implement correctly. Fortunately, all majors IDEs ...

  5. Choosing Columns and Expressions to Index

    A key is a column or expression on which you can build an index. Follow these guidelines for choosin ...

  6. Using command-line Subversion to access project source files

    Help index About source code version control with Software Configuration Management (Subversion) Usi ...

  7. DJANGO的requirements的运用

    这里记录一下我现在项目的requirements.pip文件,安装命令为: pip install -r requirements.pip 这样一来,所有依赖,全部搞定. Django== djang ...

  8. hdu 1524 A Chess Game 博弈论

    SG函数!! 代码如下: #include<stdio.h> #include<cstring> #define I(x) scanf("%d",& ...

  9. mac 下周期调度命令或脚本

    crontab 是在linux服务器上部署定时任务的方法 0 5 * * * /usr/bin/python /data/www/tools/mysql_backup.py cmd之前有5个项目要填, ...

  10. KMP笔记√//找最大子串,前缀自匹配长度

    假设s1里找s2,然后s2进去匹配假设在第三位失配那么说明前两位是匹配成功的 如果这时候将s2后移一位相当于将s2的第一位和s2的第二位比较,如果我们已知s1(1)≠s1(2)那么就可以直接后移两位 ...