Submission #593629

# Submission time Handle Problem Language Result Execution time Memory
593629 2022-07-11T12:59:45 Z thezomb1e Growing Trees (BOI11_grow) C++17
70 / 100
1000 ms 4012 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);
    }
};

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);
    dbg(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.get(mi, mi + 1) >= 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.get(r, r + 1);
                int left, right;
                {
                    int lo = 0, hi = n - 1;
                    while (lo <= hi) {
                        int mi = (lo + hi) / 2;
                        if (st.get(mi, mi + 1) <= 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.get(mi, mi + 1) >= 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.get(mi, mi + 1) <= 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.get(mi, mi + 1) >= 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:216:30: warning: 'right' may be used uninitialized in this function [-Wmaybe-uninitialized]
  216 |                 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:190:21: note: 'left' was declared here
  190 |                 int left, right;
      |                     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 730 ms 3504 KB Output is correct
2 Execution timed out 1010 ms 3732 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 340 KB Output is correct
2 Correct 13 ms 428 KB Output is correct
3 Correct 13 ms 332 KB Output is correct
4 Correct 10 ms 332 KB Output is correct
5 Correct 312 ms 844 KB Output is correct
6 Correct 336 ms 868 KB Output is correct
7 Correct 33 ms 600 KB Output is correct
8 Correct 211 ms 1032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 332 ms 972 KB Output is correct
2 Correct 361 ms 1008 KB Output is correct
3 Correct 23 ms 468 KB Output is correct
4 Correct 240 ms 872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 341 ms 1036 KB Output is correct
2 Correct 373 ms 968 KB Output is correct
3 Correct 57 ms 596 KB Output is correct
4 Correct 361 ms 964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 633 ms 2232 KB Output is correct
2 Correct 900 ms 3308 KB Output is correct
3 Correct 124 ms 1352 KB Output is correct
4 Correct 377 ms 3484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 776 ms 3436 KB Output is correct
2 Correct 872 ms 3440 KB Output is correct
3 Correct 462 ms 3492 KB Output is correct
4 Correct 130 ms 1284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 767 ms 3404 KB Output is correct
2 Correct 796 ms 3664 KB Output is correct
3 Correct 458 ms 3660 KB Output is correct
4 Correct 126 ms 1276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1058 ms 3640 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 950 ms 3576 KB Output is correct
2 Correct 959 ms 3780 KB Output is correct
3 Execution timed out 1094 ms 4012 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 816 ms 4000 KB Output is correct