From a4edd067a0b01aa430f543ddcd7ace66d0cc1b18 Mon Sep 17 00:00:00 2001 From: 33Arsenic75 <122697564+33Arsenic75@users.noreply.github.com> Date: Thu, 13 Jun 2024 22:03:11 +0530 Subject: [PATCH] . --- problem-of-the-day/day15/Counting Tilings.cpp | 35 +++ problem-of-the-day/day15/Removal Game.cpp | 29 +++ .../day15/maximal_rectangle.cpp | 41 ++++ .../day15/palindromic_partitioning.cpp | 42 ++++ problem-of-the-day/day15/rod_cutting.cpp | 41 ++++ .../day16/Dynamic_Range_Minimum_Queries.cpp | 202 ++++++++++++++++++ .../day16/Dynamic_Range_Sum_Queries.cpp | 202 ++++++++++++++++++ problem-of-the-day/day16/Forest_Queries.cpp | 145 +++++++++++++ .../day16/Range_Xor_Queries.cpp | 40 ++++ .../day16/Static_Range_Minimum_Queries.cpp | 193 +++++++++++++++++ .../day16/Static_Range_Sum_Queries.cpp | 41 ++++ 11 files changed, 1011 insertions(+) create mode 100644 problem-of-the-day/day15/Counting Tilings.cpp create mode 100644 problem-of-the-day/day15/Removal Game.cpp create mode 100644 problem-of-the-day/day15/maximal_rectangle.cpp create mode 100644 problem-of-the-day/day15/palindromic_partitioning.cpp create mode 100644 problem-of-the-day/day15/rod_cutting.cpp create mode 100644 problem-of-the-day/day16/Dynamic_Range_Minimum_Queries.cpp create mode 100644 problem-of-the-day/day16/Dynamic_Range_Sum_Queries.cpp create mode 100644 problem-of-the-day/day16/Forest_Queries.cpp create mode 100644 problem-of-the-day/day16/Range_Xor_Queries.cpp create mode 100644 problem-of-the-day/day16/Static_Range_Minimum_Queries.cpp create mode 100644 problem-of-the-day/day16/Static_Range_Sum_Queries.cpp diff --git a/problem-of-the-day/day15/Counting Tilings.cpp b/problem-of-the-day/day15/Counting Tilings.cpp new file mode 100644 index 0000000..dd294b9 --- /dev/null +++ b/problem-of-the-day/day15/Counting Tilings.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; +typedef long long ll; +const int maxN = 11; +const int maxM = 1001; +const ll MOD = 1e9+7; + +int N, M; +ll dp[maxN][maxM][1< + +using namespace std; +typedef long long ll; +const int maxN = 5000; + +int N, x[maxN+1]; +ll p[maxN+1], dp[maxN+1][maxN+1]; +bool found[maxN+1][maxN+1]; + +ll sum(int l, int r){ + return p[r] - p[l-1]; +} + +ll solve(int l, int r){ + if(found[l][r]) return dp[l][r]; + if(l == r) return x[l]; + found[l][r]=true; + return dp[l][r] = max(x[l]+sum(l+1, r)-solve(l+1, r), x[r]+sum(l,r-1)-solve(l,r-1)); +} + +int main(){ + scanf("%d", &N); + for(int i = 1; i <= N; i++){ + scanf("%d", &x[i]); + p[i] = p[i-1] + x[i]; + } + printf("%lld\n", solve(1, N)); +} diff --git a/problem-of-the-day/day15/maximal_rectangle.cpp b/problem-of-the-day/day15/maximal_rectangle.cpp new file mode 100644 index 0000000..b362225 --- /dev/null +++ b/problem-of-the-day/day15/maximal_rectangle.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +using namespace std; + +class Solution { +public: + int largestRectangleArea(vector& heights) { + int ans = 0; + stack stk; + + for (int i = 0; i <= heights.size(); ++i) { + while (!stk.empty() && (i == heights.size() || heights[stk.top()] > heights[i])) { + int h = heights[stk.top()]; + stk.pop(); + int w = stk.empty() ? i : i - stk.top() - 1; + ans = max(ans, h * w); + } + stk.push(i); + } + + return ans; + } + + int maximalRectangle(vector>& matrix) { + if (matrix.empty()) + return 0; + + int ans = 0; + vector hist(matrix[0].size(), 0); + + for (auto& row : matrix) { + for (int i = 0; i < row.size(); ++i) + hist[i] = row[i] == '0' ? 0 : hist[i] + 1; + ans = max(ans, largestRectangleArea(hist)); + } + + return ans; + } +}; \ No newline at end of file diff --git a/problem-of-the-day/day15/palindromic_partitioning.cpp b/problem-of-the-day/day15/palindromic_partitioning.cpp new file mode 100644 index 0000000..f0e7bf2 --- /dev/null +++ b/problem-of-the-day/day15/palindromic_partitioning.cpp @@ -0,0 +1,42 @@ +class Solution { +public: + bool isPalindrome(int i, int j, string& s) + { + while (i < j) { + if (s[i] != s[j]) + return false; + i++; + j--; + } + return true; + } + + int solve(int i, int n, string& str, vector& dp) + { + + if (i == n) + return 0; + + if (dp[i] != -1) + return dp[i]; + + int minCost = INT_MAX; + + for (int j = i; j < n; j++) { + if (isPalindrome(i, j, str)) { + + int cost = 1 + solve(j + 1, n, str, dp); + minCost = min(minCost, cost); + } + } + return dp[i] = minCost; + } + + int palindromicPartition(string str) + { + int n = str.size(); + vector dp(n, -1); + + return solve(0, n, str, dp) - 1; + } +}; diff --git a/problem-of-the-day/day15/rod_cutting.cpp b/problem-of-the-day/day15/rod_cutting.cpp new file mode 100644 index 0000000..c266348 --- /dev/null +++ b/problem-of-the-day/day15/rod_cutting.cpp @@ -0,0 +1,41 @@ + +#include +using namespace std; + +// Function to solve the unbounded knapsack problem +int unboundedKnapsack(int n, int W, vector& val, vector& wt) { + vector cur(W + 1, 0); // Create a vector to store the current DP state + + // Base Condition + for (int i = wt[0]; i <= W; i++) { + cur[i] = (i / wt[0]) * val[0]; // Calculate the maximum value for the first item + } + + for (int ind = 1; ind < n; ind++) { + for (int cap = 0; cap <= W; cap++) { + int notTaken = cur[cap]; // Maximum value without taking the current item + + int taken = INT_MIN; + if (wt[ind] <= cap) + taken = val[ind] + cur[cap - wt[ind]]; // Maximum value by taking the current item + + cur[cap] = max(notTaken, taken); // Store the maximum value in the current DP state + } + } + + return cur[W]; // Return the maximum value considering all items and the knapsack capacity +} + +int main() { + vector wt = {2, 4, 6}; // Weight of items + vector val = {5, 11, 13}; // Value of items + int W = 10; // Weight capacity of the knapsack + int n = wt.size(); // Number of items + + // Call the function to calculate and output the maximum value the thief can steal + cout << "The Maximum value of items the thief can steal is " << unboundedKnapsack(n, W, val, wt) << endl; + + return 0; // Return 0 to indicate successful program execution +} + +// Refer to this link for detailed explanation - https://www.youtube.com/watch?v=mO8XpGoJwuo \ No newline at end of file diff --git a/problem-of-the-day/day16/Dynamic_Range_Minimum_Queries.cpp b/problem-of-the-day/day16/Dynamic_Range_Minimum_Queries.cpp new file mode 100644 index 0000000..b1a0c6e --- /dev/null +++ b/problem-of-the-day/day16/Dynamic_Range_Minimum_Queries.cpp @@ -0,0 +1,202 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +/// ordered sets support upperbound lowerbound too +#include +#include +#define len(v) (int)v.size() +#define all(v) v.begin(), v.end() +#define rall(v) v.rbegin(), v.rend() +using namespace std; +#define int long long +#define pb push_back +#define rep(i, a, b) for (int i = a; i < b; i++) +#define f first +#define mk make_pair +#define in insert +#define py cout << "YES" << endl +#define pn cout << "NO" << endl +template +struct Modular { + int value; + static const int MOD_value = MOD; + + Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;} + Modular(long long a, long long b) : value(0){ *this += a; *this /= b;} + + Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;} + Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;} + Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;} + + friend Modular mexp(Modular a, long long e) { + Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; } + return res; + } + friend Modular inverse(Modular a) { return mexp(a, MOD - 2); } + + Modular& operator/=(Modular const& b) { return *this *= inverse(b); } + friend Modular operator+(Modular a, Modular const b) { return a += b; } + friend Modular operator-(Modular a, Modular const b) { return a -= b; } + friend Modular operator-(Modular const a) { return 0 - a; } + friend Modular operator*(Modular a, Modular const b) { return a *= b; } + friend Modular operator/(Modular a, Modular const b) { return a /= b; } + friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;} + friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;} + friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;} +}; +template void chkmin(T &x, T y) { + if (x > y) + x = y; +} +template void chkmax(T &x, T y) { + if (x < y) + x = y; +} + +void sigsev(int n, vector &prime) { + prime.push_back(2); + vectorispm(n + 1, true); + + for(int j = 3; j <= n; j += 2) { + if(!ispm[j])continue; + else{ + prime.push_back(j); + for(int i = j*j; i <= n; i += j) { + ispm[i] = false; + } + } + } +} + +class UF { +public: + int *id, cnt, *sz; + // Create an empty union find data structure with N isolated sets. + UF(int N) { + cnt = N; id = new int[N]; sz = new int[N]; + for (int i = 0; idata; + int n; + vectortree; + function operation; + int default_val; + SG(vector&data , int default_val , function op):operation(op) { + this->data = data; + n = data.size(); + this->default_val = default_val; + tree.resize(4*n); + build(0, n-1, 1); + } + void build(int l, int r, int node) { + if(l == r) { + tree[node] = data[l]; + // cout< r) return default_val; + else if (l == tl && r == tr) { + return tree[v]; + } + int tm = (tl + tr) / 2; + return operation(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r)); + } + int query(int ql, int qr) { + return query(1, 0, n-1, ql, qr); + } + void buildup(int v){ + if(v==0)return; + tree[v] = operation(tree[v*2], tree[v*2+1]); + buildup(v/2); + } + void update(int v, int tl, int tr, int l, int r, int new_val) { + if (l > r) + return; + if (l == tl && r == tr) { + tree[v] = new_val; + } else { + int tm = (tl + tr) / 2; + update(v*2, tl, tm, l, min(r, tm), new_val); + update(v*2+1, tm+1, tr, max(l, tm+1), r, new_val); + buildup(v); + } + } + void update(int l, int r, int new_val){ + update(1, 0, n-1, l, r, new_val); + } +}; + +#ifndef ONLINE_JUDGE +#define debug(x) cout << #x << " = " << x << endl +#define show1(v) \ + for (auto i : v) \ + cout << i << ' ' +#define show(v) \ + show1(v); \ + cout << '\n' +#define gcd __algo_gcd +#else +#define debug(x) +#endif + + +// ------------------------------------------***-------------------------------------------------- + + +int32_t main() { + int n,q; cin>>n>>q; + vectora(n); + rep(i,0,n)cin>>a[i]; + int qq,l,r,k,u; + SG s = SG(a, INT32_MAX, [](int a, int b) { return min(a,b); }); + while(q--){ + cin>>qq; + if(qq==1){ + cin>>k>>u; + s.update(k-1, k-1, u); + } + else if(qq==2){ + cin>>l>>r; + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +/// ordered sets support upperbound lowerbound too +#include +#include +#define len(v) (int)v.size() +#define all(v) v.begin(), v.end() +#define rall(v) v.rbegin(), v.rend() +using namespace std; +#define int long long +#define pb push_back +#define rep(i, a, b) for (int i = a; i < b; i++) +#define f first +#define mk make_pair +#define in insert +#define py cout << "YES" << endl +#define pn cout << "NO" << endl +template +struct Modular { + int value; + static const int MOD_value = MOD; + + Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;} + Modular(long long a, long long b) : value(0){ *this += a; *this /= b;} + + Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;} + Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;} + Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;} + + friend Modular mexp(Modular a, long long e) { + Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; } + return res; + } + friend Modular inverse(Modular a) { return mexp(a, MOD - 2); } + + Modular& operator/=(Modular const& b) { return *this *= inverse(b); } + friend Modular operator+(Modular a, Modular const b) { return a += b; } + friend Modular operator-(Modular a, Modular const b) { return a -= b; } + friend Modular operator-(Modular const a) { return 0 - a; } + friend Modular operator*(Modular a, Modular const b) { return a *= b; } + friend Modular operator/(Modular a, Modular const b) { return a /= b; } + friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;} + friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;} + friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;} +}; +template void chkmin(T &x, T y) { + if (x > y) + x = y; +} +template void chkmax(T &x, T y) { + if (x < y) + x = y; +} + +void sigsev(int n, vector &prime) { + prime.push_back(2); + vectorispm(n + 1, true); + + for(int j = 3; j <= n; j += 2) { + if(!ispm[j])continue; + else{ + prime.push_back(j); + for(int i = j*j; i <= n; i += j) { + ispm[i] = false; + } + } + } +} + +class UF { +public: + int *id, cnt, *sz; + // Create an empty union find data structure with N isolated sets. + UF(int N) { + cnt = N; id = new int[N]; sz = new int[N]; + for (int i = 0; idata; + int n; + vectortree; + function operation; + int default_val; + SG(vector&data , int default_val , function op):operation(op) { + this->data = data; + n = data.size(); + this->default_val = default_val; + tree.resize(4*n); + build(0, n-1, 1); + } + void build(int l, int r, int node) { + if(l == r) { + tree[node] = data[l]; + // cout< r) return default_val; + else if (l == tl && r == tr) { + return tree[v]; + } + int tm = (tl + tr) / 2; + return operation(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r)); + } + int query(int ql, int qr) { + return query(1, 0, n-1, ql, qr); + } + void buildup(int v){ + if(v==0)return; + tree[v] = operation(tree[v*2], tree[v*2+1]); + buildup(v/2); + } + void update(int v, int tl, int tr, int l, int r, int new_val) { + if (l > r) + return; + if (l == tl && r == tr) { + tree[v] = new_val; + } else { + int tm = (tl + tr) / 2; + update(v*2, tl, tm, l, min(r, tm), new_val); + update(v*2+1, tm+1, tr, max(l, tm+1), r, new_val); + buildup(v); + } + } + void update(int l, int r, int new_val){ + update(1, 0, n-1, l, r, new_val); + } +}; + +#ifndef ONLINE_JUDGE +#define debug(x) cout << #x << " = " << x << endl +#define show1(v) \ + for (auto i : v) \ + cout << i << ' ' +#define show(v) \ + show1(v); \ + cout << '\n' +#define gcd __algo_gcd +#else +#define debug(x) +#endif + + +// ------------------------------------------***-------------------------------------------------- + + +int32_t main() { + int n,q; cin>>n>>q; + vectora(n); + rep(i,0,n)cin>>a[i]; + int qq,l,r,k,u; + SG s = SG(a, 0, [](int a, int b) { return a+b; }); + while(q--){ + cin>>qq; + if(qq==1){ + cin>>k>>u; + s.update(k-1, k-1, u); + } + else if(qq==2){ + cin>>l>>r; + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +/// ordered sets support upperbound lowerbound too +#include +#include +#define len(v) (int)v.size() +#define all(v) v.begin(), v.end() +#define rall(v) v.rbegin(), v.rend() +using namespace std; +#define int long long +#define pb push_back +#define rep(i, a, b) for (int i = a; i < b; i++) +#define f first +#define mk make_pair +#define in insert +#define py cout << "YES" << endl +#define pn cout << "NO" << endl +template +struct Modular { + int value; + static const int MOD_value = MOD; + + Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;} + Modular(long long a, long long b) : value(0){ *this += a; *this /= b;} + + Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;} + Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;} + Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;} + + friend Modular mexp(Modular a, long long e) { + Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; } + return res; + } + friend Modular inverse(Modular a) { return mexp(a, MOD - 2); } + + Modular& operator/=(Modular const& b) { return *this *= inverse(b); } + friend Modular operator+(Modular a, Modular const b) { return a += b; } + friend Modular operator-(Modular a, Modular const b) { return a -= b; } + friend Modular operator-(Modular const a) { return 0 - a; } + friend Modular operator*(Modular a, Modular const b) { return a *= b; } + friend Modular operator/(Modular a, Modular const b) { return a /= b; } + friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;} + friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;} + friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;} +}; +template void chkmin(T &x, T y) { + if (x > y) + x = y; +} +template void chkmax(T &x, T y) { + if (x < y) + x = y; +} + +void sigsev(int n, vector &prime) { + prime.push_back(2); + vectorispm(n + 1, true); + + for(int j = 3; j <= n; j += 2) { + if(!ispm[j])continue; + else{ + prime.push_back(j); + for(int i = j*j; i <= n; i += j) { + ispm[i] = false; + } + } + } +} + +class UF { +public: + int *id, cnt, *sz; + // Create an empty union find data structure with N isolated sets. + UF(int N) { + cnt = N; id = new int[N]; sz = new int[N]; + for (int i = 0; i +struct Modular { + int value; + static const int MOD_value = MOD; + + Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;} + Modular(long long a, long long b) : value(0){ *this += a; *this /= b;} + + Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;} + Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;} + Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;} + + friend Modular mexp(Modular a, long long e) { + Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; } + return res; + } + friend Modular inverse(Modular a) { return mexp(a, MOD - 2); } + + Modular& operator/=(Modular const& b) { return *this *= inverse(b); } + friend Modular operator+(Modular a, Modular const b) { return a += b; } + friend Modular operator-(Modular a, Modular const b) { return a -= b; } + friend Modular operator-(Modular const a) { return 0 - a; } + friend Modular operator*(Modular a, Modular const b) { return a *= b; } + friend Modular operator/(Modular a, Modular const b) { return a /= b; } + friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;} + friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;} + friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;} +}; +template void chkmin(T &x, T y) { + if (x > y) + x = y; +} +template void chkmax(T &x, T y) { + if (x < y) + x = y; +} + +void sigsev(int n, vector &prime) { + prime.push_back(2); + vectorispm(n + 1, true); + + for(int j = 3; j <= n; j += 2) { + if(!ispm[j])continue; + else{ + prime.push_back(j); + for(int i = j*j; i <= n; i += j) { + ispm[i] = false; + } + } + } +} + +class UF { +public: + int *id, cnt, *sz; + // Create an empty union find data structure with N isolated sets. + UF(int N) { + cnt = N; id = new int[N]; sz = new int[N]; + for (int i = 0; idata; + int n; + vectortree; + function operation; + int default_val; + SG(vector&data , int default_val , function op):operation(op) { + this->data = data; + n = data.size(); + this->default_val = default_val; + tree.resize(4*n); + build(0, n-1, 1); + } + void build(int l, int r, int node) { + if(l == r) { + tree[node] = data[l]; + // cout< r) return default_val; + else if (l == tl && r == tr) { + return tree[v]; + } + int tm = (tl + tr) / 2; + return operation(query(v*2, tl, tm, l, min(r, tm)),query(v*2+1, tm+1, tr, max(l, tm+1), r)); + } + int query(int ql, int qr) { + return query(1, 0, n-1, ql, qr); + } + void update(int v, int tl, int tr, int l, int r, int new_val) { + if (l > r) + return; + if (l == tl && r == tr) { + tree[v] = new_val; + } else { + int tm = (tl + tr) / 2; + update(v*2, tl, tm, l, min(r, tm), new_val); + update(v*2+1, tm+1, tr, max(l, tm+1), r, new_val); + } + } + void update(int l, int r, int new_val){ + update(1, 0, n-1, l, r, new_val); + } +}; + + +#ifndef ONLINE_JUDGE +#define debug(x) cout << #x << " = " << x << endl +#define show1(v) \ + for (auto i : v) \ + cout << i << ' ' +#define show(v) \ + show1(v); \ + cout << '\n' +#define gcd __algo_gcd +#else +#define debug(x) +#endif + + +// ------------------------------------------***-------------------------------------------------- + + + + + +int32_t main() { + int n,q; + cin>>n>>q; + vectordata(n,0); + rep(i,0,n)cin>>data[i]; + SG s(data, INT32_MAX ,[](int a, int b) { return min(a,b); }); // Using lambda function + int l,r; + while(q--){ + cin>>l>>r; + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +/// ordered sets support upperbound lowerbound too +#include +#include +#define len(v) (int)v.size() +#define all(v) v.begin(), v.end() +#define rall(v) v.rbegin(), v.rend() +using namespace std; +#define int long long +#define pb push_back +#define rep(i, a, b) for (int i = a; i < b; i++) +// ------------------------------------------***-------------------------------------------------- + + + + + +int32_t main() { + int n,q;cin>>n>>q; + vectora(n+1,0); + int x; + rep(i,1,n+1){ + cin>>x; + a[i]=a[i-1]+x; + } + int l,r; + while(q--){ + cin>>l>>r; + cout<