제출 #363499

#제출 시각아이디문제언어결과실행 시간메모리
363499Lam_lai_cuoc_doiFire (JOI20_ho_t5)C++17
100 / 100
415 ms89172 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], upp[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)
    {
        if (pre[i] == 0 || suf[i] - i < i - pre[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]});
            }
        else
            for (int j = pre[i] + 1; j <= i; ++j)
            {
                upp[i - j].push_back({j, a[i]});
                upp[suf[i] - j].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);
    }
    f.Clear();
    for (int i = 0; i <= n; ++i)
    {
        for (auto j : upp[i])
            f.Update(j.first, j.second);
        for (auto j : que[i])
            ans[j.second] += f.Get(j.first.first - i, j.first.second - i);
    }
    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();
}

컴파일 시 표준 에러 (stderr) 메시지

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