제출 #1342625

#제출 시각아이디문제언어결과실행 시간메모리
1342625nguyenkhangninh99Fire (JOI20_ho_t5)C++20
1 / 100
1095 ms5764 KiB

#include <bits/stdc++.h>
using namespace std;

#define int long long
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n, q; cin >> n >> q;
    vector<int> s(n + 1), pre(n + 1), nxt(n + 1);
    for(int i = 1; i <= n; i++) cin >> s[i];
    
    stack<int> st;
    for(int i = 1; i <= n; i++){
        while(!st.empty() && s[st.top()] < s[i]) st.pop();
        pre[i] = (st.empty() ? -2e9 : st.top());
        st.push(i);
    }

    while(!st.empty()) st.pop();
    for(int i = n; i >= 1; i--){
        while(!st.empty() && s[st.top()] <= s[i]) st.pop(); 
        nxt[i] = (st.empty() ? 2e9 : st.top()); 
        st.push(i);
    }

    while(q--){
        int t, l, r; cin >> t >> l >> r;

        int res = 0;
        for(int i = 1; i <= n; i++){
            int lbound = max({i, pre[i] + t + 1, l}), rbound = min({nxt[i] - 1, i + t, r});
            res += s[i] * max(0ll, rbound - lbound + 1);
        }
        cout << res << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...