mirror of
https://github.com/Hizenberg469/Algorithms-snippets.git
synced 2026-04-19 22:52:23 +03:00
playground updated
This commit is contained in:
@@ -1,84 +1,84 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
bool comp(vector<int>& v1 , vector<int>& v2 ){
|
bool comp(vector<int>& v1 , vector<int>& v2 ){
|
||||||
return v1[0] > v2[0];
|
return v1[0] > v2[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
class Solution{
|
class Solution{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
class dsu{
|
class dsu{
|
||||||
public:
|
public:
|
||||||
vector<int> parent;
|
vector<int> parent;
|
||||||
vector<int> size;
|
vector<int> size;
|
||||||
|
|
||||||
dsu(int sz){
|
dsu(int sz){
|
||||||
parent.resize(sz);
|
parent.resize(sz);
|
||||||
size.resize(sz);
|
size.resize(sz);
|
||||||
|
|
||||||
for(int i = 0 ; i < sz ; i++ )
|
for(int i = 0 ; i < sz ; i++ )
|
||||||
parent[i] = i , size[i] = 1;
|
parent[i] = i , size[i] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int find(int n){
|
int find(int n){
|
||||||
if( parent[n] == n ) return n;
|
if( parent[n] == n ) return n;
|
||||||
|
|
||||||
return parent[n] = find( parent[n] );
|
return parent[n] = find( parent[n] );
|
||||||
}
|
}
|
||||||
|
|
||||||
int find_iterative( int n ){
|
int find_iterative( int n ){
|
||||||
while( parent[n] != n ){
|
while( parent[n] != n ){
|
||||||
n = parent[n];
|
n = parent[n];
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unite(int a, int b){
|
void unite(int a, int b){
|
||||||
|
|
||||||
int par_a = find(a);
|
int par_a = find(a);
|
||||||
int par_b = find(b);
|
int par_b = find(b);
|
||||||
|
|
||||||
if( par_a == par_b )
|
if( par_a == par_b )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if( size[par_a] < size[par_b] ) swap(par_a, par_b);
|
if( size[par_a] < size[par_b] ) swap(par_a, par_b);
|
||||||
|
|
||||||
size[par_a] += size[par_b];
|
size[par_a] += size[par_b];
|
||||||
|
|
||||||
parent[par_b] = par_a;
|
parent[par_b] = par_a;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isSame(int a , int b){
|
bool isSame(int a , int b){
|
||||||
return find(a) == find(b);
|
return find(a) == find(b);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Solution function template...
|
//Solution function template...
|
||||||
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
|
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
|
||||||
|
|
||||||
sort( edges.begin() , edges.end() , comp );
|
sort( edges.begin() , edges.end() , comp );
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int main( int argc, char* argv[] ){
|
int main( int argc, char* argv[] ){
|
||||||
|
|
||||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||||
|
|
||||||
Solution sol;
|
Solution sol;
|
||||||
|
|
||||||
//solution function call...
|
//solution function call...
|
||||||
|
|
||||||
|
|
||||||
//Extract result and print the solution..
|
//Extract result and print the solution..
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,45 +1,45 @@
|
|||||||
class dsu{
|
class dsu{
|
||||||
public:
|
public:
|
||||||
vector<int> parent;
|
vector<int> parent;
|
||||||
vector<int> size;
|
vector<int> size;
|
||||||
|
|
||||||
dsu(int sz){
|
dsu(int sz){
|
||||||
parent.resize(sz);
|
parent.resize(sz);
|
||||||
size.resize(sz);
|
size.resize(sz);
|
||||||
|
|
||||||
for(int i = 0 ; i < sz ; i++ )
|
for(int i = 0 ; i < sz ; i++ )
|
||||||
parent[i] = i , size[i] = 1;
|
parent[i] = i , size[i] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int find(int n){
|
int find(int n){
|
||||||
if( parent[n] == n ) return n;
|
if( parent[n] == n ) return n;
|
||||||
|
|
||||||
return parent[n] = find( parent[n] );
|
return parent[n] = find( parent[n] );
|
||||||
}
|
}
|
||||||
|
|
||||||
int find_iterative( int n ){
|
int find_iterative( int n ){
|
||||||
while( parent[n] != n ){
|
while( parent[n] != n ){
|
||||||
n = parent[n];
|
n = parent[n];
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unite(int a, int b){
|
void unite(int a, int b){
|
||||||
|
|
||||||
int par_a = find(a);
|
int par_a = find(a);
|
||||||
int par_b = find(b);
|
int par_b = find(b);
|
||||||
|
|
||||||
if( par_a == par_b )
|
if( par_a == par_b )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if( size[par_a] < size[par_b] ) swap(par_a, par_b);
|
if( size[par_a] < size[par_b] ) swap(par_a, par_b);
|
||||||
|
|
||||||
size[par_a] += size[par_b];
|
size[par_a] += size[par_b];
|
||||||
|
|
||||||
parent[par_b] = par_a;
|
parent[par_b] = par_a;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isSame(int a , int b){
|
bool isSame(int a , int b){
|
||||||
return find(a) == find(b);
|
return find(a) == find(b);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1,27 +1,27 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class Solution{
|
class Solution{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
//Solution function template...
|
//Solution function template...
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int main( int argc, char* argv[] ){
|
int main( int argc, char* argv[] ){
|
||||||
|
|
||||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||||
|
|
||||||
Solution sol;
|
Solution sol;
|
||||||
|
|
||||||
//solution function call...
|
//solution function call...
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Extract result and print the solution..
|
//Extract result and print the solution..
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,32 +1,32 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
/* cantalan Number using Iterative dp */
|
/* cantalan Number using Iterative dp */
|
||||||
int catalanNumber( int n ){
|
int catalanNumber( int n ){
|
||||||
|
|
||||||
vector<int> dp(n+1);
|
vector<int> dp(n+1);
|
||||||
|
|
||||||
dp[0] = dp[1] = 1;
|
dp[0] = dp[1] = 1;
|
||||||
|
|
||||||
for(int i = 2 ; i <= n ; i++ ){
|
for(int i = 2 ; i <= n ; i++ ){
|
||||||
|
|
||||||
for(int j = 0 ; j <= i ; j++ ){
|
for(int j = 0 ; j <= i ; j++ ){
|
||||||
|
|
||||||
dp[i] += dp[j-1]*dp[i-j];
|
dp[i] += dp[j-1]*dp[i-j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dp[n];
|
return dp[n];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Formula for Catalan Number
|
Formula for Catalan Number
|
||||||
(2n-C-n)/(n+1)
|
(2n-C-n)/(n+1)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int main(int argc, char* argv[] ){
|
int main(int argc, char* argv[] ){
|
||||||
|
|
||||||
cout << catalanNumber(2) << '\n';
|
cout << catalanNumber(2) << '\n';
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,69 +1,69 @@
|
|||||||
typedef long long int ll;
|
typedef long long int ll;
|
||||||
|
|
||||||
ll mod = 998244353;
|
ll mod = 998244353;
|
||||||
|
|
||||||
vector<ll> factorial;
|
vector<ll> factorial;
|
||||||
void calculateFactorial(ll n){
|
void calculateFactorial(ll n){
|
||||||
factorial.resize(n+1);
|
factorial.resize(n+1);
|
||||||
|
|
||||||
factorial[0] = factorial[1] = 1;
|
factorial[0] = factorial[1] = 1;
|
||||||
|
|
||||||
for( ll i = 2 ; i <= n ; i++ ){
|
for( ll i = 2 ; i <= n ; i++ ){
|
||||||
factorial[i] = ( factorial[i-1] * i ) % mod;
|
factorial[i] = ( factorial[i-1] * i ) % mod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T modMul(T a,T b,T M=(ll)1e9+7){
|
T modMul(T a,T b,T M=(ll)1e9+7){
|
||||||
return ((a%M)*(b%M))%M;
|
return ((a%M)*(b%M))%M;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T power(T a, T b, T M)
|
inline T power(T a, T b, T M)
|
||||||
{
|
{
|
||||||
T x = 1;
|
T x = 1;
|
||||||
while (b)
|
while (b)
|
||||||
{
|
{
|
||||||
if (b & 1) x = modMul(x,a,M);
|
if (b & 1) x = modMul(x,a,M);
|
||||||
a = modMul(a,a,M);
|
a = modMul(a,a,M);
|
||||||
b >>= 1;
|
b >>= 1;
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Using Fermat little Theorem */
|
/* Using Fermat little Theorem */
|
||||||
ll inverse(ll n ){
|
ll inverse(ll n ){
|
||||||
return power( n , mod - 2 , mod );
|
return power( n , mod - 2 , mod );
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<ll> inverse_factorial;
|
vector<ll> inverse_factorial;
|
||||||
void calculateInverseFactorial(ll n){
|
void calculateInverseFactorial(ll n){
|
||||||
|
|
||||||
inverse_factorial.resize(n+1);
|
inverse_factorial.resize(n+1);
|
||||||
inverse_factorial[0] = inverse_factorial[1] = 1;
|
inverse_factorial[0] = inverse_factorial[1] = 1;
|
||||||
|
|
||||||
for( ll i = 2 ; i <= n ; i++ ){
|
for( ll i = 2 ; i <= n ; i++ ){
|
||||||
inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod;
|
inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ll nCr( ll n , ll r ){
|
ll nCr( ll n , ll r ){
|
||||||
|
|
||||||
// -- n! --
|
// -- n! --
|
||||||
// | ------------- | % mod
|
// | ------------- | % mod
|
||||||
// -- n! * (n - r)! --
|
// -- n! * (n - r)! --
|
||||||
|
|
||||||
return ( ( ( factorial[n] * inverse_factorial[r] ) % mod
|
return ( ( ( factorial[n] * inverse_factorial[r] ) % mod
|
||||||
) * inverse_factorial[n-r] ) % mod;
|
) * inverse_factorial[n-r] ) % mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Catalan Number */
|
/* Catalan Number */
|
||||||
ll Cn( ll n ){
|
ll Cn( ll n ){
|
||||||
|
|
||||||
// -- 2n! --
|
// -- 2n! --
|
||||||
// | --------------- | % mod
|
// | --------------- | % mod
|
||||||
// -- n! * n! * (n+1) --
|
// -- n! * n! * (n+1) --
|
||||||
|
|
||||||
return ( ( ( ( ( factorial[2*n] * inverse_factorial[n] ) % mod )
|
return ( ( ( ( ( factorial[2*n] * inverse_factorial[n] ) % mod )
|
||||||
* inverse_factorial[n] ) % mod ) * inverse( n + 1 ) ) % mod;
|
* inverse_factorial[n] ) % mod ) * inverse( n + 1 ) ) % mod;
|
||||||
}
|
}
|
||||||
@@ -1,56 +1,56 @@
|
|||||||
ll mod = 998244353;
|
ll mod = 998244353;
|
||||||
|
|
||||||
vector<ll> factorial;
|
vector<ll> factorial;
|
||||||
void calculateFactorial(ll n){
|
void calculateFactorial(ll n){
|
||||||
factorial.resize(n+1);
|
factorial.resize(n+1);
|
||||||
|
|
||||||
factorial[0] = factorial[1] = 1;
|
factorial[0] = factorial[1] = 1;
|
||||||
|
|
||||||
for( ll i = 2 ; i <= n ; i++ ){
|
for( ll i = 2 ; i <= n ; i++ ){
|
||||||
factorial[i] = ( factorial[i-1] * i ) % mod;
|
factorial[i] = ( factorial[i-1] * i ) % mod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T modMul(T a,T b,T M=(ll)1e9+7){
|
T modMul(T a,T b,T M=(ll)1e9+7){
|
||||||
return ((a%M)*(b%M))%M;
|
return ((a%M)*(b%M))%M;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline T power(T a, T b, T M)
|
inline T power(T a, T b, T M)
|
||||||
{
|
{
|
||||||
T x = 1;
|
T x = 1;
|
||||||
while (b)
|
while (b)
|
||||||
{
|
{
|
||||||
if (b & 1) x = modMul(x,a,M);
|
if (b & 1) x = modMul(x,a,M);
|
||||||
a = modMul(a,a,M);
|
a = modMul(a,a,M);
|
||||||
b >>= 1;
|
b >>= 1;
|
||||||
}
|
}
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Using Fermat little Theorem */
|
/* Using Fermat little Theorem */
|
||||||
ll inverse(ll n ){
|
ll inverse(ll n ){
|
||||||
return power( n , mod - 2 , mod );
|
return power( n , mod - 2 , mod );
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<ll> inverse_factorial;
|
vector<ll> inverse_factorial;
|
||||||
void calculateInverseFactorial(ll n){
|
void calculateInverseFactorial(ll n){
|
||||||
|
|
||||||
inverse_factorial.resize(n+1);
|
inverse_factorial.resize(n+1);
|
||||||
inverse_factorial[0] = inverse_factorial[1] = 1;
|
inverse_factorial[0] = inverse_factorial[1] = 1;
|
||||||
|
|
||||||
for( ll i = 2 ; i <= n ; i++ ){
|
for( ll i = 2 ; i <= n ; i++ ){
|
||||||
inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod;
|
inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ll nCr( ll n , ll r ){
|
ll nCr( ll n , ll r ){
|
||||||
|
|
||||||
// -- n! --
|
// -- n! --
|
||||||
// | ------------- | % mod
|
// | ------------- | % mod
|
||||||
// -- n! * (n - r)! --
|
// -- n! * (n - r)! --
|
||||||
|
|
||||||
return ( ( ( factorial[n] * inverse_factorial[r] ) % mod
|
return ( ( ( factorial[n] * inverse_factorial[r] ) % mod
|
||||||
) * inverse_factorial[n-r] ) % mod;
|
) * inverse_factorial[n-r] ) % mod;
|
||||||
}
|
}
|
||||||
@@ -1,68 +1,68 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Fenwick_Tree{
|
class Fenwick_Tree{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// 1 based indexing...
|
// 1 based indexing...
|
||||||
vector<int> tree;
|
vector<int> tree;
|
||||||
int sz;
|
int sz;
|
||||||
|
|
||||||
void init(int n, const vector<int>& arr){
|
void init(int n, const vector<int>& arr){
|
||||||
tree.resize(n+1,0);
|
tree.resize(n+1,0);
|
||||||
|
|
||||||
this->sz = n;
|
this->sz = n;
|
||||||
|
|
||||||
for(int i = 1 ; i <= sz ; i++ ){
|
for(int i = 1 ; i <= sz ; i++ ){
|
||||||
|
|
||||||
update(i, arr[i-1]);
|
update(i, arr[i-1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int sum(int k){
|
int sum(int k){
|
||||||
int s = 0;
|
int s = 0;
|
||||||
|
|
||||||
while(k >= 1){
|
while(k >= 1){
|
||||||
|
|
||||||
s += tree[k];
|
s += tree[k];
|
||||||
|
|
||||||
k -= k & -k;
|
k -= k & -k;
|
||||||
}
|
}
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
void update(int k, int val){
|
void update(int k, int val){
|
||||||
|
|
||||||
while( k <= sz ){
|
while( k <= sz ){
|
||||||
tree[k] += val;
|
tree[k] += val;
|
||||||
|
|
||||||
k += k & -k;
|
k += k & -k;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print(){
|
void print(){
|
||||||
|
|
||||||
for(int i = 1 ; i <= sz ; i++ ){
|
for(int i = 1 ; i <= sz ; i++ ){
|
||||||
cout << tree[i] << ' ';
|
cout << tree[i] << ' ';
|
||||||
}
|
}
|
||||||
cout << '\n';
|
cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,89 +1,89 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
class SegmentTree{
|
class SegmentTree{
|
||||||
|
|
||||||
|
|
||||||
int sz;
|
int sz;
|
||||||
vector<int> tree;
|
vector<int> tree;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
SegmentTree(const vector<int>& arr){
|
SegmentTree(const vector<int>& arr){
|
||||||
|
|
||||||
this->sz = arr.size();
|
this->sz = arr.size();
|
||||||
|
|
||||||
tree.resize(2*this->sz+1,0);
|
tree.resize(2*this->sz+1,0);
|
||||||
|
|
||||||
init(arr);
|
init(arr);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void init(const vector<int>& arr){
|
void init(const vector<int>& arr){
|
||||||
|
|
||||||
for(int i = sz-1 ; i >= 0 ; i-- )
|
for(int i = sz-1 ; i >= 0 ; i-- )
|
||||||
tree[i+sz] = arr[i];
|
tree[i+sz] = arr[i];
|
||||||
|
|
||||||
for(int i = sz - 1 ; i >= 1 ; i-- ){
|
for(int i = sz - 1 ; i >= 1 ; i-- ){
|
||||||
|
|
||||||
tree[i] = tree[2*i] + tree[2*i+1];
|
tree[i] = tree[2*i] + tree[2*i+1];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void print(){
|
void print(){
|
||||||
|
|
||||||
cout << "Size : " << sz << '\n';
|
cout << "Size : " << sz << '\n';
|
||||||
|
|
||||||
for(int i = 0 ; i <= 2*sz ; i++ ){
|
for(int i = 0 ; i <= 2*sz ; i++ ){
|
||||||
|
|
||||||
cout << tree[i] << ' ';
|
cout << tree[i] << ' ';
|
||||||
}
|
}
|
||||||
cout << '\n';
|
cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
int sum( int a, int b){
|
int sum( int a, int b){
|
||||||
|
|
||||||
int s = 0;
|
int s = 0;
|
||||||
a += sz , b += sz;
|
a += sz , b += sz;
|
||||||
|
|
||||||
while( a <= b ){
|
while( a <= b ){
|
||||||
|
|
||||||
if( a%2 == 1 ) s += tree[a++];
|
if( a%2 == 1 ) s += tree[a++];
|
||||||
|
|
||||||
if( b%2 == 0 ) s += tree[b--];
|
if( b%2 == 0 ) s += tree[b--];
|
||||||
|
|
||||||
a /= 2 , b /= 2;
|
a /= 2 , b /= 2;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
void add(int k, int x) {
|
void add(int k, int x) {
|
||||||
k += sz;
|
k += sz;
|
||||||
tree[k] += x;
|
tree[k] += x;
|
||||||
for (k /= 2; k >= 1; k /= 2) {
|
for (k /= 2; k >= 1; k /= 2) {
|
||||||
tree[k] = tree[2*k]+tree[2*k+1];
|
tree[k] = tree[2*k]+tree[2*k+1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
|
|
||||||
vector<int> arr = {5,8,6,3,2,7,2,6};
|
vector<int> arr = {5,8,6,3,2,7,2,6};
|
||||||
|
|
||||||
SegmentTree stree(arr);
|
SegmentTree stree(arr);
|
||||||
|
|
||||||
|
|
||||||
stree.print();
|
stree.print();
|
||||||
|
|
||||||
stree.add(7,1);
|
stree.add(7,1);
|
||||||
cout << "(2,7) : " << stree.sum(2,7) << '\n';
|
cout << "(2,7) : " << stree.sum(2,7) << '\n';
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,49 +1,49 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
//Sparse Table for minimum value query.
|
//Sparse Table for minimum value query.
|
||||||
|
|
||||||
vector<vector<int>> table;
|
vector<vector<int>> table;
|
||||||
|
|
||||||
void precomputeSparseTable(int n, vector<int>& arr){
|
void precomputeSparseTable(int n, vector<int>& arr){
|
||||||
|
|
||||||
int lPower = log2(n);
|
int lPower = log2(n);
|
||||||
|
|
||||||
table.resize(n+1,vector<int>(lPower+1));
|
table.resize(n+1,vector<int>(lPower+1));
|
||||||
|
|
||||||
for(int p = 0 ; p <= lPower ; p++ ){
|
for(int p = 0 ; p <= lPower ; p++ ){
|
||||||
for(int i = 0 ; i + pow(2,p) - 1 <= n ; i++ ){
|
for(int i = 0 ; i + pow(2,p) - 1 <= n ; i++ ){
|
||||||
if( p == 0 ){
|
if( p == 0 ){
|
||||||
table[i][p] = arr[i];
|
table[i][p] = arr[i];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
table[i][p] = min(table[i][p-1],table[i+pow(2,p-1)][p-1]);
|
table[i][p] = min(table[i][p-1],table[i+pow(2,p-1)][p-1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// for(int i = 0 ; i <= n ; i++ ){
|
// for(int i = 0 ; i <= n ; i++ ){
|
||||||
// cout << i << " : ";
|
// cout << i << " : ";
|
||||||
// for(int p = 0 ; p <= lPower ; p++ ){
|
// for(int p = 0 ; p <= lPower ; p++ ){
|
||||||
// cout << table[i][p] << " ";
|
// cout << table[i][p] << " ";
|
||||||
// }
|
// }
|
||||||
// cout << endl;
|
// cout << endl;
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc,char* argv[]){
|
int main(int argc,char* argv[]){
|
||||||
|
|
||||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||||
|
|
||||||
int n,q;
|
int n,q;
|
||||||
cin>>n>>q;
|
cin>>n>>q;
|
||||||
|
|
||||||
vector<int> arr(n);
|
vector<int> arr(n);
|
||||||
for(int i = 0 ; i < n ; i++ ){
|
for(int i = 0 ; i < n ; i++ ){
|
||||||
cin>>arr[i];
|
cin>>arr[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
precomputeSparseTable(n,arr);
|
precomputeSparseTable(n,arr);
|
||||||
}
|
}
|
||||||
@@ -1,49 +1,49 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main(int argc, char* argv[]){
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
int n = atoi(argv[1]);
|
int n = atoi(argv[1]);
|
||||||
// cin>>n;
|
// cin>>n;
|
||||||
|
|
||||||
int arr[n];
|
int arr[n];
|
||||||
for(int i = 1 ; i <= n ; i++ )
|
for(int i = 1 ; i <= n ; i++ )
|
||||||
arr[i-1] = i;
|
arr[i-1] = i;
|
||||||
|
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
int cnt = 1;
|
int cnt = 1;
|
||||||
|
|
||||||
int elemLeft = n;
|
int elemLeft = n;
|
||||||
|
|
||||||
cout << "Operation:\n";
|
cout << "Operation:\n";
|
||||||
|
|
||||||
while( true ){
|
while( true ){
|
||||||
// cout << "Size : " << elemLeft << '\n';
|
// cout << "Size : " << elemLeft << '\n';
|
||||||
if( elemLeft <= 0 )
|
if( elemLeft <= 0 )
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if( cnt%2 == 0 ){
|
if( cnt%2 == 0 ){
|
||||||
while( arr[idx] == -1 )
|
while( arr[idx] == -1 )
|
||||||
idx = (idx+1)%n;
|
idx = (idx+1)%n;
|
||||||
|
|
||||||
cout << arr[idx] << ' ';
|
cout << arr[idx] << ' ';
|
||||||
arr[idx] = -1;
|
arr[idx] = -1;
|
||||||
|
|
||||||
elemLeft--;
|
elemLeft--;
|
||||||
if( elemLeft <= 0 )break;
|
if( elemLeft <= 0 )break;
|
||||||
|
|
||||||
while( arr[idx] == -1 )
|
while( arr[idx] == -1 )
|
||||||
idx = (idx+1)%n;
|
idx = (idx+1)%n;
|
||||||
cnt = 1;
|
cnt = 1;
|
||||||
|
|
||||||
// idx = (idx+1)%n;
|
// idx = (idx+1)%n;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
idx = (idx+1)%n;
|
idx = (idx+1)%n;
|
||||||
cnt++;
|
cnt++;
|
||||||
}
|
}
|
||||||
cout << '\n';
|
cout << '\n';
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main(int argc, char* argv[] ){
|
int main(int argc, char* argv[] ){
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
0
playground/IOFiles/correct_answer.txt
Normal file
0
playground/IOFiles/correct_answer.txt
Normal file
0
playground/IOFiles/output.txt
Normal file
0
playground/IOFiles/output.txt
Normal file
0
playground/IOFiles/testcase.txt
Normal file
0
playground/IOFiles/testcase.txt
Normal file
59
playground/Makefile
Normal file
59
playground/Makefile
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
BUILD_DIR = build
|
||||||
|
OBJS_DIR = $(BUILD_DIR)/objs
|
||||||
|
|
||||||
|
SOLUTION_BINARY = $(BUILD_DIR)/solution
|
||||||
|
BRUTEFORCE_BINARY = $(BUILD_DIR)/$(notdir Stress_Testing/bruteForce)
|
||||||
|
GENERATOR_BINARY = $(BUILD_DIR)/$(notdir Stress_Testing/generator)
|
||||||
|
CODEDIRS = . ./Stress_Testing
|
||||||
|
INCDIRS = .
|
||||||
|
|
||||||
|
|
||||||
|
CXX = g++
|
||||||
|
DEPFLAGS = -MP -MMD
|
||||||
|
CXXFLAGS = -Wall -Wextra -g \
|
||||||
|
$(foreach D,$(INCDIRS), -I$(D)) \
|
||||||
|
$(DEPFLAGS)
|
||||||
|
|
||||||
|
CXXFILES = $(foreach D, \
|
||||||
|
$(CODEDIRS), \
|
||||||
|
$(wildcard $(D)/*.cpp))
|
||||||
|
|
||||||
|
OBJECTS = $(patsubst %.cpp, $(OBJS_DIR)/%.o, $(notdir $(CXXFILES)))
|
||||||
|
DEPFILES = $(patsubst %.cpp, $(OBJS_DIR)/%.d, $(notdir $(CXXFILES)))
|
||||||
|
|
||||||
|
all: $(SOLUTION_BINARY) \
|
||||||
|
$(GENERATOR_BINARY) \
|
||||||
|
$(BRUTEFORCE_BINARY)
|
||||||
|
|
||||||
|
|
||||||
|
$(SOLUTION_BINARY): $(OBJS_DIR)/solution.o | $(OBJS_DIR)
|
||||||
|
$(CXX) -o $@ $<
|
||||||
|
|
||||||
|
$(GENERATOR_BINARY): $(OBJS_DIR)/generator.o | $(OBJS_DIR)
|
||||||
|
$(CXX) -o $@ $<
|
||||||
|
|
||||||
|
$(BRUTEFORCE_BINARY): $(OBJS_DIR)/bruteForce.o | $(OBJS_DIR)
|
||||||
|
$(CXX) -o $@ $<
|
||||||
|
|
||||||
|
|
||||||
|
$(OBJS_DIR)/%.o: %.cpp | $(OBJS_DIR)
|
||||||
|
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
|
$(OBJS_DIR)/%.o: Stress_Testing/%.cpp | $(OBJS_DIR)
|
||||||
|
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(SOLUTION_BINARY) \
|
||||||
|
$(BRUTEFORCE_BINARY) \
|
||||||
|
$(GENERATOR_BINARY) \
|
||||||
|
$(OBJECTS) \
|
||||||
|
$(DEPFILES)
|
||||||
|
|
||||||
|
help:
|
||||||
|
@echo "make clean: To remove executable, objects and dependency files."
|
||||||
|
@echo "make $(SOLUTION_BINARY): To build $(SOLUTION_BINARY) only."
|
||||||
|
@echo "make all: To build for Stress_Testing."
|
||||||
|
|
||||||
|
-include $(DEPFILES)
|
||||||
|
|
||||||
|
.PHONY: clean help all
|
||||||
42
playground/Stress_Testing/bruteForce.cpp
Normal file
42
playground/Stress_Testing/bruteForce.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
const int N = int(2e5) + 9;
|
||||||
|
|
||||||
|
int n, k, h;
|
||||||
|
int need = int(1e9);
|
||||||
|
int cnt[N];
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
scanf("%d %d", &n, &k);
|
||||||
|
for(int i = 0; i < n; ++i){
|
||||||
|
int x;
|
||||||
|
scanf("%d", &x);
|
||||||
|
h = max(h, x);
|
||||||
|
need = min(need, x);
|
||||||
|
++cnt[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
int pos = N - 1;
|
||||||
|
int res = 0;
|
||||||
|
long long sum = 0;
|
||||||
|
int c = 0;
|
||||||
|
while(true){
|
||||||
|
long long x = sum - c * 1LL * (pos - 1);
|
||||||
|
if(x > k){
|
||||||
|
++res;
|
||||||
|
h = pos;
|
||||||
|
sum = pos * 1LL * c;
|
||||||
|
}
|
||||||
|
|
||||||
|
--pos;
|
||||||
|
if(pos == need) break;
|
||||||
|
c += cnt[pos];
|
||||||
|
sum += cnt[pos] * 1LL * pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(h != need) ++res;
|
||||||
|
|
||||||
|
cout << res << endl;
|
||||||
|
}
|
||||||
31
playground/Stress_Testing/generator.cpp
Normal file
31
playground/Stress_Testing/generator.cpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int rnd(int a,int b){
|
||||||
|
return a + rand() % ( b - a + 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
|
int seed = atoi(argv[1]);
|
||||||
|
|
||||||
|
srand(seed);
|
||||||
|
|
||||||
|
int n = rnd(1,20);
|
||||||
|
|
||||||
|
int k = rnd(20, 40);
|
||||||
|
|
||||||
|
cout << n << ' ' << k << '\n';
|
||||||
|
|
||||||
|
int val = 0;
|
||||||
|
for(int i = 0 ; i < n ; i++ ){
|
||||||
|
|
||||||
|
val = rnd(0,50);
|
||||||
|
|
||||||
|
cout << val << ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << '\n';
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
83
playground/run.sh
Normal file
83
playground/run.sh
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
#Creating .txt files
|
||||||
|
TESTCASE_FILE=IOFiles/testcase.txt
|
||||||
|
OUTPUT_FILE=IOFiles/output.txt
|
||||||
|
CORRECT_ANSWER_FILE=IOFiles/correct_answer.txt
|
||||||
|
|
||||||
|
if ! [ -f $TESTCASE_FILE ]; then
|
||||||
|
echo "No $TESTCASE_FILE detected"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! [ -f $OUTPUT_FILE ]; then
|
||||||
|
echo "No $OUTPUT_FILE detected"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! [ -f $CORRECT_ANSWER_FILE ]; then
|
||||||
|
echo "No $CORRECT_ANSWER_FILE detected"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
#Executables.
|
||||||
|
|
||||||
|
SOLUTION=build/solution
|
||||||
|
|
||||||
|
make $SOLUTION
|
||||||
|
|
||||||
|
# Read and split test cases and answers based on empty lines
|
||||||
|
echo "Running test cases..."
|
||||||
|
pass_count=0
|
||||||
|
fail_count=0
|
||||||
|
|
||||||
|
# Separate test cases and expected outputs by empty lines
|
||||||
|
mapfile -t test_cases < <(awk -v RS= '1' "$TESTCASE_FILE")
|
||||||
|
mapfile -t expected_outputs < <(awk -v RS= '1' "$CORRECT_ANSWER_FILE")
|
||||||
|
|
||||||
|
# Ensure number of testcase is same as number of output.
|
||||||
|
if [ ${#test_cases[@]} -ne ${#expected_outputs[@]} ]; then
|
||||||
|
echo "The TestCase and answer count should be same."
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
for (( test_num = 0 ; test_num < ${#test_cases[@]} ; test_num++))
|
||||||
|
do
|
||||||
|
test_case=${test_cases[test_num]}
|
||||||
|
expected_output=${expected_outputs[test_num]}
|
||||||
|
|
||||||
|
if ! echo "$test_case" | ./$SOLUTION > "$OUTPUT_FILE"; then
|
||||||
|
echo -e "\033[0;31mTest #$(($test_num+1)): Execution failed\033[0m"
|
||||||
|
echo "solution executable didn't executed successfully"
|
||||||
|
let fail_count++
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
test_output=$(<"$OUTPUT_FILE")
|
||||||
|
|
||||||
|
if diff -q -Z <(echo "$test_output") <(echo "$expected_output") > /dev/null; then
|
||||||
|
echo -e "\033[0;32mTest #$(($test_num+1)): PASS\033[0m"
|
||||||
|
let pass_count++
|
||||||
|
else
|
||||||
|
echo -e "\033[0;31mTest #$(($test_num+1)): FAIL\033[0m"
|
||||||
|
echo "Test case:"
|
||||||
|
echo $test_case
|
||||||
|
echo "Your output:"
|
||||||
|
echo $test_output
|
||||||
|
echo "Correct output:"
|
||||||
|
echo $expected_output
|
||||||
|
let fail_count++
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
#Sumary
|
||||||
|
echo -e "\033[0;32mPassed test: $pass_count\033[0m"
|
||||||
|
echo -e "\033[0;31mFailed test: $fail_count\033[0m"
|
||||||
|
|
||||||
|
# Exit with appropriate status
|
||||||
|
if [ $fail_count -eq 0 ]; then
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
exit 3
|
||||||
|
fi
|
||||||
9
playground/solution.cpp
Normal file
9
playground/solution.cpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
int a,b;
|
||||||
|
cin >> a >> b;
|
||||||
|
cout << a << ' ' << b << '\n';
|
||||||
|
}
|
||||||
47
playground/stress.sh
Normal file
47
playground/stress.sh
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]; then
|
||||||
|
echo "Provide the limit of stress testing"
|
||||||
|
echo "$0 'test case limit'"
|
||||||
|
echo "$0 -1 (for unlimited)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
#Required tools
|
||||||
|
SOLUTION=build/solution
|
||||||
|
GENERATOR=build/generator
|
||||||
|
BRUTEFORCE=build/bruteForce
|
||||||
|
|
||||||
|
#Build necessary things
|
||||||
|
make all
|
||||||
|
|
||||||
|
#Files
|
||||||
|
TESTCASE_FILE=IOFiles/testcase.txt
|
||||||
|
OUTPUT_FILE=IOFiles/output.txt
|
||||||
|
CORRECT_ANSWER_FILE=IOFiles/correct_answer.txt
|
||||||
|
|
||||||
|
pass_count=0
|
||||||
|
|
||||||
|
while (( 1 ))
|
||||||
|
do
|
||||||
|
./generator > $TESTCASE_FILE
|
||||||
|
./SOLUTION < $TESTCASE_FILE > $OUTPUT_FILE
|
||||||
|
./BRUTEFORCE < $TESTCASE_FILE > $CORRECT_ANSWER_FILE
|
||||||
|
|
||||||
|
if diff -q -Z $OUTPUT_FILE $CORRECT_ANSWER_FILE >> /dev/null; then
|
||||||
|
echo -e "\033[0;31mTest #$(($pass_count+1)): FAIL\033[0m"
|
||||||
|
echo "Test case:"
|
||||||
|
cat $TESTCASE_FILE
|
||||||
|
echo "Your output:"
|
||||||
|
cat $OUTPUT_FILE
|
||||||
|
echo "Correct output:"
|
||||||
|
cat $CORRECT_ANSWER_FILE
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "\033[0;32mPassed test: #$(($pass_count+1))\033[0m"
|
||||||
|
|
||||||
|
if [ $1 -ne -1 ] || [ $pass_count -lt $1 ]; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
Reference in New Issue
Block a user