제출 #1330424

#제출 시각아이디문제언어결과실행 시간메모리
1330424nguyenkhangninh99Dungeon 3 (JOI21_ho_t5)C++20
11 / 100
4093 ms15764 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int maxn = 2e5 + 5;
int a[maxn], b[maxn], d[maxn]; 
int mnl[maxn], mnr[maxn];

signed main(){
    ios_base::sync_with_stdio(false); 
    cin.tie(0); cout.tie(0);

    int n, m; cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> b[i];

    d[1] = 0;
    for(int i = 1; i <= n; i++) d[i + 1] = d[i] + a[i];

    stack<int> st;
    b[0] = 0; 
    for(int i = 1; i <= n; i++){
        while(!st.empty() && b[st.top()] >= b[i]) st.pop();
        mnl[i] = (st.empty() ? 0 : st.top());
        st.push(i);
    }

    while(!st.empty()) st.pop();
    b[n + 1] = 0;
    for(int i = n; i >= 1; i--){
        while(!st.empty() && b[st.top()] > b[i]) st.pop(); 
        mnr[i] = (st.empty() ? n + 1 : st.top()); 
        st.push(i);
    }

    vector<array<int, 3>> range;
    for(int i = 1; i <= n; i++) range.push_back({mnl[i], mnr[i], b[i] - max(b[mnl[i]], b[mnr[i]])});

    while(m--){
        int s, t, u; cin >> s >> t >> u;

        bool ok = true;
        for(int i = s; i < t; i++){
            if(a[i] > u){
                ok = false;
                break;
            }
        }

        if(!ok){
            cout << -1 << "\n";
            continue;
        }

        long res = 0;
        for(auto [l, r, w]: range){
            int lbound = max(s, l), rbound = min(t, r);
            if(lbound < rbound){
                int dist = d[rbound] - d[lbound];
                if(l < s) res += dist * w;
                else res += max(dist - u, 0ll) * w;
            }
        }
        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...