QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#317816#5415. RopewayduckindogWA 5ms11860kbC++142.7kb2024-01-29 19:43:122024-01-29 19:43:12

Judging History

你现在查看的是最新测评结果

  • [2024-01-29 19:43:12]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:11860kb
  • [2024-01-29 19:43:12]
  • 提交

answer

//from duckindog wth depression
#include<bits/stdc++.h>

using namespace std;

#define int long long

const int N = 5e5 + 10;
int n, k;
int a[N];
string s;
int f[N], g[N], h[N], j[N];
int q;

void harm() {
  deque<int> dq;
  dq.push_back(0);
  for (int i = 1; i <= n; ++i) {
    while (dq.size() && dq.back() < i - k) dq.pop_back();
    f[i] = f[dq.back()] + a[i];
    while (dq.size() && f[dq.front()] >= f[i]) dq.pop_front();
    if (s[i] == '1') dq.clear();
    dq.push_front(i);
  }

  dq.clear(); dq.push_back(n + 1);
  for (int i = n; i >= 1; --i) {
    while (dq.size() && dq.back() > i + k) dq.pop_back();
    g[i] = g[dq.back()] + a[i];
    while (dq.size() && g[dq.front()] >= g[i]) dq.pop_front();
    if (s[i] == '1') dq.clear();
    dq.push_front(i);
  }

}

int32_t main() {
  cin.tie(0)->sync_with_stdio(0);

  if (fopen("duck.inp", "r")) {
    freopen("duck.inp", "r", stdin);
    freopen("duck.out", "w", stdout);
  }
  int test; cin >> test;
  while (test--) {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    a[n + 1] = f[n + 1] = g[n + 1] = 0;

    cin >> s; s = '@' + s;
    harm();

    cin >> q;
    while (q--) {
      int p, v; cin >> p >> v;
      int pre = a[p]; a[p] = v;
      for (int i = p; i <= min(n, p + k); ++i) h[i] = f[i];
      for (int i = min(n, p + k); i >= p; --i) j[i] = g[i];

      deque<int> dq; dq.push_back(min(n + 1, p + k + 1));
      for (int i = min(n, p + k); i >= max(0ll, p - k - 1); --i) {
        while (dq.size() && dq.back() > i + k) dq.pop_back();
        g[i] = g[dq.back()] + a[i];
        while (dq.size() && g[dq.front()] >= g[i]) dq.pop_front();
        if (s[i] == '1') dq.clear();
        dq.push_front(i);
      }
      dq.clear();

      for (int i = max(0ll, p - k - 1); i < p; ++i) {
        while (dq.size() && f[dq.front()] >= f[i]) dq.pop_front();
        if (s[i] == '1') dq.clear();
        dq.push_front(i);
      }

      for (int i = p; i <= min(n, p + k); ++i) {
        while (dq.size() && dq.back() < i - k) dq.pop_back();
        f[i] = f[dq.back()] + a[i];
        while (dq.size() && f[dq.front()] >= f[i]) dq.pop_front();
        if (s[i] == '1') dq.clear();
        dq.push_front(i);
      }

      int answer = 1e15;
      while (dq.size()) {
        int it = dq.back(), mi = 1e15;
        for (int i = it + 1; i <= min(n + 1, it + k + 1); ++i) {
          mi = min(mi, g[i]);
          if (s[i] == '1') break;
        }
        answer = min(answer, f[it] + mi);
        dq.pop_back();
      }
      cout << answer << "\n";
      for (int i = p; i <= min(n, p + k); ++i) f[i] = h[i];
      for (int i = min(n, p + k); i >= max(0ll, p - k - 1); --i) g[i] = j[i];
      a[p] = pre;
    }
  }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11736kb

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: 5ms
memory: 11860kb

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
1902547460
2508694561
2285479513
2521569560
2521569560
2240907690
2577284958
2521569560
2766700195
2511807344
2521569560
2209026003
2669077215
2682112324
470735446
470735446
211705888
564509290
715543137
470735446
470735446
18
19
19
19
20
586300163
54
8499...

result:

wrong answer 5th numbers differ - expected: '2417273966', found: '1902547460'