传送门

题意:给出一个长度为$N$的数列,$Q$次询问,每一次询问$[l,r]$之间的最大子段和,相同的数只计算一次。所有数字的绝对值$\leq 10^5$


GSS系列中不板子的大火题,单独拿出来写

因为相同的数字只计算一次,像GSS1中的合并操作就无法进行,传统做法失效,我们需要一种更强大的做法。

考虑到去重,与HH的项链很相似,所以考虑离线、对询问以$r$从小到大进行排序后进行计算。

考虑到每一次$r$的增加都会产生新的可能的最大子段和,我们用如下方式维护线段树:对于第$i$个叶子节点,它包含两个元素:$sum$表示$\sum \limits_i^r num_i$(不算重复元素),$hisMax$表示$\sum \limits_i^r num_i$在曾经的过程中取到的最大值(也就是左端点为$l$,右端点在$[l,r]$之间的最大子段和)。而对于每一个非叶子节点,它的$sum$和$hisMax$都取其左右儿子的最大值。这样每一次询问操作只需要询问$[l,r]$的$hisMax$即可。

对于$r$的右移操作,设$pre_i$表示数字$i$最后一次出现的位置,将右端点从$r$移到$r+1$的过程就是对$pre_{num_{r+1}}$到$r+1$的所有位置加上$num_{r+1}$的操作。

考虑到复杂度,所以我们需要用到懒标记,而pushdown在其中是十分讲究的,具体代码和解释在下面

($h\_tag$表示$hisMax$的标记(相当于在当前$hisMax$还需要加多少),$s\_tag$表示$sum$的懒标记)

inline void pushdown(int now){
    if(Tree[now].h_tag){
        Tree[lch].hisMax = max(Tree[lch].hisMax , Tree[lch].sum + Tree[now].h_tag);
        Tree[rch].hisMax = max(Tree[rch].hisMax , Tree[rch].sum + Tree[now].h_tag);
        Tree[lch].h_tag = max(Tree[lch].h_tag , Tree[lch].s_tag + Tree[now].h_tag);
        Tree[rch].h_tag = max(Tree[rch].h_tag , Tree[rch].s_tag + Tree[now].h_tag);
/*
h_tag和hisMax实质上都是前缀最大值
在一个后缀加入的时候(也就是Tree[now].h_tag传下来的时候),s_tag和sum可以跟当前的h_tag接上变成一个新的前缀而s_tag与h_tag两个标记对应的区间的左端点一定是一致的,sum和hisMax显然是一致的,于是h_tag和hisMax可以这样转移
*/
        Tree[now].h_tag = ;
    }
    if(Tree[now].s_tag){
        Tree[lch].sum += Tree[now].s_tag;
        Tree[rch].sum += Tree[now].s_tag;
        Tree[lch].s_tag += Tree[now].s_tag;
        Tree[rch].s_tag += Tree[now].s_tag;
        Tree[now].s_tag = ;
    }//这个没什么好说的,基本的线段树操作
}
//注意一定要先传h_tag再传s_tag,因为h_tag和hisMax传下来的时候是与之前的那一段进行连接,而不是与当前计算的这一段进行连接

完整代码:

 #include<bits/stdc++.h>
 #define lch (now << 1)
 #define rch (now << 1 | 1)
 #define mid ((l + r) >> 1)
 #define int long long
 //This code is written by Itst
 using namespace std;

 inline int read(){
     ;
     ;
     char c = getchar();
     while(c != EOF && !isdigit(c)){
         if(c == '-')
             f = ;
         c = getchar();
     }
     while(c != EOF && isdigit(c)){
         a = (a << ) + (a << ) + (c ^ ');
         c = getchar();
     }
     return f ? -a : a;
 }

 ;
 struct node{
     int sum , hisMax , s_tag , h_tag;
 }Tree[MAXN << ];
 struct query{
     int l , r , ind;
 }now[MAXN];
 map < int , int > appear;
 int N , Q , num[MAXN] , ans[MAXN];

 bool operator <(query a , query b){
     return a.r < b.r;
 }

 inline void pushup(int now){
     Tree[now].sum = max(Tree[lch].sum , Tree[rch].sum);
     Tree[now].hisMax = max(Tree[lch].hisMax , Tree[rch].hisMax);
 }

 inline void pushdown(int now){
     if(Tree[now].h_tag){
         Tree[lch].hisMax = max(Tree[lch].hisMax , Tree[lch].sum + Tree[now].h_tag);
         Tree[rch].hisMax = max(Tree[rch].hisMax , Tree[rch].sum + Tree[now].h_tag);
         Tree[lch].h_tag = max(Tree[lch].h_tag , Tree[lch].s_tag + Tree[now].h_tag);
         Tree[rch].h_tag = max(Tree[rch].h_tag , Tree[rch].s_tag + Tree[now].h_tag);
         Tree[now].h_tag = ;
     }
     if(Tree[now].s_tag){
         Tree[lch].sum += Tree[now].s_tag;
         Tree[rch].sum += Tree[now].s_tag;
         Tree[lch].s_tag += Tree[now].s_tag;
         Tree[rch].s_tag += Tree[now].s_tag;
         Tree[now].s_tag = ;
     }
 }

 void modify(int now , int l , int r , int L , int R , int add){
     if(l >= L && r <= R){
         Tree[now].s_tag += add;
         Tree[now].sum += add;
         Tree[now].hisMax = max(Tree[now].hisMax , Tree[now].sum);
         Tree[now].h_tag = max(Tree[now].h_tag , Tree[now].s_tag);
         return;
     }
     pushdown(now);
     if(mid >= L)
         modify(lch , l , mid , L , R , add);
     if(mid < R)
         modify(rch , mid +  , r , L , R , add);
     pushup(now);
 }

 int query(int now , int l , int r , int L , int R){
     if(l >= L && r <= R)
         return Tree[now].hisMax;
     pushdown(now);
     ;
     if(mid >= L)
         maxN = query(lch , l , mid , L , R);
     if(mid < R)
         maxN = max(maxN , query(rch , mid +  , r , L , R));
     return maxN;
 }

 signed main(){
 #ifndef ONLINE_JUDGE
     freopen("1557.in" , "r" , stdin);
     //freopen("1557.out" , "w" , stdout);
 #endif
     N = read();
      ; i <= N ; ++i)
         num[i] = read();
     Q = read();
      ; i <= Q ; ++i){
         now[i].l = read();
         now[i].r = read();
         now[i].ind = i;
     }
     sort(now +  , now + Q + );
      ; i <= Q ; ++i){
         ].r +  ; j <= now[i].r ; ++j){
             modify( ,  , N , appear[num[j]] +  , j , num[j]);
             appear[num[j]] = j;
         }
         ans[now[i].ind] = query( ,  , N , now[i].l , now[i].r);
     }
      ; i <= Q ; ++i)
         printf("%lld\n" , ans[i]);
     ;
 }

SPOJ1557 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. 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 离线&&线段树

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

  4. SPOJ GSS2 Can you answer these queries II

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

  5. SP1557 GSS2 - Can you answer these queries II

    一开始看不懂题解,看懂了题解之后觉得还是挺妙的. 好多题解里都提到了HH的项链,但是我觉得关系并不大啊…… 先把所有询问离线下来按照右端点排序,按照询问的要求一个一个加入数字,怎么加入数字,我们设计一 ...

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

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

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

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

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

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

  9. Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)

    You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defi ...

随机推荐

  1. 运行gulp项目报错:AssertionError: Task function must be specified。

    一.问题描述: gulp项目在本地windows 10机器上跑没有任何问题,但是放在centos 7虚拟机上跑报错:AssertionError: Task function must be spec ...

  2. 搜狐eHR团队-曾经一起奋斗过的~

    昨天所有搜狐eHR团队同事再相聚(在职+离职),大家聊的都很开心,共同回顾了eHR项目从无到有的过程. 非常感谢在搜狐的工作经历,自己成长很多,目前大家分布在各个公司为eHR做着贡献,大家都注意身体~ ...

  3. Linux LB--负载均衡和高可靠

    1.负载均衡典型应用场景,外网.内网.私网公共服务. 典型场景: (1)用户通过公网访问数据中心的ftp.web.https服务器. (2) 在数据中心内部东西向访问其他服务时,例如,访问其他虚拟机. ...

  4. [ng:areq] Argument 'XXXXCtrl' is not a function, got undefined

    angular.module('MyApp', []) 这里的[]重复了,以后引入新的controller.js文件会覆盖前面那个,所以此处的[]去掉 .controller('MyCtrl', fu ...

  5. 多表联合Update更新数据

    UPDATE A SET A.c2 =B.c3 from A inner join B on A.c1=B.c1 多表from子句后面

  6. 贝塞尔曲线UIBezierPath简单使用

    //常用属性 /* 1.CGPath: 将UIBezierPath类转换成CGPath 2.currentPoint: 当前path的位置,可以理解为path的终点 3.lineWidth: 线条宽度 ...

  7. [Android] ubuntu 下不识别 Android 设备

    之前的android手机给家人用了,手里现在有一个旧手机,调试过程又出现不识别的问题,这次要记录一下. 首先,需要把手机开发者选项打开,在设置里对着android版本或者型号多点几次,就会打开. 原文 ...

  8. [20170623]利用传输表空间恢复数据库2.txt

    [20170623]利用传输表空间恢复数据库2.txt --//继续上午的测试,测试truncate,是否可行,理论讲应该没有问题.我主要的目的测试是否要切换日志.--//参考链接 : http:// ...

  9. Apache与Tomcat有什么关系和区别

    Apache 和 Tomcat 都是web网络服务器,两者既有联系又有区别,在进行HTML.PHP.JSP.Perl等开发过程中,需要准确掌握其各自特点,选择最佳的服务器配置. Apache是web服 ...

  10. MailKit帮助类

    public class EmailHelp { /// <summary> /// Smtp服务器地址 /// </summary> private static reado ...