├── README.md ├── QuickSort.cpp ├── Bellman Ford Algorithm.cpp ├── Binary Tree BST or not.cpp ├── kth smallest element in MXN matrix.cpp ├── nCr.py ├── fastInput.txt ├── fastPow.txt ├── copyArray.cpp ├── GCD.txt ├── Totient Function.txt ├── Stock Price Max Profit Buy and Sell only once.cpp ├── compare function in sorting.cpp ├── shiftZeroes.cpp ├── GSS3 Spoj.cpp ├── BinaryRepresentationofNumber.cpp ├── Catalan using DP.cpp ├── Second Best Element Tournament Method.cpp ├── CredExam.cpp ├── Catalan using Inverse Modulo.cpp ├── Next_biggest_number.cpp ├── SuperTables-Interesting Binary Search Prob.cpp ├── Bucket Sort.cpp ├── Fibonacci in LogN time.cpp ├── InversionCount.cpp ├── RPLN -RMQ Spoj.cpp ├── Stock Prices if buying and selling costs are involved.cpp ├── Stock Prices using graph Peaks.cpp ├── CountOccurancesUsingBinarySearch.cpp ├── PythagoreanTriplets.cpp ├── Get Middle of Linked List.cpp ├── Optimal Game Strategy DP.cpp ├── InclusionExclusionPrinciple.cpp ├── Magic Sum - DFS HackerEarth.cpp ├── Max path sum Tree(array) using DFS.cpp ├── Segment Tree RMQ.cpp ├── Reverse Min egdes in graph.cpp ├── Pythagoran Triplets.txt ├── SimpleSegmentTree.cpp ├── MatrixExponentiation.cpp ├── Dijkshtra Adjaceny List and Heap.cpp ├── Dijkshtra Adjaceny List and Heap (ELogV).cpp ├── BasicMath.cpp ├── Dijkshtra using operator overloading Priority Queue.cpp ├── Nut and Bolt Matching Problem.cpp ├── Lazy Propagation - Internet.cpp └── Lazy Propagation Template - Sum.cpp /README.md: -------------------------------------------------------------------------------- 1 | Lgoes 2 | ===== 3 | -------------------------------------------------------------------------------- /QuickSort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prateek27/Algorithms/HEAD/QuickSort.cpp -------------------------------------------------------------------------------- /Bellman Ford Algorithm.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prateek27/Algorithms/HEAD/Bellman Ford Algorithm.cpp -------------------------------------------------------------------------------- /Binary Tree BST or not.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /kth smallest element in MXN matrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prateek27/Algorithms/HEAD/kth smallest element in MXN matrix.cpp -------------------------------------------------------------------------------- /nCr.py: -------------------------------------------------------------------------------- 1 | def f(n): 2 | return reduce(lambda x,y:x*y,[1]+range(1,n+1)) 3 | t=int(raw_input()) 4 | while t: 5 | n=map(int,raw_input().split(' ')) 6 | print f(n[0])/(f(n[1])*f(n[0]-n[1])) 7 | t=t-1 -------------------------------------------------------------------------------- /fastInput.txt: -------------------------------------------------------------------------------- 1 | int scan() 2 | { 3 | int t=0; 4 | char c; 5 | c=getchar_unlocked(); 6 | while(c<'0' || c>'9') 7 | c=getchar_unlocked(); 8 | while(c>='0' && c<='9') 9 | { 10 | t=(t<<3)+(t<<1)+c-'0'; 11 | c=getchar_unlocked(); 12 | } 13 | return t; 14 | } -------------------------------------------------------------------------------- /fastPow.txt: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | ll fastPow(ll a,ll b) 6 | { 7 | int result = 1; 8 | while(b>0){ 9 | 10 | if(b&1) 11 | result=result*a; 12 | 13 | a=a*a; 14 | b=b>>1; 15 | 16 | } 17 | return result; 18 | } 19 | 20 | int main(){ 21 | 22 | cout< 2 | using namespace std; 3 | 4 | struct copyArray{ 5 | int a[10]; 6 | }; 7 | 8 | 9 | int main(){ 10 | int i; 11 | struct copyArray a,b ; 12 | for(i=0;i<10;i++) 13 | a.a[i]=i; 14 | 15 | b=a; //Using the assignment the whole array a is copied into b 16 | 17 | for(i=0;i<10;i++) 18 | cout< 2 | using namespace std; 3 | #define ll long long 4 | 5 | //Totient Function gives number of nos less than a given n and coprime with n. 6 | 7 | int totient(int n) 8 | { 9 | int result = n; 10 | 11 | for(int i=2;i*i <= n;i++) 12 | { 13 | if (n % i == 0) 14 | result =result- result/i; 15 | while (n%i==0) 16 | n /= i; 17 | } 18 | if(result>1) 19 | result =result- result/n; 20 | 21 | return result; 22 | } 23 | 24 | int main(){ 25 | 26 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int cum[100]; 6 | 7 | int func(int *a,int n){ 8 | int i,profit=0; 9 | 10 | cum[n-1]=a[n-1]; 11 | 12 | for(i=n-2;i>=0;i--) 13 | cum[i]=max(a[i],cum[i+1]); 14 | 15 | for(i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int compareInts(const void* a, const void* b) 7 | { 8 | // Ascending Order 9 | return *(const int *)a > *(const int *)b; 10 | //Descending *(const int *)a < *(const int *)b; 11 | } 12 | 13 | int main(void) 14 | { 15 | int items[] = { 4, 3, 1, 2 }; 16 | qsort(items, sizeof(items) / sizeof(items[0]), sizeof(items[0]), compareInts); 17 | int i; 18 | for( i=0;i<4;i++) 19 | printf("%d\n",items[i]); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /shiftZeroes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void shiftZeroes(int *a,int n) 5 | { 6 | int count=0; 7 | int pos=0; 8 | int i; 9 | for(i=0;i 2 | using namespace std; 3 | 4 | #define ll long long 5 | ll a[50000]; 6 | ll st[1000000]; 7 | 8 | void update(int ) 9 | 10 | int main(){ 11 | int n,q,t,x,y; 12 | cin>>n; 13 | for(i=0;i>q; 17 | while(q--){ 18 | scanf("%d%d%d",&t,&x,&y) 19 | 20 | if(t==0) // Modify Ax into y 21 | { 22 | update(x,y); 23 | } 24 | 25 | //Else Print the max of range x,y 26 | else{ 27 | rangeSumMaxQuery(0,n-1,x-1,y-1); 28 | } 29 | 30 | 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /BinaryRepresentationofNumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | 8 | //This program prints the Binary Representation of a Number upto N (uses BFS traversal) 9 | void printBinUptoN(int n){ 10 | if(n==0) 11 | return; 12 | list q; 13 | q.push_back("1"); 14 | 15 | 16 | while(1){ 17 | string temp = q.front(); 18 | cout<>n; 30 | printBinUptoN(n); 31 | 32 | system("pause"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Catalan using DP.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define MAX 1000 6 | 7 | /*DP Solution for Catalan Numbers.TLE for Large value of MAX*/ 8 | long long dp[MAX]; 9 | void catalan(){ 10 | dp[0]=dp[1]=1; 11 | for(int i=2;i<10000;i++){ 12 | dp[i]=0; 13 | for(int j=0;j>t; 26 | 27 | while(t--){ 28 | scanf("%d",&n); 29 | printf("%lld\n",dp[n]); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Second Best Element Tournament Method.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | /* 5 | 0 6 | / \ 7 | 0 5 8 | / \ / \ 9 | 2 0 12 5 10 | */ 11 | 12 | int a[]={0,0,5,2,0,12,5}; 13 | 14 | int func(int index,int n,int val) 15 | { 16 | if(index>=n) 17 | return 10000000; 18 | if(a[index]!=val) 19 | return a[index]; 20 | else 21 | return min(func(2*index+1,n,val),func((2*index+2),n,val)); 22 | 23 | } 24 | int main(){ 25 | 26 | //Tournament method takes a complete binary tree so array as given as input 27 | 28 | //Given first best elemnt - 0 ; Output Second Min Element - ie 2 29 | cout<<"Second Min Element is "< 2 | #include 3 | using namespace std; 4 | 5 | int a[1000005]; 6 | 7 | int main(){ 8 | int m,n,i; 9 | cin>>m>>n; 10 | 11 | for(i=0;ia[index]) 25 | index=index+1; 26 | else{ 27 | swap(a[index],a[index+1]); 28 | index=index+1; 29 | } 30 | } 31 | if(a[index]) 32 | ans+=1; 33 | 34 | if(ans>=n) 35 | printf("YES"); 36 | else 37 | printf("NO"); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Catalan using Inverse Modulo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define MOD 1000000007 6 | #define ll long long 7 | ll fact[2000005]; 8 | 9 | 10 | void factorial(){ 11 | fact[0]=fact[1]=1; 12 | for(int i=2;i<=2000000;i++) 13 | {fact[i]=i*fact[i-1]; 14 | fact[i]%=MOD; 15 | } 16 | } 17 | 18 | ll pow(ll a,ll b) 19 | { 20 | ll res=1; 21 | while(b){ 22 | 23 | if(b&1) 24 | res=(res*a)%MOD; 25 | 26 | a=(a*a)%MOD; 27 | b=b>>1; 28 | } 29 | return res; 30 | } 31 | 32 | ll catalan(ll n){ 33 | return ((((fact[2*n]*pow(fact[n],MOD-2))%MOD)*pow(fact[n+1],MOD-2))%MOD)%MOD; 34 | } 35 | 36 | 37 | 38 | int main(){ 39 | ll t,n; 40 | factorial(); 41 | cin>>t; 42 | while(t--){ 43 | scanf("%lld",&n); 44 | printf("%lld\n",catalan(n)); 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Next_biggest_number.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | /*Short cut method 6 | int main(){ 7 | int numbers[]={6,7,3}; 8 | next_permutation(numbers,numbers+3); 9 | cout<=0;i--){ 25 | if(a[i-1]current_min) 32 | {current_min=min(current_min,a[j]); 33 | index = j;} 34 | } 35 | 36 | int temp = a[i-1]; 37 | a[i-1] = a[index]; 38 | a[index] = temp; 39 | 40 | sort(a+i,a+len); 41 | 42 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | #define ll long long 9 | 10 | //SuperTables on HackerEarth based on Binary Search. 11 | int main(){ 12 | ll t,n,a,b,L,R,M; 13 | ll ans; 14 | cin>>t; 15 | while(t--){ 16 | cin>>a>>b>>n; 17 | L=1; 18 | R=max(a,b)*n; 19 | while(L<=R){ 20 | ll mid = L + (R-L)/2; 21 | //No of factors till mid 22 | 23 | ll factors = mid/a + mid/b - mid*__gcd(a,b)/(a*b); 24 | if(n==factors) 25 | { 26 | ans = max( (mid/a)*a,(mid/b)*b); 27 | printf("%lld\n",ans); 28 | break; 29 | } 30 | else if(factors>n){ 31 | L = mid; 32 | } 33 | else 34 | R = mid; 35 | 36 | } 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Bucket Sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Bucket Sort Program when integers are floating point numbers in some range 7 | 8 | void bucketSort(float *arr,int n) 9 | { 10 | vector b[n]; 11 | int i,j; 12 | for(i=0;i>n; 38 | int i; 39 | for(i=0;i>arr[i]; 41 | 42 | bucketSort(arr,n); 43 | cout<<"Sorted Array"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define ll long long 7 | #define MOD 1000000007 8 | using namespace std; 9 | #define int long long 10 | 11 | void multiply(int F[2][2],int M[2][2]) 12 | { 13 | ll x = F[0][0]*M[0][0] + F[0][1]*M[1][0]; 14 | ll y = F[0][0]*M[0][1] + F[0][1]*M[1][1]; 15 | ll z = F[1][0]*M[0][0] + F[1][1]*M[1][0]; 16 | ll w = F[1][0]*M[0][1] + F[1][1]*M[1][1]; 17 | 18 | F[0][0] = x%MOD; 19 | F[0][1] = y%MOD; 20 | F[1][0] = z%MOD; 21 | F[1][1] = w%MOD; 22 | 23 | } 24 | 25 | int power(int F[2][2],int n){ 26 | int M[2][2]={{1,1},{1,0}}; 27 | if(n==0||n==1) 28 | return 0; 29 | 30 | power(F,n/2); 31 | multiply(F,F); 32 | 33 | if(n%2!=0) 34 | multiply(F,M); 35 | } 36 | 37 | 38 | int fib(int n){ 39 | 40 | if(n==0) 41 | return 0; 42 | int F[2][2]={{1,1},{1,0}}; 43 | power(F,n-1); 44 | return F[0][0]; 45 | } 46 | 47 | main(){ 48 | int t,n; 49 | cin>>t; 50 | while(t--){ 51 | scanf("%lld",&n); 52 | printf("%lld\n",fib(n+2)); 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /InversionCount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | long long count; 5 | int temp[200000]; 6 | int arr[200000]; 7 | void countInversions(int arr[],int low,int high); 8 | void merge(int arr[],int low,int mid,int high); 9 | 10 | void countInversions(int arr[],int low,int high){ 11 | int mid; 12 | if(low>t; 51 | 52 | 53 | while(t--){ 54 | cin>>n; 55 | 56 | for(i=0;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | int arr[100000]; 8 | //int st[200001]; 9 | int *st; 10 | 11 | int makeSegmentTree(int ss,int se,int si) 12 | { 13 | if(ss==se) 14 | { 15 | st[si]=arr[ss]; 16 | return arr[ss]; 17 | } 18 | 19 | int mid = (ss+se)/2; 20 | st[si] = min(makeSegmentTree(ss,mid,2*si+1),makeSegmentTree(mid+1,se,2*si+2)); 21 | return st[si]; 22 | } 23 | 24 | int RMQ(int ss,int se,int qs,int qe,int ci){ 25 | if(qs<=ss&&qe>=se) 26 | return st[ci]; 27 | if(seqe) 28 | return INT_MAX; 29 | int mid = (ss+se)/2; 30 | return min(RMQ(ss,mid,qs,qe,2*ci+1),RMQ(mid+1,se,qs,qe,2*ci+2)); 31 | } 32 | 33 | int main(){ 34 | int t,n,q,c=0; 35 | cin>>t; 36 | while(t--){ 37 | c++; 38 | scanf("%d%d",&n,&q); 39 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | //In this problem , price of buffloe is given on different days.Ram can buy and sell atmost once buffloe at time. 11 | //There is a price with each buy and sell operation 12 | 13 | int func(int *a,int n,int cost){ 14 | int with[100]; 15 | int without[100]; 16 | 17 | with[0] = 0 -a[0] - cost; 18 | without[0] = 0; 19 | int i; 20 | 21 | //DP Solution : With means Ram has bufflo on day i , previously had it our buys it on day i 22 | // Without : Without means Ram doesnt have a bufflo , previously dont had it or sold it on day i. 23 | 24 | for(i=1;i>n; 38 | int a[n+1]; 39 | int i; 40 | cout<<"Enter prices :"; 41 | 42 | for(i=0;i>a[i]; 44 | } */ 45 | 46 | int cost = 2; 47 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int peak[100]; 11 | 12 | int func(int *a,int n){ 13 | peak[0]=a[0]; 14 | 15 | int j=1,i,profit=0; 16 | 17 | for(i=0;ia[i-1]&&a[i]>a[i+1]) 23 | { //cout<<"MAX"<peak[0]){ 38 | for(i=1;i<=j;i+=2){ 39 | profit+= peak[i]-peak[i-1]; 40 | } 41 | } 42 | 43 | else{ 44 | for(i=2;i<=j;i+=2){ 45 | profit+= peak[i]-peak[i-1]; 46 | } 47 | } 48 | 49 | return profit; 50 | } 51 | 52 | 53 | int main(){ 54 | int n=10; 55 | int a[]={11 ,7 ,10, 9, 13, 14, 10, 15, 12, 10}; 56 | /*cout<<"Enter no of days: "; 57 | cin>>n; 58 | int a[n+1]; 59 | int i; 60 | cout<<"Enter prices :"; 61 | 62 | for(i=0;i>a[i]; 64 | }*/ 65 | 66 | cout< 3 | #include 4 | using namespace std; 5 | int a[1000000]; 6 | int count = 0; 7 | 8 | int getLeft(int low,int high,int key){ 9 | int mid ,ans = -1; 10 | while(low<=high){ 11 | mid = low + (high-low)/2; 12 | 13 | if(a[mid]>key) 14 | high = mid - 1; 15 | else if(a[mid]key){ 29 | high = mid-1; 30 | } 31 | else if(a[mid]>t; 57 | while(t--){ 58 | cin>>n; 59 | for(i=0;i>key; 62 | countOccurances(n,key); 63 | } 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /PythagoreanTriplets.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | struct triplet{ 8 | int a,b,c; 9 | }; 10 | 11 | int countTriplets(int n){ 12 | int ans=0; 13 | stack s; 14 | struct triplet temp; 15 | temp.a=3; 16 | temp.b=4; 17 | temp.c=5; 18 | s.push(temp); 19 | while(!s.empty()) 20 | { 21 | int a,b,c; 22 | a = s.top().a; 23 | b= s.top().b; 24 | c=s.top().c; 25 | s.pop(); 26 | 27 | ans+=n/c; 28 | //Case 1: 1-22 2-12 2-23 29 | temp.a = a -2*b + 2*c; 30 | temp.b = 2*a - b + 2*c; 31 | temp.c = 2*a - 2*b + 3*c; 32 | if(temp.a<=n&&temp.b<=n&&temp.c<=n){ 33 | s.push(temp); 34 | } 35 | 36 | //Case 2: 122 212 223 37 | temp.a = a + 2*b + 2*c; 38 | temp.b = 2*a + b + 2*c; 39 | temp.c = 2*a + 2*b + 3*c; 40 | if(temp.a<=n&&temp.b<=n&&temp.c<=n){ 41 | s.push(temp); 42 | } 43 | //Case 3: -122 -212 -223 44 | temp.a = -a + 2*b + 2*c; 45 | temp.b = -2*a + b + 2*c; 46 | temp.c = -2*a + 2*b + 3*c; 47 | 48 | if(temp.a<=n&&temp.b<=n&&temp.c<=n){ 49 | s.push(temp); 50 | } 51 | 52 | } 53 | return ans; 54 | } 55 | 56 | int main(){ 57 | int t,n; 58 | cin>>t; 59 | while(t--){ 60 | scanf("%d",&n); 61 | printf("%d\n",countTriplets(n)); 62 | } 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /Get Middle of Linked List.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct node{ 5 | int data; 6 | struct node*next; 7 | }; 8 | 9 | //-------------------------------New Node 10 | struct node*newNode(int data) 11 | { 12 | struct node*node=new struct node; 13 | node->data=data; 14 | node->next=NULL; 15 | return node; 16 | }; 17 | //----------------------------------Insert Front 18 | void push(struct node**head,int data) 19 | { 20 | if(*head==NULL) 21 | *head=newNode(data); 22 | else 23 | { 24 | struct node*temp= newNode(data); 25 | temp->next=*head; 26 | *head=temp; 27 | } 28 | 29 | } 30 | void print(struct node*node) 31 | { 32 | while(node!=NULL) 33 | { 34 | cout<data<<" -> "; 35 | node=node->next; 36 | } 37 | } 38 | //--------------------------Pairwise swap 39 | int getMiddle(struct node*node) 40 | { 41 | struct node*slow=node; 42 | struct node*fast=slow->next; 43 | 44 | while(fast!=NULL&&fast->next!=NULL) 45 | { 46 | 47 | { 48 | slow=slow->next; 49 | fast=fast->next->next; 50 | } 51 | } 52 | 53 | cout<data; 54 | return 0; 55 | }; 56 | 57 | int main(){ 58 | struct node*head=NULL; 59 | for(int i=9;i>=1;i--) 60 | push(&head,i); 61 | 62 | cout<<"Orginal LL :"< 2 | #include 3 | #include 4 | using namespace std; 5 | // Given an array , two players play the game of picking numbers alternatively from either ends. Find the max Score one can pick. 6 | 7 | int arr[100000]; 8 | int dp[1000][1000]; 9 | 10 | int playOptimally(int n){ 11 | int ans; 12 | int table[n][n], gap, i, j, x, y, z; 13 | 14 | // Fill table using above recursive formula. Note that the table 15 | // is filled in diagonal fashion (similar to http://goo.gl/PQqoS), 16 | // from diagonal elements to table[0][n-1] which is the result. 17 | for (gap = 0; gap < n; ++gap) 18 | { 19 | for (i = 0, j = gap; j < n; ++i, ++j) 20 | { 21 | // Here x is value of F(i+2, j), y is F(i+1, j-1) and 22 | // z is F(i, j-2) in above recursive formula 23 | x = ((i+2) <= j) ? table[i+2][j] : 0; 24 | y = ((i+1) <= (j-1)) ? table[i+1][j-1] : 0; 25 | z = (i <= (j-2))? table[i][j-2]: 0; 26 | 27 | table[i][j] = max(arr[i] + min(x, y), arr[j] + min(y, z)); 28 | } 29 | } 30 | 31 | return table[0][n-1]; 32 | 33 | return ans; 34 | } 35 | 36 | int main(){ 37 | int t,n,i; 38 | cin>>t; 39 | while(t--){ 40 | memset(dp,0,sizeof dp); 41 | cout<<"Enter the array size : "; 42 | cin>>n; 43 | cout<<"Enter the numbers "; 44 | for(i=0;i>arr[i]; 46 | } 47 | int ans = playOptimally(n); 48 | 49 | } 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /InclusionExclusionPrinciple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | //Problem - http://www.hackerearth.com/problem/algorithm/mehta-and-the-tricky-triplets/editorial/ 5 | #define ll long long 6 | ll a[100005]; 7 | ll count[20]; 8 | 9 | ll nC3(ll n){ 10 | if(n<3) 11 | return 0; 12 | ll ans ; 13 | ans = (n*(n-1)*(n-2))/6; 14 | return ans; 15 | } 16 | 17 | int main(){ 18 | ll n,i; 19 | cin>>n; 20 | for(i=0;i0){ 28 | ll rem = num%10; 29 | if(rem==2) c|=1; 30 | else if(rem==3) c|=2; 31 | else if(rem==5) c|=4; 32 | else if(rem==7) c|=8; 33 | num /= 10; 34 | } 35 | count[c]++; 36 | } 37 | 38 | ll numBits,answer=0; 39 | //The i iteraters over the bit positions and array positons. 40 | for(i=1;i<16;i++){ 41 | ll j = i ; 42 | numBits=0; 43 | //Just count the number of bits in that particular number at arr[i] 44 | while(j>0){ 45 | if(j&1) 46 | numBits++; 47 | j=j/2; 48 | } 49 | 50 | //How many numbers after 2 contain 2.. 51 | //How many numbers after 3 contain 3.. 52 | //Like for 7 can come in 9,10,12,15. 53 | int total=0 ; 54 | for(ll k=i;k<16;k++){ 55 | if((k&i)==i) 56 | total+=count[k]; 57 | } 58 | 59 | //Include the positions having odd count and subtract having even count. 60 | if(numBits%2!=0) 61 | answer += nC3(total); 62 | else 63 | answer -= nC3(total); 64 | } 65 | 66 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | //Magic-sum on HackerEarth 10 | vector list[515]; 11 | int visited[515],ans,sum; 12 | 13 | 14 | int dfs(int i,int a[],int firstNode=0){ 15 | visited[i]=1; 16 | 17 | //If it is a leaf node , then update ans in the case the path starts and ends at the same node 18 | if(list[i].size()==1) 19 | ans=max(ans,a[i]); 20 | 21 | //If it is a leaf node and and not the starting leaf node. 22 | if(list[i].size()==1&&!firstNode) 23 | return a[i]; 24 | 25 | int sum=INT_MIN; 26 | for(int j=0;j>t; 37 | while(t--){ 38 | cin>>n; 39 | int a[n]; 40 | assert(N>=1 && N<=511); 41 | 42 | if(n==1) 43 | {cin>>a[0]; 44 | cout< 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | //Magic-sum on HackerEarth 10 | vector list[515]; 11 | int visited[515],ans,sum; 12 | 13 | 14 | int dfs(int i,int a[],int firstNode=0){ 15 | visited[i]=1; 16 | 17 | //If it is a leaf node , then update ans in the case the path starts and ends at the same node 18 | if(list[i].size()==1) 19 | ans=max(ans,a[i]); 20 | 21 | //If it is a leaf node and and not the starting leaf node. 22 | if(list[i].size()==1&&!firstNode) 23 | return a[i]; 24 | 25 | int sum=INT_MIN; 26 | for(int j=0;j>t; 37 | while(t--){ 38 | cin>>n; 39 | int a[n]; 40 | assert(N>=1 && N<=511); 41 | 42 | if(n==1) 43 | {cin>>a[0]; 44 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std ; 6 | 7 | int min(int a,int b) 8 | { 9 | return a>b?b:a; 10 | } 11 | 12 | int RMQUtil(int *st,int ss,int se,int qs,int qe,int index){ 13 | 14 | if(qs<=ss&&qe>=se) 15 | return st[index]; 16 | 17 | if(sen-1||qs>qe){ 30 | cout<<"Invalid Input"; 31 | return -1; 32 | } 33 | 34 | return RMQUtil(st,0,n-1,qs,qe,0); 35 | } 36 | 37 | 38 | 39 | // si - starting index , ei - ending index , ci - current index 40 | int makeSegmentTreeUtil(int *arr,int *st,int ss,int se,int ci){ 41 | 42 | if(ss==se){ 43 | st[ci]=arr[ss]; 44 | return arr[ss]; 45 | } 46 | 47 | int mid = (ss+se)/2; 48 | st[ci] = min(makeSegmentTreeUtil(arr,st,ss,mid,2*ci+1),makeSegmentTreeUtil(arr,st,mid+1,se,2*ci+2)); 49 | 50 | return st[ci]; 51 | } 52 | 53 | 54 | int*makeSegmentTree(int *arr,int n) 55 | { 56 | int x = (int)(ceil(log2(n))); 57 | int max_size = 2*(int)pow(2,x) - 1; 58 | 59 | int *st = new int[max_size]; 60 | makeSegmentTreeUtil(arr,st,0,n-1,0); 61 | return st; 62 | } 63 | 64 | 65 | int main(){ 66 | int i; 67 | int arr[] = {1, -3, 2, 7, 9, 11}; 68 | int n = sizeof(arr)/sizeof(int); 69 | 70 | int *st = makeSegmentTree(arr,n); 71 | for(i=0;i<=11;++i) 72 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | /* Given a directed graph with vertice V ( from 1 to N ) and 8 | Edges E , the following program calcualtes the min no of edges to be reversed to establish 9 | a path from vertex 1 to N */ 10 | 11 | class Graph{ 12 | int V,E; 13 | list *SAdj; 14 | list *RAdj; 15 | public: 16 | Graph(int V,int E); 17 | void addSEdge(int u,int v); 18 | void addREdge(int u,int v); 19 | int calcMinEdges(int s); 20 | }; 21 | 22 | Graph::Graph(int V,int E){ 23 | this->V = V; 24 | this->E = E; 25 | SAdj = new list[V+1]; 26 | RAdj = new list[V+1]; 27 | } 28 | 29 | void Graph::addSEdge(int u,int v){ 30 | SAdj[u].push_back(v); 31 | } 32 | 33 | void Graph::addREdge(int u,int v){ 34 | RAdj[u].push_back(v); 35 | } 36 | 37 | int Graph::calcMinEdges(int s){ 38 | 39 | int dist[V+1]; 40 | for(int i=2;i<=V;i++) 41 | dist[V]=INT_MAX; 42 | 43 | list q; 44 | dist[s]=0; 45 | q.push_back(s); 46 | list::iterator i,j; 47 | 48 | while(!q.empty()){ 49 | int s = q.front(); 50 | q.pop_front(); 51 | 52 | for(i=SAdj[s].begin();i!=SAdj[s].end();i++){ 53 | if(dist[*i]>dist[s]) 54 | { dist[*i]=dist[s]; 55 | q.push_back(*i); 56 | } 57 | } 58 | for(j=RAdj[s].begin();j!=RAdj[s].end();j++){ 59 | if(dist[*j]>dist[s]+1) 60 | { dist[*j]=dist[s]+1; 61 | q.push_back(*j); 62 | } 63 | } 64 | } 65 | 66 | if(dist[V]==INT_MAX) 67 | return -1; 68 | 69 | else 70 | return dist[V]; 71 | 72 | } 73 | 74 | 75 | int main(){ 76 | int V,E; 77 | int u,v; 78 | scanf("%d%d",&V,&E); 79 | Graph g(V,E); 80 | for(int i=0;i j. The formula states that the integers a = i^2 - j^2 , b = 2ij , 3 | c = i^2+ j^2 form a Pythagorean triple. Conditions on i and j are they must be co prime and 4 | (i-j) must be odd Pseudo code for that portion is: 5 | 6 | for (i = 2; i < =(sqrt(t) + 1); i++) 7 | for (j = 1; j < i; j++) 8 | if ((gcd(i,j) == 1) && ((i-j) % 2) && ((i*i + j*j) <= t)) 9 | { 10 | k = 0; 11 | a = i * i - j * j; 12 | b = 2 * i * j; 13 | c = i * i + j * j; 14 | while ((++k) * c <= t) 15 | counter++; 16 | } 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | struct triple 23 | { 24 | int a,b,c; 25 | }; 26 | using namespace std; 27 | int func(int m) 28 | { 29 | int count=0; 30 | stack < struct triple > pyth ; 31 | struct triple temp; 32 | temp.a=3; 33 | temp.b=4; 34 | temp.c=5; 35 | pyth.push(temp); 36 | while(pyth.size()!=0) 37 | { 38 | int aa,bb,cc; 39 | aa=pyth.top().a; 40 | bb=pyth.top().b; 41 | cc=pyth.top().c; 42 | count=count+(m/cc); 43 | pyth.pop(); 44 | temp.a=aa-2*bb+2*cc; 45 | temp.b=2*aa-bb+2*cc; 46 | temp.c=2*aa-2*bb+3*cc; 47 | if(temp.c<=m && temp.a<=m && temp.b<=m) 48 | pyth.push(temp); 49 | temp.a=aa+2*bb+2*cc; 50 | temp.b=2*aa+bb+2*cc; 51 | temp.c=2*aa+2*bb+3*cc; 52 | if(temp.c<=m && temp.a<=m && temp.b<=m) 53 | pyth.push(temp); 54 | temp.a=2*(bb+cc)-aa; 55 | temp.b=bb+2*cc-2*aa; 56 | temp.c=2*bb+3*cc-2*aa; 57 | if(temp.c<=m && temp.a<=m && temp.b<=m) 58 | pyth.push(temp); 59 | } 60 | return count; 61 | } 62 | int main() 63 | { 64 | int t; 65 | scanf("%d",&t); 66 | while(t--) 67 | { 68 | int n,m; 69 | scanf("%d",&n); 70 | printf("%d\n",func(n)); 71 | } 72 | return 0; 73 | } -------------------------------------------------------------------------------- /SimpleSegmentTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define MAX 54000 6 | #define INT_MIN -22121 7 | 8 | struct treeNode{ 9 | int prefixSum; 10 | int suffixSum; 11 | int sum; 12 | int maxSum; 13 | }; 14 | 15 | int arr[MAX+1]; 16 | treeNode tree[3*MAX+1]; 17 | 18 | void makeSegmentTree(int idx,int ss,int se){ 19 | if(ss==se){ 20 | tree[idx]=((treeNode){arr[ss],arr[ss],arr[ss],arr[ss]}); 21 | 22 | } 23 | 24 | else{ 25 | int mid=(ss+se)/2; 26 | makeSegmentTree(idx*2+1,ss,mid); 27 | makeSegmentTree(idx*2+2,mid+1,se); 28 | 29 | treeNode left = tree[idx*2+1]; 30 | treeNode right = tree[idx*2+2]; 31 | 32 | tree[idx].prefixSum = max(left.prefixSum , left.sum+right.prefixSum); 33 | tree[idx].suffixSum = max(right.suffixSum, left.suffixSum+right.sum); 34 | tree[idx].sum = left.sum + right.sum; 35 | tree[idx].maxSum = max(left.suffixSum+right.prefixSum,max(left.maxSum,right.maxSum)); 36 | } 37 | } 38 | 39 | 40 | 41 | treeNode maxSumQuery(int idx,int ss,int se,int qs,int qe){ 42 | 43 | if(ss>=qs&&se<=qe) 44 | { return tree[idx];} 45 | if(qs>se||qe 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | using namespace std; 19 | 20 | #define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i)) 21 | #define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i)) 22 | #define CLEAR(a) memset((a),0,sizeof(a)) 23 | #define INF 1000000000 24 | #define PB push_back 25 | #define ALL(c) (c).begin(), (c).end() 26 | #define pi 2*acos(0.0) 27 | #define SQR(a) (a)*(a) 28 | #define MP make_pair 29 | #define MAX 110 30 | #define si(n) scanf("%d",&n); 31 | #define sl(n) scanf("%lld",&n); 32 | #define MOD 1000000007 33 | #define printl(n) ("%lld\n",n); 34 | #define ll long long 35 | using namespace std; 36 | #define int long long 37 | int f[2][2] ; 38 | 39 | 40 | void multiply(int F[2][2],int M[2][2]) 41 | { 42 | ll x = F[0][0]*M[0][0] + F[0][1]*M[1][0]; 43 | ll y = F[0][0]*M[0][1] + F[0][1]*M[1][1]; 44 | ll z = F[1][0]*M[0][0] + F[1][1]*M[1][0]; 45 | ll w = F[1][0]*M[0][1] + F[1][1]*M[1][1]; 46 | 47 | F[0][0] = x%MOD; 48 | F[0][1] = y%MOD; 49 | F[1][0] = z%MOD; 50 | F[1][1] = w%MOD; 51 | 52 | } 53 | 54 | void fastPower(int m[2][2],int n){ 55 | if(n==0||n==1) 56 | return ; 57 | 58 | fastPower(m,n/2); 59 | multiply(m,m); 60 | 61 | if(n%2!=0) 62 | multiply(m,f); 63 | } 64 | 65 | int solveLinearRec(int m[2][2],int colVec[2][1],int n){ 66 | int i,j; 67 | for(i=0;i<2;i++){ 68 | for(j=0;j<2;j++){ 69 | f[i][j] = m[i][j]; 70 | } 71 | } 72 | fastPower(m,n-1); 73 | // Multiply a with ColVector 1 , 3 // 74 | return (m[0][0] + 3*m[0][1]%MOD)%MOD; 75 | 76 | } 77 | 78 | 79 | 80 | 81 | main(){ 82 | int t; 83 | int n; 84 | cin>>t; 85 | while(t--){ 86 | int n; 87 | cin>>n; 88 | 89 | int m[2][2] = {{0,1},{2,2}}; 90 | int colVector[2][1] = {1,3}; 91 | 92 | if(n==1) 93 | cout<<"1"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define INT_MAX 1000000000 7 | using namespace std; 8 | 9 | 10 | int dist[100]; 11 | 12 | class myComparison{ 13 | public: 14 | bool operator()(const int&a,const int&b){ 15 | return dist[a]>dist[b]; // this is for Min-heap based on distance comparison 16 | } 17 | }; 18 | 19 | struct node{ 20 | int dest; 21 | int wt; 22 | }; 23 | 24 | 25 | vector adj[100]; 26 | 27 | void addEdge(int src,int dest,int wt){ 28 | node n; 29 | n.dest = dest; 30 | n.wt = wt; 31 | node n1; 32 | n1.dest =src; 33 | n1.wt=wt; 34 | adj[src].push_back(n); 35 | adj[dest].push_back(n1); 36 | } 37 | 38 | 39 | 40 | 41 | 42 | 43 | void dijkstra(int s){ 44 | 45 | bool visited[100]; 46 | memset(dist,INT_MAX,sizeof dist); 47 | memset(visited,false,sizeof visited); 48 | priority_queue,myComparison> q; 49 | 50 | 51 | 52 | dist[s]=0; 53 | visited[s]=1; 54 | q.push(s); 55 | 56 | while(!q.empty()){ 57 | int cur = q.top(); 58 | q.pop(); 59 | visited[cur]=2; 60 | int j; 61 | for(j=0;j dist[cur]+wt) 72 | dist[des]=dist[cur]+wt; 73 | if(des==4){ 74 | cout<<"Parent " < 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define INT_MAX 1000000000 7 | using namespace std; 8 | 9 | 10 | int dist[100]; 11 | 12 | class myComparison{ 13 | public: 14 | bool operator()(const int&a,const int&b){ 15 | return dist[a]>dist[b]; // this is for Min-heap based on distance comparison 16 | } 17 | }; 18 | 19 | struct node{ 20 | int dest; 21 | int wt; 22 | }; 23 | 24 | 25 | vector adj[100]; 26 | 27 | void addEdge(int src,int dest,int wt){ 28 | node n; 29 | n.dest = dest; 30 | n.wt = wt; 31 | node n1; 32 | n1.dest =src; 33 | n1.wt=wt; 34 | adj[src].push_back(n); 35 | adj[dest].push_back(n1); 36 | } 37 | 38 | 39 | 40 | 41 | 42 | 43 | void dijkstra(int s){ 44 | 45 | bool visited[100]; 46 | memset(dist,INT_MAX,sizeof dist); 47 | memset(visited,false,sizeof visited); 48 | priority_queue,myComparison> q; 49 | 50 | 51 | 52 | dist[s]=0; 53 | visited[s]=1; 54 | q.push(s); 55 | 56 | while(!q.empty()){ 57 | int cur = q.top(); 58 | q.pop(); 59 | visited[cur]=2; 60 | int j; 61 | for(j=0;j dist[cur]+wt) 72 | dist[des]=dist[cur]+wt; 73 | if(des==4){ 74 | cout<<"Parent " < 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | using namespace std; 19 | 20 | #define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i)) 21 | #define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i)) 22 | #define CLEAR(a) memset((a),0,sizeof(a)) 23 | #define INF 1000000000 24 | #define PB push_back 25 | #define ALL(c) (c).begin(), (c).end() 26 | #define pi 2*acos(0.0) 27 | #define SQR(a) (a)*(a) 28 | #define MP make_pair 29 | #define MAX 110 30 | #define si(n) scanf("%d",&n); 31 | #define sl(n) scanf("%lld",&n); 32 | #define MOD 1000000007 33 | #define printl(n) ("%lld\n",n); 34 | #define ll long long 35 | 36 | ll multiply(ll a,ll b,ll mod){ 37 | ll res = 0 ; 38 | ll x = a%mod; 39 | 40 | while(b>0){ 41 | if(b&1) 42 | res = (res+x)%mod; 43 | 44 | a = (a*2)%mod; 45 | b>>=1; 46 | } 47 | return res%mod; 48 | } 49 | 50 | 51 | ll fastPower(ll a,ll b,ll mod){ 52 | int res=1; 53 | a=a%mod; 54 | while(b>0) 55 | { 56 | if(b&1) 57 | res=(res*a)%mod; 58 | a=(a*a)%mod; 59 | b>>=1; 60 | } 61 | return res%mod; 62 | } 63 | 64 | ll inverseMod(ll no,ll mod){ 65 | return fastPower(no,mod-2,mod); 66 | } 67 | 68 | ll square(ll a){ 69 | return multiply(a,a,MOD); 70 | } 71 | 72 | ll ncr(ll n,ll k,ll mod){ 73 | if(k>n/2) 74 | return ncr(n,n-k,mod); 75 | ll num = 1; 76 | ll nth = n; 77 | for(ll i=0;iv[100005],v1[100005]; 9 | 10 | bool operator<(const node1& a,const node1&b) 11 | { 12 | return dist[a.idx]>dist[b.idx]; 13 | } 14 | 15 | 16 | void djistra(int src) 17 | { 18 | int i; 19 | int visited[100005]; 20 | priority_queueq; 21 | memset(visited,0,sizeof(visited)); 22 | 23 | for(i=1;i<=n;++i) 24 | dist[i]=INT_MAX; 25 | 26 | dist[src]=0; 27 | visited[src]=1; 28 | 29 | node1 p; 30 | p.idx=src; 31 | q.push(p); 32 | while(!q.empty()) 33 | { 34 | node1 p1=q.top(); 35 | int tp=p1.idx; 36 | q.pop(); 37 | //cout< 3 | #include 4 | #include 5 | using namespace std ; 6 | 7 | class Bolt; //Forward Declaration 8 | class Nut{ 9 | int Size; 10 | public: 11 | Nut(int Size){ 12 | this->Size = Size; 13 | } 14 | int getSize(){ 15 | return this->Size; 16 | } 17 | friend int::compare(const Nut*,const Bolt*); 18 | friend int::compare(const Bolt*,const Nut*); 19 | }; 20 | 21 | class Bolt{ 22 | int Size; 23 | 24 | public: 25 | Bolt(int Size){ 26 | this->Size = Size; 27 | } 28 | int getSize(){ 29 | return this->Size; 30 | } 31 | friend int::compare(const Nut*,const Bolt*); 32 | friend int::compare(const Bolt*,const Nut*); 33 | }; 34 | 35 | // Return -1 Nut < Bolt , 0 equal , else 1 36 | int compare(const Nut* n,const Bolt*b){ 37 | if( n->Size < b->Size) 38 | return -1; 39 | else if(n->Size==b->Size) 40 | return 0; 41 | else 42 | return 1; 43 | } 44 | //Return -1 Bolt < Nut , 0 equal , else 1 45 | int compare(const Bolt*b,const Nut*n){ 46 | if( b->Size < n->Size) 47 | return -1; 48 | else if(n->Size==b->Size) 49 | return 0; 50 | else 51 | return 1; 52 | } 53 | //Since we need two types of partition function , hence better to use a template 54 | template 55 | int partition(X** a,int low,int high,Y*b){ 56 | int i,j,k; 57 | i = low - 1; 58 | 59 | for(j=low;j>n; 95 | Nut **nuts; 96 | Bolt **bolts; 97 | 98 | cout<<"Enter Nut Sizes ,then Bolt Sizes :"<>s; 105 | nuts[i] = new Nut(s); 106 | } 107 | for(i=0;i>s; 109 | bolts[i] = new Bolt(s); 110 | } 111 | matchNutsBolts(nuts,bolts,0,n-1); 112 | 113 | cout<<"BOLTS : "; 114 | for(i=0;igetSize()<<" "; 116 | } 117 | cout<getSize()<<" " ; 120 | } 121 | return 0; 122 | } 123 | -------------------------------------------------------------------------------- /Lazy Propagation - Internet.cpp: -------------------------------------------------------------------------------- 1 | /** Segment Tree - Range Max Query with Updates and Lazy Propagtion." 2 | * In this code we have a very large array called arr, and very large set of operations 3 | * Operation #1: Increment the elements within range [i, j] with value val 4 | * Operation #2: Get max element within range [i, j] 5 | * Build tree: build_tree(1, 0, N-1) 6 | * Update tree: update_tree(1, 0, N-1, i, j, value) 7 | * Query tree: query_tree(1, 0, N-1, i, j) 8 | */ 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | #include 15 | #include 16 | 17 | #define N 20 18 | #define MAX (1+(1<<6)) 19 | #define inf 0x7fffffff 20 | 21 | int arr[N]; 22 | int tree[MAX]; 23 | int lazy[MAX]; 24 | 25 | /** 26 | * Build and init tree 27 | */ 28 | void build_tree(int node, int a, int b) { 29 | if(a > b) return; // Out of range 30 | 31 | if(a == b) { // Leaf node 32 | tree[node] = arr[a]; // Init value 33 | return; 34 | } 35 | 36 | build_tree(node*2, a, (a+b)/2); // Init left child 37 | build_tree(node*2+1, 1+(a+b)/2, b); // Init right child 38 | 39 | tree[node] = max(tree[node*2], tree[node*2+1]); // Init root value 40 | } 41 | 42 | /** 43 | * Increment elements within range [i, j] with value value 44 | */ 45 | void update_tree(int node, int a, int b, int i, int j, int value) { 46 | 47 | if(lazy[node] != 0) { // This node needs to be updated 48 | tree[node] += lazy[node]; // Update it 49 | 50 | if(a != b) { 51 | lazy[node*2] += lazy[node]; // Mark child as lazy 52 | lazy[node*2+1] += lazy[node]; // Mark child as lazy 53 | } 54 | 55 | lazy[node] = 0; // Reset it 56 | } 57 | 58 | if(a > b || a > j || b < i) // Current segment is not within range [i, j] 59 | return; 60 | 61 | if(a >= i && b <= j) { // Segment is fully within range 62 | tree[node] += value; 63 | 64 | if(a != b) { // Not leaf node 65 | lazy[node*2] += value; 66 | lazy[node*2+1] += value; 67 | } 68 | 69 | return; 70 | } 71 | 72 | update_tree(node*2, a, (a+b)/2, i, j, value); // Updating left child 73 | update_tree(1+node*2, 1+(a+b)/2, b, i, j, value); // Updating right child 74 | 75 | tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value 76 | } 77 | 78 | /** 79 | * Query tree to get max element value within range [i, j] 80 | */ 81 | int query_tree(int node, int a, int b, int i, int j) { 82 | 83 | if(a > b || a > j || b < i) return -inf; // Out of range 84 | 85 | if(lazy[node] != 0) { // This node needs to be updated 86 | tree[node] += lazy[node]; // Update it 87 | 88 | if(a != b) { 89 | lazy[node*2] += lazy[node]; // Mark child as lazy 90 | lazy[node*2+1] += lazy[node]; // Mark child as lazy 91 | } 92 | 93 | lazy[node] = 0; // Reset it 94 | } 95 | 96 | if(a >= i && b <= j) // Current segment is totally within range [i, j] 97 | return tree[node]; 98 | 99 | int q1 = query_tree(node*2, a, (a+b)/2, i, j); // Query left child 100 | int q2 = query_tree(1+node*2, 1+(a+b)/2, b, i, j); // Query right child 101 | 102 | int res = max(q1, q2); // Return final result 103 | 104 | return res; 105 | } 106 | 107 | int main() { 108 | for(int i = 0; i < N; i++) arr[i] = 1; 109 | 110 | build_tree(1, 0, N-1); 111 | 112 | memset(lazy, 0, sizeof lazy); 113 | 114 | update_tree(1, 0, N-1, 0, 6, 5); // Increment range [0, 6] by 5 115 | update_tree(1, 0, N-1, 7, 10, 12); // Incremenet range [7, 10] by 12 116 | update_tree(1, 0, N-1, 10, N-1, 100); // Increment range [10, N-1] by 100 117 | 118 | cout << query_tree(1, 0, N-1, 0, N-1) << endl; // Get max element in range [0, N-1] 119 | } 120 | -------------------------------------------------------------------------------- /Lazy Propagation Template - Sum.cpp: -------------------------------------------------------------------------------- 1 | /* Problem : HORRIBLE (Spoj) */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | using namespace std; 20 | 21 | #define FOR(i,a,b) for(int (i) = (a); (i) < (b); ++(i)) 22 | #define RFOR(i,a,b) for(int (i) = (a)-1; (i) >= (b); --(i)) 23 | #define CLEAR(a) memset((a),0,sizeof(a)) 24 | #define INF 1000000000 25 | #define PB push_back 26 | #define ALL(c) (c).begin(), (c).end() 27 | #define pi 2*acos(0.0) 28 | #define SQR(a) (a)*(a) 29 | #define MP make_pair 30 | #define MAX 110 31 | #define si(n) scanf("%d",&n); 32 | #define sl(n) scanf("%lld",&n); 33 | #define MOD 1000000007 34 | #define printl(n) ("%lld\n",n); 35 | #define ll long long 36 | 37 | 38 | 39 | struct node{ 40 | ll sum; 41 | int lazy; 42 | }; 43 | 44 | node tree[300000]; 45 | int arr[100005]; 46 | 47 | void init(int idx,int ss,int se){ 48 | tree[idx].sum = arr[ss]; 49 | tree[idx].lazy = 0; 50 | } 51 | 52 | node combine(struct node left,struct node right) 53 | { 54 | node temp; 55 | temp.sum = left.sum + right.sum; 56 | temp.lazy = 0; 57 | return temp; 58 | } 59 | 60 | 61 | void build(int idx,int ss,int se){ 62 | if(ss==se){ 63 | init(idx,ss,se); 64 | } 65 | else{ 66 | int mid = ss + (se-ss)/2; 67 | build(2*idx+1,ss,mid); 68 | build(2*idx+2,mid+1,se); 69 | tree[idx] = combine(tree[2*idx+1],tree[2*idx+2]); 70 | } 71 | } 72 | 73 | 74 | void lazyProp(int idx,int ss,int se){ 75 | if(tree[idx].lazy==0) 76 | return ; 77 | if(ss!=se){ 78 | int left=2*idx+1 ; int right = left + 1; 79 | tree[left].lazy+=tree[idx].lazy; 80 | tree[right].lazy+=tree[idx].lazy; 81 | tree[idx].sum += ((se-ss+1)*tree[idx].lazy); 82 | // cout<se || qe < ss) 95 | return; 96 | if(ss>=qs && se <= qe){ 97 | tree[idx].lazy +=val; 98 | lazyProp(idx,ss,se); 99 | return; 100 | } 101 | if(ss!=se){ 102 | int left = 2*idx +1 ; int right = left+1; int mid = ss + (se-ss)/2; 103 | update(left,ss,mid,qs,qe,val); 104 | update(right,mid+1,se,qs,qe,val); 105 | tree[idx]=combine(tree[left],tree[right]); 106 | } 107 | } 108 | 109 | node query(int idx,int ss,int se,int qs,int qe){ 110 | lazyProp(idx,ss,se); 111 | // Four types of cases are possible 112 | if(ss>=qs && se <= qe) 113 | return tree[idx]; 114 | 115 | int mid = ss + (se-ss)/2; 116 | int left = 2*idx+1; 117 | int right = left + 1; 118 | 119 | if(qe<=mid){ 120 | return query(left,ss,mid,qs,qe); 121 | } 122 | if(qs>mid) 123 | return query(right,mid+1,se,qs,qe); 124 | 125 | return combine(query(left,ss,mid,qs,qe),query(right,mid+1,se,qs,qe)); 126 | } 127 | 128 | int main(){ 129 | int t,n,c,ch,p,q,v,i; 130 | si(t); 131 | while(t--){ 132 | si(n); si(c); 133 | CLEAR(arr); 134 | 135 | FOR(i,0,3*n) 136 | { 137 | tree[i].sum = tree[i].lazy = 0; 138 | } 139 | build(0,0,n-1); 140 | //FOR(i,0,2*n) 141 | //cout<