QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#317788#5415. RopewayVinhLuuWA 3ms16144kbC++203.8kb2024-01-29 18:25:282024-01-29 18:25:28

Judging History

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

  • [2024-01-29 18:25:28]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:16144kb
  • [2024-01-29 18:25:28]
  • 提交

answer

// happy

//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <bits/stdc++.h>
#define int long long
//#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
//typedef tuple<int,int,int> tp;
const int N = 1e6 + 2;
const int oo = 1e16;
//const int mod = 1e9 + 7;
//const int base = 29; // >= so pt pb max

int n, k, a[N], f[N], g[N], dp[2][N], L[N], R[N];

string s;

void solve(){
    cin >> n >> k;

    for(int i = 1; i <= n; i ++) cin >> a[i];

    cin >> s;
    s = " " + s;

    int dem = 0;

    deque<pii> dq;
    int tmp = 0;
    dq.push_back({0, 0});
    for(int i = 1; i <= n; i ++){
        while(!dq.empty() && dq.front().fi < i - k || dq.front().fi < tmp) dq.pop_front();

        f[i] = dq.front().se + a[i];
        while(!dq.empty() && dq.back().se > f[i]) dq.pop_back();
        dq.push_back({i, f[i]});
        if(s[i] == '1') tmp = i, dem += a[i];

        L[i] = tmp;

    }
    while(!dq.empty()) dq.pop_front();
    tmp = n + 1;
    dq.push_back({n + 1, 0});
    for(int i = n; i >= 1; i --){
        while(!dq.empty() && dq.front().fi > i + k || dq.front().fi > tmp) dq.pop_front();
        g[i] = dq.front().se + a[i];
        while(!dq.empty() && dq.back().se > f[i]) dq.pop_back();
        dq.push_back({i, g[i]});
        if(s[i] == '1') tmp = i;
        R[i] = tmp;

    }

    int q; cin >> q;

    while(q--){
        int x, v; cin >> x >> v;
        if(k > n){
            cout << dem + (s[x] == '1' ? v - a[x] : 0) << "\n";
            continue;
        }
        int now = a[x];
        a[x] = v;
        int l = max({0ll, x - k, L[x]});
        int r = min({n + 1, x + k, R[x]});
        int ret = oo, cnt = a[x];

//        for(int i = l; i <= r; i ++) dp[0][i] = dp[1][i] = 0;

        while(!dq.empty()) dq.pop_front();


        for(int i = l; i <= x - 1; i ++){
            dp[0][i] = f[i];
            while(!dq.empty() && dq.front().fi < i - k) dq.pop_front();
            while(!dq.empty() && dq.back().se > dp[0][i]) dq.pop_back();
            dq.push_back({i, dp[0][i]});
        }
        for(int i = x; i <= min(r, n); i ++){
            while(!dq.empty() && dq.front().fi < i - k) dq.pop_front();
            dp[0][i] = dq.front().se + a[i];
//            cout << i << " " << dq.front().se + g[i] << " hhh\n";
            if(i > x) ret = min(ret, dq.front().se + g[i]);
            else cnt += dq.front().se;
            while(!dq.empty() && dq.back().se > dp[0][i]) dq.pop_back();
            dq.push_back({i, dp[0][i]});
        }
        while(!dq.empty()) dq.pop_front();

        for(int i = r; i >= x + 1; i --){
            dp[1][i] = g[i];
            while(!dq.empty() && dq.front().fi > i + k) dq.pop_front();
            while(!dq.empty() && dq.back().se > dp[1][i]) dq.pop_back();
            dq.push_back({i, dp[1][i]});
        }

        for(int i = x; i >= max(l, 1ll); i --){
            while(!dq.empty() && dq.front().fi > i + k) dq.pop_front();
            dp[1][i] = dq.front().se + a[i];
//            cout << i << " " << dq.front().se + f[i] << " hhh\n";
            if(i < x) ret = min(ret, dq.front().se + f[i]);
            else cnt += dq.front().se;
            while(!dq.empty() && dq.back().se > dp[1][i]) dq.pop_back();
            dq.push_back({i, dp[0][i]});
        }


//        cout << cnt << " g\n";

        ret = min(ret, cnt);

        cout << ret << "\n";

        a[x] = now;
    }
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #define task "v"
    if(fopen(task ".inp","r")){
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }

    int t; cin >> t;
    while(t--) solve();


}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 15880kb

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: 0ms
memory: 16144kb

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:

127056246
100630226
301161052
331781882
218792226
190274295
49227476
2521569560
2521569560
4541590
299661840
2521569560
297105109
1048997458
2521569560
3614073462
4137898692
4628557661
470735446
937839013
234573119
1147938151
1080868551
937839013
470735446
4672806320
2521569561
297105111
163439085
4...

result:

wrong answer 1st numbers differ - expected: '2472886431', found: '127056246'