Submission #515795

#TimeUsernameProblemLanguageResultExecution timeMemory
515795Jarif_RahmanFire (JOI20_ho_t5)C++17
1 / 100
1096 ms30008 KiB
#include <bits/stdc++.h> #define pb push_back #define f first #define sc second using namespace std; typedef long long int ll; typedef string str; #define debug(s) for(auto x: s) cerr << x << " "; cerr << #s << "\n"; struct BIT{ int n; vector<ll> sm; BIT(int _n){ n = _n; sm.resize(n); } void add(int in, int x){ in++; while(in <= n) sm[in-1]+=x, in+=in&-in; } ll sum(int in){ in++; ll s = 0; while(in >= 1) s+=sm[in-1], in-=in&-in; return s; } ll sum(int l, int r){ return sum(r)-(l == 0? 0:sum(l-1)); } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; vector<ll> s(n); for(ll &x: s) cin >> x; vector<int> prev(n, -1); stack<int> st; for(int i = n-1; i >= 0; i--){ while(!st.empty() && s[i] > s[st.top()]) prev[st.top()] = i, st.pop(); st.push(i); } while(!st.empty()) st.pop(); vector<vector<int>> end(n+1); vector<vector<pair<int, int>>> chng(n+1); for(int i = 0; i < n; i++){ while(!st.empty() && s[st.top()] <= s[i]){ int x = st.top(); st.pop(); end[i-x-1].pb(x); if(prev[x] == -1) continue; if(s[prev[x]] <= s[i]) continue; chng[i-prev[x]-1].pb({prev[x], i}); } st.push(i); } while(!st.empty()) end[n-st.top()-1].pb(st.top()), st.pop(); vector<vector<array<int, 3>>> queries(n); for(int i = 0; i < q; i++){ int t, l, r; cin >> t >> l >> r; t--, l--, r--; queries[t].pb({l, r, i}); } vector<ll> ans(q, 0); BIT ss(n), bit1(n), bit2(n); for(int i = 0; i < n; i++) ss.add(i, s[i]); vector<ll> sth(n, 0); for(int i = 0; i+1 < n; i++) sth[i] = max(0LL, s[i]-s[i+1]); for(int t = 1; t <= n; t++){ for(int in: end[t-1]){ if(in != n-1) for(int i = in+1; i <= in+t-1; i++) ss.add(i, sth[in]); sth[in] = 0; } for(auto [in, nxt]: chng[t-1]){ ll x = sth[in]-(s[in]-s[nxt]); for(int i = in+1; i < nxt; i++) ss.add(i, x); sth[in] = (s[in]-s[nxt]); } for(auto [l, r, in]: queries[t-1]){ ans[in]+=ss.sum(l, r); int mx, a, b; if(t <= r-l+1){ mx = t; a = l, b = r-mx+1; a--, b--; a = max(a, 0); } else{ mx = r-l+1; a = r-t+1, b = l; a--, b--; a = max(a, 0); } for(int i = a; i <= b; i++) ans[in]+=sth[i]*mx; for(int i = a-1, c = mx-1; i >= 0 && c > 0; i--, c--) ans[in]+=sth[i]*c; for(int i = b+1, c = mx-1; i < n && c > 0; i++, c--) ans[in]+=sth[i]*c; } } for(ll x: ans) cout << x << "\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...