QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#266858#5415. RopewaykhorayWA 3ms3820kbC++173.1kb2023-11-26 18:29:022023-11-26 18:29:03

Judging History

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

  • [2023-11-26 18:29:03]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3820kb
  • [2023-11-26 18:29:02]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define int long long
const int inf = 1e18;
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";
    vector<int> dp1(n + 2, inf), dp2(n + 2, inf);
    vector<int> last(n + 2), nex(n + 2);
    dp1[0] = 0;
    dp2[n + 1] = 0;
    for(int i = 0; i <= n + 1; i++) {
        if(s[i] == '1') {
            last[i] = i;
        } else {
            last[i] = last[i - 1];
        }
    }
    for(int i = n + 1; i >= 0; i--) {
        if(s[i] == '1') {
            nex[i] = i;
        } else {
            nex[i] = nex[i + 1];
        }
    }
    deque<int> dq;
    dq.push_back(0);
    for(int i = 1; i <= n + 1; i++) {
        int L = max(last[i - 1], i - k);
        // cerr << "i = " << i << " dq.front() = " << dq.front() << "\n";
        while(dq.front() < L) dq.pop_front();
        dp1[i] = dp1[dq.front()] + a[i];
        while(dq.size() && dp1[dq.back()] >= dp1[i]) dq.pop_back();
        dq.push_back(i);
    }
    dq.clear();
    dq.push_back(n + 1);
    for(int i = n; i >= 0; i--) {
        int R = min(nex[i + 1], i + k);
        while(dq.front() > R) dq.pop_front();
        dp2[i] = dp2[dq.front()] + a[i];
        while(dq.size() && dp2[dq.back()] >= dp2[i]) dq.pop_back();
        dq.push_back(i);
    }
    // cerr << dp2[0] << "\n";
    int q; cin >> q;
    while(q--) {
        int p, x; cin >> p >> x;
        int prea = a[p];
        a[p] = x;
        deque<int> dq1;
        deque<int> dq2;
        vector<int> nowdp(k);
        auto getdpval = [&] (int idx) {
            if(idx >= p) return nowdp[idx - p];
            else return dp1[idx];
        };
        int pt2 = p + 1;
        int ans = inf;
        for(int i = max(0LL, p - k); i <= min(n + 1, p + k - 1); i++) {
            // cerr << "i = " << i << "\n";
            if(i >= p) {
                int L = max(last[i - 1], i - k);
                while(dq1.size() && dq1.front() < L) dq1.pop_front();
                nowdp[i - p] = getdpval(dq1.front()) + a[i];
                // cerr << "dp[i] = " << getdpval(i) << "\n";
                if(i <= n) {
                        // cerr << "pt2 = " << pt2 << " i = " << i << " dqb = " << nex[i + 1] << "\n";
                    while(pt2 <= n + 1 && pt2 <= min(nex[i + 1], i + k)) {
                        while(dq2.size() && dp2[dq2.back()] >= dp2[pt2]) dq2.pop_back();
                        dq2.push_back(pt2);
                        pt2++;
                    }
                    while(dq2.size() && dq2.front() <= i) dq2.pop_front();
                    ans = min(ans, getdpval(i) + dp2[dq2.front()]);
                } else {
                    ans = min(ans, getdpval(i));
                }
            }
            while(dq1.size() && getdpval(dq1.back()) >= getdpval(i)) dq1.pop_back();
            dq1.push_back(i);
        }
        cout << ans << "\n";
    }
}   

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    int t; cin >> t;
    while(t--) solve();
    return 0;
}

/*
4 5 3
ULDDRR
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3820kb

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: 3640kb

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
2379061425
2417273966
2355715838
2285479513
2285479513
2521569560
2075053968
2341194911
2521569560
2530610148
2345953622
2521569560
2438434986
2669077215
2682112324
470735446
470735446
211705888
564509290
715543137
470735446
564509290
18
18
18
18
20
19
54
849950346
8...

result:

wrong answer 4th numbers differ - expected: '2650202148', found: '2379061425'