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[500005];

inline void push_down(int o)
{
    SgTreeDataType Prelazy = tree[o].prelazy;
    SgTreeDataType Lazy = tree[o].lazy;
    tree[2*o].prelazy = max(tree[2*o].prelazy,tree[o*2].lazy + Prelazy);
    tree[2*o].cursum = max(tree[2*o].cursum,tree[o*2].sum + Prelazy);
    tree[2*o].lazy += Lazy; tree[2*o].sum += Lazy;
    tree[2*o+1].prelazy = max(tree[2*o+1].prelazy,tree[o*2+1].lazy + Prelazy);
    tree[2*o+1].cursum = max(tree[2*o+1].cursum,tree[o*2+1].sum + Prelazy);
    tree[2*o+1].lazy += Lazy; tree[2*o+1].sum += Lazy;
    tree[o].lazy = 0,tree[o].prelazy = 0;
}

inline void push_up(int o)
{
    tree[o].sum = max(tree[2*o].sum,tree[2*o+1].sum);
    tree[o].cursum = max(tree[2*o].cursum,tree[2*o+1].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 = 0 ;
    if (R > L)
    {
        int mid = (L+R) >> 1;
        build_tree(L,mid,o*2);
        build_tree(mid+1,R,o*2+1);
    }
}

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)>>1;
        if (QL <= mid) updata(QL,QR,v,o*2);
        if (QR >  mid) updata(QL,QR,v,o*2+1);
        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)>>1;
        SgTreeDataType res = 0;
        if (QL <= mid) res =max(res, query(QL,QR,2*o));
        if (QR > mid) res =max(res,query(QL,QR,2*o+1));
        push_up(o);
        return res;
    }
}

int n,m;
int a[100005];
struct node
{
    int l,r,id;
};
bool cmp(node A,node B)
{
    return A.r<B.r;
}
node Query[100005];
int pos[3000005];
long long ans[1000005];
int main()
{
    memset(pos,0,sizeof(pos));
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    build_tree(1,n,1);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&Query[i].l,&Query[i].r);
        Query[i].id = i;
    }
    sort(Query,Query+m,cmp);
    int N = 100005;
    for(int i=1,j=0;i<=n;i++)
    {
        updata(pos[a[i]+N]+1,i,a[i],1);
        pos[a[i]+N]=i;
        while(j<m&&Query[j].r==i)
        {
            ans[Query[j].id]=query(Query[j].l,Query[j].r,1);
            j++;
        }
    }
    for(int i=0;i<m;i++)
        printf("%lld\n",ans[i]);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]的描述,p1168问题使用了线段树解法。在构造树的过程,需要遍历整棵树,所以时间复杂度为O(n)。但是在改变一个元素的值时,时间复杂度只有O(log(n))。求和的时候,树的节点表示一个索引范围内元素值之和,只要将区间分割对应上,平均时间复杂度是O(log(n)),最坏情况下不会超过O(n*log(n))。\[1\] 根据引用\[2\]的描述,QUERY 5 12应该是第4、5条边的极大值。\[2\] 根据引用\[3\]的描述,代码的if(L<=MID)和else if(R>MID)的判断条件是为了确保查询范围在左子树或右子树。如果加上else,会导致错误。\[3\] #### 引用[.reference_title] - *1* [leetCode307:线段树解法](https://blog.csdn.net/cyd1999/article/details/123963164)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Spoj 375 Qtree 树链剖分 + 线段树 解法](https://blog.csdn.net/niuox/article/details/8145842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值