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

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

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 150000
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define LL long long
using namespace std;

typedef struct {
    LL nmax,hmax,nlazy,hlazy;
}Tree;
Tree tree[maxn*10];

typedef struct {
    int l,r,id;
} Que;
Que que[maxn*2];

LL num[maxn*2],ans[maxn*2];
int n,m,pre[maxn*4];

int cmp(Que x,Que y)
{
    if (x.r<y.r) return 1;
    return 0;
}

void update(int x)
{
    tree[x].nmax=max(tree[x<<1].nmax,tree[x<<1|1].nmax);
    tree[x].hmax=max(tree[x<<1].hmax,tree[x<<1|1].hmax);
}

void pushdown(int x)
{
    if (tree[x].hlazy) {
        tree[x<<1].hmax=max(tree[x<<1].hmax,tree[x<<1].nmax+tree[x].hlazy);
        tree[x<<1|1].hmax=max(tree[x<<1|1].hmax,tree[x<<1|1].nmax+tree[x].hlazy);
        tree[x<<1].hlazy=max(tree[x<<1].hlazy,tree[x].hlazy+tree[x<<1].nlazy);
        tree[x<<1|1].hlazy=max(tree[x<<1|1].hlazy,tree[x].hlazy+tree[x<<1|1].nlazy);
        tree[x].hlazy=0;
    }
    if (tree[x].nlazy) {
        tree[x<<1].nmax=tree[x<<1].nmax+tree[x].nlazy;
        tree[x<<1|1].nmax=tree[x<<1|1].nmax+tree[x].nlazy;
        tree[x<<1].nlazy+=tree[x].nlazy;
        tree[x<<1|1].nlazy+=tree[x].nlazy;
        tree[x].nlazy=0;
    }
}

void change(int x,int l,int r,int ll,int rr,LL y)
{
    if (ll<=l && r<=rr) {
        tree[x].nlazy+=y;
        tree[x].nmax+=y;
        tree[x].hlazy=max(tree[x].hlazy,tree[x].nlazy);
        tree[x].hmax=max(tree[x].nmax,tree[x].hmax);
        return;
    }
    pushdown(x);
    int mid=(l+r)>>1;
    if (ll<=mid) change(x<<1,l,mid,ll,rr,y);
    if (rr>mid) change(x<<1|1,mid+1,r,ll,rr,y);
    update(x);
}

LL ask(int x,int l,int r,int ll,int rr)
{
//    printf("%d %d %d %lld %lld\n",x,l,r,tree[x].hmax,tree[x].nmax);
    if (ll<=l && r<=rr) return tree[x].hmax;
    pushdown(x);
    int mid=(l+r)>>1;
    if (rr<=mid) return ask(x<<1,l,mid,ll,rr);
    else
    if (ll>mid) return ask(x<<1|1,mid+1,r,ll,rr);
    else
    return max(ask(x<<1,l,mid,ll,mid),ask(x<<1|1,mid+1,r,mid+1,rr));
}

void build(int x,int l,int r)
{
    tree[x].hlazy=tree[x].nlazy=0;
    if (l==r) {
        scanf("%lld",&num[l]);
        tree[x].hmax=tree[x].nmax=0;
        return;
    }
    int mid=(l+r)>>1;
    if (l<=mid) build(x<<1,l,mid);
    if (mid<r) build(x<<1|1,mid+1,r);
    update(x);
}

int main()
{
    scanf("%d",&n);
    build(1,1,n);
    scanf("%d",&m);
    rep(i,0,m-1) {
        scanf("%d %d",&que[i].l,&que[i].r);
        que[i].id=i;
    }
    sort(que,que+m,cmp);
    memset(pre,0,sizeof(pre));
    int now=0;
    rep(i,1,n) {
        change(1,1,n,pre[num[i]+maxn]+1,i,num[i]);
        pre[num[i]+maxn]=i;
        while (now<=m && que[now].r==i) {
            ans[que[now].id]=ask(1,1,n,que[now].l,que[now].r);
            ++now;
        }
    }
    rep(i,0,m-1) printf("%lld\n",ans[i]);
    return 0;
}

时间: 2024-10-05 21:03:05

【SPOJ - GSS2】Can you answer these queries II(线段树)的相关文章

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  Solved: 76[Submit][Status][Discuss] Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和(可选空子段). 这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次. 比如:1,2,3,2,2,2出现了3次,但只算一次,

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

Spoj 1557 Can you answer these queries II 线段树 任意区间最大子段和 不重复数字

题目链接:点击打开链接 每个点都是最大值,把一整个序列和都压缩在一个点里. #include <vector> #include <iostream> #include <algorithm> #include <string.h> #include <stdio.h> using namespace std; #define N 100005 #define Lson(x) (x<<1) #define Rson(x) (x<

spoj gss2 : Can you answer these queries II 离线&amp;&amp;线段树

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 co

SPOJ GSS3 Can you answer these queries III (线段树)

题目大意: 求区间最大子区间的和. 思路分析: 记录左最大,右最大,区间最大. 注意Q_L  和 Q_R  就好. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e #define maxn 55555 using na

SPOJ GSS4 Can you answer these queries IV (线段树)

题目大意: 给出N个数 0     操作   把 l -----  r之间的数全部开平方 1     操作  输出 l -----r  之间的和 思路分析: 判断区间里的数字是否全相同.如果相同, 将cov 置为该数 查询的时候和更新的时候,如果碰到cov != -1 的  就直接返回就可以了 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #incl

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

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

[BZOJ2482][Spoj1557] Can you answer these queries II Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和(可选空子段). 这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次. 比如:1,2,3,2,2,2出现了3次,但只算一次,于是这个序列的和是1+2+3=6. Input 第一行一个数n. 第二行n个数,为给定的序列,这些数的绝对值小于等于100000. 第三行一个数m. 接下来m行,每行两个

SPOJ GSS2 Can you answer these queries II

Time Limit: 1000MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu 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

SPOJ GSS5 Can you answer these queries V ——线段树

[题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #include <string> #include <iostream> #i