WA30

P2839 [国家集训队] middle

Kevinx @ 2024-11-23 23:09:17

已经照的第一篇题解大改了,但还是WA30,求调

#include<bits/stdc++.h>
#define ll long long
#define int long long
#define Mid ll mid = (l+r)>>1
using namespace std;
const int N = 3e5 + 20;
ll n, m, q[10], id[N], a[N];
ll rt[N], lc[N], rc[N], tot = 0;
ll ans = 0;
struct TREE{
    ll lmax, rmax, sum;
    void init(){lmax = rmax = -1e18, sum = 0;}
}T[N<<5];
TREE merge(TREE A, TREE B){
    TREE C;
    C.lmax = max(A.lmax, A.sum + B.lmax);
    C.rmax = max(B.rmax, A.rmax + B.sum);
    C.sum = A.sum + B.sum;
    return C;
}
void build(ll &o, ll l , ll r) {
    o = ++tot;
    T[o].lmax = T[o].rmax = T[o].sum = r-l+1;
    if(l == r) return;
    Mid;
    build(lc[o], l, mid);
    build(rc[o], mid+1, r);
}
void change(ll pre, ll &o, ll l, ll r, ll x) {
    o = ++tot;
    lc[o] = lc[pre], rc[o] = rc[pre];
    T[o] = T[pre];
    if(l == r) {
        T[o].lmax = T[o].rmax = T[o].sum = -1;
        return;
    }
    Mid;
    if(x <= mid) change(lc[pre], lc[o], l, mid, x);
    if(x >  mid) change(rc[pre], rc[o], mid+1, r, x);
    T[o] = merge(T[lc[o]], T[rc[o]]);
}
TREE query(ll p, ll l, ll r, ll x, ll y) {
    if(x <= l && r <= y) {
        return T[p];
    }
    Mid;
    TREE res;
    res.init();
    if(x <= mid) res = merge(res, query(lc[p], l, mid, x, y));
    if(y >  mid) res = merge(res, query(rc[p], mid+1, r, x, y));
    return res;
}
int Check(ll mid){
    ll val = 0;
    TREE Ans;
    if(q[1] + 1 <= q[2] - 1) Ans = query(rt[mid], 1, n, q[1] + 1, q[2] - 1), val += Ans.sum;
    Ans = query(rt[mid], 1, n, q[0], q[1]), val += Ans.rmax;
    Ans = query(rt[mid], 1, n, q[2], q[3]), val += Ans.lmax;
    return val >= 0;
}
bool cmp(ll x, ll y) {return a[x] < a[y];}
signed main(){
    scanf("%lld", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
        id[i] = i;
    }
    sort(id+1, id+n+1, cmp);
    build(rt[1], 1, n);
    T[0].init();
    for(int i = 2; i <= n; i++) {change(rt[i-1], rt[i], 1, n, id[i-1]);}
    scanf("%lld", &m);
    while(m--){
        for(int i = 0; i < 4; ++i) {
            ll x;
            scanf("%lld", &x);
            q[i] = (x + ans) % n + 1;
        }
        sort(q, q + 4);
        ll l = 1, r = n;
        while(l <= r){
            Mid;
            if(Check(mid)) ans = a[id[mid]], l = mid + 1;
            else r = mid - 1;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

by Kevinx @ 2024-11-23 23:30:19

不知道为什么,全部输出0


|