Submission #1037366

# Submission time Handle Problem Language Result Execution time Memory
1037366 2024-07-28T15:22:04 Z TrinhKhanhDung Measures (CEOI22_measures) C++14
100 / 100
177 ms 40020 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

struct SegmentTree{
    struct Node{
        ll mi, ma, ans, lazy;
        bool exist;
        Node(){
            mi = 0;
            ma = 0;
            ans = 0;
            lazy = 0;
            exist = false;
        }
    };
    vector<Node> it;
    int n;

    SegmentTree(int _n = 0){
        n = _n;
        it.resize(n * 4 + 3);
    }

    void push(int i){
        if(it[i].lazy == 0) return;
        if(it[i * 2].exist){
            it[i * 2].mi += it[i].lazy;
            it[i * 2].ma += it[i].lazy;
            it[i * 2].lazy += it[i].lazy;
        }
        if(it[i * 2 + 1].exist){
            it[i * 2 + 1].mi += it[i].lazy;
            it[i * 2 + 1].ma += it[i].lazy;
            it[i * 2 + 1].lazy += it[i].lazy;
        }
        it[i].lazy = 0;
    }

    Node combine(Node a, Node b){
        Node ans;
        ans.mi = oo; ans.ma = -oo;
        if(a.exist){
            minimize(ans.mi, a.mi);
            maximize(ans.ma, a.ma);
            maximize(ans.ans, a.ans);
        }
        if(b.exist){
            minimize(ans.mi, b.mi);
            maximize(ans.ma, b.ma);
            maximize(ans.ans, b.ans);
        }
        if(a.exist && b.exist){
            maximize(ans.ans, a.ma - b.mi);
        }
        ans.exist = a.exist || b.exist;
        return ans;
    }

    void update(int i, int l, int r, int u, int v, ll c, bool ok){
        if(r < u || v < l) return;
        if(ok && l == r){
            it[i].mi += c;
            it[i].ma += c;
            it[i].exist = true;
            return;
        }
        if(u <= l && r <= v){
            if(it[i].exist){
                it[i].mi += c;
                it[i].ma += c;
                it[i].lazy += c;
            }
            return;
        }
        push(i);
        int m = (l + r) >> 1;
        update(i * 2, l, m, u, v, c, ok);
        update(i * 2 + 1, m + 1, r, u, v, c, ok);
        it[i] = combine(it[i * 2], it[i * 2 + 1]);
    }

    void update(int u, int v, ll c, bool ok){
        update(1, 1, n, u, v, c, ok);
    }
};

struct FenwickTree{
    vector<int> tree;
    int n;

    FenwickTree(int _n = 0){
        n = _n;
        tree.resize(n + 3, 0);
    }

    void update(int p, int c){
        while(p <= n){
            tree[p] += c;
            p += p & -p;
        }
    }

    int getVal(int p){
        int ans = 0;
        while(p > 0){
            ans += tree[p];
            p -= p & -p;
        }
        return ans;
    }
};

void solve(){
//    SegmentTree it(5);
//    it.update(1, 1, -2);
//    it.update(2, 5, -3);
//    cout << it.it[1].ans << '\n';
//    it.update(2, 2, -4);
//    it.update(3, 5, -3);
//    cout << it.it[1].ans << '\n';
//    it.update(3, 3, -6);
//    it.update(4, 5, -3);
//    cout << it.it[1].ans << '\n';


    int N, Q, D;
    cin >> N >> Q >> D;

    vector<int> a(N+Q), order(N+Q), pos(N+Q);
    for(int i=0; i<N+Q; i++){
        cin >> a[i];
        order[i] = i;
    }
    sort(ALL(order), [&](int i, int j){return a[i] < a[j];});
    for(int i=0; i<N+Q; i++){
        pos[order[i]] = i + 1;
    }
    SegmentTree it(N+Q);
    FenwickTree bit(N+Q);
    for(int i=0; i<N+Q; i++){
        bit.update(pos[i], 1);
        int p = bit.getVal(pos[i]);
        it.update(pos[i], pos[i], a[i] - 1LL * p * D, true);
//        cout << p << ' ' << a[i] << '\n';
        it.update(pos[i] + 1, N+Q, -D, false);

        if(i + 1 > N){
            ll res = it.it[1].ans;
            if(res & 1){
                cout << res/2 << ".5 ";
            }
            else{
                cout << res/2 << ' ';
            }
        }
    }
}

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("task.inp", "r", stdin);
//    freopen("task.out", "w", stdout);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 155 ms 36832 KB Output is correct
10 Correct 157 ms 36688 KB Output is correct
11 Correct 108 ms 36692 KB Output is correct
12 Correct 131 ms 36732 KB Output is correct
13 Correct 111 ms 36380 KB Output is correct
14 Correct 112 ms 36952 KB Output is correct
15 Correct 158 ms 35932 KB Output is correct
16 Correct 105 ms 36692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 35924 KB Output is correct
2 Correct 116 ms 37716 KB Output is correct
3 Correct 121 ms 39580 KB Output is correct
4 Correct 111 ms 37456 KB Output is correct
5 Correct 115 ms 38736 KB Output is correct
6 Correct 110 ms 37972 KB Output is correct
7 Correct 106 ms 38888 KB Output is correct
8 Correct 107 ms 37456 KB Output is correct
9 Correct 96 ms 37460 KB Output is correct
10 Correct 117 ms 40020 KB Output is correct
11 Correct 113 ms 38280 KB Output is correct
12 Correct 115 ms 39252 KB Output is correct
13 Correct 109 ms 37456 KB Output is correct
14 Correct 111 ms 39504 KB Output is correct
15 Correct 115 ms 39364 KB Output is correct
16 Correct 102 ms 36996 KB Output is correct
17 Correct 112 ms 38796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 35924 KB Output is correct
2 Correct 116 ms 37716 KB Output is correct
3 Correct 121 ms 39580 KB Output is correct
4 Correct 111 ms 37456 KB Output is correct
5 Correct 115 ms 38736 KB Output is correct
6 Correct 110 ms 37972 KB Output is correct
7 Correct 106 ms 38888 KB Output is correct
8 Correct 107 ms 37456 KB Output is correct
9 Correct 96 ms 37460 KB Output is correct
10 Correct 117 ms 40020 KB Output is correct
11 Correct 113 ms 38280 KB Output is correct
12 Correct 115 ms 39252 KB Output is correct
13 Correct 109 ms 37456 KB Output is correct
14 Correct 111 ms 39504 KB Output is correct
15 Correct 115 ms 39364 KB Output is correct
16 Correct 102 ms 36996 KB Output is correct
17 Correct 112 ms 38796 KB Output is correct
18 Correct 173 ms 37820 KB Output is correct
19 Correct 166 ms 39524 KB Output is correct
20 Correct 121 ms 39764 KB Output is correct
21 Correct 155 ms 37712 KB Output is correct
22 Correct 145 ms 37928 KB Output is correct
23 Correct 119 ms 37720 KB Output is correct
24 Correct 177 ms 38312 KB Output is correct
25 Correct 110 ms 37360 KB Output is correct
26 Correct 169 ms 37460 KB Output is correct
27 Correct 165 ms 39924 KB Output is correct
28 Correct 140 ms 37968 KB Output is correct
29 Correct 150 ms 39252 KB Output is correct
30 Correct 161 ms 37460 KB Output is correct
31 Correct 155 ms 39532 KB Output is correct
32 Correct 132 ms 39252 KB Output is correct
33 Correct 164 ms 37200 KB Output is correct
34 Correct 158 ms 38800 KB Output is correct