mirror of
https://github.com/Hizenberg469/Algorithms-snippets.git
synced 2026-04-19 14:42:23 +03:00
playground updated
This commit is contained in:
@@ -1,84 +1,84 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool comp(vector<int>& v1 , vector<int>& v2 ){
|
||||
return v1[0] > v2[0];
|
||||
}
|
||||
|
||||
class Solution{
|
||||
|
||||
public:
|
||||
|
||||
class dsu{
|
||||
public:
|
||||
vector<int> parent;
|
||||
vector<int> size;
|
||||
|
||||
dsu(int sz){
|
||||
parent.resize(sz);
|
||||
size.resize(sz);
|
||||
|
||||
for(int i = 0 ; i < sz ; i++ )
|
||||
parent[i] = i , size[i] = 1;
|
||||
}
|
||||
|
||||
int find(int n){
|
||||
if( parent[n] == n ) return n;
|
||||
|
||||
return parent[n] = find( parent[n] );
|
||||
}
|
||||
|
||||
int find_iterative( int n ){
|
||||
while( parent[n] != n ){
|
||||
n = parent[n];
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void unite(int a, int b){
|
||||
|
||||
int par_a = find(a);
|
||||
int par_b = find(b);
|
||||
|
||||
if( par_a == par_b )
|
||||
return;
|
||||
|
||||
if( size[par_a] < size[par_b] ) swap(par_a, par_b);
|
||||
|
||||
size[par_a] += size[par_b];
|
||||
|
||||
parent[par_b] = par_a;
|
||||
}
|
||||
|
||||
bool isSame(int a , int b){
|
||||
return find(a) == find(b);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
//Solution function template...
|
||||
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
|
||||
|
||||
sort( edges.begin() , edges.end() , comp );
|
||||
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main( int argc, char* argv[] ){
|
||||
|
||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||
|
||||
Solution sol;
|
||||
|
||||
//solution function call...
|
||||
|
||||
|
||||
//Extract result and print the solution..
|
||||
|
||||
|
||||
return 0;
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool comp(vector<int>& v1 , vector<int>& v2 ){
|
||||
return v1[0] > v2[0];
|
||||
}
|
||||
|
||||
class Solution{
|
||||
|
||||
public:
|
||||
|
||||
class dsu{
|
||||
public:
|
||||
vector<int> parent;
|
||||
vector<int> size;
|
||||
|
||||
dsu(int sz){
|
||||
parent.resize(sz);
|
||||
size.resize(sz);
|
||||
|
||||
for(int i = 0 ; i < sz ; i++ )
|
||||
parent[i] = i , size[i] = 1;
|
||||
}
|
||||
|
||||
int find(int n){
|
||||
if( parent[n] == n ) return n;
|
||||
|
||||
return parent[n] = find( parent[n] );
|
||||
}
|
||||
|
||||
int find_iterative( int n ){
|
||||
while( parent[n] != n ){
|
||||
n = parent[n];
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void unite(int a, int b){
|
||||
|
||||
int par_a = find(a);
|
||||
int par_b = find(b);
|
||||
|
||||
if( par_a == par_b )
|
||||
return;
|
||||
|
||||
if( size[par_a] < size[par_b] ) swap(par_a, par_b);
|
||||
|
||||
size[par_a] += size[par_b];
|
||||
|
||||
parent[par_b] = par_a;
|
||||
}
|
||||
|
||||
bool isSame(int a , int b){
|
||||
return find(a) == find(b);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
//Solution function template...
|
||||
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
|
||||
|
||||
sort( edges.begin() , edges.end() , comp );
|
||||
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main( int argc, char* argv[] ){
|
||||
|
||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||
|
||||
Solution sol;
|
||||
|
||||
//solution function call...
|
||||
|
||||
|
||||
//Extract result and print the solution..
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,45 +1,45 @@
|
||||
class dsu{
|
||||
public:
|
||||
vector<int> parent;
|
||||
vector<int> size;
|
||||
|
||||
dsu(int sz){
|
||||
parent.resize(sz);
|
||||
size.resize(sz);
|
||||
|
||||
for(int i = 0 ; i < sz ; i++ )
|
||||
parent[i] = i , size[i] = 1;
|
||||
}
|
||||
|
||||
int find(int n){
|
||||
if( parent[n] == n ) return n;
|
||||
|
||||
return parent[n] = find( parent[n] );
|
||||
}
|
||||
|
||||
int find_iterative( int n ){
|
||||
while( parent[n] != n ){
|
||||
n = parent[n];
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void unite(int a, int b){
|
||||
|
||||
int par_a = find(a);
|
||||
int par_b = find(b);
|
||||
|
||||
if( par_a == par_b )
|
||||
return;
|
||||
|
||||
if( size[par_a] < size[par_b] ) swap(par_a, par_b);
|
||||
|
||||
size[par_a] += size[par_b];
|
||||
|
||||
parent[par_b] = par_a;
|
||||
}
|
||||
|
||||
bool isSame(int a , int b){
|
||||
return find(a) == find(b);
|
||||
}
|
||||
class dsu{
|
||||
public:
|
||||
vector<int> parent;
|
||||
vector<int> size;
|
||||
|
||||
dsu(int sz){
|
||||
parent.resize(sz);
|
||||
size.resize(sz);
|
||||
|
||||
for(int i = 0 ; i < sz ; i++ )
|
||||
parent[i] = i , size[i] = 1;
|
||||
}
|
||||
|
||||
int find(int n){
|
||||
if( parent[n] == n ) return n;
|
||||
|
||||
return parent[n] = find( parent[n] );
|
||||
}
|
||||
|
||||
int find_iterative( int n ){
|
||||
while( parent[n] != n ){
|
||||
n = parent[n];
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
void unite(int a, int b){
|
||||
|
||||
int par_a = find(a);
|
||||
int par_b = find(b);
|
||||
|
||||
if( par_a == par_b )
|
||||
return;
|
||||
|
||||
if( size[par_a] < size[par_b] ) swap(par_a, par_b);
|
||||
|
||||
size[par_a] += size[par_b];
|
||||
|
||||
parent[par_b] = par_a;
|
||||
}
|
||||
|
||||
bool isSame(int a , int b){
|
||||
return find(a) == find(b);
|
||||
}
|
||||
};
|
||||
@@ -1,27 +1,27 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution{
|
||||
|
||||
public:
|
||||
|
||||
//Solution function template...
|
||||
|
||||
};
|
||||
|
||||
int main( int argc, char* argv[] ){
|
||||
|
||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||
|
||||
Solution sol;
|
||||
|
||||
//solution function call...
|
||||
|
||||
|
||||
|
||||
//Extract result and print the solution..
|
||||
|
||||
|
||||
return 0;
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Solution{
|
||||
|
||||
public:
|
||||
|
||||
//Solution function template...
|
||||
|
||||
};
|
||||
|
||||
int main( int argc, char* argv[] ){
|
||||
|
||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||
|
||||
Solution sol;
|
||||
|
||||
//solution function call...
|
||||
|
||||
|
||||
|
||||
//Extract result and print the solution..
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,32 +1,32 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* cantalan Number using Iterative dp */
|
||||
int catalanNumber( int n ){
|
||||
|
||||
vector<int> dp(n+1);
|
||||
|
||||
dp[0] = dp[1] = 1;
|
||||
|
||||
for(int i = 2 ; i <= n ; i++ ){
|
||||
|
||||
for(int j = 0 ; j <= i ; j++ ){
|
||||
|
||||
dp[i] += dp[j-1]*dp[i-j];
|
||||
}
|
||||
}
|
||||
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/*
|
||||
Formula for Catalan Number
|
||||
(2n-C-n)/(n+1)
|
||||
*/
|
||||
|
||||
int main(int argc, char* argv[] ){
|
||||
|
||||
cout << catalanNumber(2) << '\n';
|
||||
return 0;
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* cantalan Number using Iterative dp */
|
||||
int catalanNumber( int n ){
|
||||
|
||||
vector<int> dp(n+1);
|
||||
|
||||
dp[0] = dp[1] = 1;
|
||||
|
||||
for(int i = 2 ; i <= n ; i++ ){
|
||||
|
||||
for(int j = 0 ; j <= i ; j++ ){
|
||||
|
||||
dp[i] += dp[j-1]*dp[i-j];
|
||||
}
|
||||
}
|
||||
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/*
|
||||
Formula for Catalan Number
|
||||
(2n-C-n)/(n+1)
|
||||
*/
|
||||
|
||||
int main(int argc, char* argv[] ){
|
||||
|
||||
cout << catalanNumber(2) << '\n';
|
||||
return 0;
|
||||
}
|
||||
@@ -1,69 +1,69 @@
|
||||
typedef long long int ll;
|
||||
|
||||
ll mod = 998244353;
|
||||
|
||||
vector<ll> factorial;
|
||||
void calculateFactorial(ll n){
|
||||
factorial.resize(n+1);
|
||||
|
||||
factorial[0] = factorial[1] = 1;
|
||||
|
||||
for( ll i = 2 ; i <= n ; i++ ){
|
||||
factorial[i] = ( factorial[i-1] * i ) % mod;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T modMul(T a,T b,T M=(ll)1e9+7){
|
||||
return ((a%M)*(b%M))%M;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T power(T a, T b, T M)
|
||||
{
|
||||
T x = 1;
|
||||
while (b)
|
||||
{
|
||||
if (b & 1) x = modMul(x,a,M);
|
||||
a = modMul(a,a,M);
|
||||
b >>= 1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/* Using Fermat little Theorem */
|
||||
ll inverse(ll n ){
|
||||
return power( n , mod - 2 , mod );
|
||||
}
|
||||
|
||||
vector<ll> inverse_factorial;
|
||||
void calculateInverseFactorial(ll n){
|
||||
|
||||
inverse_factorial.resize(n+1);
|
||||
inverse_factorial[0] = inverse_factorial[1] = 1;
|
||||
|
||||
for( ll i = 2 ; i <= n ; i++ ){
|
||||
inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod;
|
||||
}
|
||||
}
|
||||
|
||||
ll nCr( ll n , ll r ){
|
||||
|
||||
// -- n! --
|
||||
// | ------------- | % mod
|
||||
// -- n! * (n - r)! --
|
||||
|
||||
return ( ( ( factorial[n] * inverse_factorial[r] ) % mod
|
||||
) * inverse_factorial[n-r] ) % mod;
|
||||
}
|
||||
|
||||
/* Catalan Number */
|
||||
ll Cn( ll n ){
|
||||
|
||||
// -- 2n! --
|
||||
// | --------------- | % mod
|
||||
// -- n! * n! * (n+1) --
|
||||
|
||||
return ( ( ( ( ( factorial[2*n] * inverse_factorial[n] ) % mod )
|
||||
* inverse_factorial[n] ) % mod ) * inverse( n + 1 ) ) % mod;
|
||||
typedef long long int ll;
|
||||
|
||||
ll mod = 998244353;
|
||||
|
||||
vector<ll> factorial;
|
||||
void calculateFactorial(ll n){
|
||||
factorial.resize(n+1);
|
||||
|
||||
factorial[0] = factorial[1] = 1;
|
||||
|
||||
for( ll i = 2 ; i <= n ; i++ ){
|
||||
factorial[i] = ( factorial[i-1] * i ) % mod;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T modMul(T a,T b,T M=(ll)1e9+7){
|
||||
return ((a%M)*(b%M))%M;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T power(T a, T b, T M)
|
||||
{
|
||||
T x = 1;
|
||||
while (b)
|
||||
{
|
||||
if (b & 1) x = modMul(x,a,M);
|
||||
a = modMul(a,a,M);
|
||||
b >>= 1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/* Using Fermat little Theorem */
|
||||
ll inverse(ll n ){
|
||||
return power( n , mod - 2 , mod );
|
||||
}
|
||||
|
||||
vector<ll> inverse_factorial;
|
||||
void calculateInverseFactorial(ll n){
|
||||
|
||||
inverse_factorial.resize(n+1);
|
||||
inverse_factorial[0] = inverse_factorial[1] = 1;
|
||||
|
||||
for( ll i = 2 ; i <= n ; i++ ){
|
||||
inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod;
|
||||
}
|
||||
}
|
||||
|
||||
ll nCr( ll n , ll r ){
|
||||
|
||||
// -- n! --
|
||||
// | ------------- | % mod
|
||||
// -- n! * (n - r)! --
|
||||
|
||||
return ( ( ( factorial[n] * inverse_factorial[r] ) % mod
|
||||
) * inverse_factorial[n-r] ) % mod;
|
||||
}
|
||||
|
||||
/* Catalan Number */
|
||||
ll Cn( ll n ){
|
||||
|
||||
// -- 2n! --
|
||||
// | --------------- | % mod
|
||||
// -- n! * n! * (n+1) --
|
||||
|
||||
return ( ( ( ( ( factorial[2*n] * inverse_factorial[n] ) % mod )
|
||||
* inverse_factorial[n] ) % mod ) * inverse( n + 1 ) ) % mod;
|
||||
}
|
||||
@@ -1,56 +1,56 @@
|
||||
ll mod = 998244353;
|
||||
|
||||
vector<ll> factorial;
|
||||
void calculateFactorial(ll n){
|
||||
factorial.resize(n+1);
|
||||
|
||||
factorial[0] = factorial[1] = 1;
|
||||
|
||||
for( ll i = 2 ; i <= n ; i++ ){
|
||||
factorial[i] = ( factorial[i-1] * i ) % mod;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T modMul(T a,T b,T M=(ll)1e9+7){
|
||||
return ((a%M)*(b%M))%M;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T power(T a, T b, T M)
|
||||
{
|
||||
T x = 1;
|
||||
while (b)
|
||||
{
|
||||
if (b & 1) x = modMul(x,a,M);
|
||||
a = modMul(a,a,M);
|
||||
b >>= 1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/* Using Fermat little Theorem */
|
||||
ll inverse(ll n ){
|
||||
return power( n , mod - 2 , mod );
|
||||
}
|
||||
|
||||
vector<ll> inverse_factorial;
|
||||
void calculateInverseFactorial(ll n){
|
||||
|
||||
inverse_factorial.resize(n+1);
|
||||
inverse_factorial[0] = inverse_factorial[1] = 1;
|
||||
|
||||
for( ll i = 2 ; i <= n ; i++ ){
|
||||
inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod;
|
||||
}
|
||||
}
|
||||
|
||||
ll nCr( ll n , ll r ){
|
||||
|
||||
// -- n! --
|
||||
// | ------------- | % mod
|
||||
// -- n! * (n - r)! --
|
||||
|
||||
return ( ( ( factorial[n] * inverse_factorial[r] ) % mod
|
||||
) * inverse_factorial[n-r] ) % mod;
|
||||
ll mod = 998244353;
|
||||
|
||||
vector<ll> factorial;
|
||||
void calculateFactorial(ll n){
|
||||
factorial.resize(n+1);
|
||||
|
||||
factorial[0] = factorial[1] = 1;
|
||||
|
||||
for( ll i = 2 ; i <= n ; i++ ){
|
||||
factorial[i] = ( factorial[i-1] * i ) % mod;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T modMul(T a,T b,T M=(ll)1e9+7){
|
||||
return ((a%M)*(b%M))%M;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T power(T a, T b, T M)
|
||||
{
|
||||
T x = 1;
|
||||
while (b)
|
||||
{
|
||||
if (b & 1) x = modMul(x,a,M);
|
||||
a = modMul(a,a,M);
|
||||
b >>= 1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
/* Using Fermat little Theorem */
|
||||
ll inverse(ll n ){
|
||||
return power( n , mod - 2 , mod );
|
||||
}
|
||||
|
||||
vector<ll> inverse_factorial;
|
||||
void calculateInverseFactorial(ll n){
|
||||
|
||||
inverse_factorial.resize(n+1);
|
||||
inverse_factorial[0] = inverse_factorial[1] = 1;
|
||||
|
||||
for( ll i = 2 ; i <= n ; i++ ){
|
||||
inverse_factorial[i] = (inverse(i)*inverse_factorial[i-1])%mod;
|
||||
}
|
||||
}
|
||||
|
||||
ll nCr( ll n , ll r ){
|
||||
|
||||
// -- n! --
|
||||
// | ------------- | % mod
|
||||
// -- n! * (n - r)! --
|
||||
|
||||
return ( ( ( factorial[n] * inverse_factorial[r] ) % mod
|
||||
) * inverse_factorial[n-r] ) % mod;
|
||||
}
|
||||
@@ -1,68 +1,68 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
class Fenwick_Tree{
|
||||
|
||||
public:
|
||||
|
||||
// 1 based indexing...
|
||||
vector<int> tree;
|
||||
int sz;
|
||||
|
||||
void init(int n, const vector<int>& arr){
|
||||
tree.resize(n+1,0);
|
||||
|
||||
this->sz = n;
|
||||
|
||||
for(int i = 1 ; i <= sz ; i++ ){
|
||||
|
||||
update(i, arr[i-1]);
|
||||
}
|
||||
}
|
||||
|
||||
int sum(int k){
|
||||
int s = 0;
|
||||
|
||||
while(k >= 1){
|
||||
|
||||
s += tree[k];
|
||||
|
||||
k -= k & -k;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void update(int k, int val){
|
||||
|
||||
while( k <= sz ){
|
||||
tree[k] += val;
|
||||
|
||||
k += k & -k;
|
||||
}
|
||||
}
|
||||
|
||||
void print(){
|
||||
|
||||
for(int i = 1 ; i <= sz ; i++ ){
|
||||
cout << tree[i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
class Fenwick_Tree{
|
||||
|
||||
public:
|
||||
|
||||
// 1 based indexing...
|
||||
vector<int> tree;
|
||||
int sz;
|
||||
|
||||
void init(int n, const vector<int>& arr){
|
||||
tree.resize(n+1,0);
|
||||
|
||||
this->sz = n;
|
||||
|
||||
for(int i = 1 ; i <= sz ; i++ ){
|
||||
|
||||
update(i, arr[i-1]);
|
||||
}
|
||||
}
|
||||
|
||||
int sum(int k){
|
||||
int s = 0;
|
||||
|
||||
while(k >= 1){
|
||||
|
||||
s += tree[k];
|
||||
|
||||
k -= k & -k;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void update(int k, int val){
|
||||
|
||||
while( k <= sz ){
|
||||
tree[k] += val;
|
||||
|
||||
k += k & -k;
|
||||
}
|
||||
}
|
||||
|
||||
void print(){
|
||||
|
||||
for(int i = 1 ; i <= sz ; i++ ){
|
||||
cout << tree[i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,89 +1,89 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class SegmentTree{
|
||||
|
||||
|
||||
int sz;
|
||||
vector<int> tree;
|
||||
|
||||
public:
|
||||
|
||||
SegmentTree(const vector<int>& arr){
|
||||
|
||||
this->sz = arr.size();
|
||||
|
||||
tree.resize(2*this->sz+1,0);
|
||||
|
||||
init(arr);
|
||||
|
||||
}
|
||||
|
||||
void init(const vector<int>& arr){
|
||||
|
||||
for(int i = sz-1 ; i >= 0 ; i-- )
|
||||
tree[i+sz] = arr[i];
|
||||
|
||||
for(int i = sz - 1 ; i >= 1 ; i-- ){
|
||||
|
||||
tree[i] = tree[2*i] + tree[2*i+1];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void print(){
|
||||
|
||||
cout << "Size : " << sz << '\n';
|
||||
|
||||
for(int i = 0 ; i <= 2*sz ; i++ ){
|
||||
|
||||
cout << tree[i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int sum( int a, int b){
|
||||
|
||||
int s = 0;
|
||||
a += sz , b += sz;
|
||||
|
||||
while( a <= b ){
|
||||
|
||||
if( a%2 == 1 ) s += tree[a++];
|
||||
|
||||
if( b%2 == 0 ) s += tree[b--];
|
||||
|
||||
a /= 2 , b /= 2;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void add(int k, int x) {
|
||||
k += sz;
|
||||
tree[k] += x;
|
||||
for (k /= 2; k >= 1; k /= 2) {
|
||||
tree[k] = tree[2*k]+tree[2*k+1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
vector<int> arr = {5,8,6,3,2,7,2,6};
|
||||
|
||||
SegmentTree stree(arr);
|
||||
|
||||
|
||||
stree.print();
|
||||
|
||||
stree.add(7,1);
|
||||
cout << "(2,7) : " << stree.sum(2,7) << '\n';
|
||||
|
||||
return 0;
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class SegmentTree{
|
||||
|
||||
|
||||
int sz;
|
||||
vector<int> tree;
|
||||
|
||||
public:
|
||||
|
||||
SegmentTree(const vector<int>& arr){
|
||||
|
||||
this->sz = arr.size();
|
||||
|
||||
tree.resize(2*this->sz+1,0);
|
||||
|
||||
init(arr);
|
||||
|
||||
}
|
||||
|
||||
void init(const vector<int>& arr){
|
||||
|
||||
for(int i = sz-1 ; i >= 0 ; i-- )
|
||||
tree[i+sz] = arr[i];
|
||||
|
||||
for(int i = sz - 1 ; i >= 1 ; i-- ){
|
||||
|
||||
tree[i] = tree[2*i] + tree[2*i+1];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void print(){
|
||||
|
||||
cout << "Size : " << sz << '\n';
|
||||
|
||||
for(int i = 0 ; i <= 2*sz ; i++ ){
|
||||
|
||||
cout << tree[i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int sum( int a, int b){
|
||||
|
||||
int s = 0;
|
||||
a += sz , b += sz;
|
||||
|
||||
while( a <= b ){
|
||||
|
||||
if( a%2 == 1 ) s += tree[a++];
|
||||
|
||||
if( b%2 == 0 ) s += tree[b--];
|
||||
|
||||
a /= 2 , b /= 2;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void add(int k, int x) {
|
||||
k += sz;
|
||||
tree[k] += x;
|
||||
for (k /= 2; k >= 1; k /= 2) {
|
||||
tree[k] = tree[2*k]+tree[2*k+1];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
vector<int> arr = {5,8,6,3,2,7,2,6};
|
||||
|
||||
SegmentTree stree(arr);
|
||||
|
||||
|
||||
stree.print();
|
||||
|
||||
stree.add(7,1);
|
||||
cout << "(2,7) : " << stree.sum(2,7) << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,49 +1,49 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
//Sparse Table for minimum value query.
|
||||
|
||||
vector<vector<int>> table;
|
||||
|
||||
void precomputeSparseTable(int n, vector<int>& arr){
|
||||
|
||||
int lPower = log2(n);
|
||||
|
||||
table.resize(n+1,vector<int>(lPower+1));
|
||||
|
||||
for(int p = 0 ; p <= lPower ; p++ ){
|
||||
for(int i = 0 ; i + pow(2,p) - 1 <= n ; i++ ){
|
||||
if( p == 0 ){
|
||||
table[i][p] = arr[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
table[i][p] = min(table[i][p-1],table[i+pow(2,p-1)][p-1]);
|
||||
}
|
||||
}
|
||||
|
||||
// for(int i = 0 ; i <= n ; i++ ){
|
||||
// cout << i << " : ";
|
||||
// for(int p = 0 ; p <= lPower ; p++ ){
|
||||
// cout << table[i][p] << " ";
|
||||
// }
|
||||
// cout << endl;
|
||||
// }
|
||||
}
|
||||
|
||||
int main(int argc,char* argv[]){
|
||||
|
||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||
|
||||
int n,q;
|
||||
cin>>n>>q;
|
||||
|
||||
vector<int> arr(n);
|
||||
for(int i = 0 ; i < n ; i++ ){
|
||||
cin>>arr[i];
|
||||
}
|
||||
|
||||
precomputeSparseTable(n,arr);
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
//Sparse Table for minimum value query.
|
||||
|
||||
vector<vector<int>> table;
|
||||
|
||||
void precomputeSparseTable(int n, vector<int>& arr){
|
||||
|
||||
int lPower = log2(n);
|
||||
|
||||
table.resize(n+1,vector<int>(lPower+1));
|
||||
|
||||
for(int p = 0 ; p <= lPower ; p++ ){
|
||||
for(int i = 0 ; i + pow(2,p) - 1 <= n ; i++ ){
|
||||
if( p == 0 ){
|
||||
table[i][p] = arr[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
table[i][p] = min(table[i][p-1],table[i+pow(2,p-1)][p-1]);
|
||||
}
|
||||
}
|
||||
|
||||
// for(int i = 0 ; i <= n ; i++ ){
|
||||
// cout << i << " : ";
|
||||
// for(int p = 0 ; p <= lPower ; p++ ){
|
||||
// cout << table[i][p] << " ";
|
||||
// }
|
||||
// cout << endl;
|
||||
// }
|
||||
}
|
||||
|
||||
int main(int argc,char* argv[]){
|
||||
|
||||
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
|
||||
|
||||
int n,q;
|
||||
cin>>n>>q;
|
||||
|
||||
vector<int> arr(n);
|
||||
for(int i = 0 ; i < n ; i++ ){
|
||||
cin>>arr[i];
|
||||
}
|
||||
|
||||
precomputeSparseTable(n,arr);
|
||||
}
|
||||
@@ -1,49 +1,49 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]){
|
||||
|
||||
int n = atoi(argv[1]);
|
||||
// cin>>n;
|
||||
|
||||
int arr[n];
|
||||
for(int i = 1 ; i <= n ; i++ )
|
||||
arr[i-1] = i;
|
||||
|
||||
int idx = 0;
|
||||
int cnt = 1;
|
||||
|
||||
int elemLeft = n;
|
||||
|
||||
cout << "Operation:\n";
|
||||
|
||||
while( true ){
|
||||
// cout << "Size : " << elemLeft << '\n';
|
||||
if( elemLeft <= 0 )
|
||||
break;
|
||||
|
||||
if( cnt%2 == 0 ){
|
||||
while( arr[idx] == -1 )
|
||||
idx = (idx+1)%n;
|
||||
|
||||
cout << arr[idx] << ' ';
|
||||
arr[idx] = -1;
|
||||
|
||||
elemLeft--;
|
||||
if( elemLeft <= 0 )break;
|
||||
|
||||
while( arr[idx] == -1 )
|
||||
idx = (idx+1)%n;
|
||||
cnt = 1;
|
||||
|
||||
// idx = (idx+1)%n;
|
||||
continue;
|
||||
}
|
||||
|
||||
idx = (idx+1)%n;
|
||||
cnt++;
|
||||
}
|
||||
cout << '\n';
|
||||
return 0;
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]){
|
||||
|
||||
int n = atoi(argv[1]);
|
||||
// cin>>n;
|
||||
|
||||
int arr[n];
|
||||
for(int i = 1 ; i <= n ; i++ )
|
||||
arr[i-1] = i;
|
||||
|
||||
int idx = 0;
|
||||
int cnt = 1;
|
||||
|
||||
int elemLeft = n;
|
||||
|
||||
cout << "Operation:\n";
|
||||
|
||||
while( true ){
|
||||
// cout << "Size : " << elemLeft << '\n';
|
||||
if( elemLeft <= 0 )
|
||||
break;
|
||||
|
||||
if( cnt%2 == 0 ){
|
||||
while( arr[idx] == -1 )
|
||||
idx = (idx+1)%n;
|
||||
|
||||
cout << arr[idx] << ' ';
|
||||
arr[idx] = -1;
|
||||
|
||||
elemLeft--;
|
||||
if( elemLeft <= 0 )break;
|
||||
|
||||
while( arr[idx] == -1 )
|
||||
idx = (idx+1)%n;
|
||||
cnt = 1;
|
||||
|
||||
// idx = (idx+1)%n;
|
||||
continue;
|
||||
}
|
||||
|
||||
idx = (idx+1)%n;
|
||||
cnt++;
|
||||
}
|
||||
cout << '\n';
|
||||
return 0;
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[] ){
|
||||
|
||||
|
||||
return 0;
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[] ){
|
||||
|
||||
|
||||
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