QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#399201 | #5415. Ropeway | Ste | WA | 3ms | 5800kb | C++14 | 3.1kb | 2024-04-26 04:23:27 | 2024-04-26 04:23:27 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
const int K = 21;
int log_two[MAXN];
struct SparseTable {
int N;
long long **mi;
SparseTable(int n, vector<long long> &a) : N(n) {
mi = new long long*[N];
for (int i = 0; i < N; i++) {
mi[i] = new long long[K + 1];
mi[i][0] = a[i];
}
for (int j = 1; j <= K; j++) {
for(int i = 0; i + (1 << j) <= N; i++) {
mi[i][j] = min(mi[i][j - 1], mi[i + (1 << (j - 1))][j - 1]);
}
}
}
~SparseTable() {
for (int i = 0; i < N; i++) delete[] mi[i];
delete[] mi;
}
long long rmq(int l, int r) {
int j = log_two[r - l + 1];
return min(mi[l][j], mi[r - (1 << j) + 1][j]);
}
};
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;
vector<bool> forced(n + 2, true);
for (int i = 1; i <= n; i++) {
if (s[i - 1] == '0') {
forced[i] = false;
}
}
vector<long long> forward(n + 2);
// {cost, ind}, ind increasing and cost increasing
deque<pair<long long, int>> window;
window.emplace_back(0, 0);
for (int i = 1; i <= n + 1; i++) {
while (window.front().second < i - k) {
window.pop_front();
}
forward[i] = window.front().first + a[i];
if (forced[i]) {
window.clear();
} else {
while (!window.empty() && window.back().first >= forward[i]) {
window.pop_back();
}
}
window.emplace_back(forward[i], i);
}
window.clear();
vector<long long> backward(n + 2);
window.emplace_back(0, n + 1);
for (int i = n; i >= 0; i--) {
while (window.front().second > i + k) {
window.pop_front();
}
backward[i] = window.front().first + a[i];
if (forced[i]) {
window.clear();
} else {
while (!window.empty() && window.back().first >= backward[i]) {
window.pop_back();
}
}
window.emplace_back(backward[i], i);
}
SparseTable rmq_back(n + 2, backward);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int p, v;
cin >> p >> v;
if (forced[p]) {
cout << backward[0] + v - a[p] << "\n";
} else {
long long use = forward[p] + backward[p] - 2 * a[p] + v;
long long no_use = LONG_LONG_MAX;
for (int j = p - 1; j >= max(0, p - k + 1); j--) {
no_use = min(no_use, forward[j] + rmq_back.rmq(p + 1, min(j + k, n + 1)));
}
cout << min(use, no_use) << "\n";
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
log_two[1] = 0;
for (int i = 2; i < MAXN; i++) {
log_two[i] = log_two[i / 2] + 1;
}
int t;
cin >> t;
while (t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5800kb
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: 5552kb
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:
2472886431 2299111966 2796055445 2650202148 2417273966 2508694561 2285479513 1765265336 2318835744 2240907690 2577284958 2318835744 2766700195 2511807344 2318835744 2438434986 2669077215 2682112324 341886459 470735446 211705888 564509290 715543137 470735446 146144035 18 19 19 19 20 15 54 15465560 15...
result:
wrong answer 8th numbers differ - expected: '2521569560', found: '1765265336'