QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#317799 | #5415. Ropeway | VinhLuu | RE | 2ms | 15884kb | C++20 | 4.2kb | 2024-01-29 18:54:58 | 2024-01-29 18:54:58 |
Judging History
answer
// happy
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define int long long
//#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef pair<int,int> pii;
//typedef tuple<int,int,int> tp;
const int N = 1e6 + 2;
const int oo = 1e16;
//const int mod = 1e9 + 7;
//const int base = 29; // >= so pt pb max
int n, k, a[N], f[N], g[N], dp[2][N], L[N], R[N];
string s;
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i ++) cin >> a[i];
cin >> s;
s = " " + s;
int dem = 0;
deque<pii> dq;
int tmp = 0;
dq.push_back({0, 0});
for(int i = 1; i <= n; i ++){
while(!dq.empty() && dq.front().fi < i - k || dq.front().fi < tmp) dq.pop_front();
f[i] = dq.front().se + a[i];
while(!dq.empty() && dq.back().se > f[i]) dq.pop_back();
dq.push_back({i, f[i]});
L[i] = tmp;
if(s[i] == '1') tmp = i, dem += a[i];
}
while(!dq.empty()) dq.pop_front();
tmp = n + 1;
dq.push_back({n + 1, 0});
for(int i = n; i >= 1; i --){
while(!dq.empty() && dq.front().fi > i + k || dq.front().fi > tmp) dq.pop_front();
g[i] = dq.front().se + a[i];
while(!dq.empty() && dq.back().se > f[i]) dq.pop_back();
dq.push_back({i, g[i]});
R[i] = tmp;
if(s[i] == '1') tmp = i;
}
int q; cin >> q;
while(q--){
int x, v; cin >> x >> v;
if(k > n){
cout << dem + (s[x] == '1' ? v - a[x] : 0) << "\n";
continue;
}
int now = a[x];
a[x] = v;
int l = max({0ll, x - k, L[x]});
int r = min({n + 1, x + k, R[x]});
int ret = oo, cnt = a[x];
// cout << l << " " << r << " g\n";
// for(int i = l; i <= r; i ++) dp[0][i] = dp[1][i] = 0;
while(!dq.empty()) dq.pop_front();
tmp = 0;
for(int i = l; i <= x - 1; i ++){
dp[0][i] = f[i];
while(!dq.empty() && dq.front().fi < i - k || dq.front().fi < tmp) dq.pop_front();
while(!dq.empty() && dq.back().se > dp[0][i]) dq.pop_back();
dq.push_back({i, dp[0][i]});
if(s[i] == '1') tmp = i;
}
for(int i = x; i <= min(r, n); i ++){
while(!dq.empty() && dq.front().fi < i - k || dq.front().fi < tmp) dq.pop_front();
dp[0][i] = dq.front().se + a[i];
// cout << i << " " << dq.front().se + g[i] << " hhh\n";
if(i > x) ret = min(ret, dq.front().se + g[i]);
else cnt += dq.front().se;
while(!dq.empty() && dq.back().se > dp[0][i]) dq.pop_back();
dq.push_back({i, dp[0][i]});
if(s[i] == '1') tmp = i;
}
// cout << cnt << " hh\n";
while(!dq.empty()) dq.pop_front();
tmp = n + 1;
for(int i = r; i >= x + 1; i --){
dp[1][i] = g[i];
while(!dq.empty() && dq.front().fi > i + k || dq.front().fi > tmp) dq.pop_front();
while(!dq.empty() && dq.back().se > dp[1][i]) dq.pop_back();
dq.push_back({i, dp[1][i]});
if(s[i] == '1') tmp = i;
}
// cout << tmp << " dd\n";
for(int i = x; i >= max(l, 1ll); i --){
while(!dq.empty() && dq.front().fi > i + k || dq.front().fi > tmp) dq.pop_front();
dp[1][i] = dq.front().se + a[i];
// cout << i << " " << dq.front().se + f[i] << " " << dp[1]<< " hhh\n";
if(i < x) ret = min(ret, dq.front().se + f[i]);
else cnt += dq.front().se;
while(!dq.empty() && dq.back().se > dp[1][i]) dq.pop_back();
dq.push_back({i, dp[1][i]});
if(s[i] == '1') tmp = i;
}
// cout << cnt << " g\n";
ret = min(ret, cnt);
cout << ret << "\n";
a[x] = now;
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
int t; cin >> t;
while(t--) solve();
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 15884kb
input:
3 10 3 5 10 7 100 4 3 12 5 100 1 0001000010 2 2 3 6 15 5 6 1 1 1 1 1 00000 1 3 100 5 6 1 1 1 1 1 00100 1 3 100
output:
206 214 0 100
result:
ok 4 number(s): "206 214 0 100"
Test #2:
score: -100
Runtime Error
input:
500 19 6 285203460 203149294 175739375 211384407 323087820 336418462 114884618 61054702 243946442 19599175 51974474 285317523 222489944 26675167 300331960 1412281 324105264 33722550 169011266 1111111110110100011 18 3 127056246 5 100630226 14 301161052 2 331781882 5 218792226 2 190274295 12 49227476 ...