Submission #593634

# Submission time Handle Problem Language Result Execution time Memory
593634 2022-07-11T13:04:16 Z thezomb1e Growing Trees (BOI11_grow) C++17
100 / 100
444 ms 3180 KB
//thatsramen

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)

using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;

//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld PI = 3.14159265359;

//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set

void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
    unsyncIO(); setPrec();
    if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}

// #define TEST_CASES

struct SegmentTree {

    struct Node {
        int val, modi;
    };

    vector<Node> tre;
    const int NO_OPERATION = 0;
    const int NEUTRAL = 0;
    int sz;

    SegmentTree(vector<int> &a) {
        int n = (int) a.size();
        sz = 1;
        while (sz < n) sz *= 2;
        tre.assign(2 * sz, {NEUTRAL, NO_OPERATION});
        build(a, 1, 0, sz);
    }

    SegmentTree(int n) {
        sz = 1;
        while (sz < n) sz *= 2;
        tre.assign(2 * sz, {NEUTRAL, NO_OPERATION});
    }

    int op_modify(int x, int y) {
        return x + y;
    }

    int op_modify2(int x, int y, int lx, int rx) {
        return x + (rx - lx) * y;
    }

    int op_calc(int x, int y) {
        return x + y;
    }

    void propagate(int x, int lx, int rx) {
        if (rx - lx == 1 || tre[x].modi == NO_OPERATION) return;
        int m = (lx + rx) / 2;
        tre[2 * x].modi     = op_modify(tre[2 * x].modi, tre[x].modi);
        tre[2 * x].val      = op_modify2(tre[2 * x].val, tre[x].modi, lx, m);
        tre[2 * x + 1].modi = op_modify(tre[2 * x + 1].modi, tre[x].modi);
        tre[2 * x + 1].val  = op_modify2(tre[2 * x + 1].val, tre[x].modi, m, rx);
        tre[x].modi = NO_OPERATION;
    }

    void build(vector<int> &a, int x, int lx, int rx) {
        if (rx - lx == 1) {
            if (lx < a.size()) {
                tre[x].val = a[lx];
            }
            return;
        }
        int m = (lx + rx) / 2;
        build(a, 2 * x, lx, m);
        build(a, 2 * x + 1, m, rx);
        tre[x].val = op_calc(tre[2 * x].val, tre[2 * x + 1].val);
    }

    void set(int l, int r, int v, int x, int lx, int rx) {
        propagate(x, lx, rx);
        if (rx <= l || lx >= r) {
            return;
        }
        if (l <= lx && r >= rx) {
            if (tre[x].modi == NO_OPERATION) {
                tre[x].modi = v;
            } else {
                tre[x].modi = op_modify(tre[x].modi, v);
            }
            tre[x].val = op_modify2(tre[x].val, v, lx, rx);
            return;
        }
        int m = (lx + rx) / 2;
        set(l, r, v, 2 * x, lx, m);
        set(l, r, v, 2 * x + 1, m, rx);
        tre[x].val = op_calc(tre[2 * x].val, tre[2 * x + 1].val);
    }

    void set(int l, int r, int v) {
        set(l, r, v, 1, 0, sz);
    }

    int get(int l, int r, int x, int lx, int rx) {
        propagate(x, lx, rx);
        if (rx <= l || lx >= r) {
            return NEUTRAL;
        }
        if (l <= lx && r >= rx) {
            return tre[x].val;
        }
        int m = (lx + rx) / 2;
        int s1 = get(l, r, 2 * x, lx, m);
        int s2 = get(l, r, 2 * x + 1, m, rx);
        return op_calc(s1, s2);
    }

    int get(int l, int r) {
        return get(l, r, 1, 0, sz);
    }

    int point_get(int i, int x, int lx, int rx) {
        propagate(x, lx, rx);
        if (rx - lx == 1) {
            return tre[x].val;
        }
        int m = (lx + rx) / 2;
        if (i < m) {
            return point_get(i, 2 * x, lx, m);
        } else {
            return point_get(i, 2 * x + 1, m, rx);
        }
    }

    int point_get(int i) {
        return point_get(i, 1, 0, sz);
    }
};

void solve() {
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(all(a));
    SegmentTree st(a);
    for (int it = 0; it < q; it++) {
        char op; cin >> op;
        if (op == 'F') {
            int x, h;
            cin >> x >> h;
            int l = -1;
            {
                int lo = 0, hi = n - 1;
                while (lo <= hi) {
                    int mi = (lo + hi) / 2;
                    if (st.point_get(mi) >= h) {
                        l = mi;
                        hi = mi - 1;
                    } else {
                        lo = mi + 1;
                    }
                }
            }
            if (l == -1) continue;
            int r = min(l + x, n) - 1;
            if (r == n - 1) {
                st.set(l, r + 1, 1);
            } else {
                int val = st.point_get(r);
                int left, right;
                {
                    int lo = 0, hi = n - 1;
                    while (lo <= hi) {
                        int mi = (lo + hi) / 2;
                        if (st.point_get(mi) <= val) {
                            right = mi;
                            lo = mi + 1;
                        } else {
                            hi = mi - 1;
                        }
                    }
                }
                {
                    int lo = 0, hi = n - 1;
                    while (lo <= hi) {
                        int mi = (lo + hi) / 2;
                        if (st.point_get(mi) >= val) {
                            left = mi;
                            hi = mi - 1;
                        } else {
                            lo = mi + 1;
                        }
                    }
                }
                int need = r - left + 1;
                st.set(right - need + 1, right + 1, 1);
                st.set(l, left, 1);
            }
        } else {
            int l, r;
            cin >> l >> r;
            int left = -1, right = -1;
            {
                int lo = 0, hi = n - 1;
                while (lo <= hi) {
                    int mi = (lo + hi) / 2;
                    if (st.point_get(mi) <= r) {
                        right = mi;
                        lo = mi + 1;
                    } else {
                        hi = mi - 1;
                    }
                }
            }
            {
                int lo = 0, hi = n - 1;
                while (lo <= hi) {
                    int mi = (lo + hi) / 2;
                    if (st.point_get(mi) >= l) {
                        left = mi;
                        hi = mi - 1;
                    } else {
                        lo = mi + 1;
                    }
                }
            }
            int ans = 0;
            if (left != -1 && right != -1) {
                ans = right - left + 1;
            }
            cout << ans << '\n';
        }
    }
}

int main() {
    setIO();

    int tt = 1;
    #ifdef TEST_CASES
        cin >> tt;
    #endif

    while (tt--)
        solve();

    return 0;
}

Compilation message

grow.cpp: In member function 'void SegmentTree::build(std::vector<int>&, int, int, int)':
grow.cpp:102:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |             if (lx < a.size()) {
      |                 ~~~^~~~~~~~~~
grow.cpp: In function 'void setIn(std::string)':
grow.cpp:44:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
grow.cpp: In function 'void setOut(std::string)':
grow.cpp:45:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp: In function 'void solve()':
grow.cpp:232:30: warning: 'right' may be used uninitialized in this function [-Wmaybe-uninitialized]
  232 |                 st.set(right - need + 1, right + 1, 1);
      |                        ~~~~~~^~~~~~
grow.cpp:134:12: warning: 'left' may be used uninitialized in this function [-Wmaybe-uninitialized]
  134 |         set(l, r, v, 1, 0, sz);
      |         ~~~^~~~~~~~~~~~~~~~~~~
grow.cpp:206:21: note: 'left' was declared here
  206 |                 int left, right;
      |                     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 286 ms 2864 KB Output is correct
2 Correct 442 ms 2812 KB Output is correct
3 Correct 150 ms 2820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 5 ms 340 KB Output is correct
3 Correct 5 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 141 ms 648 KB Output is correct
6 Correct 168 ms 788 KB Output is correct
7 Correct 11 ms 468 KB Output is correct
8 Correct 69 ms 636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 820 KB Output is correct
2 Correct 172 ms 788 KB Output is correct
3 Correct 2 ms 468 KB Output is correct
4 Correct 84 ms 672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 908 KB Output is correct
2 Correct 209 ms 768 KB Output is correct
3 Correct 24 ms 468 KB Output is correct
4 Correct 184 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 229 ms 1736 KB Output is correct
2 Correct 420 ms 2804 KB Output is correct
3 Correct 41 ms 952 KB Output is correct
4 Correct 94 ms 2656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 366 ms 2800 KB Output is correct
2 Correct 369 ms 2876 KB Output is correct
3 Correct 116 ms 2644 KB Output is correct
4 Correct 44 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 272 ms 2836 KB Output is correct
2 Correct 245 ms 2928 KB Output is correct
3 Correct 140 ms 2644 KB Output is correct
4 Correct 42 ms 920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 397 ms 2852 KB Output is correct
2 Correct 365 ms 2976 KB Output is correct
3 Correct 57 ms 2908 KB Output is correct
4 Correct 183 ms 2944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 300 ms 2960 KB Output is correct
2 Correct 360 ms 2840 KB Output is correct
3 Correct 444 ms 2752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 3180 KB Output is correct