Submission #363455

#TimeUsernameProblemLanguageResultExecution timeMemory
363455Lam_lai_cuoc_doiFire (JOI20_ho_t5)C++17
14 / 100
501 ms132176 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

const bool typetest = 0;
const int N = 2e5 + 5;
struct FenwickTree
{
    ll a[N];
    int n;
    FenwickTree()
    {
        memset(a, 0, sizeof a);
    }
    void Clear()
    {
        memset(a, 0, sizeof a);
    }
    void Update(int p, ll v)
    {
        for (; p <= n; p += p & -p)
            a[p] += v;
    }
    ll Get(int p)
    {
        ll ans(0);
        for (; p > 0; p -= p & -p)
            ans += a[p];
        return ans;
    }
    ll Get(int l, int r)
    {
        return Get(r) - Get(l - 1);
    }
} f;
int n, pre[N], suf[N], q;
ll a[N], ans[N];
vector<pair<pair<int, int>, int>> que[N];
vector<pair<int, ll>> upd[N];

void Read()
{
    cin >> n >> q;
    f.n = n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 1; i <= q; ++i)
    {
        int t, l, r;
        cin >> t >> l >> r;
        que[t].push_back({{l, r}, i});
    }
}

void Solve()
{
    vector<int> s;
    s.reserve(n);
    for (int i = 1; i <= n; ++i)
    {
        while (s.size() && a[s.back()] < a[i])
            s.pop_back();
        pre[i] = s.empty() ? 0 : s.back();
        s.push_back(i);
    }
    s.clear();
    for (int i = n; i; --i)
    {
        while (s.size() && a[s.back()] < a[i])
            s.pop_back();
        suf[i] = s.empty() ? n + 1 : s.back();
        s.push_back(i);
    }
    for (int i = 1; i <= n; ++i)
    {
        for (int j = i; j < suf[i]; ++j)
        {
            upd[j - i].push_back({j, a[i]});
            if (pre[i])
                upd[j - pre[i]].push_back({j, -a[i]});
        }
    }
    for (int i = 0; i <= n; ++i)
    {
        for (auto j : upd[i])
            f.Update(j.first, j.second);
        for (auto j : que[i])
            ans[j.second] = f.Get(j.first.first, j.first.second);
    }
    for (int i = 1; i <= q; ++i)
        cout << ans[i] << "\n";
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        Read();
        Solve();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...