Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added segment tree code with some basic problems #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions Add Code Here/C++/segment_tree/build_query.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define MOD1 998244353
#define INF 1e18
#define endl "\n"
#define vi vector<int>
#define pb push_back
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define ff first
#define ss second
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define f(i,a,b) for(ll i=a;i<b;i++)

using namespace std;

class SGTree {
vector<int> seg;
public:
SGTree(int n) {
seg.resize(4 * n + 1);
}

void build(int ind, int low, int high, int arr[]) {
if (low == high) {
seg[ind] = arr[low];
return;
}

int mid = (low + high) / 2;
build(2 * ind + 1, low, mid, arr);
build(2 * ind + 2, mid + 1, high, arr);
seg[ind] = min(seg[2 * ind + 1], seg[2 * ind + 2]);
}

int query(int ind, int low, int high, int l, int r) {
// no overlap
// l r low high or low high l r
if (r < low || high < l) return INT_MAX;

// complete overlap
// [l low high r]
if (low >= l && high <= r) return seg[ind];

int mid = (low + high) >> 1;
int left = query(2 * ind + 1, low, mid, l, r);
int right = query(2 * ind + 2, mid + 1, high, l, r);
return min(left, right);
}

void update(int ind, int low, int high, int i, int val) {
if (low == high) {
seg[ind] = val;
return;
}

int mid = (low + high) >> 1;
if (i <= mid) update(2 * ind + 1, low, mid, i, val);
else update(2 * ind + 2, mid + 1, high, i, val);
seg[ind] = min(seg[2 * ind + 1], seg[2 * ind + 2]);
}
};


#define watch(x) cout << (#x) << " = " << (x) << endl
const int MOD = 1e9 + 7;
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll mult(ll x, ll y) {ll res = x * y;return (res >= MOD ? res % MOD : res);}
ll factorial(ll v){ll ans=1;for(int i=2;i<=v;i++){ans*=i;ans%=MOD;}return ans;}
ll power(ll x, ll y){ if (y < 0)return 1;ll res = 1; x %= MOD;while (y!=0) {if ((y & 1)==1)res = mult(res, x); y >>= 1;x = mult(x, x);} return res;}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
void vin( vector<int> &v , int n ){for (int i = 0; i < n; i++){int x ;cin >> x; v.push_back(x);}}
void vout(vector<int> &v){for (int i = 0; i < v.size(); i++){cout << v[i] << " " ;}cout << endl;}
ll highestPowerof2(ll n){ ll p = (ll)log2(n); return (ll)pow(2, p);}
bool isPowerOfTwo(int n){ if(n==0) return false; return (ceil(log2(n)) == floor(log2(n)));}
bool chkprime(int n){ for(int i = 2 ; i*i <= n ; i++){ if( n % i == 0) return false;} return true; }
string to_binary(int n) { string s = ""; for (int i = 31; i >= 0; i--) { int k = n >> i; if (k & 1) s = s + "1";else s = s + "0";}return s ; }
static void removeTrailingCharacters(std::string &str, const char charToRemove) {str.erase (str.find_last_not_of(charToRemove) + 1, std::string::npos );}
static void removeLeadingCharacters(std::string &str, const char charToRemove) {str.erase(0, std::min(str.find_first_not_of(charToRemove), str.size() - 1));}
long long lcm(int a, int b){ return (a / gcd(a, b)) * b;}



void prakhar() {

int n ;
cin >> n ;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
SGTree sgt(n);
sgt.build(0, 0 , n - 1 , arr);

cout << sgt.query(0, 0 , n - 1 , 2, n -1);



}

int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
ll t = 1 ;
// cin >> t ;
for (int i = 0; i < t; i++) {
prakhar();
}

return 0;

}
141 changes: 141 additions & 0 deletions Add Code Here/C++/segment_tree/dynamic_range_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define MOD1 998244353
#define INF 1e18
#define endl "\n"
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vs vector<string>
#define vc vector<char>ṇ
#define vb vector<bool>
#define pb push_back
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define ff first
#define ss second
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define f(i,a,b) for(ll i=a;i<b;i++)

using namespace std;

class SGTree {
vector<int> seg;
public:
SGTree(int n) {
seg.resize(4 * n + 1);
}

void build(int ind, int low, int high, int arr[]) {
if (low == high) {
seg[ind] = arr[low];
return;
}

int mid = (low + high) / 2;
build(2 * ind + 1, low, mid, arr);
build(2 * ind + 2, mid + 1, high, arr);
seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];

// cout << seg << endl;
}

int query(int ind, int low, int high, int l, int r) {
// no overlap
// l r low high or low high l r
if (r < low || high < l) return 0;

// complete overlap
// [l low high r]
if (low >= l && high <= r) return seg[ind];

int mid = (low + high) >> 1;
int left = query(2 * ind + 1, low, mid, l, r);
int right = query(2 * ind + 2, mid + 1, high, l, r);
return (left+right);
}
void update(int ind, int low, int high, int i, int val) {
if (low == high) {
seg[ind] = val;
return;
}

int mid = (low + high) >> 1;
if (i <= mid) update(2 * ind + 1, low, mid, i, val);
else update(2 * ind + 2, mid + 1, high, i, val);
seg[ind] = seg[2 * ind + 1] + seg[2 * ind + 2];
}
};


#define watch(x) cout << (#x) << " = " << (x) << endl
const int MOD = 1e9 + 7;
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll mult(ll x, ll y) {ll res = x * y;return (res >= MOD ? res % MOD : res);}
ll factorial(ll v){ll ans=1;for(int i=2;i<=v;i++){ans*=i;ans%=MOD;}return ans;}
ll power(ll x, ll y){ if (y < 0)return 1;ll res = 1; x %= MOD;while (y!=0) {if ((y & 1)==1)res = mult(res, x); y >>= 1;x = mult(x, x);} return res;}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
void vin( vector<int> &v , int n ){for (int i = 0; i < n; i++){int x ;cin >> x; v.push_back(x);}}
void vout(vector<int> &v){for (int i = 0; i < v.size(); i++){cout << v[i] << " " ;}cout << endl;}
ll highestPowerof2(ll n){ ll p = (ll)log2(n); return (ll)pow(2, p);}
bool isPowerOfTwo(int n){ if(n==0) return false; return (ceil(log2(n)) == floor(log2(n)));}
bool chkprime(int n){ for(int i = 2 ; i*i <= n ; i++){ if( n % i == 0) return false;} return true; }
string to_binary(int n) { string s = ""; for (int i = 31; i >= 0; i--) { int k = n >> i; if (k & 1) s = s + "1";else s = s + "0";}return s ; }
static void removeTrailingCharacters(std::string &str, const char charToRemove) {str.erase (str.find_last_not_of(charToRemove) + 1, std::string::npos );}
static void removeLeadingCharacters(std::string &str, const char charToRemove) {str.erase(0, std::min(str.find_first_not_of(charToRemove), str.size() - 1));}
long long lcm(int a, int b){ return (a / gcd(a, b)) * b;}



void prakhar() {

int n, q ;
cin >> n >> q ;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}

SGTree sgt(n);
sgt.build(0, 0 , n - 1 , arr);


for (int i = 0; i < q; i++)
{
int c, l , r ;
cin >> c >> l >> r ;
if( c == 1)
{
sgt.update(0, 0, n-1, --l, r );
}
else
cout << sgt.query(0, 0, n-1, --l , --r ) << endl;

}



}

int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
ll t = 1 ;
// cin >> t ;
for (int i = 0; i < t; i++) {
prakhar();
}

return 0;

}
Loading