제출 #433185

#제출 시각아이디문제언어결과실행 시간메모리
433185benedict0724역사적 조사 (JOI14_historical)C++17
100 / 100
1755 ms9448 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int bucket = 316;
int n, q;

struct query
{
    int s, e, i;
    query() : query(0, 0, 0) {}
    query(int _s, int _e, int _i) : s(_s), e(_e), i(_i) {}

    bool operator < (const query &O)
    {
        if(s/bucket == O.s/bucket) return e < O.e;
        return s/bucket < O.s/bucket;
    }
};

ll X[100002], Y[100002], I[100002];
query Q[100002];

ll tree[400002];

void update(ll p, ll val)
{
    for(tree[p+=n]+=val;p>1;p>>=1) tree[p>>1] = max(tree[p], tree[p^1]);
}

ll ans[100002];

int main()
{
    ios::sync_with_stdio(false); cin.tie(NULL);
    cin >> n >> q;
    for(int i=1;i<=n;i++) cin >> X[i];

    for(int i=1;i<=q;i++)
    {
        int l, r; cin >> l >> r;
        Q[i] = query(l, r, i);
    }
    sort(Q + 1, Q + q + 1);

    for(int i=1;i<=n;i++) Y[i] = X[i];
    sort(Y + 1, Y + n + 1);
    for(int i=1;i<=n;i++) I[i] = lower_bound(Y + 1, Y + n + 1, X[i]) - Y;

    for(int i=Q[1].s;i<=Q[1].e;i++)
        update(I[i], X[i]);

    ans[Q[1].i] = tree[1];
    int s = Q[1].s, e = Q[1].e;
    for(int i=2;i<=q;i++)
    {
        while(s < Q[i].s)
        {
            update(I[s], -X[s]);
            s++;
        }
        while(s > Q[i].s)
        {
            s--;
            update(I[s], X[s]);
        }
        while(e < Q[i].e)
        {
            e++;
            update(I[e], X[e]);
        }
        while(e > Q[i].e)
        {
            update(I[e], -X[e]);
            e--;
        }

        ans[Q[i].i] = tree[1];
    }

    for(int i=1;i<=q;i++) cout << ans[i] << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...