QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#698708#5415. RopewayblackfrogWA 2ms3668kbC++143.6kb2024-11-01 21:29:252024-11-01 21:29:27

Judging History

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

  • [2024-11-01 21:29:27]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3668kb
  • [2024-11-01 21:29:25]
  • 提交

answer

#include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;

int t,n,k,qu;

string s;

struct node{
    long long val, ord;
};

void solve(){
    deque<node> q;
    cin>>n>>k;
    vector<long long> a(n*2+10), predp(n*2+10), sufdp(n*2+10);
    for(int i = 1;i<=n;i++){
        cin>>a[i];
    }
    cin>>s;
    s = s + "  ";
    for(int i = 0;i<=n+1;i++){
        while(!q.empty() && q.front().ord < i-k)q.pop_front();
        if(i<=1)predp[i] = a[i];
        else{
            predp[i] = a[i] + q.front().val;
            if(s[i-1] == '1'){
                q.clear();
            }
            while(!q.empty() && q.back().val > predp[i])q.pop_back();   
        }
        q.push_back(node{predp[i], i});
    }
    q.clear();
    for(int i = n+1;i>=0;i--){
        while(!q.empty() && q.front().ord > i + k)q.pop_front();
        if(i>=n)sufdp[i] = a[i];
        else{
            sufdp[i] = a[i] + q.front().val;
            if(i !=0 &&s[i-1] == '1'){
                q.clear();
            }
            while(!q.empty() && q.back().val > sufdp[i])q.pop_back();   
        }
        q.push_back(node{sufdp[i], i});
    }
    cin>>qu;
    while(qu--){
        long long p,v;
        cin>>p>>v;
        if(s[p-1] == '1'){
            cout<<predp[n+1] - a[p]+v<<'\n';
            continue;
        }
        if(a[p]>=v)
        {
            long long l=max(0ll,p-k);
            long long prex=1e18;
            for(int i=p-1;i>=l;i--)
            {
                if(i!=0&&s[i-1]=='1')
                {
                    prex=predp[i];
                    break;
                }
                prex=min(prex,predp[i]);
            }
            long long r=min((long long)(n+1),p+k);
            long long sufx=1e18;
            for(int i=p+1;i<=r;i++)
            {
                if(i!=n+1&&s[i-1]=='1')
                {
                    sufx=sufdp[i];
                    break;
                }
                sufx=min(sufx,sufdp[i]);
            }
            cout<<min(v+prex+sufx,predp[n+1])<<'\n';
        }
        else
        {
            long long l=max(0ll,p-k+1);
            long long r=min((long long)n+1,p+k-1);
            long long res=1e18;
            q.clear();
            for(int i=l;i<p;i++)
            {
                if(i!=0&&s[i-1]=='1')
                {
                    l=i;
                }
            }
            for(int i=r;i>p;i--)
            {
                if(i!=n+1&&s[i-1]=='1')
                {
                    r=i;
                }
            }
            if(r-l<=k)
            {
                cout<<min(predp[l]+sufdp[r],predp[n+1]+v-a[p])<<'\n';
                continue;
            }
            else
            {
                int j=l+k;
                long long sufx=1e18;
                for(int i=p+1;i<=j;i++)
                {
                    sufx=min(sufx,sufdp[i]);
                }
                long long res=1e18;
                for(int i=l;i<p;i++)
                {
                    res=min(predp[i]+sufx,res);
                    //cout<<"!"<<i<<" "<<sufdp[i]<<" "<<sufx<<" "<<res<<endl;
                    if(j<=r)
                    {
                    j++;
                    sufx=min(sufx,sufdp[j]);
                    }
                }
                cout<<min(res,predp[n+1]+v-a[p])<<'\n';
                continue;
            }
        }
    }
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 3668kb

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:

2187682971
2013908506
2510851985
2364998688
2132070506
2223491101
2000276053
2067354834
2067354834
1955704230
2292081498
2236366100
2481496735
2226603884
2067354834
2153231526
2383873755
2396908864
173701552
146144035
211705888
564509290
715543137
167690056
470735446
18
19
19
19
20
19
54
849950346
8...

result:

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