From ce313ff07ae91492cf93ba9166fa689fc98509c9 Mon Sep 17 00:00:00 2001 From: Jatin Agrawal Date: 2017年12月14日 23:49:39 +0530 Subject: [PATCH 1/4] added algorithm for sparse table --- .../Search/sparse-table/readme.md | Bin 0 -> 4226 bytes .../Search/sparse-table/sparse-table.cpp | 60 ++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Competitive Coding/Search/sparse-table/readme.md create mode 100644 Competitive Coding/Search/sparse-table/sparse-table.cpp diff --git a/Competitive Coding/Search/sparse-table/readme.md b/Competitive Coding/Search/sparse-table/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..22414fbc375cd85fd3163b3ba027765234b6797b GIT binary patch literal 4226 zcmb`K+izP%5XSczi6`C=NC*1jkmlx8LLe?G5~xTKKOXpg zGoI`o=h&oCW!c_6J3I5uea^rCJW5r1nJ!WHmwNtAPe!(y=?Txr=|-#Psg*wedXl!Zo*9A@ zQ9stMZ#*jT>7}=N#=Vjn{^urfE`F6s7)ZiR+SRVE4Lm{{?x4jqy%ya}QZDpCySd2# zsq;a0$UyRepg6 ztcZQjC39j}uC&H}L;dz-!?FI+p|9PgVYxAGPBGJ+zl}pXor!W`{=y%X?XT_DNPDb1 z)P3YuX;j6PuySNtAuZ=&s(#y`_laYH=^j7?l_*3vMV{q zsVj`fqQe>!;pyq=#E>@I15Zq3aq!KI9-ndtFJ$dT+Cho`o9qwW}tPxbtbg~eo z`+BHTk0|2Xqq5Ys^xx0A71_TU$w&!np;_jObTWG?iO3|@$Uou0M)lcI%`cHQ;~b@JX>^vL_8U~LxQi@wTtyQyu)|%faq3*cYaeRg zpGW^*R%n!7`%Jx1)3m>m7VEw+g6yS-#&{oz!)^Tt5$DKUF@X-P|B{6HOJtBCe820ql+!Sg$D z%ywv{!$MM?9K6jYRj9hc0xcFs%4%0V(k${qdf)@sxv59x$rm1*`f0~xGRJPI=Z!Zz5izv54G&3e){q3nmK{9sgvGM-%WE# wSm34Enzw_PXT1hfTYb{rRDFq`{Ei&C$y?HUt88>If5R%@sy)Nwjhd?ZA0z>*U;qFB literal 0 HcmV?d00001 diff --git a/Competitive Coding/Search/sparse-table/sparse-table.cpp b/Competitive Coding/Search/sparse-table/sparse-table.cpp new file mode 100644 index 000000000..cebe6f3af --- /dev/null +++ b/Competitive Coding/Search/sparse-table/sparse-table.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +#define md 1000000007 +#define ll long long int +#define vi vector +#define vll vector +#define pb push_back +#define all(c) (c).begin(),(c).end() +int main() +{ + + int n; + cin>>n;//Input the number of elements in the array. + int a[n]; + for(int i=0;i>a[i];//read all the elements + } + int cols = log(n)/log(2);//Cols represent the no of coloumns in the sparse table . + int rows = n;//rows represent the no of rows in the sparse table + int stable[rows][cols+1]; + for(int i=0;i<=cols;i++)//this is the procedure to build a sparse table + { + + for(int j=0;ja[stable[j][i-1]])//This is the procedure to create a sparse table for range minimum query. This statement of code can be appropriately changed for another type of query. + stable[j][i]=stable[j][i-1]; + else + stable[j][i] = stable[j+(int)pow(2,i-1)][i-1]; + + } + } + //cout<>left>>right; + int k = right-left+1;//This represent the total length of the query range. + + int col = (int)(log(k)/log(2));//This represent the coloumn we first look for in the sparse table. + + int min1 = a[stable[left-1][col]]; + int remaining = k- pow(2,col); + int min2 = a[stable[left+remaining-1][col]]; + + cout<<"minimum in the given range is :"< Date: 2017年12月14日 23:53:03 +0530 Subject: [PATCH 2/4] readme updated --- .../Search/sparse-table/readme.md | Bin 4226 -> 4072 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/Competitive Coding/Search/sparse-table/readme.md b/Competitive Coding/Search/sparse-table/readme.md index 22414fbc375cd85fd3163b3ba027765234b6797b..0521091cb66db46cb6e444cfd94e6aec1c5045d5 100644 GIT binary patch delta 10 RcmZotd?7!7vgCUhcfgzD0i6Nh%49qSDvT_(o8A=#3fvh~BY!Ohr5GaxfW)%ZP zK;k(Jl|Yt1g9d|?QXYdQkX_7B05mBXs9FceO9Gpr05m6&AsZ+L(ub@XXucFfF4)Qv Wpn(ctD^h`0R5Iu>C~UM`&kq2t^&eOO From c47eef516cb0a6a4de9c286c1d3a756fdd7e24cc Mon Sep 17 00:00:00 2001 From: Jatin Agrawal Date: 2017年12月14日 23:57:45 +0530 Subject: [PATCH 3/4] final updations made --- .../Search/sparse-table/readme.md | Bin 4072 -> 3320 bytes .../Search/sparse-table/sparse-table.cpp | 14 +++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Competitive Coding/Search/sparse-table/readme.md b/Competitive Coding/Search/sparse-table/readme.md index 0521091cb66db46cb6e444cfd94e6aec1c5045d5..219bc050498c5861eb359645f95ff0fda5e00f50 100644 GIT binary patch delta 31 pcmV+)0O0@VANU!tumzJ?2Dp+B7_-g?t^||R3^J3f4YZR459Hjj4M_k1 delta 578 zcmew%`9glf23A&m27LyF$?@E>!Uhbn4ABfmKr)s=fx(8s7AW4!Eg5NoB(4LL;bq_g z>H+FvC}GH8NM*T39C!m1{@;XW&27^SR86e`g0x^iemmv_veuy170v8y3ij!-&XCQ2c1|iIL PXb@7-Zn!Uif%g&smXcl} diff --git a/Competitive Coding/Search/sparse-table/sparse-table.cpp b/Competitive Coding/Search/sparse-table/sparse-table.cpp index cebe6f3af..131cfc027 100644 --- a/Competitive Coding/Search/sparse-table/sparse-table.cpp +++ b/Competitive Coding/Search/sparse-table/sparse-table.cpp @@ -19,24 +19,24 @@ int main() } int cols = log(n)/log(2);//Cols represent the no of coloumns in the sparse table . int rows = n;//rows represent the no of rows in the sparse table - int stable[rows][cols+1]; + int lookup[rows][cols+1]; for(int i=0;i<=cols;i++)//this is the procedure to build a sparse table { for(int j=0;ja[stable[j][i-1]])//This is the procedure to create a sparse table for range minimum query. This statement of code can be appropriately changed for another type of query. - stable[j][i]=stable[j][i-1]; + if(a[lookup[j+(int)pow(2,i-1)][i-1]]>a[lookup[j][i-1]])//This is the procedure to create a sparse table for range minimum query. This statement of code can be appropriately changed for another type of query. + lookup[j][i]=lookup[j][i-1]; else - stable[j][i] = stable[j+(int)pow(2,i-1)][i-1]; + lookup[j][i] = lookup[j+(int)pow(2,i-1)][i-1]; } } @@ -50,9 +50,9 @@ int main() int col = (int)(log(k)/log(2));//This represent the coloumn we first look for in the sparse table. - int min1 = a[stable[left-1][col]]; + int min1 = a[lookup[left-1][col]]; int remaining = k- pow(2,col); - int min2 = a[stable[left+remaining-1][col]]; + int min2 = a[lookup[left+remaining-1][col]]; cout<<"minimum in the given range is :"< Date: 2017年12月28日 16:24:46 +0530 Subject: [PATCH 4/4] tilde added --- .../Search/sparse-table/readme.md | Bin 3320 -> 3344 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/Competitive Coding/Search/sparse-table/readme.md b/Competitive Coding/Search/sparse-table/readme.md index 219bc050498c5861eb359645f95ff0fda5e00f50..676d01ef52d3ba588472a9c5f7ad4abf032a128b 100644 GIT binary patch delta 117 zcmew%IYDZ}8%D;u$p=}*Co?fkVXWIcl}VKuDA3O>KKTNx1eEoURRgNXhK&QvlAWBu mrhp=5&tts#0^2kesFoUT5ilziNak`&PUhh;0kZ6Qb^-w9LnRvk delta 77 zcmbOr^+R&Q8^*~7Y*Ldsm?mtV#iYVK`2efl

AltStyle によって変換されたページ (->オリジナル) /