QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789503 | #5415. Ropeway | yugen | WA | 3ms | 3876kb | C++17 | 3.3kb | 2024-11-27 20:36:18 | 2024-11-27 20:36:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void solve() {
int n, k;
cin >> n >> k;
vector<int> a(n + 2);
for(int i = 1; i <= n; i ++ ) {
cin >> a[i];
}
string s;
cin >> s;
s = '1' + s + '1';
int q;
cin >> q;
vector<vector<pii> > que(n + 1);
for(int i = 1; i <= q; i ++ ) {
int pos, v;
cin >> pos >> v;
que[pos].push_back({v, i});
}
vector<ll> g(n + 2);
deque<ll> dq;
dq.push_back(n + 1);
for(int i = n; i >= 1; i -- ) {
while(dq.size() && i + k < dq.front()) {
// cout << '?' << i << ' ' << dq.front() << '\n';
dq.pop_front();
}
// cout << "h" << i << ' ' << dq.front() << '\n';
g[i] = g[dq.front()] + a[i];
while(dq.size() && g[i] <= g[dq.back()]) {
// cout << '!' << i << ' ' << dq.back() << '\n';
dq.pop_back();
}
if(s[i] == '1') {
while(dq.size()) dq.pop_front();
}
dq.push_back(i);
// cout << i << ' ' << g[i] << '\n';
}
// cout << '\n';
vector<ll> f(n + 2);
while(dq.size()) dq.pop_front();
dq.push_back(0);
vector<ll> ans(q + 1, 1e18);
for(int i = 1; i <= n; i ++ ) {
while(dq.size() && i - k > dq.front()) {
dq.pop_front();
}
int tmp = a[i];
for(auto [v, num] : que[i]) {
auto ndq = dq;
a[i] = v;
for(int j = i; j <= min(n + 1, i + k - 1); j ++ ) {
while(ndq.size() && j - k > ndq.front()) {
ndq.pop_front();
}
f[j] = f[ndq.front()] + a[j];
// cout << ndq.front() << ' ' << f[ndq.front()] << '\n';
// if(i != j) {
// cout << j << ' ' << f[j] << ' ' << g[j] << ' ' << a[j] << ' ' << f[j] + g[j] - a[j] << '\n';
// } else {
// cout << j << ' ' << f[j] << ' ' << g[j] << ' ' << tmp << ' ' << f[j] + g[j] - tmp << '\n';
// }
while(ndq.size() && f[j] <= f[ndq.back()]) {
// cout << '!' << j << ' ' << dq.back() << '\n';
ndq.pop_back();
}
if(s[j] == '1') {
while(ndq.size()) {
ndq.pop_front();
}
}
ndq.push_back(j);
if(j == i) {
ans[num] = min(ans[num], f[j] + g[j] - tmp);
} else {
ans[num] = min(ans[num], f[j] + g[j] - a[j]);
}
}
// cout << '\n';
}
f[i] = f[dq.front()] + a[i];
if(que.size()) {
for(int j = i + 1; j <= min(n + 1, i + k - 1); j ++ ) {
f[j] = 0;
}
}
a[i] = tmp;
if(s[i] == '1') {
while(dq.size()) dq.pop_front();
}
dq.push_back(i);
}
for(int i = 1; i <= q; i ++ ) {
cout << ans[i] << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while(t -- ) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3812kb
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
Wrong Answer
time: 3ms
memory: 3876kb
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 ...
output:
2450249216 2227791622 2749568574 2640439932 2345953622 2498932345 2475082689 2466042101 2749568574 2240907690 2466042101 2749568574 2711172736 2511807344 2749568574 2262819048 2410326703 2910111338 470735446 470735446 456513579 809316981 715543137 470735446 715543137 19 20 20 20 20 19 57 849950346 8...
result:
wrong answer 1st numbers differ - expected: '2472886431', found: '2450249216'