From 1102359bb9617d7a6cfa0cd0aa7de5dd3aa96871 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 25 Feb 2020 10:37:13 +0530 Subject: [PATCH 01/22] answering format --- answering.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 answering.md diff --git a/answering.md b/answering.md new file mode 100644 index 0000000..7a99785 --- /dev/null +++ b/answering.md @@ -0,0 +1,8 @@ + answer code goes as follows + + EASY PROBLEM 7 + +ANSWER CODE : E07.cpp OR E07.py etc + +similarly M for medium and H for hard + From b0d7d90eb08506c88302081d098c7f5bdecc160d Mon Sep 17 00:00:00 2001 From: root Date: Tue, 25 Feb 2020 10:40:08 +0530 Subject: [PATCH 02/22] added folder --- readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index b199f2f..3087b48 100644 --- a/readme.md +++ b/readme.md @@ -263,4 +263,5 @@ To mark the checkbox, change [ ] to [x]. 30. [ ] [https://goo.gl/nqXngJ](https://goo.gl/nqXngJ) 31. [ ] [https://www.hackerrank.com/challenges/iterate-it/problem](https://www.hackerrank.com/challenges/iterate-it/problem) 32. [ ] [https://www.codechef.com/problems/DGCD](https://www.codechef.com/problems/DGCD) -33. [ ] [http://www.spoj.com/problems/ANARC05H/](http://www.spoj.com/problems/ANARC05H/) \ No newline at end of file +33. [ ] [http://www.spoj.com/problems/ANARC05H/](http://www.spoj.com/problems/ANARC05H/) + From c9020b11b0c390d538e4a96d68a79568e5224034 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Tue, 25 Feb 2020 11:31:02 +0530 Subject: [PATCH 03/22] E01 added --- answers/E01.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 answers/E01.cpp diff --git a/answers/E01.cpp b/answers/E01.cpp new file mode 100644 index 0000000..0bbc82a --- /dev/null +++ b/answers/E01.cpp @@ -0,0 +1,16 @@ +#include +//dont forget the case of a=0; +using namespace std; +long long int sol(long long int a){ + if (a==0)return 0; + if(a%2==1)return 1; + else return 2*sol(a/2); +} +int main(){ + long long int a; + while(!feof(stdin)){ + cin>>a; + cout< Date: Tue, 25 Feb 2020 11:33:44 +0530 Subject: [PATCH 04/22] marked E01 --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 3087b48..5ae6b86 100644 --- a/readme.md +++ b/readme.md @@ -98,7 +98,7 @@ To mark the checkbox, change [ ] to [x]. #### Easy -1. [ ] [https://goo.gl/Kkf8eD](https://goo.gl/Kkf8eD) +1. [x] [https://goo.gl/Kkf8eD](https://goo.gl/Kkf8eD) 2. [ ] [https://www.codechef.com/problems/SCC0103](https://www.codechef.com/problems/SCC0103) 3. [ ] [http://www.spoj.com/problems/GSS1/](http://www.spoj.com/problems/GSS1/) 4. [ ] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) From 378b1bc3f8e74330094a0e61fc608fa720d0a20b Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Tue, 25 Feb 2020 11:48:28 +0530 Subject: [PATCH 05/22] Delete E01.cpp --- answers/E01.cpp | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 answers/E01.cpp diff --git a/answers/E01.cpp b/answers/E01.cpp deleted file mode 100644 index 0bbc82a..0000000 --- a/answers/E01.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -//dont forget the case of a=0; -using namespace std; -long long int sol(long long int a){ - if (a==0)return 0; - if(a%2==1)return 1; - else return 2*sol(a/2); -} -int main(){ - long long int a; - while(!feof(stdin)){ - cin>>a; - cout< Date: Tue, 25 Feb 2020 11:48:41 +0530 Subject: [PATCH 06/22] Delete answering.md --- answering.md | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 answering.md diff --git a/answering.md b/answering.md deleted file mode 100644 index 7a99785..0000000 --- a/answering.md +++ /dev/null @@ -1,8 +0,0 @@ - answer code goes as follows - - EASY PROBLEM 7 - -ANSWER CODE : E07.cpp OR E07.py etc - -similarly M for medium and H for hard - From 5a3e4b3a9390cf7c2dcc28f8a02508a8bb006699 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Tue, 25 Feb 2020 11:49:01 +0530 Subject: [PATCH 07/22] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 5ae6b86..3087b48 100644 --- a/readme.md +++ b/readme.md @@ -98,7 +98,7 @@ To mark the checkbox, change [ ] to [x]. #### Easy -1. [x] [https://goo.gl/Kkf8eD](https://goo.gl/Kkf8eD) +1. [ ] [https://goo.gl/Kkf8eD](https://goo.gl/Kkf8eD) 2. [ ] [https://www.codechef.com/problems/SCC0103](https://www.codechef.com/problems/SCC0103) 3. [ ] [http://www.spoj.com/problems/GSS1/](http://www.spoj.com/problems/GSS1/) 4. [ ] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) From 3caa64abb7888dc581052521c2cb3573db5dec67 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Tue, 31 Mar 2020 10:00:49 +0530 Subject: [PATCH 08/22] done 2 sums( E03 E04) 1 repeted(E06) 1 not available(E05) --- answers/E03.cpp | 39 +++++++++++++++++++++++++++++++++++++++ answers/E04.cpp | 21 +++++++++++++++++++++ readme.md | 8 ++++---- 3 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 answers/E03.cpp create mode 100644 answers/E04.cpp diff --git a/answers/E03.cpp b/answers/E03.cpp new file mode 100644 index 0000000..bc3712f --- /dev/null +++ b/answers/E03.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +#define geti(x) int x;cin>>x; +#define getl(x) long long int x;in>>x; +#define FOR(i,a,b) for(i=a;i=l;i--){ + ans1+=a[i]; + ans2=max(ans1,ans2); + } + return max(ans,ans2+ans0+a[mid]); +} +int run(){ + geti(l) + geti(r) + cout<>a[i]; + geti(t) + while(t--) + run(); + return 0; +} diff --git a/answers/E04.cpp b/answers/E04.cpp new file mode 100644 index 0000000..73713bc --- /dev/null +++ b/answers/E04.cpp @@ -0,0 +1,21 @@ +#include +#include +using namespace std; +#define geti(x) int x;cin>>x; +#define getl(x) long long int x;in>>x; +#define FOR(i,a,b) for(i=a;i>N; + double sol2=(sqrt(1+8*N)-1)/2; + long long int sol=sol2; + if(sol+0.0!=sol2)sol+=1; + cout< Date: Tue, 31 Mar 2020 10:15:03 +0530 Subject: [PATCH 09/22] added deleted files --- answers/E01.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 answers/E01.cpp diff --git a/answers/E01.cpp b/answers/E01.cpp new file mode 100644 index 0000000..5650c8a --- /dev/null +++ b/answers/E01.cpp @@ -0,0 +1,17 @@ + +#include +//dont forget the case of a=0; +using namespace std; +long long int sol(long long int a){ + if (a==0)return 0; + if(a%2==1)return 1; + else return 2*sol(a/2); +} +int main(){ + long long int a; + while(!feof(stdin)){ + cin>>a; + cout< Date: Tue, 31 Mar 2020 10:20:55 +0530 Subject: [PATCH 10/22] modified readme and added answering format --- answering.md | 7 +++++++ readme.md | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 answering.md diff --git a/answering.md b/answering.md new file mode 100644 index 0000000..68f2995 --- /dev/null +++ b/answering.md @@ -0,0 +1,7 @@ +answer code goes as follows + +EASY PROBLEM 7 + +ANSWER CODE : E07.cpp OR E07.py etc + +similarly M for medium and H for hard \ No newline at end of file diff --git a/readme.md b/readme.md index d2f568f..8a2ed45 100644 --- a/readme.md +++ b/readme.md @@ -98,7 +98,7 @@ To mark the checkbox, change [ ] to [x]. #### Easy -1. [ ] [https://goo.gl/Kkf8eD](https://goo.gl/Kkf8eD) +1. [X] [https://goo.gl/Kkf8eD](https://goo.gl/Kkf8eD) 2. [ ] [https://www.codechef.com/problems/SCC0103](https://www.codechef.com/problems/SCC0103) 3. [X] [http://www.spoj.com/problems/GSS1/](http://www.spoj.com/problems/GSS1/) 4. [X] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) From c1aa2d96afd07bbafd3bd79f93b742bab350acc2 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Tue, 31 Mar 2020 14:09:15 +0530 Subject: [PATCH 11/22] added answer to E02 --- answers/E02.cpp | 25 +++++++++++++++++++++++++ readme.md | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 answers/E02.cpp diff --git a/answers/E02.cpp b/answers/E02.cpp new file mode 100644 index 0000000..a34498a --- /dev/null +++ b/answers/E02.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; +int i; +#define geti(x) int x;cin>>x; +#define getl(x) long long int x;in>>x; +#define FOR(i,a,b) for(i=a;i>h>>m; + a[h][m]++; + maximum=max(maximum,a[h][m]); + } + cout< Date: Tue, 31 Mar 2020 15:24:30 +0530 Subject: [PATCH 12/22] 7 --- answers/E05.txt | 1 + answers/E06.txt | 1 + answers/E07.cpp | 101 ++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 2 +- 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 answers/E05.txt create mode 100644 answers/E06.txt create mode 100644 answers/E07.cpp diff --git a/answers/E05.txt b/answers/E05.txt new file mode 100644 index 0000000..a67620c --- /dev/null +++ b/answers/E05.txt @@ -0,0 +1 @@ +problem unvailable \ No newline at end of file diff --git a/answers/E06.txt b/answers/E06.txt new file mode 100644 index 0000000..2d2dd09 --- /dev/null +++ b/answers/E06.txt @@ -0,0 +1 @@ +refer E04.cpp \ No newline at end of file diff --git a/answers/E07.cpp b/answers/E07.cpp new file mode 100644 index 0000000..504bbac --- /dev/null +++ b/answers/E07.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +vector split_string(string); + +// Complete the twoArrays function below. +string twoArrays(int k, vector A, vector B) { + sort(A.begin(),A.end()); + sort(B.begin(),B.end(),greater()); + int l=A.size(); + while(l--){ + if(A[l]+B[l]> q; + cin.ignore(numeric_limits::max(), '\n'); + + for (int q_itr = 0; q_itr < q; q_itr++) { + string nk_temp; + getline(cin, nk_temp); + + vector nk = split_string(nk_temp); + + int n = stoi(nk[0]); + + int k = stoi(nk[1]); + + string A_temp_temp; + getline(cin, A_temp_temp); + + vector A_temp = split_string(A_temp_temp); + + vector A(n); + + for (int i = 0; i < n; i++) { + int A_item = stoi(A_temp[i]); + + A[i] = A_item; + } + + string B_temp_temp; + getline(cin, B_temp_temp); + + vector B_temp = split_string(B_temp_temp); + + vector B(n); + + for (int i = 0; i < n; i++) { + int B_item = stoi(B_temp[i]); + + B[i] = B_item; + } + + string result = twoArrays(k, A, B); + + fout << result << "\n"; + } + + fout.close(); + + return 0; +} + +vector split_string(string input_string) { + string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { + return x == y and x == ' '; + }); + + input_string.erase(new_end, input_string.end()); + + while (input_string[input_string.length() - 1] == ' ') { + input_string.pop_back(); + } + + vector splits; + char delimiter = ' '; + + size_t i = 0; + size_t pos = input_string.find(delimiter); + + while (pos != string::npos) { + splits.push_back(input_string.substr(i, pos - i)); + + i = pos + 1; + pos = input_string.find(delimiter, i); + } + + splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); + + return splits; +} diff --git a/readme.md b/readme.md index 8a8d63f..d20bdb5 100644 --- a/readme.md +++ b/readme.md @@ -104,7 +104,7 @@ To mark the checkbox, change [ ] to [x]. 4. [X] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) 5. [ ] {not available}[https://goo.gl/PTpmny](https://goo.gl/PTpmny) 6. [X] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) -7. [ ] [https://www.hackerrank.com/challenges/two-arrays/problem](https://www.hackerrank.com/challenges/two-arrays/problem) +7. [X] [https://www.hackerrank.com/challenges/two-arrays/problem](https://www.hackerrank.com/challenges/two-arrays/problem) 8. [ ] [http://www.spoj.com/problems/FREQUENT/](http://www.spoj.com/problems/FREQUENT/) 9. [ ] [http://www.spoj.com/problems/BUGLIFE/](http://www.spoj.com/problems/BUGLIFE/) 10. [ ] [https://goo.gl/shR8UK](https://goo.gl/shR8UK) From 53bccfa548ac8bc79ee8bb404280295650018b4a Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Wed, 29 Apr 2020 08:20:18 +0530 Subject: [PATCH 13/22] edited readme --- answers/E03.cpp | 76 +++++++++++++++++++++++++++---------------------- answers/E08.cpp | 39 +++++++++++++++++++++++++ readme.md | 2 +- 3 files changed, 82 insertions(+), 35 deletions(-) create mode 100644 answers/E08.cpp diff --git a/answers/E03.cpp b/answers/E03.cpp index bc3712f..ea7244e 100644 --- a/answers/E03.cpp +++ b/answers/E03.cpp @@ -1,39 +1,47 @@ -#include +#include using namespace std; -#define geti(x) int x;cin>>x; -#define getl(x) long long int x;in>>x; -#define FOR(i,a,b) for(i=a;i=l;i--){ - ans1+=a[i]; - ans2=max(ans1,ans2); - } - return max(ans,ans2+ans0+a[mid]); } -int run(){ - geti(l) - geti(r) - cout<end) + return -15009; + else if(x<=start && y>=end) + return tree[node]; + else{ + mid=(start+end)/2; + p1=maxsum(2*node,start,mid,x,y); + p2=maxsum(2*node+1,mid+1,end,x,y); + return max(max(p1,p2),p1+p2); + } + } -int main(){ - geti(n) - a=new int[n+5]; - FOR(i,1,n+1)cin>>a[i]; - geti(t) - while(t--) - run(); - return 0; +int main() +{ +ll n,m,x,y,i; +cin>>n; +ll a[n+1]; +for(i=1;i<=n;i++) +cin>>a[i]; +build_tree(a,1,1,n); +cin>>m; +while(m--) +{ +cin>>x>>y; +cout< +#include +using namespace std; +int i; +#define geti(x) int x;cin>>x; +#define getl(x) long long int x;in>>x; +#define FOR(i,a,b) for(i=a;ir) + return 0; + int mid=(l+r)/2; + int ans=max(run(a,l,mid-1),run(a,mid+1,r)); + int midans=1; + FOR(i,mid+1,r){ + if(a[mid]==a[i])midans++; + } + for(i=mid-1;i>=l;i--){ + if(a[mid]==a[i])midans--; + } + return max(ans,midans); +} +int main(){ + cin>>n; + int l,r,q; + while(n){ + cin>>q; + int a[n]; + FOR(i,0,n)cin>>a[i]; + while(q--){ + cin>>l>>r; + cout<>n; + } + return 0; +} diff --git a/readme.md b/readme.md index d20bdb5..f187c17 100644 --- a/readme.md +++ b/readme.md @@ -100,7 +100,7 @@ To mark the checkbox, change [ ] to [x]. 1. [X] [https://goo.gl/Kkf8eD](https://goo.gl/Kkf8eD) 2. [X] [https://www.codechef.com/problems/SCC0103](https://www.codechef.com/problems/SCC0103) -3. [X] [http://www.spoj.com/problems/GSS1/](http://www.spoj.com/problems/GSS1/) +3. [ ] [http://www.spoj.com/problems/GSS1/](http://www.spoj.com/problems/GSS1/) 4. [X] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) 5. [ ] {not available}[https://goo.gl/PTpmny](https://goo.gl/PTpmny) 6. [X] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) From 48129d856a1d45bc4fd97662ec3cd15f19b444c1 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Wed, 29 Apr 2020 08:39:58 +0530 Subject: [PATCH 14/22] added 1 --- answers/E08.cpp | 8 +++++--- answers/E13_COINS.cpp | 24 ++++++++++++++++++++++++ readme.md | 6 +++--- 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 answers/E13_COINS.cpp diff --git a/answers/E08.cpp b/answers/E08.cpp index 39acd26..d64c142 100644 --- a/answers/E08.cpp +++ b/answers/E08.cpp @@ -14,15 +14,17 @@ int run(int *a,int l,int r){ int mid=(l+r)/2; int ans=max(run(a,l,mid-1),run(a,mid+1,r)); int midans=1; - FOR(i,mid+1,r){ + FOR(i,mid+1,r+1){ if(a[mid]==a[i])midans++; } for(i=mid-1;i>=l;i--){ - if(a[mid]==a[i])midans--; + if(a[mid]==a[i])midans++; } return max(ans,midans); } int main(){ + ios_base::sync_with_stdio(false); + cin.tie(NULL); cin>>n; int l,r,q; while(n){ @@ -31,7 +33,7 @@ int main(){ FOR(i,0,n)cin>>a[i]; while(q--){ cin>>l>>r; - cout<>n; } diff --git a/answers/E13_COINS.cpp b/answers/E13_COINS.cpp new file mode 100644 index 0000000..f2d8cfb --- /dev/null +++ b/answers/E13_COINS.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; +class solution{ + private: + map m; + public: + long long int byteland_doller_conversion(int n){ + if(n==0)return 0; + if (m[n]!=0)return m[n]; + long long int answer=byteland_doller_conversion(n/2)+byteland_doller_conversion(n/3)+byteland_doller_conversion(n/4); + if(answer>n){ + cout< Date: Wed, 29 Apr 2020 10:03:03 +0530 Subject: [PATCH 15/22] added 1 --- answers/E09_BUGLIFE.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ readme.md | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 answers/E09_BUGLIFE.cpp diff --git a/answers/E09_BUGLIFE.cpp b/answers/E09_BUGLIFE.cpp new file mode 100644 index 0000000..29b2ae9 --- /dev/null +++ b/answers/E09_BUGLIFE.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +#define geti(x) int x;cin>>x; +#define getl(x) long long int x;in>>x; +#define FOR(i,a,b) for(i=a;i *v,int *color,int i){ + for(int k:v[i]){ + if(color[k]==0){ + color[k]=-color[i]; + if(dfs(v,color,k))return true; + } + if(color[k]==color[i]){ + return true; + } + } + return false; +} +bool run(){ + int i,t1,t2,n,q; + cin>>n>>q; + vector v[n+1]; + while(q--){ + cin>>t1>>t2; + v[t1].pb(t2); + v[t2].pb(t1); + } + int color[n+1]={0}; + for(i=1;i<=n;i++){ + if(color[i]==0&&v[i].size()!=0){ + color[i]=1; + if(dfs(v,color,i))return true; + } + } + return false; +} +int main(){ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + geti(t) + int i; + FOR(i,1,t+1){ + cout<<"Scenario #"< Date: Fri, 8 May 2020 13:23:48 +0530 Subject: [PATCH 16/22] gss1 --- answers/E03.cpp | 47 ------------------------ answers/E03_GSS1.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++ answers/input | 9 +++++ 3 files changed, 96 insertions(+), 47 deletions(-) delete mode 100644 answers/E03.cpp create mode 100644 answers/E03_GSS1.cpp create mode 100644 answers/input diff --git a/answers/E03.cpp b/answers/E03.cpp deleted file mode 100644 index ea7244e..0000000 --- a/answers/E03.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -using namespace std; -typedef long long int ll; -ll tree[400005]; -void build_tree(ll a[],ll node,ll start,ll end) -{ - if(start==end) - tree[node]=a[start]; - else - { - ll mid=(start+end)/2; - build_tree(a,2*node,start,mid); - build_tree(a,2*node+1,mid+1,end); - tree[node]=max(max(tree[2*node],tree[2*node+1]),tree[2*node]+tree[2*node+1]); - // cout<end) - return -15009; - else if(x<=start && y>=end) - return tree[node]; - else{ - mid=(start+end)/2; - p1=maxsum(2*node,start,mid,x,y); - p2=maxsum(2*node+1,mid+1,end,x,y); - return max(max(p1,p2),p1+p2); - } - -} -int main() -{ -ll n,m,x,y,i; -cin>>n; -ll a[n+1]; -for(i=1;i<=n;i++) -cin>>a[i]; -build_tree(a,1,1,n); -cin>>m; -while(m--) -{ -cin>>x>>y; -cout< +using namespace std; +typedef long long int ll; +class tree{ + public: + ll sum; + ll maxSum; + ll maxPrefixSum; + ll maxSuffixSum; +}segTree[200005]; +int a[50004],n; +tree zero; +tree join(tree left,tree right){ + tree sol; + sol.sum=left.sum+right.sum; + sol.maxSum=max(left.maxSum,right.maxSum); + if(left.maxSum>=0&&right.maxSum>=0) + sol.maxSum=max(sol.maxSum,left.maxSuffixSum+right.maxPrefixSum); + sol.maxPrefixSum=max(left.maxPrefixSum,left.sum+right.maxPrefixSum); + sol.maxSuffixSum=max(right.maxSuffixSum,right.sum+left.maxSuffixSum); + return sol; +} +void buildSegTree(int i,int l,int r){ + if(l>r)return ; + if(l==r){ + segTree[i].sum=a[l]; + segTree[i].maxSum=a[l]; + segTree[i].maxPrefixSum=a[l]; + segTree[i].maxSuffixSum=a[l]; + } + else{ + int mid=(l+r)/2; + buildSegTree(2*i,l,mid); + buildSegTree(2*i+1,mid+1,r); + segTree[i]=join(segTree[2*i],segTree[2*i+1]); + } +} +tree query_old(int treeIndex,int treeLeft,int treeRight,int l,int r){ + if(treeLeft>treeRight)return zero; + if(treeLeft==treeRight)return segTree[treeIndex]; + int treeMid=(treeLeft+treeRight)/2; + if(treeMid=r)return query(2*treeIndex,treeLeft,treeMid,l,r); + return join(query(2*treeIndex,treeLeft,treeMid,l,r),query(2*treeIndex+1,treeMid+1,treeRight,l,r)); +} + +tree query_new(int node,int start,int end,int l,int r) +{ + if(rend) + { + tree zero; + zero.sum=-20000; +zero.maxSum=-20000; +zero.maxPrefixSum=-20000; +zero.maxSuffixSum=-20000; + return zero; + } + if(start>=l && end<=r) + { + return segTree[node]; + } + int mid=(start+end)/2; + tree p1=query_new(2*node,start,mid,l,r); + tree p2=query_new(2*node+1,mid+1,end,l,r); + tree temp=join(p1,p2); + return temp; +} +int main(){ + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + zero.maxPrefixSum=zero.maxSuffixSum=zero.sum=-20000; + zero.maxSum=-20000; + cin>>n; + for(int i=0;i>a[i]; + } + buildSegTree(1,0,n-1); + int q; + cin>>q; + int l,r; + while(q--){ + cin>>l>>r; + cout< Date: Fri, 8 May 2020 13:26:53 +0530 Subject: [PATCH 17/22] readme added gss1 --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 99e06e2..cb226d0 100644 --- a/readme.md +++ b/readme.md @@ -100,7 +100,7 @@ To mark the checkbox, change [ ] to [x]. 1. [X] [https://goo.gl/Kkf8eD](https://goo.gl/Kkf8eD) 2. [X] [https://www.codechef.com/problems/SCC0103](https://www.codechef.com/problems/SCC0103) -3. [ ] [http://www.spoj.com/problems/GSS1/](http://www.spoj.com/problems/GSS1/) +3. [X] [http://www.spoj.com/problems/GSS1/](http://www.spoj.com/problems/GSS1/) 4. [X] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) 5. [X] {not available}[https://goo.gl/PTpmny](https://goo.gl/PTpmny) 6. [X] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) From b31cf10b907ba7d87d3673a09b78cb495b25fd41 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Fri, 8 May 2020 14:11:39 +0530 Subject: [PATCH 18/22] gss3 --- answers/E03_GSS1.cpp | 6 +-- answers/M52_GSS3.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 answers/M52_GSS3.cpp diff --git a/answers/E03_GSS1.cpp b/answers/E03_GSS1.cpp index 121eaf7..e99ff23 100644 --- a/answers/E03_GSS1.cpp +++ b/answers/E03_GSS1.cpp @@ -39,9 +39,9 @@ tree query_old(int treeIndex,int treeLeft,int treeRight,int l,int r){ if(treeLeft>treeRight)return zero; if(treeLeft==treeRight)return segTree[treeIndex]; int treeMid=(treeLeft+treeRight)/2; - if(treeMid=r)return query(2*treeIndex,treeLeft,treeMid,l,r); - return join(query(2*treeIndex,treeLeft,treeMid,l,r),query(2*treeIndex+1,treeMid+1,treeRight,l,r)); + if(treeMid=r)return query_old(2*treeIndex,treeLeft,treeMid,l,r); + return join(query_old(2*treeIndex,treeLeft,treeMid,l,r),query_old(2*treeIndex+1,treeMid+1,treeRight,l,r)); } tree query_new(int node,int start,int end,int l,int r) diff --git a/answers/M52_GSS3.cpp b/answers/M52_GSS3.cpp new file mode 100644 index 0000000..1a18f04 --- /dev/null +++ b/answers/M52_GSS3.cpp @@ -0,0 +1,97 @@ +#include +using namespace std; +typedef long long int ll; +class tree{ + public: + ll sum; + ll maxSum; + ll maxPrefixSum; + ll maxSuffixSum; +}segTree[200005]; +int a[50004],n; +tree zero; +tree join(tree left,tree right){ + tree sol; + sol.sum=left.sum+right.sum; + sol.maxSum=max(left.maxSum,right.maxSum); + if(left.maxSum>=0&&right.maxSum>=0) + sol.maxSum=max(sol.maxSum,left.maxSuffixSum+right.maxPrefixSum); + sol.maxPrefixSum=max(left.maxPrefixSum,left.sum+right.maxPrefixSum); + sol.maxSuffixSum=max(right.maxSuffixSum,right.sum+left.maxSuffixSum); + return sol; +} +void buildSegTree(int i,int l,int r){ + if(l>r)return ; + if(l==r){ + segTree[i].sum=a[l]; + segTree[i].maxSum=a[l]; + segTree[i].maxPrefixSum=a[l]; + segTree[i].maxSuffixSum=a[l]; + } + else{ + int mid=(l+r)/2; + buildSegTree(2*i,l,mid); + buildSegTree(2*i+1,mid+1,r); + segTree[i]=join(segTree[2*i],segTree[2*i+1]); + } +} + +tree query(int node,int start,int end,int l,int r) +{ + if(rend) + { + tree zero; + zero.sum=-20000; +zero.maxSum=-20000; +zero.maxPrefixSum=-20000; +zero.maxSuffixSum=-20000; + return zero; + } + if(start>=l && end<=r) + { + return segTree[node]; + } + int mid=(start+end)/2; + tree p1=query(2*node,start,mid,l,r); + tree p2=query(2*node+1,mid+1,end,l,r); + tree temp=join(p1,p2); + return temp; +} +void update(int node,int start,int end,int leaf,int value){ + if(start==end){ + segTree[node].sum=segTree[node].maxSum=segTree[node].maxPrefixSum=segTree[node].maxSuffixSum=value; + return; + } + int mid=(start+end)/2; + if(leaf<=mid){ + update(node*2,start,mid,leaf,value); + } + else{ + update(2*node+1,mid+1,end,leaf,value); + } + segTree[node]=join(segTree[2*node],segTree[2*node+1]); +} +int main(){ + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + zero.maxPrefixSum=zero.maxSuffixSum=zero.sum=-20000; + zero.maxSum=-20000; + cin>>n; + for(int i=0;i>a[i]; + } + buildSegTree(1,0,n-1); + int q; + cin>>q; + int l,r; + int type; + while(q--){ + cin>>type>>l>>r; + if(type) + cout< Date: Fri, 8 May 2020 14:12:16 +0530 Subject: [PATCH 19/22] added in readme --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index cb226d0..8b289d0 100644 --- a/readme.md +++ b/readme.md @@ -206,7 +206,7 @@ To mark the checkbox, change [ ] to [x]. 49. [ ] [http://codeforces.com/problemset/problem/596/D](http://codeforces.com/problemset/problem/596/D) 50. [ ] [https://goo.gl/oXTucZ](https://goo.gl/oXTucZ) 51. [ ] [http://www.spoj.com/problems/INVCNT/](http://www.spoj.com/problems/INVCNT/) -52. [ ] [http://www.spoj.com/problems/GSS3/](http://www.spoj.com/problems/GSS3/) +52. [X] [http://www.spoj.com/problems/GSS3/](http://www.spoj.com/problems/GSS3/) 53. [ ] [http://codeforces.com/problemset/problem/340/D](http://codeforces.com/problemset/problem/340/D) 54. [ ] [http://www.spoj.com/problems/RMID2/ [heaps]](http://www.spoj.com/problems/RMID2/) 55. [ ] [http://www.spoj.com/problems/DCEPC206/](http://www.spoj.com/problems/DCEPC206/) From 5cacc4477fd3f48eaf10315b67e0af232baa8f63 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Fri, 8 May 2020 23:10:57 +0530 Subject: [PATCH 20/22] added horrible --- answers/E19_HORRIBLE.cpp | 88 ++++++++++++++++++++++++++++++++++++++++ answers/input | 24 +++++++---- 2 files changed, 103 insertions(+), 9 deletions(-) create mode 100644 answers/E19_HORRIBLE.cpp diff --git a/answers/E19_HORRIBLE.cpp b/answers/E19_HORRIBLE.cpp new file mode 100644 index 0000000..cdc8b2b --- /dev/null +++ b/answers/E19_HORRIBLE.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; +typedef long long ll; +class node{ +public: + long long int sum; + long long int extra; + node *left=NULL; + node *right=NULL; + node(ll sum=0,ll extra=0){ + this->sum=sum; + this->extra=extra; + } + void destroy(){ + if(left!=NULL){ + left->destroy(); + free(left); + } + if(right!=NULL){ + right->destroy(); + free(right); + } + } + ~node(){ + destroy(); + } + void createChildren(){ + if(left==NULL)left=(node *)malloc(sizeof(node)); + if(right==NULL)right=(node *)malloc(sizeof(node)); + } + void propagateExtra(ll start,ll end){ + if(start==end)return; + ll mid=(start+end)/2; + left->extra+=extra; + left->sum+=extra*(mid-start+1); + right->extra+=extra; + right->sum+=extra*(end-mid-1+1); + extra=0; + } + void update(ll start,ll end,ll l,ll r,ll value){ + if(l>end||rupdate(start,mid,l,r,value); + right->update(mid+1,end,l,r,value); + sum=right->sum+left->sum; + } + long long int query(ll start,ll end,ll l,ll r){ + if(l>end||rquery(start,mid,l,r)+right->query(mid+1,end,l,r); + } +}; +void run(node &head){ + int n,c; + cin>>n>>c; + ll type,l,r,value; + while(c--){ + cin>>type>>l>>r; + if(!type){ + cin>>value; + head.update(1,n,l,r,value); + } + else{ + cout<>t; + node head[t]; + while(t--){ + run(head[t]); + } +} \ No newline at end of file diff --git a/answers/input b/answers/input index 10d6324..92b215c 100644 --- a/answers/input +++ b/answers/input @@ -1,9 +1,15 @@ -3 --1 2 3 -6 -1 1 -1 2 -1 3 -2 2 -2 3 -3 3 \ No newline at end of file +2 +8 6 +0 2 4 26 +0 4 8 80 +0 4 5 20 +1 8 8 +0 5 7 14 +1 4 8 +8 6 +0 2 4 26 +0 4 8 80 +0 4 5 20 +1 8 8 +0 5 7 14 +1 4 8 \ No newline at end of file From c41dc26e994f2fc3d16b0850a0a032e73b32f230 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Fri, 8 May 2020 23:11:39 +0530 Subject: [PATCH 21/22] added horrible --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 8b289d0..dd5523d 100644 --- a/readme.md +++ b/readme.md @@ -116,7 +116,7 @@ To mark the checkbox, change [ ] to [x]. 16. [ ] [http://www.spoj.com/problems/TOPOSORT/](http://www.spoj.com/problems/TOPOSORT/) 17. [ ] [https://www.hackerrank.com/challenges/angry-children/problem](https://www.hackerrank.com/challenges/angry-children/problem) 18. [ ] [http://www.spoj.com/problems/QUEEN/](http://www.spoj.com/problems/QUEEN/) -19. [ ] [http://www.spoj.com/problems/HORRIBLE/](http://www.spoj.com/problems/HORRIBLE/) +19. [X] [http://www.spoj.com/problems/HORRIBLE/](http://www.spoj.com/problems/HORRIBLE/) 20. [ ] [http://www.spoj.com/problems/PAIRS1/](http://www.spoj.com/problems/PAIRS1/) 21. [ ] [http://www.spoj.com/problems/JUSTAPAL/](http://www.spoj.com/problems/JUSTAPAL/) 22. [ ] [https://goo.gl/m5DbXZ](https://goo.gl/m5DbXZ) From 4525e0002dd5b8026cf1d87f19fc96c57e6326d2 Mon Sep 17 00:00:00 2001 From: CHILUKURI SRI HARSHA Date: Sat, 9 May 2020 19:09:47 +0530 Subject: [PATCH 22/22] FREQUENT --- answers/E08.cpp | 41 ------------------ answers/E08_FREQUENT.cpp | 93 ++++++++++++++++++++++++++++++++++++++++ readme.md | 2 +- 3 files changed, 94 insertions(+), 42 deletions(-) delete mode 100644 answers/E08.cpp create mode 100644 answers/E08_FREQUENT.cpp diff --git a/answers/E08.cpp b/answers/E08.cpp deleted file mode 100644 index d64c142..0000000 --- a/answers/E08.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -using namespace std; -int i; -#define geti(x) int x;cin>>x; -#define getl(x) long long int x;in>>x; -#define FOR(i,a,b) for(i=a;ir) - return 0; - int mid=(l+r)/2; - int ans=max(run(a,l,mid-1),run(a,mid+1,r)); - int midans=1; - FOR(i,mid+1,r+1){ - if(a[mid]==a[i])midans++; - } - for(i=mid-1;i>=l;i--){ - if(a[mid]==a[i])midans++; - } - return max(ans,midans); -} -int main(){ - ios_base::sync_with_stdio(false); - cin.tie(NULL); - cin>>n; - int l,r,q; - while(n){ - cin>>q; - int a[n]; - FOR(i,0,n)cin>>a[i]; - while(q--){ - cin>>l>>r; - cout<>n; - } - return 0; -} diff --git a/answers/E08_FREQUENT.cpp b/answers/E08_FREQUENT.cpp new file mode 100644 index 0000000..3c371fe --- /dev/null +++ b/answers/E08_FREQUENT.cpp @@ -0,0 +1,93 @@ +#include +using namespace std; +typedef int ll; +class tree{ + public: + ll maxFrequency; + ll leftFrequency; + ll rightFrequency; + ll rightElement; + ll leftElement; +}; +tree *segTree; +int a[100004],n; +tree zero; +tree join(tree left,tree right){ + tree sol; + sol.maxFrequency=max(left.maxFrequency,right.maxFrequency); + if(left.rightElement==right.leftElement) + sol.maxFrequency=max(sol.maxFrequency,left.rightFrequency+right.leftFrequency); + sol.leftFrequency=left.leftElement==right.leftElement?left.leftFrequency+right.leftFrequency:left.leftFrequency; + sol.rightFrequency=left.rightElement==right.rightElement?left.rightFrequency+right.rightFrequency:right.rightFrequency; + sol.leftElement=left.leftElement; + sol.rightElement=right.rightElement; + return sol; +} +void buildSegTree(int i,int l,int r){ + if(l>r)return ; + if(l==r){ + segTree[i].rightElement=a[l]; + segTree[i].leftElement=a[l]; + segTree[i].maxFrequency=1; + segTree[i].leftFrequency=segTree[i].rightFrequency=1; + } + else{ + int mid=(l+r)/2; + buildSegTree(2*i,l,mid); + buildSegTree(2*i+1,mid+1,r); + segTree[i]=join(segTree[2*i],segTree[2*i+1]); + } +} +tree query_old(int treeIndex,int treeLeft,int treeRight,int l,int r){ + if(treeLeft>treeRight)return zero; + if(treeLeft==treeRight)return segTree[treeIndex]; + int treeMid=(treeLeft+treeRight)/2; + if(treeMid=r)return query_old(2*treeIndex,treeLeft,treeMid,l,r); + return join(query_old(2*treeIndex,treeLeft,treeMid,l,r),query_old(2*treeIndex+1,treeMid+1,treeRight,l,r)); +} + +tree query_new(int node,int start,int end,int l,int r) +{ + if(rend) + { + tree zero; + zero.rightElement=0; + zero.leftElement=0; + zero.maxFrequency=0; + zero.leftFrequency=zero.rightFrequency=0; + return zero; + } + if(start>=l && end<=r) + { + return segTree[node]; + } + int mid=(start+end)/2; + tree p1=query_new(2*node,start,mid,l,r); + tree p2=query_new(2*node+1,mid+1,end,l,r); + tree temp=join(p1,p2); + return temp; +} +int main(){ + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + int q; + cin>>n; + while(n){ + tree myTree[4*n]; + segTree=myTree; + cin>>q; + for(int i=0;i>a[i]; + } + buildSegTree(1,0,n-1); + int l,r; + while(q--){ + cin>>l>>r; + cout<>n; + } + return 0; +} \ No newline at end of file diff --git a/readme.md b/readme.md index dd5523d..8da9788 100644 --- a/readme.md +++ b/readme.md @@ -105,7 +105,7 @@ To mark the checkbox, change [ ] to [x]. 5. [X] {not available}[https://goo.gl/PTpmny](https://goo.gl/PTpmny) 6. [X] [https://www.codechef.com/problems/BITMASK3](https://www.codechef.com/problems/BITMASK3) 7. [X] [https://www.hackerrank.com/challenges/two-arrays/problem](https://www.hackerrank.com/challenges/two-arrays/problem) -8. [ ] [http://www.spoj.com/problems/FREQUENT/](http://www.spoj.com/problems/FREQUENT/) +8. [X] [http://www.spoj.com/problems/FREQUENT/](http://www.spoj.com/problems/FREQUENT/) 9. [X] [http://www.spoj.com/problems/BUGLIFE/](http://www.spoj.com/problems/BUGLIFE/) 10. [X] {not available}[https://goo.gl/shR8UK](https://goo.gl/shR8UK) 11. [ ] [http://www.spoj.com/problems/GERGOVIA/](http://www.spoj.com/problems/GERGOVIA/)