From 5f79782a881838f234cbfdb83c37d7cf0102af2a Mon Sep 17 00:00:00 2001 From: Sahu2002 <84759422+utkarsh-iitbhu@users.noreply.github.com> Date: Fri, 30 Sep 2022 15:23:02 +0530 Subject: [PATCH] Added code for Segment Tree --- .../max_num_in_range_SegmentTree.cpp | 178 ++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 C++/Data Structure/max_num_in_range_SegmentTree.cpp diff --git a/C++/Data Structure/max_num_in_range_SegmentTree.cpp b/C++/Data Structure/max_num_in_range_SegmentTree.cpp new file mode 100644 index 000000000..1415fd57e --- /dev/null +++ b/C++/Data Structure/max_num_in_range_SegmentTree.cpp @@ -0,0 +1,178 @@ +#pragma GCC optimize("Ofast") +#pragma GCC optimization("unroll-loops") +#include +using namespace std; +//#include +//#include +//using namespace __gnu_pbds; +#define max(a, b) (a < b ? b : a) +#define min(a, b) ((a > b) ? b : a) +#define mod 1000000007 +#define set_bits __builtin_popcountll +#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++) +#define FORL(a, b, c) for (int(a) = (b); (a) <= (c); (a)++) +#define FORR(a, b, c) for (int(a) = (b); (a) >= (c); (a)--) +#define INF 1e18 +typedef long long int ll; +typedef unsigned long long ull; +typedef long double lld; +//typedef tree, null_type, less>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key +typedef priority_queue, greater> minhp; +typedef priority_queue maxhp; +typedef vector vi; +typedef pair pi; +typedef vector> vdp; +typedef priority_queue , greater> minhppi; +#define sz(x) (int)(x).size() +#define F first +#define S second +#define PB push_back +#define POB pop_back +#define MP make_pair +#define YES cout<<"YES"< void _print(pair p); +template void _print(vector v); +template void _print(set v); +template class Array; +template void _print(map v); +template void _print(unordered_map v); +template void _print(multiset v); +template void _print(unordered_set v); +template void _print(pair p) {cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}";} +template void _print(vector v){cerr<<"[ ";for(T i: v){_print(i);cerr<<", ";}cerr<<"]"< void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(map v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} +template void _print(unordered_map v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} +template void print(vector >& a){cerr << "[ ";for (auto i : a){print(i);cerr << " ";}cerr << "]";} +template void print(vector >& a){cerr << '[' << ' ';for (auto x : a){print(x.F);cerr << ":";print(x.S);cerr << ' ';}cerr << ']';} +template void _print(unordered_set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} + + +const int N = 1e5+2; +int a[N], tree[4*N]; + +// partial querry and complete querry +/* Node + 1 // node 1 + 2 3 // For left node*2 and right 2*node + 1 + 4 5 6 7 // For left node*2 i.e (2*4)&(3*2) and right 2*node + 1 i.e (2*4+1)&(3*2+1) + +*/ +// Construct Segment Tree using Build Function +void build(int node, int st, int en){ + // Agr st en same hai matlab leaf node par ho tum + if(st == en){ + tree[node] = a[st]; + return; + } + // Ab mid me dnc karo + int m = (st+en)/2; + // Left me build karo + build(node*2, st, m); + // Right me build karo + build(node*2+1, m+1, en); + + // Question prefix sum type ka hai isliye sum store ho raha hai isme + // Ye sum ka tree hai, sum in a given querry + tree[node] = tree[node*2] + tree[node*2+1] ; +} + +// Fir this quuestion we are using solve func to construct our tree +void solve(int node, int st, int en){ + if(st==en){ + tree[node] = a[st];// a[en] && a[st] dono same hai + return; + } + int m = (st+en)/2; + solve(2*node, st, m); + solve(2*node+1, m+1, en); + tree[node] = max(tree[2*node] , tree[2*node+1]); +} + +// st and en pe khelenge isme +int querry(int node, int st, int en, int l, int r){ + // No overlap case + if(st>r || en=l && en<=r) return tree[node]; + // Ab partial overlap case wale me we go down and chose own desired range + int m = (st+en)/2; + return max(querry(2*node, st, m, l, r) , querry(2*node+1, m+1, en, l, r)); +} + +// this will update that number and seg tree me bhi changes hoge isse +void update(int node , int st, int en, int idx ,int val){ + // index waise bhi leaf node me hi milega bro + if(st==en){ + a[st] = val; + tree[node] = val; + return; + } + int m = (st+en)/2; + // Binary search lag raha hai to find the ind of the number + if(idx<=m) update(2*node, st, m, idx, val); + else update(2*node+1, m+1, en, idx, val); + tree[node] = max(tree[2*node] , tree[2*node+1]); +} + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + +#ifndef ONLINE_JUDGE +freopen("Error.txt", "w", stderr); +#endif + +int n ; cin>>n ; +FOR(i, n){ + cin>>a[i]; +} + +// Construct our Segment Tree structure +solve(1, 0, n-1); + +while(1){ + int q; cin>>q; + // Exit the querries + if(q==-1) break; + + // Find max in the given range + if(q==1){ + int l, r; cin>>l>>r; + cout<>ind>>val; + update(1,0,n-1,ind,val); + // Poore ka max nikal ke dikhao sahi hai ya nahi + cout<