Submission #492345

# Submission time Handle Problem Language Result Execution time Memory
492345 2021-12-06T23:17:02 Z maksim1744 Distributing Candies (IOI21_candies) C++17
27 / 100
1063 ms 46804 KB
/*
    author:  Maksim1744
    created: 07.12.2021 02:14:12
*/

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T> &v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T> &v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T> &v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>  v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U> &p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>  p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "/mnt/c/Libs/tools/print.cpp"
#else
#define show(...) void(0)
#define debugf(fun)   fun
#define debugv(var)   var
#define mclock    void(0)
#define shows     void(0)
#define debug  if (false)
#endif

namespace segtree_ns {

// supports min=, max=, += on a segment, maintains sum on a segment

const ll inf = 1e18;

struct mod_max { ll v; };
struct mod_min { ll v; };
struct mod_add { ll v; };

struct item {
    ll mn1, mn2, mx1, mx2, sm;
    ll cmn1, cmx1;
    mod_add madd{0};

    template<typename T>
    void init(const T &t, int l, int r) {
        mn1 = mx1 = t;
        mn2 = inf;
        mx2 = -inf;
        cmn1 = cmx1 = r - l + 1;
        sm = t * cmn1;
    }

    void update(const item &first, const item &second, int l, int r) {
        sm = first.sm + second.sm;

        mn1 = min(first.mn1, second.mn1);
        if (first.mn1 < second.mn1) cmn1 = first.cmn1;
        else if (first.mn1 > second.mn1) cmn1 = second.cmn1;
        else cmn1 = first.cmn1 + second.cmn1;

        mx1 = max(first.mx1, second.mx1);
        if (first.mx1 > second.mx1) cmx1 = first.cmx1;
        else if (first.mx1 < second.mx1) cmx1 = second.cmx1;
        else cmx1 = first.cmx1 + second.cmx1;

        mn2 = min(mn1 ==  first.mn1 ?  first.mn2 :  first.mn1,
                  mn1 == second.mn1 ? second.mn2 : second.mn1);
        mx2 = max(mx1 ==  first.mx1 ?  first.mx2 :  first.mx1,
                  mx1 == second.mx1 ? second.mx2 : second.mx1);
    }

    static item merge(const item &first, const item &second, int l, int r) {
        item res;
        res.update(first, second, l, r);  // careful with different lengths
        return res;
    }

    // return true if modification can be applied
    bool modify(const mod_add &md, int l, int r) {
        ll m = md.v;
        sm += m * (r - l + 1);
        mn1 += m;
        mn2 += m;
        mx1 += m;
        mx2 += m;
        madd.v += m;
        return true;
    }

    // return true if modification can be applied
    bool modify(const mod_min &md, int l, int r) {
        ll m = md.v;

        if (m >= mx1)
            return true;

        if (m <= mn1) {
            init(m, l, r);
            return true;
        }
        if (m > mx2) {
            sm += cmx1 * (m - mx1);
            if (mx1 == mn2)
                mn2 = m;
            mx1 = m;
            return true;
        }
        return false;
    }

    // return true if modification can be applied
    bool modify(const mod_max &md, int l, int r) {
        ll m = md.v;

        if (m <= mn1)
            return true;

        if (m >= mx1) {
            init(m, l, r);
            return true;
        }
        if (m < mn2) {
            sm += cmn1 * (m - mn1);
            if (mn1 == mx2)
                mx2 = m;
            mn1 = m;
            return true;
        }
        return false;
    }

    void push(item &first, item &second, int l, int r) {
        int m = (l + r) / 2;
        if (mn1 == mx1) {
            first.init(mn1, l, m);
            second.init(mn1, m + 1, r);
        } else {
            first.modify(madd, l, m);
            first.modify(mod_min{mx1}, l, m);
            first.modify(mod_max{mn1}, l, m);
            second.modify(madd, m + 1, r);
            second.modify(mod_min{mx1}, m + 1, r);
            second.modify(mod_max{mn1}, m + 1, r);
        }
        madd.v = 0;
    }
};

string to_string(const item &i) {
    stringstream ss;
    ss << "[" << "]";
    return ss.str();
}
ostream& operator << (ostream &o, const item &i) {
    return o << to_string(i);
}

struct segtree {
    vector<item> tree;
    int n = 1;

    segtree(int n = 1) : n(n) {
        tree.resize(1 << (__lg(max(1, n - 1)) + 2));
    }

    template<typename T>
    void build(const vector<T> &v, int i, int l, int r) {
        if (l == r) {
            tree[i].init(v[l], l, r);
            return;
        }
        int m = (l + r) >> 1;
        build(v, i * 2 + 1, l, m);
        build(v, i * 2 + 2, m + 1, r);
        tree[i].update(tree[i * 2 + 1], tree[i * 2 + 2], l, r);
    }

    template<typename T>
    void build(const vector<T> &v) {
        n = v.size();
        tree.resize(1 << (__lg(max(1, n - 1)) + 2));
        build(v, 0, 0, n - 1);
    }

    item ask(int l, int r, int i, int vl, int vr) {
        if (vl != vr) {
            tree[i].push(tree[i * 2 + 1], tree[i * 2 + 2], vl, vr);
        }
        if (l == vl && r == vr) {
            return tree[i];
        }
        int m = (vl + vr) >> 1;
        if (r <= m) {
            return ask(l, r, i * 2 + 1, vl, m);
        } else if (m < l) {
            return ask(l, r, i * 2 + 2, m + 1, vr);
        } else {
            return item::merge(ask(l, m, i * 2 + 1, vl, m), ask(m + 1, r, i * 2 + 2, m + 1, vr), l, r);
        }
    }

    item ask(int l, int r) {
        l = max(l, 0); r = min(r, n - 1);
        if (l > r) return item();
        return ask(l, r, 0, 0, n - 1);
    }

    template<typename T>
    void set(int ind, const T &t) {
        static array<pair<int, int>, 30> st;
        int l = 0, r = n - 1, i = 0;
        int ptr = -1;
        while (l != r) {
            if (l != r) {
                tree[i].push(tree[i * 2 + 1], tree[i * 2 + 2], l, r);
            }
            st[++ptr] = {l, r};
            int m = (l + r) >> 1;
            if (ind <= m) {
                i = i * 2 + 1;
                r = m;
            } else {
                i = i * 2 + 2;
                l = m + 1;
            }
        }
        tree[i].init(t, l, r);
        while (i != 0) {
            i = (i - 1) / 2;
            tree[i].update(tree[i * 2 + 1], tree[i * 2 + 2], st[ptr].first, st[ptr].second);
            --ptr;
        }
    }

    template<typename Modifier>
    void modify(int l, int r, const Modifier &modifier, int i, int vl, int vr) {
        if (vl != vr) {
            tree[i].push(tree[i * 2 + 1], tree[i * 2 + 2], vl, vr);
        }
        if (l == vl && r == vr && tree[i].modify(modifier, vl, vr)) {
            return;
        }
        int m = (vl + vr) >> 1;
        if (r <= m) {
            modify(l, r, modifier, i * 2 + 1, vl, m);
        } else if (m < l) {
            modify(l, r, modifier, i * 2 + 2, m + 1, vr);
        } else {
            modify(l, m, modifier, i * 2 + 1, vl, m);
            modify(m + 1, r, modifier, i * 2 + 2, m + 1, vr);
        }
        tree[i].update(tree[i * 2 + 1], tree[i * 2 + 2], vl, vr);
    }

    template<typename Modifier>
    void modify(int l, int r, const Modifier &modifier) {
        l = max(l, 0); r = min(r, n - 1);
        if (l > r) return;
        modify(l, r, modifier, 0, 0, n - 1);
    }
};
}
using segtree_ns::segtree, segtree_ns::mod_min, segtree_ns::mod_max, segtree_ns::mod_add;

void test_case() {}

vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
    int n = c.size();
    segtree tree(n);
    tree.build(vector<int>(n, 0));
    for (int i = 0; i < v.size(); ++i) {
        tree.modify(l[i], r[i], mod_add{v[i]});
        tree.modify(l[i], r[i], mod_min{c[0]});
        tree.modify(l[i], r[i], mod_max{0});
    }
    vector<int> s(n);
    for (int i = 0; i < n; ++i) {
        s[i] = tree.ask(i, i).sm;
    }
    return s;
}

#ifdef HOUSE
int main() {
    int n;
    assert(1 == scanf("%d", &n));
    std::vector<int> c(n);
    for(int i = 0; i < n; ++i) {
        assert(scanf("%d", &c[i]) == 1);
    }

    int q;
    assert(1 == scanf("%d", &q));
    std::vector<int> l(q), r(q), v(q);
    for(int i = 0; i < q; ++i) {
        assert(scanf("%d %d %d", &l[i], &r[i], &v[i]) == 3);
    }

    std::vector<int> ans = distribute_candies(c, l, r, v);

    for(int i = 0; i < n; ++i) {
        if (i > 0) {
            printf(" ");
        }
        printf("%d", ans[i]);
    }
    printf("\n");
    fclose(stdout);
    return 0;
}
#endif

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:292:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  292 |     for (int i = 0; i < v.size(); ++i) {
      |                     ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 809 ms 45024 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 443 ms 8284 KB Output is correct
3 Correct 116 ms 37788 KB Output is correct
4 Correct 1063 ms 46080 KB Output is correct
5 Correct 1046 ms 46412 KB Output is correct
6 Correct 901 ms 46804 KB Output is correct
7 Correct 865 ms 46160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Halted 0 ms 0 KB -