diff --git a/content/Contest/template.cpp b/content/Contest/template.cpp index 097ad5a7..5f5b4ba7 100644 --- a/content/Contest/template.cpp +++ b/content/Contest/template.cpp @@ -1,32 +1,28 @@ /*8<{==========~ BEGIN TEMPLATE ~============>8*/ #include using namespace std; - #ifdef LOCAL #include "debug.cpp" #else #define dbg(...) #endif - #define endl '\n' - #define fastio \ ios_base::sync_with_stdio(0); \ cin.tie(0); - #define int long long - #define all(j) j.begin(), j.end() #define rall(j) j.rbegin(), j.rend() #define len(j) (int)j.size() #define rep(i, a, b) \ for (common_type_t \ - i = a; \ - i < b; i++) + i = (a); \ + i < (b); i++) #define rrep(i, a, b) \ for (common_type_t \ - i = a; \ - i > b; i--) + i = (a); \ + i > (b); i--) +#define trav(a,x) for (auto& a : x) #define pb push_back #define pf push_front #define ppb pop_back @@ -35,7 +31,8 @@ using namespace std; #define lb lower_bound #define ub upper_bound #define emp emplace - +#define ins insert +#define divc(a,b) ((a)+(b)-1ll)/(b) using str = string; using ll = long long; using ull = unsigned long long; @@ -49,14 +46,11 @@ using pii = pair; using vpii = vector; using vc = vector; using vs = vector; - template -using min_heap = +using pqmn = priority_queue, greater>; - template -using max_heap = priority_queue>; - +using pqmx = priority_queue>; template inline bool chmax(T &a, U const &b) { return (a < b ? a = b, 1 : 0); diff --git a/content/Data Structures/Segment tree range update range query/Increment update query max (bottom up).cpp b/content/Data Structures/Segment tree range update range query/Increment update query min & max (bottom up).cpp similarity index 86% rename from content/Data Structures/Segment tree range update range query/Increment update query max (bottom up).cpp rename to content/Data Structures/Segment tree range update range query/Increment update query min & max (bottom up).cpp index 646ea98f..9231fb8d 100644 --- a/content/Data Structures/Segment tree range update range query/Increment update query max (bottom up).cpp +++ b/content/Data Structures/Segment tree range update range query/Increment update query min & max (bottom up).cpp @@ -1,28 +1,27 @@ -/*8< - @Title: - - Increment update, range query max ->8*/ - using SegT = ll; struct QueryT { - SegT v; - QueryT() : v(numeric_limits::min()) {} - QueryT(SegT _v) : v(_v) {} + SegT mx, mn; + QueryT() + : mx(numeric_limits::min()), + mn(numeric_limits::max()) {} + QueryT(SegT _v) : mx(_v), mn(_v) {} }; inline QueryT combine(QueryT ln, QueryT rn, pii lr1, pii lr2) { - return QueryT(max(ln.v, rn.v)); + chmax(ln.mx, rn.mx); + chmin(ln.mn, rn.mn); + return ln; } using LazyT = SegT; inline QueryT applyLazyInQuery(QueryT q, LazyT l, pii lr) { - if (q.v == QueryT().v) q.v = 0; - q.v += l; + if (q.mx == QueryT().mx) q.mx = SegT(); + if (q.mn == QueryT().mn) q.mn = SegT(); + q.mx += l, q.mn += l; return q; } @@ -35,8 +34,9 @@ using UpdateT = SegT; inline QueryT applyUpdateInQuery(QueryT q, UpdateT u, pii lr) { - if (q.v == QueryT().v) q.v = 0; - q.v += u; + if (q.mx == QueryT().mx) q.mx = SegT(); + if (q.mn == QueryT().mn) q.mn = SegT(); + q.mx += u, q.mn += u; return q; } diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/01.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/01.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/01.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/01.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/02.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/02.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/02.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/02.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/03.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/03.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/03.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/03.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/04.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/04.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/04.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/04.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/05.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/05.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/in/05.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/in/05.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/main.cpp b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/main.cpp similarity index 71% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/main.cpp rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/main.cpp index 6112ac09..3eb0d194 100644 --- a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/main.cpp +++ b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/main.cpp @@ -1,12 +1,15 @@ #include "../../../content/Contest/template.cpp" -#include "../../../content/Data Structures/Segment tree range update range query/Increment update query max (bottom up).cpp" +#include "../../../content/Data Structures/Segment tree range update range query/Increment update query min & max (bottom up).cpp" auto run() { int n, q; cin >> n >> q; - vector xs(n); - for (auto& xi : xs) cin >> xi.v; - LazySegmentTree st(xs); + LazySegmentTree st(n); + rep(i, 0, n) { + int x; + cin >> x; + st.set(i, QueryT(x)); + } rep(i, 0, q) { int o; cin >> o; @@ -19,7 +22,7 @@ auto run() { int k; cin >> k; k--; - cout << st.qry(k, k).v << endl; + cout << st.qry(k, k).mx << endl; } } } diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/01.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/01.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/01.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/01.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/02.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/02.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/02.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/02.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/03.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/03.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/03.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/03.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/04.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/04.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/04.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/04.txt diff --git a/tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/05.txt b/tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/05.txt similarity index 100% rename from tests/Segment tree range update increment update query max (bottom up)/CSES 1651/out/05.txt rename to tests/Segment tree range update increment update query min & max (bottom up)/CSES 1651/out/05.txt