답안 #503075

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503075 2022-01-07T06:31:15 Z blue 3단 점프 (JOI19_jumps) C++17
100 / 100
899 ms 105868 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;

const int maxN = 500'000;
const ll INF = 1'000'000'000'000'000'000LL;


int N, Q;
vll A;
const int Z = (1<<19);

struct segtree
{
    vll lp = vll(Z<<1, -INF);
    vll half = vll(Z<<1, 0);
    vll full = vll(Z<<1, -INF);

    void build(int i, int l, int r)
    {
        if(l == r)
        {
            half[i] = A[l];
        }
        else
        {
            build(2*i, l, (l+r)/2);
            build(2*i+1, (l+r)/2+1, r);
            half[i] = max(half[2*i], half[2*i+1]);
        }
    }

    void deploy(int i, int l, int r, int L, int R, ll V)
    {
        if(R < l || r < L) return;
        else if(L <= l && r <= R)
        {
            // cerr << "direct deployed A: " << i << ' ' << l << ' ' << r << ' ' << lp[i] << ' ' << full[i] << '\n';
            lp[i] = max(lp[i], V);
            full[i] = max(full[i], half[i] + lp[i]);
            // cerr << "direct deployed B: " << i << ' ' << l << ' ' << r << ' ' << lp[i] << ' ' << full[i] << '\n';
        }
        else
        {
            deploy(2*i, l, (l+r)/2, L, R, V);
            deploy(2*i+1, (l+r)/2+1, r, L, R, V);

            full[i] = max({half[i] + lp[i], full[2*i], full[2*i+1]});

            // cerr << "full " << i << ' ' << l << ' ' << r << " = " << full[i] << '\n';
        }
    }

    pair<ll, ll> rangemax(int i, int l, int r, int L, int R)
    {
        if(R < l || r < L) return {-INF, -INF};
        else if(L <= l && r <= R)
        {
            // cerr << "range: " << i << ' ' << l << ' ' << r << " - " << full[i] << ' ' << half[i] << '\n';
            return {full[i], half[i]};
        }
        else
        {
            pair<ll, ll> lft =rangemax(2*i, l, (l+r)/2, L, R);
            pair<ll, ll> rgt = rangemax(2*i+1, (l+r)/2+1, r, L, R);
            return {max({lft.first, rgt.first, lft.second + lp[i], rgt.second + lp[i]}), max(lft.second, rgt.second)};
        }
    }
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> N;

    A = vll(1+N+1);
    for(int i = 1; i <= N; i++) cin >> A[i];

    vi re[1+N];

    A[N+1] = INF;

    vi st{N+1};
    for(int i = N; i >= 1; i--)
    {
        while(A[i] > A[st.back()])
        {
            // cerr << "pair: " << i << ' ' << st.back() << '\n';
            re[i].push_back(st.back());
            st.pop_back();
        }
        if(st.back() != N+1)
            re[i].push_back(st.back());

        // if(st.back() != N+1) cerr << "npair: " << i << " " << st.back() << '\n';

        st.push_back(i);
    }


    cin >> Q;
    vi l(1+Q), r(1+Q), lqi[1+N];
    for(int j = 1; j <= Q; j++)
    {
        cin >> l[j] >> r[j];
        lqi[l[j]].push_back(j);
    }

    vll res(1+Q, -INF);

    segtree S;
    S.build(1, 1, N);

    // for(int f = 1; f <= N; f++) cerr << S.rangemax(1, 1, N, f, f).first << ' ';
    // cerr << '\n';

    for(int li = N; li >= 1; li--)
    {
        // cerr << "\n\n\n\n";
        // if(li == 2) cerr << "!!!! " << li << ' ' << S.full[3] << '\n';
        for(int ri: re[li])
        {
            if(ri + (ri - li) > N) continue;
            S.deploy(1, 1, N, ri + (ri - li), N, A[li] + A[ri]);
            // cerr << "deploying " << li << ' ' << ri << " : " << A[li]+A[ri] << " to range " << ri + (ri - li) << ' ' << N << '\n';

            // cerr << "segtree = ";
            // for(int f = 1; f <= N; f++) cerr << S.rangemax(1, 1, N, f, f).first << ' ';
            // cerr << '\n';
        }
        // cerr << "\n\n";
        // if(li == 2) cerr << "!!!! " << li << ' ' << S.full[3] << '\n';

        // if(li == 2)
        // {
        //     cerr << "segtree = \n";
        //     for(int f = 1; f <= N; f++)
        //     {
        //         cerr << "f = " << f << " : " << S.rangemax(1, 1, N, f, f).first << '\n';
        //     }
        // }

        // if(li == 2) cerr << "!!!! " << li << ' ' << S.full[3] << '\n';

        for(int q: lqi[li])
        {
            // cerr << "answering: " << q << '\n';
            res[q] = S.rangemax(1, 1, N, li, r[q]).first;
        }
    }

    for(int j = 1; j <= Q; j++) cout << res[j] << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 24884 KB Output is correct
2 Correct 9 ms 24908 KB Output is correct
3 Correct 9 ms 24908 KB Output is correct
4 Correct 9 ms 24916 KB Output is correct
5 Correct 9 ms 24880 KB Output is correct
6 Correct 9 ms 24908 KB Output is correct
7 Correct 11 ms 24912 KB Output is correct
8 Correct 10 ms 24908 KB Output is correct
9 Correct 9 ms 24920 KB Output is correct
10 Correct 10 ms 24876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 24884 KB Output is correct
2 Correct 9 ms 24908 KB Output is correct
3 Correct 9 ms 24908 KB Output is correct
4 Correct 9 ms 24916 KB Output is correct
5 Correct 9 ms 24880 KB Output is correct
6 Correct 9 ms 24908 KB Output is correct
7 Correct 11 ms 24912 KB Output is correct
8 Correct 10 ms 24908 KB Output is correct
9 Correct 9 ms 24920 KB Output is correct
10 Correct 10 ms 24876 KB Output is correct
11 Correct 287 ms 46376 KB Output is correct
12 Correct 231 ms 46440 KB Output is correct
13 Correct 271 ms 46436 KB Output is correct
14 Correct 235 ms 46448 KB Output is correct
15 Correct 231 ms 46392 KB Output is correct
16 Correct 244 ms 45756 KB Output is correct
17 Correct 255 ms 45772 KB Output is correct
18 Correct 233 ms 45692 KB Output is correct
19 Correct 237 ms 46244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 152 ms 44060 KB Output is correct
2 Correct 86 ms 44596 KB Output is correct
3 Correct 84 ms 43852 KB Output is correct
4 Correct 139 ms 44100 KB Output is correct
5 Correct 140 ms 44084 KB Output is correct
6 Correct 134 ms 43468 KB Output is correct
7 Correct 135 ms 43312 KB Output is correct
8 Correct 150 ms 43324 KB Output is correct
9 Correct 137 ms 43700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 24884 KB Output is correct
2 Correct 9 ms 24908 KB Output is correct
3 Correct 9 ms 24908 KB Output is correct
4 Correct 9 ms 24916 KB Output is correct
5 Correct 9 ms 24880 KB Output is correct
6 Correct 9 ms 24908 KB Output is correct
7 Correct 11 ms 24912 KB Output is correct
8 Correct 10 ms 24908 KB Output is correct
9 Correct 9 ms 24920 KB Output is correct
10 Correct 10 ms 24876 KB Output is correct
11 Correct 287 ms 46376 KB Output is correct
12 Correct 231 ms 46440 KB Output is correct
13 Correct 271 ms 46436 KB Output is correct
14 Correct 235 ms 46448 KB Output is correct
15 Correct 231 ms 46392 KB Output is correct
16 Correct 244 ms 45756 KB Output is correct
17 Correct 255 ms 45772 KB Output is correct
18 Correct 233 ms 45692 KB Output is correct
19 Correct 237 ms 46244 KB Output is correct
20 Correct 152 ms 44060 KB Output is correct
21 Correct 86 ms 44596 KB Output is correct
22 Correct 84 ms 43852 KB Output is correct
23 Correct 139 ms 44100 KB Output is correct
24 Correct 140 ms 44084 KB Output is correct
25 Correct 134 ms 43468 KB Output is correct
26 Correct 135 ms 43312 KB Output is correct
27 Correct 150 ms 43324 KB Output is correct
28 Correct 137 ms 43700 KB Output is correct
29 Correct 899 ms 100388 KB Output is correct
30 Correct 737 ms 99932 KB Output is correct
31 Correct 767 ms 98288 KB Output is correct
32 Correct 884 ms 98660 KB Output is correct
33 Correct 883 ms 98268 KB Output is correct
34 Correct 873 ms 97988 KB Output is correct
35 Correct 871 ms 98660 KB Output is correct
36 Correct 871 ms 96140 KB Output is correct
37 Correct 887 ms 96452 KB Output is correct
38 Correct 653 ms 105836 KB Output is correct
39 Correct 614 ms 105868 KB Output is correct
40 Correct 615 ms 104292 KB Output is correct
41 Correct 590 ms 104004 KB Output is correct
42 Correct 636 ms 99960 KB Output is correct
43 Correct 593 ms 100932 KB Output is correct
44 Correct 647 ms 101248 KB Output is correct
45 Correct 654 ms 101348 KB Output is correct
46 Correct 623 ms 99692 KB Output is correct
47 Correct 636 ms 99584 KB Output is correct
48 Correct 639 ms 99516 KB Output is correct
49 Correct 640 ms 100804 KB Output is correct
50 Correct 715 ms 99132 KB Output is correct
51 Correct 712 ms 102424 KB Output is correct
52 Correct 698 ms 102116 KB Output is correct
53 Correct 717 ms 102124 KB Output is correct
54 Correct 700 ms 102120 KB Output is correct
55 Correct 713 ms 103472 KB Output is correct