救。。。。。。命

P2839 [国家集训队] middle

An_Account @ 2018-10-31 20:45:30

有没有大佬能帮忙看看主席树,已经Wa得生无可恋了,十分感谢。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 20010
/*
    sum1:区间和
    sum2:区间最大前缀和
    sum3:区间最大后缀和
*/
struct node {
    int l,r,sum1,sum2,sum3,lson,rson;
    node (int a=0,int b=0):l(a),r(b) {
        sum1=sum2=sum3=-1e5;
        lson=rson=0;
    }
}T[N*10];
int ncnt,num[N];
inline void pushup(int u) {
    T[u].sum1=T[T[u].lson].sum1+T[T[u].rson].sum1;
    T[u].sum2=max(T[T[u].lson].sum1+T[T[u].rson].sum2,T[T[u].lson].sum2);
    T[u].sum3=max(T[T[u].lson].sum3+T[T[u].rson].sum1,T[T[u].rson].sum3);
}
int build(int l,int r) {
    int u=++ncnt;T[u]=node(l,r);
    if (l==r) {
        T[u].sum1=T[u].sum2=T[u].sum3=num[l];
        return u;
    }
    int mid=(l+r)>>1;
    T[u].lson=build(l,mid),T[u].rson=build(mid+1,r);
    pushup(u);return u;
} 
int update(int rt,int at) {
    int u=++ncnt,l=T[rt].l,r=T[rt].r,mid=(l+r)>>1;
    T[u]=T[rt];
    if (l==r) {
        T[u].sum2=T[u].sum3=T[u].sum1=-1;
        return u;
    }
    if (at<=mid) T[u].lson=update(T[u].lson,at);
    else T[u].rson=update(T[u].rson,at);
    pushup(u);return u;
}
struct Data {
    int sum1,sum2,sum3;
    Data(int a=0,int b=-1e5,int c=-1e5):sum1(a),sum2(b),sum3(c) {}
};
Data query(int rt,int start,int end) {
    if (start>end) return Data(0,0,0);
    int l=T[rt].l,r=T[rt].r,mid=(l+r)>>1;
    if (start<=l&&r<=end) return Data(T[rt].sum1,T[rt].sum2,T[rt].sum3);
    Data ans,ans2;
    if (end<=mid) return query(T[rt].lson,start,end);
    if (start>mid) return query(T[rt].rson,start,end);
    ans=query(T[rt].lson,start,end),ans2=query(T[rt].rson,start,end);
    ans.sum2=max(ans.sum1+ans2.sum2,ans.sum2),ans.sum3=max(ans2.sum1+ans.sum3,ans2.sum3);
    return ans;
}
int id[N],RT[N],input[N];
bool cmp(int i,int j) {return input[i]<input[j];}
int main() {
    // freopen("1.in","r",stdin),freopen("1.out","w",stdout);
    int n;scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&input[i]),id[i]=i;
    sort(id+1,id+n+1,cmp);
    for (int i=1;i<=n;i++) num[i]=1;
    RT[1]=build(1,n);
    for (int i=2;i<=n;i++) RT[i]=update(RT[i]=RT[i-1],id[i-1]);
    int q,Q[5],lastans=0;scanf("%d",&q);
    while (q--) {
        for (int i=1;i<=4;i++) scanf("%d",&Q[i]),Q[i]=(Q[i]+lastans)%n+1;
        sort(Q+1,Q+5);
        int l=1,r=n,mid,ans;
        while (l<=r) {
            mid=(l+r)>>1;Data a,b,c;
            a=query(RT[mid],Q[1],Q[2]),b=query(RT[mid],Q[2]+1,Q[3]-1),c=query(RT[mid],Q[3],Q[4]);
            if (a.sum3+b.sum1+c.sum2>=0) ans=input[id[mid]],l=mid+1;
            else r=mid-1;
        }
        printf("%d\n",lastans=ans);
    }
    return 0;
}

by wxy_god @ 2018-10-31 20:48:47


by tonylin1026 @ 2018-10-31 20:50:20

问主席


by An_Account @ 2018-10-31 20:51:38

...能帮忙看看吗


by ForkΨKillet @ 2018-10-31 20:55:10

WAWA


|