Submission #363488

#TimeUsernameProblemLanguageResultExecution timeMemory
363488Lam_lai_cuoc_doiFire (JOI20_ho_t5)C++17
14 / 100
757 ms262144 KiB
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#define task "A"
using namespace std;
using ll = long long;
using ld = long double;

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)
    {
        //cout << i << ": ";
        for (auto j : upd[i])
            f.Update(j.first, j.second);
        //for (int i = 1; i <= n; ++i)
        //    cout << f.Get(i, i) << " ";
        //cout << "\n";
        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_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if (fopen(task ".INP", "r"))
    {
        freopen(task ".INP", "r", stdin);
        freopen(task ".OUT", "w", stdout);
    }
    Read();
    Solve();
}

Compilation message (stderr)

ho_t5.cpp: In function 'int32_t main()':
ho_t5.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  109 |         freopen(task ".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t5.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  110 |         freopen(task ".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#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...