QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#349264#6564. Frequent FlierjanY#AC ✓54ms10520kbC++202.1kb2024-03-10 00:30:242024-03-10 00:30:24

Judging History

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

  • [2024-03-10 00:30:24]
  • 评测
  • 测评结果:AC
  • 用时:54ms
  • 内存:10520kb
  • [2024-03-10 00:30:24]
  • 提交

answer

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

struct fenwick {
    ll* bit;
    int fsize;

    void init(int n){
        bit = new ll[n+1];
        fsize = n;
        for (int i = 1; i <= n; i++){
            bit[i] = 0;
        }
    }

    int lsb(int x){
        return x&(-x);
    }

    ll query(int v){
        ll sum = 0;
        while (v > 0){
            sum += bit[v];
            v -= lsb(v);
        }
        return sum;
    }

    void add(int v, ll delta){
        v++;
        while (v <= fsize){
            bit[v] += delta;
            v += lsb(v);
        }
    }

    ll calc(int l, int r){
        return query(r+1) - query(l);
    }
};

void solve(){
    ll n, m, x;
    cin >> n >> m >> x;

    vector<ll> z(n);
    for (int i = 0; i < n; i++){
        cin >> z[i];
    }
    for (int i = 0; i < m+6; i++){
        z.push_back(0);
    }
    n += m+5;

    fenwick fn;
    fn.init(n);

    deque<pair<ll,ll>> q;

    for (ll i = 0; i < n; i++){
        q.push_front({z[i], i});
        ll lb = max(0ll, i+1-m);
        ll have = fn.calc(lb, i);
        while (q.size()){
            pair<ll, ll> bck = q.back();
            if (bck.second > i-m) break;
            q.pop_back();
        }
        //cout << i << ": ";
        //for (int i = 0; i < n; i++){
         //   cout << fn.calc(i, i) << " ";
        //}
        //cout << "\n";
        //cout << lb << " " << i << " " << have << "?";
        ll need = x-have;
        if (need <= 0) continue;
        while (q.size() && need > 0){
            pair<ll,ll> frnt = q.front();
            q.pop_front();
            ll by = min(frnt.first, need);
            frnt.first -= by;
            need -= by;
            if (frnt.first > 0) q.push_front(frnt);
            fn.add(frnt.second, by);
        }

    }
    //cout << "\n";
    //for (int i = 0; i < n; i++){
     //   cout << fn.calc(i, i) << " ";
    //}
    //cout << "\n";
    cout << fn.calc(0, n-1) << "\n";

}

int main(){

    int t = 1;
    //cin >> t;
    for (int i = 0; i < t; i++) solve();

    return 0;
}


詳細信息

Test #1:

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

input:

8 3 2
3
1
4
1
5
9
2
6

output:

8

result:

ok single line: '8'

Test #2:

score: 0
Accepted
time: 54ms
memory: 6296kb

input:

200000 2467 999931035
182548858
69876218
33328350
919486767
739850600
948199964
392292320
39077742
366752074
917496841
246661698
37219034
56677740
188278971
965701828
28059790
13200243
825730775
542597589
320715170
939054378
470563899
914247467
990100491
290827128
903662394
611104879
18631185
412134...

output:

82994275905

result:

ok single line: '82994275905'

Test #3:

score: 0
Accepted
time: 8ms
memory: 6304kb

input:

1 200000 999959273
1255319

output:

1255319

result:

ok single line: '1255319'

Test #4:

score: 0
Accepted
time: 49ms
memory: 10520kb

input:

200000 118880 996878327
993340390
999483057
808153702
337349872
863222189
7495963
995920883
12950768
958082368
993215196
967152406
1388062
949959944
836952150
964071667
5291
139115263
958470154
51295691
175385
925242139
995685554
10895812
12563
55482
479983443
42805
996241239
6228013
302633329
10331...

output:

2990634981

result:

ok single line: '2990634981'

Test #5:

score: 0
Accepted
time: 50ms
memory: 7232kb

input:

200000 15 44049967
999927791
29351
999894087
687373343
518282902
19725626
452930
6701
999697321
27032796
5498
259987
999999980
485673990
805879
315651507
166801295
189865616
999910671
6203805
978801777
41916529
942904087
997589912
17146790
922137016
444882952
999884603
999926345
938979650
18597283
9...

output:

598769574112

result:

ok single line: '598769574112'