/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 11ms 28.055 MiB
#2 Accepted 28ms 28.324 MiB
#3 Accepted 41ms 28.207 MiB
#4 Accepted 56ms 28.219 MiB
#5 Accepted 66ms 28.02 MiB
#6 Accepted 82ms 28.297 MiB
#7 Accepted 31ms 29.586 MiB
#8 Accepted 123ms 29.59 MiB
#9 Accepted 124ms 29.566 MiB
#10 Accepted 91ms 29.562 MiB
#11 Accepted 124ms 29.52 MiB
#12 Accepted 126ms 29.602 MiB

Code

/**
 *    author:   Binoy Barman
 *    created:  2024-12-10 15:28:18
**/

#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
#define all(v) v.begin(), v.end()
#define Too_Many_Jobs int tts, tc = 1; cin >> tts; hell: while(tts--)
#define Dark_Lord_Binoy ios_base::sync_with_stdio(false); cin.tie(NULL);

#ifdef LOCAL
#include "debug/whereisit.hpp"
#else
#define dbg(...) 42
#endif
#define int long long

const int N = 3e5 + 9, inf = 2e9;

int a[N];
struct Node { // change this
    int mn, mx, sum;
    Node() {
        sum = 0;
        mn = inf;
        mx = -inf;
    }
};
struct SegmentTree {
    #define lc (n << 1)
    #define rc ((n << 1) | 1)
    #define out LLONG_MIN // change this
    vector<Node> t;
    SegmentTree() {
        t.resize(4 * N);
    }
    inline void pull(int n) { // change this
        t[n].mx = max(t[lc].mx, t[rc].mx);
        t[n].mn = min(t[lc].mn, t[rc].mn);
        t[n].sum = t[lc].sum + t[rc].sum;
    }
    inline Node combine(Node a, Node b) { // change this
        if(a.sum == out) return b;
        if(b.sum == out) return a;
        Node n;
        n.mx = max(a.mx, b.mx);
        n.mn = min(a.mn, b.mn);
        n.sum = a.sum + b.sum;
        return n;
    }
    void build(int n, int b, int e) {
        if (b == e) {
            t[n].mn = t[n].mx = t[n].sum = a[b];  // change this
            return;
        }
        int mid = (b + e) >> 1;
        build(lc, b, mid);
        build(rc, mid + 1, e);
        pull(n);
    }
    Node query(int n, int b, int e, int i, int j) {
        if (b > j || e < i) { 
            Node x;
            x.sum = out; 
            return x; // return approriate value
        }
        if (b >= i && e <= j) return t[n];
        int mid = (b + e) >> 1;
        return combine(query(lc, b, mid, i, j), query(rc, mid + 1, e, i, j));
    }
}t;

int32_t main() {
Dark_Lord_Binoy
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    Too_Many_Jobs {
        int n, k;
        cin >> n >> k;
        vector<int> ps(n + 1);
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            ps[i] = ps[i - 1] + a[i];
        }
        t.build(1, 1, n);
        int ans = ps[k];
        for (int i = 1; i + k - 1 <= n; i++) {
            Node x = t.query(1, 1, n, i, i + k - 1);
            int mn = min(t.query(1, 1, n, 1, i - 1).mn, t.query(1, 1, n, i + k, n).mn);
            int sum = min(x.sum, x.sum - x.mx + mn);
            ans = min(sum, ans);
        }
        cout << ans << nl;
    }
    
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1149 Swap and Minimize
Contest
LU IUJPC : Sylhet Division 2024 Replay Contest
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-10 09:51:44
Judged At
2024-12-10 09:51:44
Judged By
Score
100
Total Time
126ms
Peak Memory
29.602 MiB