├── __template.cpp ├── _pbds.cpp ├── binary_search.cpp ├── bit.cpp ├── dfs.cpp ├── dsu.cpp ├── lca_bl.cpp ├── mod_arithm.cpp ├── number_theory.cpp ├── scc.cpp ├── segment_tree.cpp ├── topo_sort.cpp └── trie.cpp /__template.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: abhishekvtangod 3 | **/ 4 | // #undef _GLIBCXX_DEBUG 5 | // #undef _ABHI 6 | #include 7 | using namespace std; 8 | 9 | #define mod 1000000007 10 | #define gcd(a,b) __gcd(a,b) 11 | #define lcm(a,b) a/gcd(a,b)*b // no overflow 12 | #define bits(x) __builtin_popcountll(x) 13 | #define endl "\n" 14 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 15 | typedef long long int ll; 16 | 17 | string to_string(const string &s){ 18 | return "{" + s + "}"; 19 | } 20 | 21 | string to_string(const char &c){ 22 | string s = ""; 23 | s += c; 24 | return s; 25 | } 26 | 27 | template 28 | string to_string(const pair &p){ 29 | return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; 30 | } 31 | 32 | template 33 | string to_string(const tuple &t){ 34 | return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ")"; 35 | } 36 | 37 | template 38 | string to_string(A v){ 39 | string res = "{"; 40 | bool f = 0; 41 | for(const auto &u: v){ 42 | if(f){ 43 | res += ", "; 44 | } 45 | f = 1; 46 | res += to_string(u); 47 | } 48 | res += "}"; 49 | return res; 50 | } 51 | 52 | void cus_debug() { cerr << "]" << endl; } 53 | 54 | template 55 | void cus_debug(Head H, Tail... T) { 56 | cerr << to_string(H) << ", "; 57 | cus_debug(T...); 58 | } 59 | 60 | #ifdef _GLIBCXX_DEBUG 61 | #define debug(x...) cerr << "[" << #x << "]:[", cus_debug(x) 62 | #else 63 | #define debug(...) 42 64 | #endif 65 | 66 | 67 | 68 | // start of CP 2.0 69 | void solve(){ 70 | 71 | 72 | } 73 | 74 | int main() 75 | { 76 | IOS; 77 | 78 | #ifdef _ABHI 79 | freopen("/home/abhi/Documents/input.txt", "r", stdin); 80 | freopen("/home/abhi/Documents/output.txt", "w", stdout); 81 | #endif 82 | 83 | 84 | ll t=1; 85 | // cin>>t; 86 | while(t--){ 87 | solve(); 88 | } 89 | 90 | 91 | return 0; 92 | } -------------------------------------------------------------------------------- /_pbds.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include 5 | #include 6 | using namespace __gnu_pbds; 7 | template using oset=tree, rb_tree_tag, tree_order_statistics_node_update>; 8 | // oset> indexed_set; 9 | -------------------------------------------------------------------------------- /binary_search.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: abhishekvtangod 3 | **/ 4 | // #undef _GLIBCXX_DEBUG 5 | // #undef _ABHI 6 | #include 7 | using namespace std; 8 | 9 | #define mod 1000000007 10 | #define gcd(a,b) __gcd(a,b) 11 | #define lcm(a,b) a/gcd(a,b)*b // no overflow 12 | #define bits(x) __builtin_popcountll(x) 13 | #define endl "\n" 14 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 15 | typedef long long int ll; 16 | 17 | string to_string(const string &s){ 18 | return "{" + s + "}"; 19 | } 20 | 21 | string to_string(const char &c){ 22 | string s = ""; 23 | s += c; 24 | return s; 25 | } 26 | 27 | template 28 | string to_string(const pair &p){ 29 | return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; 30 | } 31 | 32 | template 33 | string to_string(const tuple &t){ 34 | return "(" + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ")"; 35 | } 36 | 37 | template 38 | string to_string(A v){ 39 | string res = "{"; 40 | bool f = 0; 41 | for(const auto &u: v){ 42 | if(f){ 43 | res += ", "; 44 | } 45 | f = 1; 46 | res += to_string(u); 47 | } 48 | res += "}"; 49 | return res; 50 | } 51 | 52 | void cus_debug() { cerr << "]" << endl; } 53 | 54 | template 55 | void cus_debug(Head H, Tail... T) { 56 | cerr << to_string(H) << ", "; 57 | cus_debug(T...); 58 | } 59 | 60 | #ifdef _GLIBCXX_DEBUG 61 | #define debug(x...) cerr << "[" << #x << "]:[", cus_debug(x) 62 | #else 63 | #define debug(...) 42 64 | #endif 65 | 66 | 67 | // returns index of lower_bound of target 68 | ll lowerBound(vector v, ll target){ 69 | ll n = v.size(); 70 | ll ans = -1; 71 | ll l = 0, r= n-1; 72 | while(l <= r){ 73 | ll mid = l + (r - l)/2; 74 | if(target <= v[mid]){ 75 | ans = mid; 76 | r = mid - 1; // may be there is even smaller index where target is present 77 | }else{ 78 | l = mid + 1; 79 | } 80 | } 81 | return ans; 82 | } 83 | 84 | /* 85 | target = 5 86 | l m r 87 | 0 1 2 3 4 5 6 7 8 88 | 1 3 5 5 5 5 67 123 125 89 | f f f f f f t t t 90 | */ 91 | 92 | /* 93 | target = 6 94 | l m r 95 | 0 1 2 3 4 5 6 7 8 96 | 1 3 5 5 5 5 7 123 125 97 | f f f f f f t t t 98 | */ 99 | 100 | 101 | // returns index of upper_bound of target 102 | ll upperBound(vector v, ll target){ 103 | ll n = v.size(); 104 | ll ans = 0; 105 | ll l = 0, r= n-1; 106 | while(l <= r){ 107 | ll mid = l + (r - l)/2; 108 | if(target < v[mid]){ 109 | r = mid - 1; 110 | }else{ 111 | l = mid + 1; 112 | ans = l; 113 | } 114 | } 115 | if(ans >= n){ 116 | ans = -1; 117 | } 118 | return ans; 119 | } 120 | 121 | 122 | /* 123 | t t t t f f 124 | 4,5,6,7,0,1 125 | 126 | f f f f f f 127 | 0 1 4 5 6 7 128 | 129 | t f f f f f f 130 | 8 0 1 4 5 6 7 131 | 132 | */ 133 | 134 | // returns index of min 135 | ll findMinRotatedSortedArray(vector v){ 136 | ll n = v.size(); 137 | ll ans = -1; 138 | ll l = 0, r= n-1; 139 | while(l <= r){ 140 | ll mid = l + (r - l)/2; 141 | if(v[mid] <= v[n-1]){ 142 | ans = mid; 143 | r = mid - 1; //try to find even smaller element on it's left 144 | } else{ 145 | l = mid + 1; 146 | } 147 | } 148 | return l; 149 | } 150 | 151 | // ll searchRotatedArray(vector v, ll target){ 152 | // ll n = v.size(); 153 | // ll ans = -1; 154 | // ll l = 0, r = n - 1; 155 | // while(l <= r){ 156 | 157 | // } 158 | // } 159 | 160 | // double bsSqrt(ll n){ 161 | 162 | // } 163 | 164 | 165 | // TTTTFFF 166 | 167 | // start of CP 2.0 168 | void solve(){ 169 | ll n, x; 170 | // cin >> n >> x; 171 | cin >> n; 172 | vector v; 173 | for(int i = 0; i < n; i++){ 174 | ll temp; 175 | cin >> temp; 176 | v.push_back(temp); 177 | } 178 | // cout << lowerBound(v, x) << endl; 179 | // cout << upperBound(v, x) << endl; 180 | 181 | 182 | cout << findMinRotatedSortedArray(v) << endl; 183 | 184 | } 185 | 186 | int main() 187 | { 188 | IOS; 189 | 190 | #ifdef _ABHI 191 | freopen("/home/abhi/Documents/input.txt", "r", stdin); 192 | freopen("/home/abhi/Documents/output.txt", "w", stdout); 193 | #endif 194 | 195 | 196 | ll t=1; 197 | // cin>>t; 198 | while(t--){ 199 | solve(); 200 | } 201 | 202 | 203 | return 0; 204 | } -------------------------------------------------------------------------------- /bit.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: abhishekvtangod 3 | **/ 4 | // #undef _GLIBCXX_DEBUG 5 | // #undef _ABHI 6 | #include 7 | using namespace std; 8 | 9 | #include 10 | #include 11 | using namespace __gnu_pbds; 12 | template using oset=tree, rb_tree_tag, tree_order_statistics_node_update>; 13 | // oset> indexed_set; 14 | 15 | #define mod 1000000007 16 | #define gcd(a,b) __gcd(a,b) 17 | #define lcm(a,b) (a*b)/gcd(a,b) 18 | #define bits(x) __builtin_popcountll(x) 19 | #define endl "\n" 20 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 21 | typedef long long int ll; 22 | 23 | void debug_out() { cerr << "]" << endl; } 24 | template 25 | void debug_out(Head H, Tail... T) { 26 | cerr << H << ", "; 27 | debug_out(T...); 28 | } 29 | 30 | #ifdef _GLIBCXX_DEBUG 31 | #define debug(x...) cerr << "[" << #x << "]:[", debug_out(x) 32 | #else 33 | #define debug(...) 42 34 | #endif 35 | 36 | 37 | // 1 based indexing 38 | struct bit{ 39 | int n; 40 | vector tree; 41 | 42 | void init(int _n){ 43 | n = _n; 44 | tree.resize(_n+1); 45 | } 46 | 47 | void add(int idx, int val){ 48 | for(; idx <= n; idx += (idx & -idx)){ 49 | tree[idx] += val; 50 | } 51 | } 52 | 53 | // void range_add(int l, int r, int val){ 54 | // add(l, val); 55 | // add(r+1, -val); 56 | // } 57 | 58 | // [ (idx - 2^r + 1) to idx ] (where r is the position of least significant set bit of idx) 59 | int point_query(int idx){ 60 | ll sum = 0; 61 | for(; idx > 0; idx -= (idx & -idx)){ 62 | sum += tree[idx]; 63 | } 64 | return sum; 65 | } 66 | int range_query(int l, int r){ 67 | return point_query(r) - point_query(l-1); 68 | } 69 | 70 | 71 | }; 72 | 73 | bit bt; 74 | 75 | 76 | void solve(){ 77 | int n; 78 | cin >> n; 79 | bt.init(n); 80 | for(int i =1; i <= n ; i++){ 81 | int temp; cin>> temp; 82 | bt.add(i, temp); 83 | } 84 | cout << bt.point_query(4) << endl; 85 | for(int i : bt.tree){ 86 | cerr << i << " "; 87 | } 88 | cerr << endl; 89 | bt.range_add(2, 4, 100); 90 | for(int i : bt.tree){ 91 | cerr << i << " "; 92 | } 93 | cerr << endl; 94 | for(int i = 1; i <=n ;i++){ 95 | cout << bt.point_query(i) << endl; 96 | } 97 | } 98 | 99 | 100 | int main() 101 | { 102 | IOS; 103 | 104 | #ifdef _ABHI 105 | freopen("/home/abhi/Documents/input.txt", "r", stdin); 106 | freopen("/home/abhi/Documents/output.txt", "w", stdout); 107 | #endif 108 | 109 | 110 | ll t=1; 111 | // cin>>t; 112 | while(t--){ 113 | solve(); 114 | } 115 | 116 | 117 | return 0; 118 | } -------------------------------------------------------------------------------- /dfs.cpp: -------------------------------------------------------------------------------- 1 | struct s_dfs{ 2 | vector> adj; 3 | int n; 4 | vector visited; 5 | 6 | void init(int sz){ 7 | n = sz; 8 | visited.resize(n); 9 | adj.resize(n); 10 | for(auto& u: visited){ 11 | u = 0; 12 | } 13 | } 14 | 15 | void dfs(int src){ 16 | visited[src] = 1; 17 | for(auto u: adj[src]){ 18 | if(!visited[u]){ 19 | dfs(u); 20 | } 21 | } 22 | } 23 | 24 | }; 25 | 26 | struct generic_dfs{ 27 | vector> adj; 28 | int n; 29 | vector visited; 30 | 31 | vector time_in, time_out; 32 | int dfs_timer = 0; 33 | 34 | void init(int sz){ 35 | n = sz; 36 | visited.resize(n); 37 | adj.resize(n); 38 | time_in.resize(n); 39 | time_out.resize(n); 40 | for(auto& u: visited){ 41 | u = 0; 42 | } 43 | } 44 | 45 | void dfs(int src){ 46 | time_in[src] = dfs_timer++; 47 | visited[src] = 1; 48 | for(auto u: adj[src]){ 49 | if(!visited[u]){ 50 | dfs(u); 51 | } 52 | } 53 | visited[src] = 2; 54 | time_out[src] = dfs_timer++; 55 | } 56 | 57 | }; -------------------------------------------------------------------------------- /dsu.cpp: -------------------------------------------------------------------------------- 1 | // union by size 2 | 3 | // sort edge list 4 | struct dsu{ 5 | int n; 6 | vector siz, link; 7 | vector> adj; 8 | 9 | void init(int nodes){ 10 | n = nodes; 11 | siz.resize(n); 12 | link.resize(n); 13 | adj.resize(n); 14 | for(int i = 0; i < n; i++){ 15 | link[i] = i; 16 | siz[i] = 1; 17 | } 18 | } 19 | 20 | int find_set(int x){ 21 | if(link[x] == x){ 22 | return x; 23 | } 24 | return link[x] = find_set(link[x]); 25 | } 26 | 27 | int unite_sets(int a, int b){ 28 | a = find_set(a); 29 | b = find_set(b); 30 | if(a == b){ 31 | return 0; 32 | } 33 | adj[a].push_back(b); 34 | adj[b].push_back(a); 35 | if(siz[a] < siz[b]){ 36 | swap(a, b); 37 | } 38 | siz[a] += siz[b]; 39 | link[b] = a; 40 | return 1; 41 | } 42 | 43 | }; 44 | 45 | -------------------------------------------------------------------------------- /lca_bl.cpp: -------------------------------------------------------------------------------- 1 | // Lowest Common Ancestor - Binary Lifting 2 | struct lca_bl{ 3 | int n, l; 4 | vector> adj; 5 | vector> dp; 6 | 7 | vector time_in, time_out; 8 | int dfs_timer = 0; 9 | 10 | 11 | void init(int sz){ 12 | n = sz; 13 | l = ceil(log2(n)); 14 | adj.resize(n); 15 | dp.resize(n); 16 | time_in.resize(n); 17 | time_out.resize(n); 18 | for(int i = 0; i < n; i++){ 19 | dp[i].resize(l+1); 20 | } 21 | } 22 | 23 | // dfs(root, root); 24 | void dfs(int sr, int p){ 25 | 26 | time_in[sr] = dfs_timer++; 27 | dp[sr][0] = p; 28 | for(int i = 1; i <= l; i++){ 29 | dp[sr][i] = dp[dp[sr][i-1]][i-1]; 30 | } 31 | 32 | for(auto u: adj[sr]){ 33 | if(u != p){ 34 | dfs(u, sr); 35 | } 36 | } 37 | time_out[sr] = dfs_timer++; 38 | } 39 | 40 | int is_ancestor(int u, int v){ 41 | return time_in[u] <= time_in[v] && time_out[u] >= time_out[v]; 42 | } 43 | 44 | int lca(int u, int v){ 45 | if(is_ancestor(u, v)){ 46 | return u; 47 | } 48 | if(is_ancestor(v, u)){ 49 | return v; 50 | } 51 | 52 | for(int i = l; i >= 0; i--){ 53 | if(!is_ancestor(dp[u][i], v)){ 54 | u = dp[u][i]; 55 | } 56 | } 57 | 58 | return dp[u][0]; 59 | 60 | } 61 | 62 | }; -------------------------------------------------------------------------------- /mod_arithm.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: abhishekvtangod 3 | **/ 4 | // #undef _GLIBCXX_DEBUG 5 | // #undef _ABHI 6 | #include 7 | using namespace std; 8 | 9 | #include 10 | #include 11 | using namespace __gnu_pbds; 12 | template using oset=tree, rb_tree_tag, tree_order_statistics_node_update>; 13 | // oset> indexed_set; 14 | 15 | #define mod 1000000007 16 | #define gcd(a,b) __gcd(a,b) 17 | #define lcm(a,b) (a*b)/gcd(a,b) 18 | #define bits(x) __builtin_popcountll(x) 19 | #define endl "\n" 20 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 21 | typedef long long int ll; 22 | 23 | void debug_out() { cerr << "]" << endl; } 24 | template 25 | void debug_out(Head H, Tail... T) { 26 | cerr << H << ", "; 27 | debug_out(T...); 28 | } 29 | 30 | #ifdef _GLIBCXX_DEBUG 31 | #define debug(x...) cerr << "[" << #x << "]:[", debug_out(x) 32 | #else 33 | #define debug(...) 42 34 | #endif 35 | 36 | struct mod_arithm{ 37 | // a^b % k 38 | ll binary_exp(ll a, ll b, ll m){ 39 | ll ans = 1; 40 | while(b){ 41 | if(b&1){ 42 | ans = (ans * a) % m; 43 | } 44 | // b/=2; 45 | b = b>>1; 46 | a = (a * a) % m; 47 | } 48 | return ans; 49 | } 50 | 51 | ll modInverse(ll n, ll m){ 52 | // n^m-2 % m 53 | return binary_exp(n, m-2, m); 54 | } 55 | 56 | 57 | 58 | 59 | }; 60 | 61 | mod_arithm ma; 62 | 63 | void solve(){ 64 | 65 | } 66 | 67 | 68 | int main() 69 | { 70 | IOS; 71 | 72 | #ifdef _ABHI 73 | freopen("/home/abhi/Documents/input.txt", "r", stdin); 74 | freopen("/home/abhi/Documents/output.txt", "w", stdout); 75 | #endif 76 | 77 | 78 | ll t=1; 79 | // cin>>t; 80 | while(t--){ 81 | solve(); 82 | } 83 | 84 | 85 | return 0; 86 | } -------------------------------------------------------------------------------- /number_theory.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: abhishekvtangod 3 | **/ 4 | // #undef _GLIBCXX_DEBUG 5 | // #undef _ABHI 6 | #include 7 | using namespace std; 8 | 9 | #include 10 | #include 11 | using namespace __gnu_pbds; 12 | template using oset=tree, rb_tree_tag, tree_order_statistics_node_update>; 13 | // oset> indexed_set; 14 | 15 | #define mod 1000000007 16 | #define gcd(a,b) __gcd(a,b) 17 | #define lcm(a,b) (a*b)/gcd(a,b) 18 | #define bits(x) __builtin_popcountll(x) 19 | #define endl "\n" 20 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 21 | typedef long long int ll; 22 | 23 | void debug_out() { cerr << "]" << endl; } 24 | template 25 | void debug_out(Head H, Tail... T) { 26 | cerr << H << ", "; 27 | debug_out(T...); 28 | } 29 | 30 | #ifdef _GLIBCXX_DEBUG 31 | #define debug(x...) cerr << "[" << #x << "]:[", debug_out(x) 32 | #else 33 | #define debug(...) 42 34 | #endif 35 | 36 | struct number_theory{ 37 | 38 | // Euclid's Algorithm 39 | // => gcd(a, b) = gcd(b, a%b); 40 | 41 | ll gcd(ll a, ll b){ 42 | while(b){ 43 | a = a%b; 44 | swap(a, b); 45 | } 46 | return a; 47 | } 48 | ll lcm(ll a, ll b){ 49 | return a / gcd(a, b) * b; 50 | } 51 | // ll gcd(ll a, ll b){ 52 | // return b ? gcd(b, a%b) : a; 53 | // } 54 | 55 | 56 | 57 | // Extended Euclid Algorithm 58 | // ax + by = gcd(a, b) 59 | 60 | ll extendedEuclid(ll a, ll b, ll &x, ll &y){ 61 | if(!b){ 62 | x = 1; 63 | y = 0; 64 | return a; 65 | } 66 | ll x1, y1; 67 | ll d = extendedEuclid(b, a%b, x1, y1); 68 | x = y1; 69 | y = x1 - y1 * (a/b); 70 | return d; 71 | } 72 | int findInitSol(ll a, ll b, ll c, ll &x0, ll &y0){ 73 | ll g = extendedEuclid(abs(a), abs(b), x0, y0); 74 | if(c%g){ 75 | return 0; 76 | } 77 | x0 *= c / g; 78 | y0 *= c / g; 79 | if(a < 0) x0 *= -1; 80 | if(b < 0) y0 *= -1; 81 | return 1; 82 | } 83 | 84 | 85 | int isprime(int n){ 86 | if(n <= 1){ 87 | return 0; 88 | } else if(n <= 3){ 89 | return 1; 90 | } 91 | if(n%2 == 0 || n%3 == 0){ 92 | return 0; 93 | } 94 | for(int i = 5; i * i <= n; i+=6){ 95 | if(n%i == 0 || n%(i+2) == 0){ 96 | return 0; 97 | } 98 | } 99 | 100 | return 1; 101 | } 102 | 103 | int prime[(int)1e8]; 104 | void sieve_of_eratosthenes(int n){ 105 | for(int i = 0; i <= n; i++){ 106 | prime[i] = 1; 107 | } 108 | prime[1] = 0; 109 | for(int i = 2; i*i <= n; i++){ 110 | if(prime[i]){ 111 | for(int j = i*i; j <= n; j += i){ 112 | prime[j] = 0; 113 | } 114 | } 115 | } 116 | } 117 | 118 | vector primesList; 119 | void genPrimes(int n){ 120 | sieve_of_eratosthenes(n); 121 | for(int i = 2; i <= n; i++){ 122 | if(prime[i]){ 123 | primesList.push_back(i); 124 | } 125 | } 126 | } 127 | 128 | vector fac; 129 | void pFactors(int n){ 130 | genPrimes(n); 131 | for(int i = 0; primesList[i]*primesList[i] <= n; i++){ 132 | while(n%primesList[i] == 0){ 133 | fac.push_back(primesList[i]); 134 | n/=primesList[i]; 135 | } 136 | } 137 | if(n > 1){ 138 | fac.push_back(n); 139 | } 140 | // sort(fac.begin(), fac.end()); 141 | } 142 | 143 | 144 | 145 | vector divs; 146 | void getDivs(int n){ 147 | for(int i = 1; i*i <= n ; i++){ 148 | if(n%i==0){ 149 | divs.push_back(i); 150 | if(i != n/i){ 151 | divs.push_back(n/i); 152 | } 153 | } 154 | } 155 | } 156 | 157 | vector pf; 158 | void getPrimeFactors(int n){ 159 | for(int i = 2; i*i <= n; i++){ 160 | while(n%i==0){ 161 | pf.push_back(i); 162 | n/=i; 163 | } 164 | } 165 | if(n!=1){ 166 | pf.push_back(n); 167 | } 168 | } 169 | 170 | 171 | 172 | 173 | }; 174 | number_theory nt; 175 | 176 | void solve(){ 177 | 178 | } 179 | 180 | 181 | int main() 182 | { 183 | IOS; 184 | 185 | #ifdef _ABHI 186 | freopen("/home/abhi/Documents/input.txt", "r", stdin); 187 | freopen("/home/abhi/Documents/output.txt", "w", stdout); 188 | #endif 189 | 190 | 191 | ll t=1; 192 | // cin>>t; 193 | while(t--){ 194 | solve(); 195 | } 196 | 197 | 198 | return 0; 199 | } -------------------------------------------------------------------------------- /scc.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: abhishekvtangod 3 | **/ 4 | // #undef _GLIBCXX_DEBUG 5 | // #undef _ABHI 6 | #include 7 | using namespace std; 8 | 9 | #include 10 | #include 11 | using namespace __gnu_pbds; 12 | template using oset=tree, rb_tree_tag, tree_order_statistics_node_update>; 13 | // oset> indexed_set; 14 | 15 | #define mod 1000000007 16 | #define gcd(a,b) __gcd(a,b) 17 | #define lcm(a,b) (a*b)/gcd(a,b) 18 | #define bits(x) __builtin_popcountll(x) 19 | #define endl "\n" 20 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 21 | typedef long long int ll; 22 | 23 | void debug_out() { cerr << "]" << endl; } 24 | template 25 | void debug_out(Head H, Tail... T) { 26 | cerr << H << ", "; 27 | debug_out(T...); 28 | } 29 | 30 | #ifdef _GLIBCXX_DEBUG 31 | #define debug(x...) cerr << "[" << #x << "]:[", debug_out(x) 32 | #else 33 | #define debug(...) 42 34 | #endif 35 | 36 | struct kosaraju{ 37 | 38 | vector> adj, trans_adj; 39 | vector order, component, visited; 40 | int n; 41 | 42 | void init(int sz){ 43 | n = sz; 44 | adj.resize(n); 45 | trans_adj.resize(n); 46 | visited.resize(n); 47 | order.clear(); 48 | component.clear(); 49 | } 50 | 51 | void addEdge(int u, int v){ 52 | adj[u].push_back(v); 53 | trans_adj[v].push_back(u); 54 | } 55 | 56 | void dfs1(int sr){ 57 | visited[sr] = 1; 58 | for(auto u: adj[sr]){ 59 | if(!visited[u]){ 60 | dfs1(u); 61 | } 62 | } 63 | order.push_back(sr); 64 | } 65 | 66 | void dfs2(int sr){ 67 | visited[sr] = 1; 68 | component.push_back(sr); 69 | for(auto u: trans_adj[sr]){ 70 | if(!visited[u]){ 71 | dfs2(u); 72 | } 73 | } 74 | } 75 | 76 | 77 | vector> _main(){ 78 | vector> components; 79 | 80 | for(int i = 0; i < n;i++){ 81 | visited[i] = 0; 82 | } 83 | 84 | //search1 85 | for(int i = 0; i < n;i++){ 86 | if(!visited[i]){ 87 | dfs1(i); 88 | } 89 | } 90 | 91 | for(int i = 0; i < n;i++){ 92 | visited[i] = 0; 93 | } 94 | 95 | //search2 96 | for(int i =0; i < n;i++){ 97 | int sr = order[n-1-i]; 98 | if(!visited[sr]){ 99 | dfs2(sr); 100 | components.push_back(component); 101 | component.clear(); 102 | } 103 | } 104 | return components; 105 | } 106 | 107 | }; 108 | 109 | kosaraju ks; 110 | 111 | void solve(){ 112 | int n; 113 | cin >> n; 114 | ks.init(n); 115 | for(int i = 0; i < n; i++){ 116 | int u, v; 117 | cin >> u >> v; 118 | ks.addEdge(u, v); 119 | } 120 | vector> comp = ks._main(); 121 | for(auto &i: comp){ 122 | for(auto j : i){ 123 | cout << j << " "; 124 | } 125 | cout << endl; 126 | } 127 | 128 | 129 | } 130 | 131 | 132 | int main() 133 | { 134 | IOS; 135 | 136 | #ifdef _ABHI 137 | freopen("/home/abhi/Documents/input.txt", "r", stdin); 138 | freopen("/home/abhi/Documents/output.txt", "w", stdout); 139 | #endif 140 | 141 | 142 | ll t=1; 143 | // cin>>t; 144 | while(t--){ 145 | solve(); 146 | } 147 | 148 | 149 | return 0; 150 | } -------------------------------------------------------------------------------- /segment_tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * author: abhishekvtangod 3 | **/ 4 | // #undef _GLIBCXX_DEBUG 5 | // #undef _ABHI 6 | #include 7 | using namespace std; 8 | 9 | #include 10 | #include 11 | using namespace __gnu_pbds; 12 | template using oset=tree, rb_tree_tag, tree_order_statistics_node_update>; 13 | // oset> indexed_set; 14 | 15 | #define mod 1000000007 16 | #define gcd(a,b) __gcd(a,b) 17 | #define lcm(a,b) (a*b)/gcd(a,b) 18 | #define bits(x) __builtin_popcountll(x) 19 | #define endl "\n" 20 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 21 | typedef long long int ll; 22 | 23 | void debug_out() { cerr << "]" << endl; } 24 | template 25 | void debug_out(Head H, Tail... T) { 26 | cerr << H << ", "; 27 | debug_out(T...); 28 | } 29 | 30 | #ifdef _GLIBCXX_DEBUG 31 | #define debug(x...) cerr << "[" << #x << "]:[", debug_out(x) 32 | #else 33 | #define debug(...) 42 34 | #endif 35 | 36 | // associative property 37 | struct node{ 38 | int val; 39 | node(){ 40 | val = 0; 41 | } 42 | node(int _val){ 43 | val = _val; 44 | } 45 | 46 | node oper(node& other_node){ 47 | return node(val + other_node.val); 48 | } 49 | 50 | node lazy_oper(node& other_node, int size){ 51 | return node(val + other_node.val * size); 52 | } 53 | 54 | }; 55 | 56 | 57 | template 58 | struct segtree{ 59 | int n; 60 | vector tree, lazy; 61 | 62 | 63 | segtree(int _n){ 64 | n = _n; 65 | tree.resize(4 * n); 66 | lazy.resize(4 * n); 67 | } 68 | 69 | // v -> input array, ti -> index of the current vertex in segment tree 70 | // 1, 0, n-1 (root node starts from index 1) 71 | void build(vector v){ 72 | build(1, 0, n-1, v); 73 | } 74 | 75 | void build(int ti, int tl, int tr, vector v){ 76 | if(tl == tr){ 77 | tree[ti] = v[tl]; 78 | return; 79 | } 80 | int tm = tl + (tr - tl) / 2; 81 | build(ti * 2, tl, tm, v); 82 | build(ti * 2 + 1, tm + 1, tr, v); 83 | // tree[ti] = tree[ti * 2] + tree[ti * 2 + 1]; 84 | tree[ti] = tree[ti*2].oper(tree[ti*2+1]); 85 | } 86 | 87 | void apply(int ti, int tl, int tr, node_t upd){ 88 | // tree[ti] += val * (tr - tl + 1); 89 | tree[ti] = tree[ti].lazy_oper(upd, (tr - tl + 1)); 90 | if(tl != tr){ // if not leaf, make it lazy 91 | lazy[ti] = lazy[ti].lazy_oper(upd, 1); 92 | } 93 | lazy[ti] = node_t(); 94 | } 95 | 96 | void pushdown(int ti, int tl, int tr){ 97 | if(!lazy[ti].val)return; 98 | 99 | int tm = tr + (tr - tl) >> 1; 100 | apply(ti<<1, tl, tm, lazy[ti]); 101 | apply(ti<<1|1, tm+1, tr, lazy[ti]); 102 | lazy[ti] = 0; //not lazy anymore 103 | 104 | } 105 | 106 | // range update 107 | void r_update(int l, int r, int val){ 108 | node_t upd(val); 109 | r_update(1, 0, n-1, l, r, upd); 110 | } 111 | void r_update(int ti, int tl, int tr, int l, int r, node_t upd){ 112 | //no overlap [l..r tl...tr l...r] 113 | if(l > tr || r < tl){ 114 | return; 115 | } 116 | //complete overlap [l...tl...tr...r] 117 | if(tl >= l && tr <= r){ 118 | apply(ti, tl, tr, upd); 119 | return; 120 | } 121 | 122 | //partial overlap 123 | pushdown(ti, tl, tr); // remove lazy tag before moving down 124 | int tm = tl + (tr - tl) / 2; 125 | r_update(ti*2, tl, tm, l, r, upd); 126 | r_update(ti*2+1, tm+1, tr, l, r, upd); 127 | // tree[ti] = tree[ti*2] + tree[ti*2+1]; 128 | tree[ti] = tree[ti*2].oper(tree[ti*2+1]); 129 | } 130 | 131 | int query(int l, int r){ 132 | node_t ans = query(1, 0, n-1, l, r); 133 | return ans.val; 134 | } 135 | 136 | node_t query(int ti, int tl, int tr, int l, int r){ 137 | //no overlap 138 | // l...r tl...tr l...r 139 | if(l > tr || r < tl){ 140 | return 0; 141 | } 142 | // complete overlap 143 | // l...tl...tr...r 144 | if(l <= tl && r >= tr){ 145 | return tree[ti]; 146 | } 147 | // partial overlap 148 | pushdown(ti, tl, tr); // remove lazy tag before moving down 149 | 150 | int tm = (tl + (tr - tl) / 2); // [tl, tm] and [tm+1, tr] 151 | node_t a = query(ti*2, tl, tm, l, r); 152 | node_t b = query(ti*2+1, tm+1, tr, l, r); 153 | return a.oper(b); 154 | } 155 | 156 | }; 157 | 158 | 159 | void solve(){ 160 | int n; 161 | cin >> n; 162 | segtree st(n); 163 | vector v; 164 | for(int i = 0; i < n; i++){ 165 | int temp; 166 | cin >> temp; 167 | v.push_back(temp); 168 | } 169 | st.build(v); 170 | st.r_update(1, 3, 100); 171 | 172 | 173 | for(int i = 0; i < n; i++){ 174 | int temp = st.query(i, i); 175 | cout << temp << " " ; 176 | } 177 | cout << endl; 178 | 179 | 180 | } 181 | 182 | 183 | 184 | int main() 185 | { 186 | IOS; 187 | 188 | #ifdef _ABHI 189 | freopen("/home/abhi/Documents/input.txt", "r", stdin); 190 | freopen("/home/abhi/Documents/output.txt", "w", stdout); 191 | #endif 192 | 193 | 194 | ll t=1; 195 | // cin>>t; 196 | while(t--){ 197 | solve(); 198 | } 199 | 200 | 201 | return 0; 202 | } -------------------------------------------------------------------------------- /topo_sort.cpp: -------------------------------------------------------------------------------- 1 | struct topo_sort{ 2 | vector> adj; 3 | int n; 4 | vector visited; 5 | vector ans; 6 | 7 | void init(int sz){ 8 | n = sz; 9 | visited.resize(n); 10 | adj.resize(n); 11 | for(auto& u: visited){ 12 | u = 0; 13 | } 14 | } 15 | 16 | int dfs(int s){ 17 | int cyc = 0; 18 | visited[s] = 1; 19 | for(auto u: adj[s]){ 20 | if(!visited[u]){ 21 | cyc = max({cyc, dfs(u)}); 22 | } else if(visited[u] == 1){ 23 | cyc = 1; //cycle exists in DAG 24 | } 25 | } 26 | visited[s] = 2; 27 | ans.push_back(s); 28 | return cyc; 29 | } 30 | 31 | // if dfs returns 1, they cycle exists in DAG, so no topo_sort 32 | 33 | // topo order: reverse of ans 34 | void topo_order(){ 35 | reverse(ans.begin(), ans.end()); 36 | for(auto u: ans){ 37 | cout << u << " "; 38 | } 39 | cout << endl; 40 | } 41 | 42 | 43 | 44 | }; -------------------------------------------------------------------------------- /trie.cpp: -------------------------------------------------------------------------------- 1 | //tr[k] represents list of links for the k-th node 2 | //tr[k][m], node which represents son of k-th node using m-th character 3 | 4 | struct trie{ 5 | 6 | int tr[1000006][28]; 7 | 8 | int root = 0; 9 | 10 | void init() 11 | { 12 | memset(tr,-1,sizeof(tr)); 13 | } 14 | int nxt = 1; 15 | 16 | void insert(string s) 17 | { 18 | int node = root; 19 | 20 | for(int i=0; i