#include<bits/stdc++.h>
#define int long long
#define pb push_back
using namespace std;
const int maxn = 1e6 + 5, inf = 1e9, mod = 998244353, N = 2e5;
int a[maxn];
void solve(){
int res = 0;
int n, k, q;
cin >> n;
cin >> k >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
auto calc = [&]() -> vector<int> {
deque<int> dq;
vector<int> f(n + 5);
dq.pb(1);
f[1] = a[1];
for(int i = 2; i <= n; i++){
while(!dq.empty() && dq.front() < i - k){
dq.pop_front();
}
int tmp = 0;
if(!dq.empty()){
tmp = f[dq.front()];
}
f[i] = tmp + a[i];
while(!dq.empty() && f[dq.back()] >= f[i]){
dq.pop_back();
}
dq.pb(i);
}
return f;
};
vector<int> l, r;
l = calc();
reverse(a + 1, a + n + 1);
r = calc();
reverse(r.begin() + 1, r.begin() + n + 1);
reverse(a + 1, a + n + 1);
while(q--){
int x;
cin >> x;
cout << l[x] + r[x] - a[x] << '\n';
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int T = 1;
// cin >> T;
while(T--){
solve();
}
return 0;
}
【单调队列优化dp】第五届武汉纺织大学ACM程序设计竞赛
最新推荐文章于 2024-10-31 16:16:13 发布