QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#48593#4397. DOS Cardzzxzzx123AC ✓333ms34312kbC++2.8kb2022-09-14 16:59:462022-09-14 16:59:50

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-09-14 16:59:50]
  • Judged
  • Verdict: AC
  • Time: 333ms
  • Memory: 34312kb
  • [2022-09-14 16:59:46]
  • Submitted

answer

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
#define int long long
#define endl '\n'
const int N = 2e6 + 10, INF = 1e18;
int n, m, q, w[N];
struct Node
{
    int l, r;
    int ans1, ans2; // ++-- +-+-
    int a, b; // + -
    int c, d, e, f; // ++ +- -+ -- 
    int g, h, i, j; // +-- ++- +-+ -+-
    void init(int _l, int _r)
    {
        l = _l, r = _r;
        ans1 = ans2 = a = b = c = d = e = f = g = h = i = j = -INF;
    }
}tr[N << 2];

void pushup(Node &u, const Node& l, const Node& r)
{
    // 每个元素先继承左右儿子的最大值
    u.ans1 = max(l.ans1, r.ans1); 
    u.ans2 = max(l.ans2, r.ans2);
    u.a = max(l.a, r.a);
    u.b = max(l.b, r.b);
    u.c = max(l.c, r.c);
    u.d = max(l.d, r.d);
    u.e = max(l.e, r.e);
    u.f = max(l.f, r.f);
    u.g = max(l.g, r.g);
    u.h = max(l.h, r.h);
    u.i = max(l.i, r.i);
    u.j = max(l.j, r.j);
    
    // 先更新 ++-- +-+- 每个三种情况
    u.ans1 = max(u.ans1, l.a + r.g);
    u.ans1 = max(u.ans1, l.c + r.f);
    u.ans1 = max(u.ans1, l.h + r.b);
    u.ans2 = max(u.ans2, l.a + r.j);
    u.ans2 = max(u.ans2, l.d + r.d);
    u.ans2 = max(u.ans2, l.i + r.b);
    
    //一个个枚举,代码奇葩但是其实思路很简答。
    u.c = max(u.c, l.a + r.a);
    u.d = max(u.d, l.a + r.b);
    u.e = max(u.e, l.b + r.a);
    u.f = max(u.f, l.b + r.b);
    u.g = max(u.g, l.a + r.f);
    u.g = max(u.g, l.d + r.b);
    u.h = max(u.h, l.a + r.d);
    u.h = max(u.h, l.c + r.b);
    u.i = max(u.i, l.a + r.e);
    u.i = max(u.i, l.d + r.a);
    u.j = max(u.j, l.b + r.d);
    u.j = max(u.j, l.e + r.b);
}

void pushup(int u)
{
    pushup(tr[u], tr[u << 1], tr[u << 1 | 1]);
}

void build(int u, int l, int r)
{
    tr[u].init(l, r);
    if(l == r) tr[u].a = w[l] , tr[u].b = -w[l];
    else
    {
        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(l <= tr[u].l && r >= tr[u].r) return tr[u];
    else
    {
        int mid = tr[u].l + tr[u].r >> 1;
        if(r <= mid) return query(u << 1, l, r);
        if(l > mid) return query(u << 1 | 1, l, r);
        Node res;
        res.init(0, 0);
        pushup(res, query(u << 1, l, r), query(u << 1 | 1, l, r));
        return res;
    }
}

void solve()
{
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i ++ ) cin >> w[i] , w[i] = w[i] * w[i];
    build(1, 1, n);
    
    while (m -- )
    {
        int a, b;
        cin >> a >> b;
        Node t = query(1, a, b);
        cout << max(t.ans1, t.ans2) << endl;
    }
}

signed main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    int T = 1;
    cin >> T;
    while(T -- ) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 333ms
memory: 34312kb

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