QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#45912#4397. DOS CardmiaomiaoziAC ✓214ms44992kbC++174.0kb2022-08-24 17:26:052022-08-24 17:26:08

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-24 17:26:08]
  • Judged
  • Verdict: AC
  • Time: 214ms
  • Memory: 44992kb
  • [2022-08-24 17:26:05]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
// https://space.bilibili.com/672346917

#ifndef LOCAL
#define LOG(...) 42
#endif

#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long LL;
typedef pair <int, int> PII;

constexpr double EPS = 1e-8;
const double PI = acos(-1);
constexpr LL inf = 1e18;

template <typename T = long long>
struct SegTree {
#define gao(x) u.x = max(l.x, r.x)
    struct node {
        int l, r;
        LL ppnn = -inf, pnpn = -inf;
        // ++--  +-+-
        LL p = -inf, n = inf;
        LL pp = -inf, nn = -inf, pn = -inf, np = -inf;
        LL ppn = -inf, pnn = -inf, pnp = -inf, npn = -inf;
    };
    int n;
    vector <node> tr;
    vector <T> a;

    SegTree(int _n = 1): n(_n), tr((_n + 5) << 2), a(_n + 5) {}
    SegTree(int _n, const vector <T> &_a) : n(_n), tr((_n + 5) << 2), a(_a) {
        build(1, 1, n);
    }

    void init(int u, int r) {
        assert(1 <= r && r <= n);
        tr[u].p = a[r], tr[u].n = -a[r];
    }
    void pushup(node &u, node &l, node &r) {
        gao(pp); gao(nn); gao(pn); gao(np);
        gao(ppn); gao(pnn); gao(pnp); gao(npn);
        gao(ppnn); gao(pnpn);

        // one
        gao(p); gao(n);

        // two
        u.pp = max(u.pp, l.p + r.p);
        u.pn = max(u.pn, l.p + r.n);
        u.np = max(u.np, l.n + r.p);
        u.nn = max(u.nn, l.n + r.n);

        // three
        u.ppn = max({u.ppn, l.p + r.pn, l.pp + r.n});
        u.pnn = max({u.pnn, l.p + r.nn, l.pn + r.n});
        u.pnp = max({u.pnp, l.p + r.np, l.pn + r.p});
        u.npn = max({u.npn, l.n + r.pn, l.np + r.n});

        // four
        u.ppnn = max({u.ppnn, l.p + r.pnn, l.pp + r.nn, l.ppn + r.n});
        u.pnpn = max({u.pnpn, l.p + r.npn, l.pn + r.pn, l.pnp + r.n});
    }
#undef gao
    void pushdown(node &u, node &l, node &r) {
        
    }
    void modify(int u, int l, int r, LL c) {
        if(tr[u].l >= l && tr[u].r <= r) {
            
            return;
        }
        pushdown(u);
        int mid = tr[u].l + tr[u].r >> 1;
        if(l <= mid) modify(u << 1, l, r, c);
        if(r > mid) modify(u << 1 | 1, l, r, c);
        pushup(u);
    }
    LL len(int &u) {
        return 1LL * tr[u].r - tr[u].l + 1;
    }
    LL len(node &u) {
        return 1LL * u.r - u.l + 1; 
    }
    void pushup(int u) {
        pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
    }
    void pushdown(int u) {
        pushdown(tr[u], tr[u << 1], tr[u << 1 | 1]);
    }
    void build(int u, int l, int r) {
        tr[u].l = l, tr[u].r = r;
        if(l == r) {
            init(u, r);
            return;
        }
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }
    node query(int u, int l, int r) {
        if(tr[u].l >= l && tr[u].r <= r) {
            return tr[u];
        }
        pushdown(u);
        int mid = tr[u].l + tr[u].r >> 1;
        if(r <= mid) return query(u << 1, l, r);
        else if (l > mid) return query(u << 1 | 1, l, r);
        else {
            node res, left, right;
            left = query(u << 1, l, r);
            right = query(u << 1 | 1, l, r);
            pushup(res, left, right);
            return res;
        }
    }
};

int multi_cases = 1;

void A_SOUL_AvA () {
    int n, q;
    cin >> n >> q;

    vector <LL> a(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        a[i] *= a[i];
    }

    SegTree <LL> st(n, a);
    while (q--) {
        int l, r;
        cin >> l >> r;
        auto node = st.query(1, l, r);
        cout << max(node.ppnn, node.pnpn) << endl;
    }
}

int main () {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(12);

    int T = 1;
    for (multi_cases && cin >> T; T; T--) {
        A_SOUL_AvA ();
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 214ms
memory: 44992kb

input:

100
96154 95730
90210724 22635940 55815661 83807625 19279659 73772905 76214297 19124836 44176768 61118775 90180769 78603454 23786707 63922615 30379117 541896 67837670 15861700 18129979 15378730 99790737 18747118 79018780 14023804 10636607 27422459 75194869 52362958 38176367 17048673 77142527 8688873...

output:

19996866975031454
19954573944633996
19999245288760024
19991774536026976
19998516034562673
19889495723295968
19987300645542796
19999515774953477
19999691378636568
19999691135662443
19966234306637958
19999691378636568
19994914188770357
19999244057031833
19999691393398008
19999691378636568
199996913933...

result:

ok 198115 lines