QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#317818#5415. RopewayduckindogTL 1816ms24208kbC++142.7kb2024-01-29 19:46:112024-01-29 19:46:11

Judging History

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

  • [2024-01-29 19:46:11]
  • 评测
  • 测评结果:TL
  • 用时:1816ms
  • 内存:24208kb
  • [2024-01-29 19:46:11]
  • 提交

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 >= max(0ll, p - k - 1); --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); ++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: 0ms
memory: 11848kb

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: 0
Accepted
time: 5ms
memory: 12096kb

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
2521569560
2521569560
2240907690
2577284958
2521569560
2766700195
2511807344
2521569560
2438434986
2669077215
2682112324
470735446
470735446
211705888
564509290
715543137
470735446
470735446
18
19
19
19
20
19
54
849950346
8...

result:

ok 5277 numbers

Test #3:

score: 0
Accepted
time: 16ms
memory: 12096kb

input:

50
183 160
22617237 21369258 39718669 4582413 28629717 25947766 1690897 19844235 35568283 42909820 21829213 70588436 8472484 53107624 21547259 11440081 6630789 5896457 19770793 30204510 29796593 27689811 39350369 21646987 36362667 38164292 13428410 54681069 24526477 58941286 70890011 43730474 388615...

output:

5303746210
5262479222
5346682277
5299354906
5290045662
5350552810
5343191051
5268759163
5299354906
5340227887
5299354906
5253956732
5289945955
5230918240
5287812187
5299354906
5299354906
5273800386
5299354906
5299354906
5353471727
5283662384
5230330060
5319166005
5338060400
5304825609
5268929476
526...

result:

ok 9541 numbers

Test #4:

score: 0
Accepted
time: 708ms
memory: 24208kb

input:

3
500000 891
806445768 895003544 461699845 184479965 853541910 593542573 667984604 465354540 303586741 243665567 285056069 294245042 675066412 456713460 215929340 756894934 773828574 317143965 451366539 891390118 436168373 633752416 78781758 298770626 35280548 226923750 96421159 523108871 265102517 ...

output:

5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
5202946193
520...

result:

ok 9000 numbers

Test #5:

score: 0
Accepted
time: 1528ms
memory: 24056kb

input:

4
500000 3000
303543765 129078653 192039183 226458565 208138322 436645868 210834986 135457074 162914826 137413668 192916902 146302909 434058637 370665799 433071720 460608259 413021067 44008462 416238964 258228162 317315155 28299542 422136622 102236305 489978791 223662501 132197157 47793593 265792210...

output:

50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
50490529
504...

result:

ok 10000 numbers

Test #6:

score: 0
Accepted
time: 1816ms
memory: 24148kb

input:

3
500000 895
688 1201 2221 3857 4357 4465 8482 8585 9839 11049 11429 11462 11467 12642 13620 14171 14220 14419 15332 17522 18107 18753 18851 18863 18915 20273 20374 20482 22320 22934 24333 25223 25947 26879 27015 27794 27850 29892 31082 31103 35023 35611 36010 36365 36452 36708 36830 37302 39844 427...

output:

102387246746
102601622426
102404770741
102607362161
102607362161
102607362161
102475496180
102558851848
102607403181
102607362161
102427271560
102593532165
102583224195
102607362161
102607362161
102607362161
102605413877
102607362161
102607362161
102607362161
102607362161
102607362161
102607362161
1...

result:

ok 9000 numbers

Test #7:

score: -100
Time Limit Exceeded

input:

4
500000 3000
807 3313 3431 3532 3653 4229 4574 5708 6325 7571 8354 11026 11363 17596 18919 20930 23420 25860 26462 27514 29428 30001 31416 33806 35417 35902 36640 37602 38082 40496 43170 44783 45474 46146 48538 49134 49711 55502 56776 56805 57172 62132 62994 66887 69086 70397 70840 71041 74735 7581...

output:


result: