├── .vscode └── settings.json ├── AdditionalProblems └── ChessTournament │ ├── a.out │ └── m.cpp ├── DynamicProgramming ├── ArrayDescription │ ├── a.out │ └── m.cpp ├── BookShop │ ├── a.out │ └── m.cpp ├── CoinCombinations │ ├── a.out │ └── m.cpp ├── CoinCombinations2 │ ├── a.out │ └── m.cpp ├── DiceCombinations │ ├── a.out │ └── m.cpp ├── EditDistance │ ├── a.out │ └── m.cpp ├── GridPaths │ ├── a.out │ └── m.cpp ├── IncreasingSubsequence │ ├── a.out │ └── m.cpp ├── MinimizingCoins │ ├── a.out │ └── m.cpp ├── MoneySums │ ├── a.out │ ├── m.cpp │ └── m2.cpp ├── RectangleCutting │ ├── a.out │ └── m.cpp ├── RemovalGame │ ├── a.out │ └── m.cpp ├── RemovingDigits │ ├── a.out │ └── m.cpp └── TwoSets2 │ ├── a.out │ └── m.cpp ├── GraphAlgorithms ├── BuildingRoad │ ├── a.out │ └── m.cpp ├── BuildingTeams │ ├── a.out │ └── m.cpp ├── CountingRooms │ ├── a.out │ └── m.cpp ├── FlightDiscount │ ├── a.out │ └── m.cpp ├── Labyrinth │ ├── a.out │ └── m.cpp ├── LongestFlightRoute │ ├── a.out │ ├── m.cpp │ └── m2.cpp ├── MessageRoute │ ├── a.out │ ├── input.txt │ └── m.cpp ├── ShortestRoute2 │ ├── a.out │ ├── m.cpp │ └── test.txt └── ShortestRoutes1 │ ├── a.out │ ├── input.txt │ ├── m.cpp │ └── output.txt ├── IntroProblems ├── BitStrings │ ├── a.out │ ├── m.cpp │ └── s.py ├── CoinPiles │ ├── a.out │ └── m.cpp ├── CreatingStrings1 │ ├── a.out │ └── m.cpp ├── NumberSpiral │ └── s.py ├── Permutations │ └── source.py ├── TrailingZeros │ └── m.cpp └── Weird Algorithm │ └── source.py ├── LICENSE ├── Mathematics ├── CountingDivisors │ ├── a.out │ ├── input.txt │ └── m.cpp ├── CreatingStrings2 │ ├── a.out │ └── m.cpp ├── Exponentiation │ ├── a.out │ └── m.cpp └── Exponentiation2 │ └── s.py ├── README.md ├── RangeQueries ├── DistinctValuesQueries │ ├── a.out │ └── m.cpp ├── HotelQueries │ ├── a.out │ └── m.cpp ├── RangeMinimumQueries1 │ ├── a.out │ ├── m.cpp │ ├── m2.cpp │ └── m_jack.cpp ├── RangeMinimumQueries2 │ ├── a.out │ ├── m.cpp │ └── m_jack.cpp ├── RangeSumQueries1 │ ├── a.out │ └── m.cpp ├── RangeSumQueries2 │ ├── a.out │ └── m.cpp ├── RangeUpdateQueries │ ├── a.out │ ├── m.cpp │ ├── m2.cpp │ ├── m3.cpp │ └── short_m.cpp ├── RangeUpdatesandSums │ ├── a.out │ └── m.cpp ├── RangeXorQueries │ ├── a.out │ ├── m.cpp │ └── m2.cpp ├── SubarraySumQueries │ ├── a.out │ └── m.cpp ├── fenwick.cpp ├── segmenttreelazy.cpp └── short_segmenttree.cpp ├── SortingAndSearching ├── Apartments │ └── s.py ├── ConcertTicket │ ├── a.out │ ├── m.cpp │ └── s.py ├── Distinct Numbers │ ├── a.out │ ├── m.cpp │ └── s.py ├── FactoryMachines │ ├── a.out │ └── m.cpp ├── FerrisWheel │ └── s.py ├── MaximumContiguesSum │ ├── a.out │ └── m.cpp ├── MovieFestival │ ├── a.out │ └── m.cpp ├── NearestSmallerValues │ ├── a.out │ └── m.cpp ├── Playlist │ ├── a.out │ ├── m.cpp │ └── m2.cpp ├── RoomAllocation │ ├── a.out │ └── m.cpp ├── SlidingMedian │ ├── a.out │ ├── m.cpp │ └── m2.cpp ├── StickLengths │ └── s.py ├── SumOfTreeValue │ ├── 4.cpp │ ├── a.out │ └── m.cpp ├── SumOfTwoValues │ ├── a.out │ └── m.cpp ├── TaskAndDeadlines │ ├── a.out │ └── m.cpp ├── Towers │ ├── a.out │ ├── m.cpp │ └── s.py └── TrafficLights │ └── m.cpp ├── TreeAlgorithms ├── CompanyQueries1 │ ├── a.out │ ├── m.cpp │ └── test_input.txt ├── CompanyQueries2 │ ├── a.out │ ├── m.cpp │ ├── m2.cpp │ └── m3.cpp ├── DistanceQueries │ ├── a.out │ └── m.cpp ├── DistinctColors │ ├── a.out │ └── m.cpp ├── PathQueries │ ├── a.out │ ├── m.cpp │ └── m2.cpp ├── Subordinates │ ├── a.out │ ├── m.cpp │ └── m2.cpp ├── SubtreeQueries │ ├── a.out │ ├── input.txt │ └── m.cpp ├── TreeDiameter │ ├── a.out │ └── m.cpp └── TreeDistances1 │ ├── a.out │ └── m.cpp ├── utils.cpp └── utils2.cpp /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "terminal.integrated.cursorBlinking": true, 3 | "terminal.integrated.enableBold": true, 4 | "terminal.integrated.fontFamily": "MesloLGL Nerd Font", 5 | "terminal.integrated.fontSize": 14, 6 | "terminal.integrated.rightClickCopyPaste": true, 7 | } -------------------------------------------------------------------------------- /AdditionalProblems/ChessTournament/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/AdditionalProblems/ChessTournament/a.out -------------------------------------------------------------------------------- /AdditionalProblems/ChessTournament/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | typedef pair ii; 11 | typedef long long ll; 12 | const int mod = 1e9 + 7; 13 | const ll inf = 3e18 + 5; 14 | int add(int a, int b) { return (a += b) < mod ? a : a - mod; } 15 | int mul(int a, int b) { return 1LL * a * b % mod; } 16 | 17 | template T getint() { 18 | T val=0; 19 | char c; 20 | bool neg=false; 21 | while((c=getchar()) && !(c>='0' && c<='9')) { 22 | neg|=c=='-'; 23 | } 24 | do { 25 | val=(val*10)+c-'0'; 26 | } while((c=getchar()) && (c>='0' && c<='9')); 27 | 28 | return val*(neg?-1:1); 29 | } 30 | 31 | int main() { 32 | ios::sync_with_stdio(0); 33 | cin.tie(0); cout.tie(0); 34 | int N = getint(); 35 | vector v(N); 36 | for(int a = 0; a < N; ++a){ 37 | v[a] = getint(); 38 | } 39 | 40 | vector res; 41 | int b = 1; 42 | for(int a = 0; a < N; ++a){ 43 | while(v[a] > 0){ 44 | if(v[a] == 0){ 45 | break; 46 | } 47 | if(v[b] == 0){ 48 | b++; 49 | continue; 50 | } 51 | v[b]--; 52 | v[a]--; 53 | res.push_back(to_string(a+1) + " " + to_string(b+1)); 54 | } 55 | } 56 | 57 | for(auto a : v){ 58 | cout << a << " "; 59 | } 60 | 61 | // if(accumulate(v.begin(),v.end(),0) != 0){ 62 | // cout << "IMPOSSIBLE"; 63 | // return 0; 64 | // } 65 | cout << res.size() << endl; 66 | for(auto s: res) 67 | cout << s << endl; 68 | 69 | } -------------------------------------------------------------------------------- /DynamicProgramming/ArrayDescription/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/ArrayDescription/a.out -------------------------------------------------------------------------------- /DynamicProgramming/ArrayDescription/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | /* 24 | 8 5 25 | 0 0 3 4 0 0 0 1 26 | 3 4 3/4/5 2/3/4/5/6 1/2/3/4/5/6/7 1 27 | 28 | valori tra 1 e m 29 | dp[0] = 3 30 | dp[1] = 4 31 | dp[2] = 3, 4, 5 32 | dp[3] = 2, 3, 4, 5 33 | dp[4] = 1, 2, 3, 4, 5 34 | dp[5] = 1 35 | 36 | dp[0] = 3, 3 37 | dp[1] = 4, 4 38 | dp[2] = 3, 5 39 | dp[3] = 2, 5 40 | dp[4] = 1, 5 41 | dp[5] = 1, 1 42 | */ 43 | 44 | int main() { 45 | ios::sync_with_stdio(0); 46 | cin.tie(0); cout.tie(0); 47 | 48 | int N = getint(); 49 | int M = getint(); 50 | 51 | vector v(N); 52 | 53 | for(int a = 0; a < N; ++a){ 54 | v[a] = getint(); 55 | } 56 | 57 | // nel pair metto il valore del primo e dell'ultimo numero 58 | vector> dp(N); 59 | int a = 0; 60 | if(v[0] != 0){ 61 | dp[0] = make_pair(v[0], v[0]); 62 | } else { 63 | while(v[a] == 0){ 64 | a++; 65 | } 66 | } 67 | 68 | for(; a < N; ++a){ 69 | if(v[a] != 0){ 70 | dp[a] = make_pair(v[a], v[a]); 71 | } else { 72 | // dp[a] = (dp[a-1][0] - 1) + dp[a-1] + (dp[a-1][dp[a].size()-1] + 1 ) 73 | 74 | auto start = dp[a-1].first; 75 | auto end = dp[a-1].second; 76 | 77 | if(start - 1 > 0){ 78 | start--; 79 | } 80 | if(end + 1 <= M){ 81 | end++; 82 | } 83 | 84 | dp[a] = make_pair(start, end); 85 | } 86 | } 87 | 88 | // for(auto a : dp){ 89 | // cout << a.first << " " << a.second << endl; 90 | // } 91 | // cout << endl; 92 | 93 | long long sum = 0; 94 | 95 | for(int a = N-2; a >= 0; --a){ 96 | 97 | auto start = dp[a+1].first; 98 | auto end = dp[a+1].second; 99 | 100 | if(dp[a].first == 0 && dp[a].second == 0){ 101 | if(start - 1 > 0){ 102 | start--; 103 | } 104 | if(end + 1 <= M){ 105 | end++; 106 | } 107 | // cout << dp[a].first << " " << dp[a].second << endl; 108 | dp[a] = make_pair(start, end); 109 | sum += ((end-start)+1); 110 | sum %= MOD; 111 | continue; 112 | } 113 | 114 | if(dp[a].first == dp[a].second){ 115 | // sum += 1; 116 | // sum %= MOD; 117 | continue; 118 | } 119 | 120 | start = max(dp[a].first, start - 1); 121 | start = max(start, 1); 122 | 123 | end = min(dp[a].second, end + 1); 124 | end = min(end, M); 125 | 126 | dp[a] = make_pair(start, end); 127 | sum += ((end-start)+1); 128 | sum %= MOD; 129 | 130 | 131 | } 132 | 133 | // for(auto a : dp){ 134 | // cout << a.first << " " << a.second << endl; 135 | // } 136 | cout << sum; 137 | } -------------------------------------------------------------------------------- /DynamicProgramming/BookShop/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/BookShop/a.out -------------------------------------------------------------------------------- /DynamicProgramming/BookShop/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | int knapSack(int W, vector &wt, vector &val, int n) { 22 | int i, w; 23 | int K[n+1][W+1]; 24 | 25 | for (i = 0; i <= n; i++) 26 | { 27 | for (w = 0; w <= W; w++) 28 | { 29 | if (i==0 || w==0) 30 | K[i][w] = 0; 31 | else if (wt[i-1] <= w) 32 | K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]); 33 | else 34 | K[i][w] = K[i-1][w]; 35 | } 36 | } 37 | 38 | return K[n][W]; 39 | } 40 | 41 | int main() { 42 | ios::sync_with_stdio(0); 43 | cin.tie(0); cout.tie(0); 44 | 45 | int N = getint(); 46 | int W = getint(); 47 | 48 | vector values(N); 49 | vector weight(N); 50 | 51 | for(int a = 0; a < N; ++a){ 52 | weight[a] = getint(); 53 | } 54 | for(int a = 0; a < N; ++a){ 55 | values[a] = getint(); 56 | } 57 | 58 | vector dp(W+1); 59 | for(int a = 0; a < N; ++a) 60 | for (int b = W; b >= weight[a]; b--) 61 | dp[b] = max(dp[b], dp[b - weight[a]] + values[a]); 62 | 63 | cout << dp[W]; 64 | } -------------------------------------------------------------------------------- /DynamicProgramming/CoinCombinations/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/CoinCombinations/a.out -------------------------------------------------------------------------------- /DynamicProgramming/CoinCombinations/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #pragma GCC optimize("Ofast") 4 | #pragma GCC target("avx,avx2,fma") 5 | #pragma GCC optimize("unroll-loops") 6 | 7 | using namespace std; 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | #define MOD 1000000007 24 | /* 25 | count[0] = 1; 26 | for (int x = 1; x <= n; x++) { 27 | for (auto c : coins) { 28 | if (x-c >= 0) { 29 | count[x] += count[x-c]; 30 | } 31 | } 32 | } 33 | */ 34 | 35 | const int MAXN = 100, MAXX = 1e6; 36 | int c[MAXN]; 37 | long long dp[MAXX + 1] = {0}; 38 | 39 | int main() { 40 | ios::sync_with_stdio(0); 41 | cin.tie(0); cout.tie(0); 42 | 43 | int N = getint(); 44 | int M = getint(); 45 | for(int a = 0; a < N; ++a){ 46 | c[a] = getint(); 47 | } 48 | sort(c, c + N); 49 | 50 | dp[0] = 1; 51 | for (int i = c[0]; i <= M; ++i) { 52 | for (int j = 0; j < N && c[j] <= i; ++j) 53 | dp[i] += dp[i - c[j]]; 54 | 55 | for(int a = 0; a <= M; ++a){ 56 | cout << dp[a] << " "; 57 | } 58 | cout << endl; 59 | dp[i] %= MOD; 60 | } 61 | 62 | for(int a = 0; a <= M; ++a){ 63 | cout << dp[a] << " "; 64 | } 65 | 66 | cout << dp[M]; 67 | } -------------------------------------------------------------------------------- /DynamicProgramming/CoinCombinations2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/CoinCombinations2/a.out -------------------------------------------------------------------------------- /DynamicProgramming/CoinCombinations2/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | template T getint() { 6 | T val=0; 7 | char c; 8 | bool neg=false; 9 | while((c=getchar()) && !(c>='0' && c<='9')) { 10 | neg|=c=='-'; 11 | } 12 | do { 13 | val=(val*10)+c-'0'; 14 | } while((c=getchar()) && (c>='0' && c<='9')); 15 | 16 | return val*(neg?-1:1); 17 | } 18 | 19 | // create a map to store solutions of subproblems 20 | unordered_map lookup; 21 | 22 | // Function to find the total number of distinct ways to get change of N from unlimited supply of coins in set S 23 | ll nCoins(vector &S, int n, int N){ 24 | if (N == 0) 25 | return 1; 26 | if (N < 0 || n < 0) 27 | return 0; 28 | 29 | // construct a unique map key from dynamic elements of the input 30 | string key = to_string(n) + "|" + to_string(N); 31 | 32 | // if sub-problem is seen for the first time, solve it and store its result in a map 33 | if (lookup.find(key) == lookup.end()){ 34 | // Case 1. include current coin S[n] in solution and recur 35 | // with remaining change (N - S[n]) with same number of coins 36 | ll include = nCoins(S, n, N - S[n]); 37 | 38 | // Case 2. exclude current coin S[n] from solution and recur 39 | // for remaining coins (n - 1) 40 | ll exclude = nCoins(S, n - 1, N); 41 | 42 | // assign total ways by including or excluding current coin 43 | lookup[key] = include + exclude; 44 | } 45 | 46 | // return solution to current sub-problem 47 | return lookup[key]; 48 | } 49 | 50 | int main() { 51 | ios::sync_with_stdio(0); 52 | cin.tie(0); cout.tie(0); 53 | 54 | int N = getint(); 55 | int M = getint(); 56 | vector v(N); 57 | for(int a = 0; a < N; ++a){ 58 | v[a] = getint(); 59 | } 60 | sort(v.begin(), v.end()); 61 | 62 | cout << nCoins(v, N-1, M); 63 | } -------------------------------------------------------------------------------- /DynamicProgramming/DiceCombinations/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/DiceCombinations/a.out -------------------------------------------------------------------------------- /DynamicProgramming/DiceCombinations/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef pair ii; 5 | typedef long long ll; 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | long findWays(int d, int s) { 22 | long mem[d + 1][s + 1]; 23 | memset(mem,0,sizeof mem); 24 | mem[0][0] = 1; 25 | 26 | // Iterate over dices 27 | for (int i = 1; i <= d; i++) { 28 | // Iterate over sum 29 | for (int j = i; j <= s; j++) { 30 | // The result is obtained in two ways, pin the current dice and spending 1 of the value, 31 | // so we have mem[i-1][j-1] remaining combinations, to find the remaining combinations we 32 | // would have to pin the values ??above 1 then we use mem[i][j-1] to sum all combinations 33 | // that pin the remaining j-1's. But there is a way, when "j-f-1> = 0" we would be adding 34 | // extra combinations, so we remove the combinations that only pin the extrapolated dice face and 35 | // subtract the extrapolated combinations. 36 | mem[i][j] = mem[i][j - 1] + mem[i - 1][j - 1]; 37 | if (j - 3 - 1 >= 0) 38 | mem[i][j] -= mem[i - 1][j - 3 - 1]; 39 | } 40 | } 41 | return mem[d][s]; 42 | } 43 | 44 | int main() { 45 | ios::sync_with_stdio(0); 46 | cin.tie(0); cout.tie(0); 47 | 48 | int M = getint(); 49 | cout << findWays(6, M); 50 | } -------------------------------------------------------------------------------- /DynamicProgramming/EditDistance/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/EditDistance/a.out -------------------------------------------------------------------------------- /DynamicProgramming/EditDistance/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | int editDistDP(string s1, string s2, int m, int n){ 24 | // vector> dp(m+1, vector(n+1)); 25 | int dp[m+1][n+1]; 26 | 27 | for(int a = 0; a <= m; ++a){ 28 | for(int b = 0; b <= n; ++b){ 29 | if(a == 0) 30 | dp[a][b] = b; 31 | else if(b == 0) 32 | dp[a][b] = a; 33 | else if(s1[a-1] == s2[b-1]) 34 | dp[a][b] = dp[a-1][b-1]; 35 | else 36 | dp[a][b] = 1 + min(min(dp[a][b-1], dp[a-1][b]), dp[a-1][b-1]); 37 | } 38 | } 39 | return dp[m][n]; 40 | } 41 | 42 | int main() { 43 | ios::sync_with_stdio(0); 44 | cin.tie(0); cout.tie(0); 45 | 46 | string s1, s2; 47 | cin >> s1 >> s2; 48 | 49 | cout << editDistDP(s1, s2, s1.length(), s2.length()); 50 | } -------------------------------------------------------------------------------- /DynamicProgramming/GridPaths/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/GridPaths/a.out -------------------------------------------------------------------------------- /DynamicProgramming/GridPaths/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MAX 1e9 8 | #define MOD 1000000007 9 | 10 | 11 | template T getint() { 12 | T val=0; 13 | char c; 14 | bool neg=false; 15 | while((c=getchar()) && !(c>='0' && c<='9')) { 16 | neg|=c=='-'; 17 | } 18 | do { 19 | val=(val*10)+c-'0'; 20 | } while((c=getchar()) && (c>='0' && c<='9')); 21 | 22 | return val*(neg?-1:1); 23 | } 24 | 25 | 26 | int main() { 27 | ios::sync_with_stdio(false); 28 | cin.tie(0); 29 | cout.precision(10);; 30 | int N; 31 | cin >> N; 32 | vector v(N); 33 | for(int a = 0; a < N; ++a){ 34 | cin >> v[a]; 35 | } 36 | 37 | if(v[N-1][N-1] == '*' || v[0][0] == '*'){ 38 | cout << 0; 39 | return 0; 40 | } 41 | 42 | vector> dp(N, vector(N)); 43 | for(int a = 0; a < N; ++a){ 44 | if(v[0][a] == '*') 45 | break; 46 | dp[0][a] = 1; 47 | } 48 | for(int a = 0; a < N; ++a){ 49 | if(v[a][0] == '*') 50 | break; 51 | dp[a][0] = 1; 52 | } 53 | 54 | for(int a = 1; a < N; ++a){ 55 | for(int b = 1; b < N; ++b){ 56 | if(v[a][b] != '*'){ 57 | dp[a][b] = (dp[a-1][b] + dp[a][b-1]) % MOD; 58 | } 59 | } 60 | } 61 | 62 | cout << dp[N-1][N-1]; 63 | } -------------------------------------------------------------------------------- /DynamicProgramming/IncreasingSubsequence/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/IncreasingSubsequence/a.out -------------------------------------------------------------------------------- /DynamicProgramming/IncreasingSubsequence/m.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | using namespace std; 5 | 6 | #pragma GCC optimize ("O3") 7 | #pragma GCC target ("sse4") 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | int CeilIndex(vector& v, int l, int r, int key) 24 | { 25 | while (r - l > 1) { 26 | int m = l + (r - l) / 2; 27 | if (v[m] >= key) 28 | r = m; 29 | else 30 | l = m; 31 | } 32 | 33 | return r; 34 | } 35 | 36 | int LongestIncreasingSubsequenceLength(vector& v) 37 | { 38 | if (v.size() == 0) 39 | return 0; 40 | 41 | vector tail(v.size(), 0); 42 | int length = 1; // always points empty slot in tail 43 | 44 | tail[0] = v[0]; 45 | for (size_t i = 1; i < v.size(); i++) { 46 | 47 | // new smallest value 48 | if (v[i] < tail[0]) 49 | tail[0] = v[i]; 50 | 51 | // v[i] extends largest subsequence 52 | else if (v[i] > tail[length - 1]) 53 | tail[length++] = v[i]; 54 | 55 | // v[i] will become end candidate of an existing 56 | // subsequence or Throw away larger elements in all 57 | // LIS, to make room for upcoming grater elements 58 | // than v[i] (and also, v[i] would have already 59 | // appeared in one of LIS, identify the location 60 | // and replace it) 61 | else 62 | tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i]; 63 | } 64 | 65 | return length; 66 | } 67 | 68 | 69 | int main() { 70 | ios::sync_with_stdio(0); 71 | cin.tie(0); cout.tie(0); 72 | 73 | int N = getint(); 74 | vector v(N); 75 | for(int a = 0; a < N;++a) 76 | v[a] = getint(); 77 | 78 | cout << LongestIncreasingSubsequenceLength(v); 79 | } -------------------------------------------------------------------------------- /DynamicProgramming/MinimizingCoins/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/MinimizingCoins/a.out -------------------------------------------------------------------------------- /DynamicProgramming/MinimizingCoins/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template T getint() { 5 | T val=0; 6 | char c; 7 | bool neg=false; 8 | while((c=getchar()) && !(c>='0' && c<='9')) { 9 | neg|=c=='-'; 10 | } 11 | do { 12 | val=(val*10)+c-'0'; 13 | } while((c=getchar()) && (c>='0' && c<='9')); 14 | 15 | return val*(neg?-1:1); 16 | } 17 | 18 | #define MAX 1e9 19 | 20 | int minCoins(vector &coins, int M) { 21 | // int table[M+1]; 22 | // table[0] = 0; 23 | // for (int i=1; i<=M; i++) 24 | // table[i] = INT_MAX; 25 | 26 | // // Compute minimum coins required for all values from 1 to M 27 | // for (int i=1; i <= M; i++) { 28 | // // Go through all coins smaller than i 29 | // for(auto coin : coins){ 30 | // if (coin <= i) { 31 | // int sub_res = table[i-coin]; 32 | // if (sub_res != INT_MAX && sub_res + 1 < table[i]) 33 | // table[i] = sub_res + 1; 34 | // } else { 35 | // break; 36 | // } 37 | // } 38 | // } 39 | 40 | /* 41 | Consider a money system consisting of n coins. Each coin has a positive integer value. 42 | Your task is to produce a sum of money x using the available coins in such a way that the number of coins is minimal. 43 | For example, if the coins are {1,5,7} and the desired sum is 11, an optimal solution is 5+5+1 which requires 3 coins. 44 | */ 45 | 46 | int dp[M+1]; 47 | dp[0] = 0; 48 | for (int x = 1; x <= M; x++) { 49 | dp[x] = MAX; 50 | for (auto c : coins) { 51 | if (x-c >= 0) { 52 | dp[x] = min(dp[x], dp[x-c]+1); 53 | } 54 | } 55 | } 56 | 57 | if(dp[M] == MAX) 58 | return -1; 59 | return dp[M]; 60 | } 61 | 62 | int main() { 63 | ios::sync_with_stdio(0); 64 | cin.tie(0); cout.tie(0); 65 | 66 | int N = getint(); 67 | int M = getint(); 68 | vector v(N); 69 | for(int a = 0; a < N; ++a){ 70 | v[a] = getint(); 71 | } 72 | sort(v.begin(), v.end()); 73 | 74 | cout << minCoins(v, M); 75 | } -------------------------------------------------------------------------------- /DynamicProgramming/MoneySums/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/MoneySums/a.out -------------------------------------------------------------------------------- /DynamicProgramming/MoneySums/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | template T getint() { 6 | T val=0; 7 | char c; 8 | bool neg=false; 9 | while((c=getchar()) && !(c>='0' && c<='9')) { 10 | neg|=c=='-'; 11 | } 12 | do { 13 | val=(val*10)+c-'0'; 14 | } while((c=getchar()) && (c>='0' && c<='9')); 15 | 16 | return val*(neg?-1:1); 17 | } 18 | 19 | int main() { 20 | ios::sync_with_stdio(0); 21 | cin.tie(0); cout.tie(0); 22 | 23 | int N = getint(); 24 | vector v(N); 25 | for(int a = 0; a < N; ++a){ 26 | v[a] = getint(); 27 | } 28 | set s; 29 | s.insert(v[0]); 30 | for(int a = 1; a < N; ++a){ 31 | int curr = v[a]; 32 | set s2 = s; 33 | for(auto elm : s2){ 34 | s.insert(curr + elm); 35 | } 36 | s.insert(curr); 37 | } 38 | 39 | cout << s.size() << '\n'; 40 | for(auto elm : s){ 41 | cout << elm << " "; 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /DynamicProgramming/MoneySums/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 100005 3 | 4 | using namespace std; 5 | 6 | int n, x, c; 7 | bool dp[MAX]; 8 | 9 | int main() { 10 | cin.sync_with_stdio(0); cin.tie(0); 11 | cin >> n; 12 | dp[0] = 1; 13 | 14 | for(int i=0;i> x; 16 | for(int j=MAX;j>=0;j--) 17 | if(dp[j] && j+x<=MAX) { 18 | if(!dp[j+x]) c++; 19 | dp[j+x] = 1; 20 | } 21 | } 22 | 23 | cout << c << "\n"; 24 | for(int i=1;i<=MAX;i++) 25 | if(dp[i]) 26 | cout << i << " "; 27 | return 0; 28 | } -------------------------------------------------------------------------------- /DynamicProgramming/RectangleCutting/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/RectangleCutting/a.out -------------------------------------------------------------------------------- /DynamicProgramming/RectangleCutting/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | template T getint() { 6 | T val=0; 7 | char c; 8 | bool neg=false; 9 | while((c=getchar()) && !(c>='0' && c<='9')) { 10 | neg|=c=='-'; 11 | } 12 | do { 13 | val=(val*10)+c-'0'; 14 | } while((c=getchar()) && (c>='0' && c<='9')); 15 | 16 | return val*(neg?-1:1); 17 | } 18 | 19 | const int MAX = 300; 20 | int dp[MAX][MAX]; 21 | 22 | // Returns min number of squares needed 23 | int minimumSquare(int m, int n) 24 | { 25 | // Initializing max values to vertical_min 26 | // and horizontal_min 27 | int vertical_min = INT_MAX; 28 | int horizontal_min = INT_MAX; 29 | 30 | // If the given rectangle is already a square 31 | if (m == n) 32 | return 1; 33 | 34 | // If the answer for the given rectangle is 35 | // previously calculated return that answer 36 | if (dp[m][n]) 37 | return dp[m][n]; 38 | 39 | /* The rectangle is cut horizontally and 40 | vertically into two parts and the cut 41 | with minimum value is found for every 42 | recursive call. 43 | */ 44 | 45 | for (int i = 1;i<= m/2;i++) 46 | { 47 | // Calculating the minimum answer for the 48 | // rectangles with width equal to n and length 49 | // less than m for finding the cut point for 50 | // the minimum answer 51 | horizontal_min = min(minimumSquare(i, n) + 52 | minimumSquare(m-i, n), horizontal_min); 53 | } 54 | 55 | for (int j = 1;j<= n/2;j++) 56 | { 57 | // Calculating the minimum answer for the 58 | // rectangles with width less than n and 59 | // length equal to m for finding the cut 60 | // point for the minimum answer 61 | vertical_min = min(minimumSquare(m, j) + 62 | minimumSquare(m, n-j), vertical_min); 63 | } 64 | 65 | // Minimum of the vertical cut or horizontal 66 | // cut to form a square is the answer 67 | dp[m][n] = min(vertical_min, horizontal_min); 68 | 69 | return dp[m][n]; 70 | } 71 | 72 | int main() { 73 | ios::sync_with_stdio(0); 74 | cin.tie(0); cout.tie(0); 75 | 76 | int a = getint(); 77 | int b = getint(); 78 | 79 | cout << minimumSquare(a, b); 80 | } -------------------------------------------------------------------------------- /DynamicProgramming/RemovalGame/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/RemovalGame/a.out -------------------------------------------------------------------------------- /DynamicProgramming/RemovalGame/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | long long optimalStrategyOfGame(vector &arr, int n) 22 | { 23 | long long table[n][n]; 24 | 25 | for (int gap = 0; gap < n; ++gap) { 26 | for (int i = 0, j = gap; j < n; ++i, ++j) { 27 | // Here x is value of F(i+2, j), y is F(i+1, j-1) and z is F(i, j-2) 28 | auto x = ((i + 2) <= j) ? table[i + 2][j] : 0; 29 | auto y = ((i + 1) <= (j - 1)) ? table[i + 1][j - 1] : 0; 30 | auto z = (i <= (j - 2)) ? table[i][j - 2] : 0; 31 | 32 | table[i][j] = max(arr[i] + min(x, y), arr[j] + min(y, z)); 33 | } 34 | } 35 | 36 | return table[0][n - 1]; 37 | } 38 | 39 | 40 | int main() { 41 | ios::sync_with_stdio(0); 42 | cin.tie(0); cout.tie(0); 43 | 44 | int N = getint(); 45 | 46 | vector v(N); 47 | for(int a = 0; a < N; ++a){ 48 | v[a] = getint(); 49 | } 50 | 51 | cout << optimalStrategyOfGame(v, N); 52 | } -------------------------------------------------------------------------------- /DynamicProgramming/RemovingDigits/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/RemovingDigits/a.out -------------------------------------------------------------------------------- /DynamicProgramming/RemovingDigits/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template T getint() { 5 | T val=0; 6 | char c; 7 | bool neg=false; 8 | while((c=getchar()) && !(c>='0' && c<='9')) { 9 | neg|=c=='-'; 10 | } 11 | do { 12 | val=(val*10)+c-'0'; 13 | } while((c=getchar()) && (c>='0' && c<='9')); 14 | 15 | return val*(neg?-1:1); 16 | } 17 | 18 | #define MAX 1e9 19 | 20 | int maxDigit(int N){ 21 | int ret = 0; 22 | while(N > 0){ 23 | ret = max(ret, N % 10); 24 | N /= 10; 25 | } 26 | return ret; 27 | } 28 | 29 | int main() { 30 | ios::sync_with_stdio(0); 31 | cin.tie(0); cout.tie(0); 32 | 33 | int N = getint(); 34 | vector dp(N+1, MAX); 35 | dp[0] = 0; 36 | for(int a = 1; a < 10; ++a){ 37 | dp[a] = 1; 38 | } 39 | for(int a = 10; a <= N; ++a){ 40 | string num = to_string(a); 41 | for(auto s : num){ 42 | int int_s = s - 48; 43 | dp[a] = min(dp[a], dp[a-int_s]+1); 44 | } 45 | } 46 | 47 | // while(N > 0){ 48 | // N -= maxDigit(N); 49 | // ans++; 50 | // } 51 | // cout << ans; 52 | 53 | cout << dp[N]; 54 | } -------------------------------------------------------------------------------- /DynamicProgramming/TwoSets2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/DynamicProgramming/TwoSets2/a.out -------------------------------------------------------------------------------- /DynamicProgramming/TwoSets2/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | template T getint() { 6 | T val=0; 7 | char c; 8 | bool neg=false; 9 | while((c=getchar()) && !(c>='0' && c<='9')) { 10 | neg|=c=='-'; 11 | } 12 | do { 13 | val=(val*10)+c-'0'; 14 | } while((c=getchar()) && (c>='0' && c<='9')); 15 | 16 | return val*(neg?-1:1); 17 | } 18 | 19 | // int partitioning(int n, ll sum) { 20 | // int i, j; 21 | 22 | // bool part[sum+1][n+1]; 23 | 24 | // // initialize top row as true 25 | // for (i = 0; i <= n; i++) 26 | // part[0][i] = true; 27 | 28 | // // initialize leftmost column, except part[0][0], as 0 29 | // for (i = 1; i <= sum; i++) 30 | // part[i][0] = false; 31 | 32 | // // Fill the partition table in botton up manner 33 | // for (i = 1; i <= sum; i++) 34 | // { 35 | // for (j = 1; j <= n; j++) 36 | // { 37 | // part[i][j] = part[i][j-1]; 38 | // if (i >= j) 39 | // part[i][j] = part[i][j] || part[i - j][j-1]; 40 | // } 41 | // } 42 | 43 | // // uncomment this part to print table 44 | // for (i = 0; i <= sum; i++) { 45 | // for (j = 0; j <= n; j++) 46 | // printf ("%4d", part[i][j]); 47 | // printf("\n"); 48 | // } 49 | 50 | // return part[sum][n]; 51 | // } 52 | 53 | int main() { 54 | ios::sync_with_stdio(0); 55 | cin.tie(0); cout.tie(0); 56 | 57 | int N = getint(); 58 | vector v(N); 59 | 60 | // cout << partitioning(N, (N*(N+1))); 61 | } -------------------------------------------------------------------------------- /GraphAlgorithms/BuildingRoad/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/GraphAlgorithms/BuildingRoad/a.out -------------------------------------------------------------------------------- /GraphAlgorithms/BuildingRoad/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // a structure to represent an edge in the graph 5 | struct Edge { 6 | int src, dest; 7 | }; 8 | 9 | // a structure to represent a graph 10 | struct Graph { 11 | // V-> Number of vertices, E-> Number of edges 12 | int V, E; 13 | // graph is represented as an array of edges 14 | struct Edge* edge; 15 | }; 16 | 17 | struct subset { 18 | int parent; 19 | int rank; 20 | }; 21 | 22 | // Creates a graph with V vertices and E edges 23 | struct Graph* createGraph(int V, int E) 24 | { 25 | struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) ); 26 | graph->V = V; 27 | graph->E = E; 28 | 29 | graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) ); 30 | return graph; 31 | } 32 | 33 | // A utility function to find set of an element i 34 | // (uses path compression technique) 35 | int find(struct subset subsets[], int i) 36 | { 37 | // find root and make root as parent of i (path compression) 38 | if (subsets[i].parent != i) 39 | subsets[i].parent = find(subsets, subsets[i].parent); 40 | 41 | return subsets[i].parent; 42 | } 43 | 44 | // A function that does union of two sets of x and y 45 | // (uses union by rank) 46 | void Union(struct subset subsets[], int x, int y) 47 | { 48 | int xroot = find(subsets, x); 49 | int yroot = find(subsets, y); 50 | 51 | // Attach smaller rank tree under root of high rank tree 52 | // (Union by Rank) 53 | if (subsets[xroot].rank < subsets[yroot].rank) 54 | subsets[xroot].parent = yroot; 55 | else if (subsets[xroot].rank > subsets[yroot].rank) 56 | subsets[yroot].parent = xroot; 57 | 58 | // If ranks are same, then make one as root and increment 59 | // its rank by one 60 | else 61 | { 62 | subsets[yroot].parent = xroot; 63 | subsets[xroot].rank++; 64 | } 65 | } 66 | 67 | // The main function to check whether a given graph contains cycle or not 68 | int isCycle( struct Graph* graph ) 69 | { 70 | int V = graph->V; 71 | int E = graph->E; 72 | 73 | // Allocate memory for creating V sets 74 | struct subset *subsets = (struct subset*) malloc( V * sizeof(struct subset) ); 75 | 76 | for (int v = 0; v < V; ++v) 77 | { 78 | subsets[v].parent = v; 79 | subsets[v].rank = 0; 80 | } 81 | 82 | for(int e = 0; e < E; ++e) 83 | { 84 | int x = find(subsets, graph->edge[e].src); 85 | int y = find(subsets, graph->edge[e].dest); 86 | 87 | cout << x << " " << y << endl; 88 | 89 | // if (x == y) 90 | // return 1; 91 | 92 | Union(subsets, x, y); 93 | } 94 | 95 | for (int v = 0; v < E; ++v) 96 | { 97 | cout << subsets[v].parent << endl; 98 | } 99 | 100 | return 0; 101 | } 102 | 103 | // Driver program to test above functions 104 | int main() 105 | { 106 | int V = 5, E = 4; 107 | struct Graph* graph = createGraph(V, E); 108 | 109 | // add edge 0-1 110 | graph->edge[0].src = 0; 111 | graph->edge[0].dest = 1; 112 | 113 | // add edge 1-2 114 | graph->edge[1].src = 1; 115 | graph->edge[1].dest = 2; 116 | 117 | // add edge 0-2 118 | graph->edge[2].src = 0; 119 | graph->edge[2].dest = 2; 120 | 121 | graph->edge[3].src = 3; 122 | graph->edge[3].dest = 4; 123 | 124 | isCycle(graph); 125 | return 0; 126 | } -------------------------------------------------------------------------------- /GraphAlgorithms/BuildingTeams/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/GraphAlgorithms/BuildingTeams/a.out -------------------------------------------------------------------------------- /GraphAlgorithms/BuildingTeams/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | template T getint() { 6 | T val=0; 7 | char c; 8 | bool neg=false; 9 | while((c=getchar()) && !(c>='0' && c<='9')) { 10 | neg|=c=='-'; 11 | } 12 | do { 13 | val=(val*10)+c-'0'; 14 | } while((c=getchar()) && (c>='0' && c<='9')); 15 | 16 | return val*(neg?-1:1); 17 | } 18 | 19 | #define MAX 1000000 20 | 21 | int color[MAX], visited[MAX]; 22 | vector graph[MAX]; 23 | 24 | bool bfs(int start) 25 | { 26 | queue q; 27 | q.push(start); 28 | color[start] = 1; 29 | visited[start] = 1; 30 | 31 | while(!q.empty()) 32 | { 33 | auto u = q.front(); 34 | q.pop(); 35 | 36 | for(auto edge : graph[u]){ 37 | if(color[u] == color[edge]){ 38 | return false; 39 | } 40 | if(visited[edge] == 0){ 41 | visited[edge] = 1; 42 | color[edge] = 3 - color[u]; // 1 or 2 are the colors 43 | q.push(edge); 44 | } 45 | } 46 | } 47 | return true; 48 | } 49 | 50 | bool is_bipartite(int N) 51 | { 52 | for(int i=0; i < N; i++) 53 | if (visited[i] == 0 && !bfs(i)) 54 | return false; 55 | return true; 56 | } 57 | 58 | int main() 59 | { 60 | ios::sync_with_stdio(0); 61 | cin.tie(0); cout.tie(0); 62 | 63 | int i,j, x, y; 64 | int N = getint(); N++; 65 | int M = getint(); 66 | 67 | for(i=0; i(); 70 | y = getint(); 71 | graph[x].push_back(y); 72 | graph[y].push_back(x); 73 | } 74 | if(is_bipartite(N)) 75 | for (j=1; j 2 | using namespace std; 3 | 4 | // const int MAXN = 1005; 5 | // int n, m, ans; 6 | // char board[MAXN][MAXN]; 7 | // bool visited[MAXN][MAXN]; 8 | // int dRow[4] = {1, -1, 0, 0}; 9 | // int dCols[4] = {0, 0, 1, -1}; 10 | 11 | // inline bool valid(int row, int cols){ 12 | // return (row >= 0 && row < n && cols >= 0 && cols < m); 13 | // } 14 | 15 | void dfs(vector> &visited, vector> &v, int a, int b, int N, int M){ 16 | if(a > N-1 || b > M-1 || a < 0 || b < 0 || visited[a][b] == 1 || v[a][b] == '#') 17 | return; 18 | visited[a][b] = 1; 19 | 20 | if(a+1 < N && visited[a+1][b] == 0 && v[a+1][b] == '.') 21 | dfs(visited, v, a+1, b, N, M); 22 | if(a-1 >= 0 && visited[a-1][b] == 0 && v[a-1][b] == '.') 23 | dfs(visited, v, a-1, b, N, M); 24 | if(b+1 < M && visited[a][b+1] == 0 && v[a][b+1] == '.') 25 | dfs(visited, v, a, b+1, N, M); 26 | if(b-1 >= 0 && visited[a][b-1] == 0 && v[a][b-1] == '.') 27 | dfs(visited, v, a, b-1, N, M); 28 | 29 | return; 30 | } 31 | 32 | 33 | int main() { 34 | ios::sync_with_stdio(0); 35 | cin.tie(0); cout.tie(0); 36 | 37 | int N, M; 38 | cin >> N >> M; 39 | 40 | vector> v(N, vector(M)); 41 | vector> visited(N, vector(M, 0)); 42 | for(int a = 0; a < N; ++a){ 43 | for(int b = 0; b < M; ++b){ 44 | cin >> v[a][b]; 45 | } 46 | } 47 | 48 | int c = 0; 49 | for(int a = 0; a < N; ++a){ 50 | for(int b = 0; b < M; ++b){ 51 | if(!visited[a][b] && v[a][b] == '.'){ 52 | c++; 53 | dfs(visited, v, a, b, N, M); 54 | } 55 | } 56 | } 57 | cout << c; 58 | } -------------------------------------------------------------------------------- /GraphAlgorithms/FlightDiscount/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/GraphAlgorithms/FlightDiscount/a.out -------------------------------------------------------------------------------- /GraphAlgorithms/FlightDiscount/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template T getint() { 5 | T val=0; 6 | char c; 7 | bool neg=false; 8 | while((c=getchar()) && !(c>='0' && c<='9')) { 9 | neg|=c=='-'; 10 | } 11 | do { 12 | val=(val*10)+c-'0'; 13 | } while((c=getchar()) && (c>='0' && c<='9')); 14 | 15 | return val*(neg?-1:1); 16 | } 17 | 18 | #define INF 1e9 19 | 20 | 21 | vector result; 22 | long long maxElm = 0; 23 | void printPath(vector &parent, vector>> &adj, long long j) 24 | { 25 | if (parent[j] == -1) 26 | return; 27 | printPath(parent, adj, parent[j]); 28 | 29 | for(auto a : adj[parent[j]]){ 30 | if(a.first == j){ 31 | result.push_back(a.second); 32 | maxElm = max(maxElm, a.second); 33 | break; 34 | } 35 | } 36 | // cout << j << " " << parent[j] << endl; 37 | } 38 | 39 | int main() 40 | { 41 | ios::sync_with_stdio(0); 42 | cin.tie(0); cout.tie(0); 43 | 44 | int N = getint(); 45 | int M = getint(); 46 | 47 | long long source = 1; 48 | long long destination = N; 49 | 50 | vector>> adj(N+1); 51 | for(int a = 0; a < M; ++a) 52 | { 53 | int s = getint(); 54 | int d = getint(); 55 | int p = getint(); 56 | adj[s].push_back(make_pair(d, p)); 57 | } 58 | 59 | vector dist(N+1, INF); 60 | vector parent(N+1, -1); 61 | priority_queue< pair, vector>, greater> > pq; 62 | 63 | pq.push({0, source}); 64 | dist[source] = 0; 65 | 66 | while(!pq.empty()) 67 | { 68 | auto front = pq.top(); 69 | pq.pop(); 70 | 71 | auto u = front.second; 72 | if(u == destination){ 73 | printPath(parent, adj, N); 74 | long long sum = 0; 75 | bool done = true; 76 | for(auto elm : result){ 77 | if(elm == maxElm && done){ 78 | sum += round(maxElm/2); 79 | done = false; 80 | } else { 81 | sum += elm; 82 | } 83 | } 84 | cout << sum << endl; 85 | return 0; 86 | } 87 | 88 | if(front.first > dist[u]) 89 | continue; 90 | 91 | for(auto i : adj[u]) 92 | { 93 | auto v = i.first; 94 | auto w = i.second; 95 | if (dist[v] > dist[u] + w) 96 | { 97 | parent[v] = u; 98 | dist[v] = dist[u] + w; 99 | pq.push(make_pair(dist[v], v)); 100 | } 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /GraphAlgorithms/Labyrinth/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/GraphAlgorithms/Labyrinth/a.out -------------------------------------------------------------------------------- /GraphAlgorithms/Labyrinth/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int dRow[4] = {1, -1, 0, 0}; 5 | int dCols[4] = {0, 0, 1, -1}; 6 | 7 | inline bool valid(int row, int cols, int n, int m){ 8 | return (row >= 0 && row < n && cols >= 0 && cols < m); 9 | } 10 | 11 | int main() { 12 | ios::sync_with_stdio(0); 13 | cin.tie(0); cout.tie(0); 14 | 15 | int N, M; 16 | cin >> N >> M; 17 | 18 | vector> v(N, vector(M)); 19 | vector> visited(N, vector(M, 0)); 20 | pair start; 21 | for(int a = 0; a < N; ++a){ 22 | for(int b = 0; b < M; ++b){ 23 | cin >> v[a][b]; 24 | if(v[a][b] == 'A') 25 | start = make_pair(a, b); 26 | } 27 | } 28 | 29 | list> q; 30 | visited[start.first][start.second] = 1; 31 | q.push_back(start); 32 | map, pair,char> > parent; 33 | while (!q.empty()) 34 | { 35 | auto s = q.front(); 36 | int a = s.first, b = s.second; 37 | q.pop_front(); 38 | 39 | for(auto i : dRow){ 40 | for(auto j : dCols){ 41 | if(valid(a+i, b+j, N, M) && (abs(i) != abs(j)) && !visited[a+i][b+j] && (v[a+i][b+j] == '.' || v[a+i][b+j] == 'B') ){ 42 | char dir; 43 | if(i == 1 && j == 0) 44 | dir = 'D'; 45 | if(i == -1 && j == 0) 46 | dir = 'U'; 47 | if(j == 1 && i == 0) 48 | dir = 'R'; 49 | if(j == -1 && i == 0) 50 | dir = 'L'; 51 | 52 | parent[make_pair(a+i, b+j)] = make_pair(make_pair(a, b), dir); 53 | 54 | if(v[a+i][b+j] == 'B'){ 55 | auto end = make_pair(a+i, b+j); 56 | string res = ""; 57 | while(true){ 58 | res += parent[end].second; 59 | end = parent[end].first; 60 | if(end.first == start.first && end.second == start.second){ 61 | break; 62 | } 63 | } 64 | reverse(res.begin(), res.end()); 65 | cout << "YES\n"; 66 | cout << res.length() << "\n"; 67 | cout << res; 68 | return 0; 69 | } 70 | 71 | visited[a+i][b+j] = 1; 72 | q.push_back(make_pair(a+i, b+j)); 73 | } 74 | } 75 | } 76 | } 77 | 78 | cout << "NO"; 79 | } -------------------------------------------------------------------------------- /GraphAlgorithms/LongestFlightRoute/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/GraphAlgorithms/LongestFlightRoute/a.out -------------------------------------------------------------------------------- /GraphAlgorithms/LongestFlightRoute/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define NINF INT_MIN 8 | #define INF 3e18 9 | 10 | using namespace std; 11 | 12 | template T getint() { 13 | T val=0; 14 | char c; 15 | bool neg=false; 16 | while((c=getchar()) && !(c>='0' && c<='9')) { 17 | neg|=c=='-'; 18 | } 19 | do { 20 | val=(val*10)+c-'0'; 21 | } while((c=getchar()) && (c>='0' && c<='9')); 22 | 23 | return val*(neg?-1:1); 24 | } 25 | 26 | 27 | class AdjListNode 28 | { 29 | int v; 30 | int weight; 31 | public: 32 | AdjListNode(int _v, int _w){ 33 | v = _v; 34 | weight = _w; 35 | } 36 | int getV(){ 37 | return v; 38 | } 39 | int getWeight(){ 40 | return weight; 41 | } 42 | }; 43 | 44 | 45 | // Class to represent a graph using adjacency list representation 46 | class Graph{ 47 | int V; // No. of vertices’ 48 | list *adj; 49 | void topologicalSortUtil(int v, bool visited[], stack &Stack); 50 | public: 51 | Graph(int V); 52 | void addEdge(int u, int v, int weight); 53 | void longestPath(int s); 54 | }; 55 | 56 | Graph::Graph(int V){ 57 | this->V = V; 58 | adj = new list [V]; 59 | } 60 | 61 | void Graph::addEdge(int u, int v, int weight) 62 | { 63 | AdjListNode node(v, weight); 64 | adj[u].push_back(node); 65 | } 66 | 67 | void Graph::topologicalSortUtil(int v, bool visited[], stack &Stack) 68 | { 69 | visited[v] = true; 70 | for(auto node : adj[v]){ 71 | if (!visited[node.getV()]) 72 | topologicalSortUtil(node.getV(), visited, Stack); 73 | } 74 | // Push current vertex to stack which stores topological sort 75 | Stack.push(v); 76 | } 77 | 78 | // The function to find longest distances from a given vertex. It uses 79 | void Graph::longestPath(int s) 80 | { 81 | stack Stack; 82 | int dist[V]; 83 | 84 | bool *visited = new bool[V]; 85 | for (int i = 0; i < V; i++) 86 | visited[i] = false; 87 | 88 | for (int i = 0; i < V; i++) 89 | if (visited[i] == false) 90 | topologicalSortUtil(i, visited, Stack); 91 | 92 | for (int i = 0; i < V; i++) 93 | dist[i] = NINF; 94 | dist[s] = 0; 95 | 96 | vector parent(V); 97 | 98 | while(!Stack.empty()){ 99 | // Get the next vertex from topological order 100 | int u = Stack.top(); 101 | Stack.pop(); 102 | // Update distances of all adjacent vertices 103 | if (dist[u] != NINF){ 104 | for(auto node : adj[u]){ 105 | if (dist[node.getV()] < dist[u] + node.getWeight()){ 106 | parent[node.getV()] = u; 107 | dist[node.getV()] = dist[u] + node.getWeight(); 108 | } 109 | } 110 | } 111 | } 112 | 113 | if(dist[V-1] != NINF){ 114 | cout << dist[V-1]+1 << endl; 115 | 116 | int found = parent[V-1]; 117 | vector result; 118 | result.push_back(V-1); 119 | while(found != 0){ 120 | result.push_back(found); 121 | found = parent[found]; 122 | } 123 | 124 | reverse(result.begin(), result.end()); 125 | 126 | for(auto a : result) 127 | cout << a << " "; 128 | } else { 129 | cout << "IMPOSSIBLE\n"; 130 | } 131 | } 132 | 133 | 134 | int main(){ 135 | ios::sync_with_stdio(0); 136 | cin.tie(0); cout.tie(0); 137 | 138 | int N = getint(); 139 | int M = getint(); 140 | 141 | Graph g(N+1); 142 | 143 | for(int a = 0; a < M; ++a) 144 | { 145 | int s = getint(); 146 | int d = getint(); 147 | g.addEdge(s, d, 1); 148 | } 149 | 150 | g.longestPath(1); 151 | 152 | return 0; 153 | } -------------------------------------------------------------------------------- /GraphAlgorithms/LongestFlightRoute/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const int INF=1e9+7; 6 | 7 | int dp[200005]; 8 | std::vector to[200005]; 9 | bool vis[200005]; 10 | 11 | int N; 12 | 13 | void dfs(int node){ 14 | if(vis[node]) return; 15 | vis[node]=true; 16 | dp[node]=(node!=N-1)?-INF:0; 17 | for(int child:to[node]){ 18 | dfs(child); 19 | dp[node]=std::max(dp[node],dp[child]+1); 20 | } 21 | } 22 | 23 | int main(){ 24 | int M; 25 | scanf("%d %d",&N,&M); 26 | for(int i=0;i 2 | using namespace std; 3 | 4 | template T getint() { 5 | T val=0; 6 | char c; 7 | bool neg=false; 8 | while((c=getchar()) && !(c>='0' && c<='9')) { 9 | neg|=c=='-'; 10 | } 11 | do { 12 | val=(val*10)+c-'0'; 13 | } while((c=getchar()) && (c>='0' && c<='9')); 14 | 15 | return val*(neg?-1:1); 16 | } 17 | 18 | #define INF 1e9 19 | 20 | void printPath(vector &parent, int j) 21 | { 22 | if (parent[j] == - 1) 23 | return; 24 | printPath(parent, parent[j]); 25 | cout << j << " "; 26 | } 27 | 28 | int main() 29 | { 30 | ios::sync_with_stdio(0); 31 | cin.tie(0); cout.tie(0); 32 | 33 | // ifstream input("input.txt"); 34 | 35 | int N = getint(); 36 | int M = getint(); 37 | 38 | int source = 1; 39 | int destination = N; 40 | int result = 0; 41 | 42 | vector>> adj(N+1); 43 | for(int a = 0; a < M; ++a) 44 | { 45 | int s = getint(); 46 | int d = getint(); 47 | adj[s].push_back(make_pair(d, 1)); 48 | adj[d].push_back(make_pair(s, 1)); 49 | } 50 | 51 | vector dist(N+1, INF); 52 | vector parent(N+1, -1); 53 | priority_queue< pair, vector>, greater> > pq; 54 | 55 | pq.push({0, source}); 56 | dist[source] = 0; 57 | 58 | while(!pq.empty()) 59 | { 60 | auto front = pq.top(); 61 | pq.pop(); 62 | 63 | auto u = front.second; 64 | if(u == destination){ 65 | result = front.first + 1; 66 | cout << result << endl; 67 | cout << 1 << " "; 68 | printPath(parent, N); 69 | return 0; 70 | } 71 | 72 | if(front.first > dist[u]) 73 | continue; 74 | 75 | for(auto i : adj[u]) 76 | { 77 | int v = i.first; 78 | int w = i.second; 79 | if (dist[v] > dist[u] + w) 80 | { 81 | parent[v] = u; 82 | dist[v] = dist[u] + w; 83 | pq.push(make_pair(dist[v], v)); 84 | } 85 | } 86 | } 87 | 88 | cout << "IMPOSSIBLE\n"; 89 | 90 | return 0; 91 | } -------------------------------------------------------------------------------- /GraphAlgorithms/ShortestRoute2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/GraphAlgorithms/ShortestRoute2/a.out -------------------------------------------------------------------------------- /GraphAlgorithms/ShortestRoute2/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template T getint() { 5 | T val=0; 6 | char c; 7 | bool neg=false; 8 | while((c=getchar()) && !(c>='0' && c<='9')) { 9 | neg|=c=='-'; 10 | } 11 | do { 12 | val=(val*10)+c-'0'; 13 | } while((c=getchar()) && (c>='0' && c<='9')); 14 | 15 | return val*(neg?-1:1); 16 | } 17 | 18 | #define INF 3e18 19 | 20 | void floydWarshall(vector> &adj, int N) { 21 | for (int k = 0; k < N; k++){ // Pick all vertices as source one by one 22 | for (int i = 0; i < N; i++){ // Pick all vertices as destination for the above picked source 23 | for (int j = 0; j < N; j++){ 24 | // If vertex k is on the shortest path from i to j, then update the value of adj[i][j] 25 | if (adj[i][k] + adj[k][j] < adj[i][j]) 26 | adj[i][j] = adj[i][k] + adj[k][j]; 27 | } 28 | } 29 | } 30 | } 31 | 32 | 33 | int main() 34 | { 35 | ios::sync_with_stdio(0); 36 | cin.tie(0); cout.tie(0); 37 | 38 | int N = getint(); 39 | int M = getint(); 40 | int Q = getint(); 41 | 42 | vector> adj(N+1, vector(N+1, INF)); 43 | for(int a = 0; a < M; ++a) 44 | { 45 | long long s = getint(); 46 | long long d = getint(); 47 | long long p = getint(); 48 | adj[s][d] = min(p, adj[s][d]); 49 | adj[d][s] = min(p, adj[d][s]); 50 | } 51 | 52 | floydWarshall(adj, N+1); 53 | 54 | string res = ""; 55 | for(int a = 0; a < Q; ++a) 56 | { 57 | long long s = getint(); 58 | long long d = getint(); 59 | 60 | if(s == d){ 61 | res += "0\n"; 62 | continue; 63 | } 64 | 65 | long long search = min(adj[s][d], adj[d][s]); 66 | 67 | if(search != INF){ 68 | res += to_string(search) + "\n"; 69 | } else { 70 | res += "-1\n"; 71 | } 72 | } 73 | cout << res; 74 | } -------------------------------------------------------------------------------- /GraphAlgorithms/ShortestRoutes1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/GraphAlgorithms/ShortestRoutes1/a.out -------------------------------------------------------------------------------- /GraphAlgorithms/ShortestRoutes1/input.txt: -------------------------------------------------------------------------------- 1 | 3 4 2 | 1 2 6 3 | 1 3 2 4 | 3 2 3 5 | 1 3 4 -------------------------------------------------------------------------------- /GraphAlgorithms/ShortestRoutes1/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template T getint() { 5 | T val=0; 6 | char c; 7 | bool neg=false; 8 | while((c=getchar()) && !(c>='0' && c<='9')) { 9 | neg|=c=='-'; 10 | } 11 | do { 12 | val=(val*10)+c-'0'; 13 | } while((c=getchar()) && (c>='0' && c<='9')); 14 | 15 | return val*(neg?-1:1); 16 | } 17 | 18 | #define INF 3e18 19 | 20 | int main() 21 | { 22 | ios::sync_with_stdio(0); 23 | cin.tie(0); cout.tie(0); 24 | 25 | long long N = getint(); 26 | long long M = getint(); 27 | 28 | vector>> adj(N+1); 29 | for(long long a = 0; a < M; ++a) 30 | { 31 | long long s = getint(); 32 | long long d = getint(); 33 | long long p = getint(); 34 | adj[s].push_back(make_pair(d, p)); 35 | } 36 | 37 | vector dist(N+1, INF); 38 | priority_queue< pair, vector>, greater> > pq; 39 | 40 | pq.push({0, 1}); 41 | dist[1] = 0; 42 | 43 | while(!pq.empty()) 44 | { 45 | auto front = pq.top(); 46 | pq.pop(); 47 | 48 | auto u = front.second; 49 | if(front.first > dist[u]) 50 | continue; 51 | 52 | for(auto i : adj[u]) 53 | { 54 | auto v = i.first; 55 | auto w = i.second; 56 | if (dist[v] > dist[u] + w) 57 | { 58 | dist[v] = dist[u] + w; 59 | pq.push(make_pair(dist[v], v)); 60 | } 61 | } 62 | } 63 | 64 | for(long long a= 1; a < N+1; ++a){ 65 | if(dist[a] != INF) 66 | cout << dist[a] << " "; 67 | } 68 | } -------------------------------------------------------------------------------- /GraphAlgorithms/ShortestRoutes1/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/GraphAlgorithms/ShortestRoutes1/output.txt -------------------------------------------------------------------------------- /IntroProblems/BitStrings/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/IntroProblems/BitStrings/a.out -------------------------------------------------------------------------------- /IntroProblems/BitStrings/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define ll long long 8 | #define MOD 1000000007 9 | 10 | template T getint() { 11 | T val=0; 12 | char c; 13 | bool neg=false; 14 | while((c=getchar()) && !(c>='0' && c<='9')) { 15 | neg|=c=='-'; 16 | } 17 | do { 18 | val=(val*10)+c-'0'; 19 | } while((c=getchar()) && (c>='0' && c<='9')); 20 | 21 | return val*(neg?-1:1); 22 | } 23 | 24 | // Iterative Function to calculate (x^y)%p in O(log y) 25 | ll power(ll x, ll y, ll p) 26 | { 27 | ll res = 1; // Initialize result 28 | x = x % p; // Update x if it is more than or 29 | // equal to p 30 | while (y > 0) { 31 | // If y is odd, multiply x with result 32 | if (y & 1) 33 | res = (res * x) % p; 34 | // y must be even now 35 | y = y >> 1; // y = y/2 36 | x = (x * x) % p; 37 | } 38 | return res; 39 | } 40 | 41 | int main() { 42 | ios::sync_with_stdio(0); 43 | cin.tie(0); cout.tie(0); 44 | 45 | int N = getint(); 46 | 47 | cout << power(2, N, MOD); 48 | } -------------------------------------------------------------------------------- /IntroProblems/BitStrings/s.py: -------------------------------------------------------------------------------- 1 | print(pow(2, int(input())) % 1000000007) -------------------------------------------------------------------------------- /IntroProblems/CoinPiles/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/IntroProblems/CoinPiles/a.out -------------------------------------------------------------------------------- /IntroProblems/CoinPiles/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | int main() { 22 | ios::sync_with_stdio(0); 23 | cin.tie(0); cout.tie(0); 24 | 25 | int N = getint(); 26 | 27 | while(N--){ 28 | ll a = getint(); 29 | ll b = getint(); 30 | if(a%3 == 0 && b%3 == 0) 31 | cout << "YES\n"; 32 | else 33 | cout << "NO\n"; 34 | } 35 | } -------------------------------------------------------------------------------- /IntroProblems/CreatingStrings1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/IntroProblems/CreatingStrings1/a.out -------------------------------------------------------------------------------- /IntroProblems/CreatingStrings1/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | int main() { 22 | ios::sync_with_stdio(0); 23 | // cin.tie(0); cout.tie(0); 24 | 25 | string s; 26 | cin >> s; 27 | sort(s.begin(), s.end()); 28 | 29 | vector v; 30 | int c = 0; 31 | do { 32 | v.emplace_back(s); 33 | c++; 34 | } while(next_permutation(s.begin(), s.end())); 35 | 36 | cout << c << '\n'; 37 | for(auto a : v){ 38 | cout << a << '\n'; 39 | } 40 | } -------------------------------------------------------------------------------- /IntroProblems/NumberSpiral/s.py: -------------------------------------------------------------------------------- 1 | def f(a, b): 2 | M = max(a, b) 3 | return(a-b)*(-1)**M+M*M-M+1 4 | N = int(input()) 5 | for _ in range(N): 6 | a, b = input().split(' ') 7 | a = int(a) 8 | b = int(b) 9 | print(f(a, b)) -------------------------------------------------------------------------------- /IntroProblems/Permutations/source.py: -------------------------------------------------------------------------------- 1 | N=int(input()) 2 | if N==4:print("2 4 1 3");exit() 3 | if N==1:print("1");exit() 4 | if N<4:print("NO SOLUTION");exit() 5 | [print(i) for i in range(1,N+1,2)] 6 | [print(i) for i in range(2,N+1,2)] 7 | -------------------------------------------------------------------------------- /IntroProblems/TrailingZeros/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | int main() { 22 | ios::sync_with_stdio(0); 23 | cin.tie(0); cout.tie(0); 24 | 25 | int N = getint(); 26 | 27 | ll count = 0; 28 | for (ll i = 5; N / i >= 1; i *= 5) 29 | count += N / i; 30 | cout << count; 31 | } -------------------------------------------------------------------------------- /IntroProblems/Weird Algorithm/source.py: -------------------------------------------------------------------------------- 1 | N=int(input());print(N) 2 | while N!=1: 3 | if N%2==0:N//=2 4 | else:N=N*3+1 5 | print(N) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Sebastien Biollo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Mathematics/CountingDivisors/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/Mathematics/CountingDivisors/a.out -------------------------------------------------------------------------------- /Mathematics/CountingDivisors/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #pragma GCC optimize ("O3") 4 | #pragma GCC target ("sse4") 5 | #define ll long long 6 | #define vi vector 7 | #define vvi vector> 8 | #define vvii vector>> 9 | #define vvl vector> 10 | #define vvll vector>> 11 | #define FOR(x, N) for(x = 0; x < N; ++x) 12 | #define epsilon 1e-9 13 | #define INF 3e18 + 5 14 | #define MOD 1000000007 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | template 29 | inline void print(Args&&... args){ 30 | ((cout << args << " "), ...) << "\n"; 31 | } 32 | template 33 | inline void parr(vector arr){ 34 | for(auto a : arr) cout << a << " "; 35 | cout << "\n"; 36 | } 37 | /* 38 | void SieveOfEratosthenes(ll n, bool prime[], bool primesquare[], ll a[]) { 39 | // Create a boolean array "prime[0..n]" and 40 | // initialize all entries it as true. A value 41 | // in prime[i] will finally be false if i is 42 | // Not a prime, else true. 43 | for (ll i = 2; i <= n; i++) 44 | prime[i] = true; 45 | 46 | // Create a boolean array "primesquare[0..n*n+1]" 47 | // and initialize all entries it as false. A value 48 | // in squareprime[i] will finally be true if i is 49 | // square of prime, else false. 50 | for (ll i = 0; i <= (n * n + 1); i++) 51 | primesquare[i] = false; 52 | 53 | // 1 is not a prime number 54 | prime[1] = false; 55 | 56 | for (ll p = 2; p * p <= n; p++) { 57 | // If prime[p] is not changed, then 58 | // it is a prime 59 | if (prime[p] == true) { 60 | // Update all multiples of p 61 | for (ll i = p * 2; i <= n; i += p) 62 | prime[i] = false; 63 | } 64 | } 65 | 66 | ll j = 0; 67 | for (ll p = 2; p <= n; p++) { 68 | if (prime[p]) { 69 | // Storing primes in an array 70 | a[j] = p; 71 | 72 | // Update value in primesquare[p*p], 73 | // if p is prime. 74 | primesquare[p * p] = true; 75 | j++; 76 | } 77 | } 78 | } 79 | 80 | // Function to count divisors 81 | ll countDivisors(ll n) { 82 | // If number is 1, then it will have only 1 83 | // as a factor. So, total factors will be 1. 84 | if (n == 1) 85 | return 1; 86 | 87 | bool prime[n * 2], primesquare[n * n + 1]; 88 | 89 | cout << "ok1\n"; 90 | 91 | ll a[n]; // for storing primes upto n 92 | 93 | // Calling SieveOfEratosthenes to store prime 94 | // factors of n and to store square of prime 95 | // factors of n 96 | SieveOfEratosthenes(n, prime, primesquare, a); 97 | 98 | cout << "ok2\n"; 99 | 100 | // ans will contain total number of distinct 101 | // divisors 102 | ll ans = 1; 103 | 104 | // Loop for counting factors of n 105 | for (ll i = 0;; i++) { 106 | // a[i] is not less than cube root n 107 | if (a[i] * a[i] * a[i] > n) 108 | break; 109 | 110 | // Calculating power of a[i] in n. 111 | ll cnt = 1; // cnt is power of prime a[i] in n. 112 | while (n % a[i] == 0) // if a[i] is a factor of n 113 | { 114 | n = n / a[i]; 115 | cnt = cnt + 1; // incrementing power 116 | } 117 | 118 | // Calculating number of divisors 119 | // If n = a^p * b^q then total divisors of n 120 | // are (p+1)*(q+1) 121 | ans *= cnt; 122 | } 123 | 124 | // if a[i] is greater than cube root of n 125 | 126 | // First case 127 | if (prime[n]) 128 | return ans*2; 129 | // Second case 130 | else if (primesquare[n]) 131 | return ans*3; 132 | // Third casse 133 | else if (n != 1) 134 | return ans*4; 135 | return ans; // Total divisors 136 | } 137 | */ 138 | 139 | ll countDivisors(int n) { 140 | ll cnt = 0; 141 | int s = sqrt(n); 142 | for (int i = 1; i <= s; i++) { 143 | if (n % i == 0) { 144 | // If divisors are equal, count only one 145 | if (n / i == i) 146 | cnt++; 147 | else // Otherwise count both 148 | cnt += 2; 149 | } 150 | } 151 | return cnt; 152 | } 153 | 154 | int main() { 155 | // ifstream cin("input.txt"); 156 | int N = getint(); 157 | // cin >> N; 158 | while(N--){ 159 | int num = getint(); 160 | // cin >> num; 161 | cout << countDivisors(num) << "\n"; 162 | } 163 | } -------------------------------------------------------------------------------- /Mathematics/CreatingStrings2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/Mathematics/CreatingStrings2/a.out -------------------------------------------------------------------------------- /Mathematics/CreatingStrings2/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #pragma GCC optimize ("O3") 4 | #pragma GCC target ("sse4") 5 | #define ll long long 6 | #define vi vector 7 | #define vvi vector> 8 | #define vvii vector>> 9 | #define vvl vector> 10 | #define vvll vector>> 11 | #define FOR(x, N) for(x = 0; x < N; ++x) 12 | #define epsilon 1e-9 13 | #define INF 3e18 + 5 14 | #define MOD 1000000007 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | template 29 | inline void print(Args&&... args){ 30 | ((cout << args << " "), ...) << "\n"; 31 | } 32 | template 33 | inline void parr(vector arr){ 34 | for(auto a : arr) cout << a << " "; 35 | cout << "\n"; 36 | } 37 | 38 | int Q, N, M, a, b; 39 | 40 | int modFact(int n){ 41 | ll result = 1; 42 | for (int i = 1; i <= n; i++) 43 | result = (result * i) % MOD; 44 | return result; 45 | } 46 | 47 | int main() { 48 | ios::sync_with_stdio(0); 49 | 50 | string s; 51 | cin >> s; 52 | 53 | vi letters(26, 0); 54 | for(auto a : s){ 55 | letters[a - 97]++; 56 | } 57 | 58 | parr(letters); 59 | 60 | 61 | // ll res = 1; 62 | // for(auto a : letters){ 63 | // if(a == 0) 64 | // continue; 65 | // res = (res * modFact(a)) % MOD; 66 | // } 67 | // cout << (N / res) % MOD; 68 | 69 | ll N = 1; 70 | for (int i = 1; i <= s.length(); i++) 71 | N = (N * i); 72 | 73 | ll res = 1; 74 | for(auto a : letters){ 75 | if(a == 0 || a == 1) 76 | continue; 77 | res = (res * a) % MOD; 78 | } 79 | cout << (N / res) % MOD; 80 | } -------------------------------------------------------------------------------- /Mathematics/Exponentiation/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/Mathematics/Exponentiation/a.out -------------------------------------------------------------------------------- /Mathematics/Exponentiation/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #pragma GCC optimize ("O3") 4 | #pragma GCC target ("sse4") 5 | #define ll long long 6 | #define vi vector 7 | #define vvi vector> 8 | #define vvii vector>> 9 | #define vvl vector> 10 | #define vvll vector>> 11 | #define FOR(x, N) for(x = 0; x < N; ++x) 12 | #define epsilon 1e-9 13 | #define INF 3e18 + 5 14 | #define MOD 1000000007 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | template 29 | inline void print(Args&&... args){ 30 | ((cout << args << " "), ...) << "\n"; 31 | } 32 | template 33 | inline void parr(vector arr){ 34 | for(auto a : arr) cout << a << " "; 35 | cout << "\n"; 36 | } 37 | 38 | int Q, N, M, a, b, c; 39 | 40 | ll modpow(int x, int n) { 41 | if (n == 0) 42 | return 1; 43 | ll u = modpow(x, n >> 1); 44 | u = (u*u)%MOD; 45 | if (n & 1) 46 | u = (u*x)%MOD; 47 | return u; 48 | } 49 | 50 | // int modpow(int a, int b) { 51 | // a %= MOD; 52 | // ll result = 1; 53 | // while(b > 0){ 54 | // if(b & 1) 55 | // result = (result*a) % MOD; 56 | // a = (a*a) % MOD; 57 | // b >>= 1; 58 | // } 59 | // return result; 60 | // } 61 | 62 | int main() { 63 | ios::sync_with_stdio(0); 64 | cin.tie(0); cout.tie(0); 65 | 66 | N = getint(); 67 | while(N--){ 68 | a = getint(); 69 | b = getint(); 70 | cout << modpow(a, b) << "\n"; 71 | } 72 | } -------------------------------------------------------------------------------- /Mathematics/Exponentiation2/s.py: -------------------------------------------------------------------------------- 1 | 2 | def totient(n): # n - unsigned int 3 | result = 1 4 | p = 2 # prime numbers - 'iterator' 5 | while p**2 <= n: 6 | if(n % p == 0): # * (p-1) 7 | result *= (p-1) 8 | n //= p 9 | while(n % p == 0): # * p^(k-1) 10 | result *= p 11 | n //= p 12 | p += 1 13 | if n != 1: 14 | result *= (n-1) 15 | return result # in O(sqrt(n)) 16 | 17 | def modpow(p, z, b, c, m): # (p^z)^(b^c) mod m 18 | cp = 0 19 | while m % p == 0: 20 | cp += 1 21 | m //= p # m = m' now 22 | t = totient(m) 23 | exponent = ((pow(b, c, t)*z) % t + t - (cp % t)) % t 24 | # exponent = z*(b^c)-cp mod t 25 | return pow(p, cp) * pow(p, exponent, m) 26 | 27 | def solve(a, b, c, m): # split and solve 28 | result = 1 29 | p = 2 # primes 30 | while p**2 <= a: 31 | z = 0 32 | while a % p == 0: # calculate z 33 | a //= p 34 | z += 1 35 | if z != 0: 36 | result *= modpow(p, z, b, c, m) 37 | result %= m 38 | p += 1 39 | if a != 1: # Possible last prime 40 | result *= modpow(a, 1, b, c, m) 41 | return result % m 42 | 43 | N = int(input()) 44 | while(N > 0): 45 | a, b, c = input().split(' ') 46 | a = int(a) 47 | b = int(b) 48 | c = int(c) 49 | print(solve(a, b, c, 1000000007)) 50 | N -= 1 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CSES-solutions 2 | My solutions for CSES problems 3 | -------------------------------------------------------------------------------- /RangeQueries/DistinctValuesQueries/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/DistinctValuesQueries/a.out -------------------------------------------------------------------------------- /RangeQueries/DistinctValuesQueries/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | #define MAXN 900010 22 | typedef long long ll; 23 | /* 24 | struct sdata { 25 | int ans; 26 | set s; 27 | }; 28 | 29 | sdata tree[MAXN]; 30 | 31 | sdata combine(sdata l, sdata r) { 32 | sdata res; 33 | merge(l.s.begin(), l.s.end(), 34 | r.s.begin(), r.s.end(), 35 | inserter(res.s, res.s.begin())); 36 | res.ans = res.s.size(); 37 | // res.sum = l.sum + r.sum; 38 | // res.pref = max(l.pref, l.sum + r.pref); 39 | // res.suff = max(r.suff, r.sum + l.suff); 40 | // res.ans = max(max(l.ans, r.ans), l.suff + r.pref); 41 | return res; 42 | } 43 | 44 | sdata make_data(int val, bool empty=false) { 45 | sdata res; 46 | if(empty){ 47 | res.ans = 0; 48 | return res; 49 | } 50 | 51 | res.s.insert(val); 52 | res.ans = res.s.size(); 53 | return res; 54 | } 55 | */ 56 | 57 | struct sdata { 58 | vector s; 59 | }; 60 | 61 | sdata tree[MAXN]; 62 | 63 | sdata combine(sdata l, sdata r) { 64 | sdata res; 65 | copy(r.s.begin(), r.s.end(), back_inserter(res.s)); 66 | copy(l.s.begin(), l.s.end(), back_inserter(res.s)); 67 | return res; 68 | } 69 | 70 | sdata make_data(int val, bool empty=false) { 71 | sdata res; 72 | if(empty){ 73 | return res; 74 | } 75 | res.s.emplace_back(val); 76 | return res; 77 | } 78 | 79 | void build(vector &a, int v, int tl, int tr) { 80 | if (tl == tr) { 81 | tree[v] = make_data(a[tl]); 82 | } else { 83 | int tm = (tl + tr) / 2; 84 | build(a, v*2, tl, tm); 85 | build(a, v*2+1, tm+1, tr); 86 | tree[v] = combine(tree[v*2], tree[v*2+1]); 87 | } 88 | } 89 | 90 | void update(int v, int tl, int tr, int pos, int new_val) { 91 | if (tl == tr) { 92 | tree[v] = make_data(new_val); 93 | } else { 94 | int tm = (tl + tr) / 2; 95 | if (pos <= tm) 96 | update(v*2, tl, tm, pos, new_val); 97 | else 98 | update(v*2+1, tm+1, tr, pos, new_val); 99 | tree[v] = combine(tree[v*2], tree[v*2+1]); 100 | } 101 | } 102 | 103 | sdata query(int v, int tl, int tr, int l, int r) { 104 | if (l > r) 105 | return make_data(0, true); 106 | if (l == tl && r == tr) 107 | return tree[v]; 108 | int tm = (tl + tr) / 2; 109 | return combine(query(v*2, tl, tm, l, min(r, tm)), query(v*2+1, tm+1, tr, max(l, tm+1), r)); 110 | } 111 | 112 | int main() { 113 | ios::sync_with_stdio(0); 114 | cin.tie(0); cout.tie(0); 115 | 116 | int N = getint(); 117 | int Q = getint(); 118 | 119 | vector v(N); 120 | for(int a = 0; a < N; ++a){ 121 | v[a] = getint(); 122 | } 123 | build(v, 1, 0, N-1); 124 | 125 | for(int a = 0; a < Q; ++a){ 126 | int l = getint(); 127 | int r = getint(); 128 | l--; 129 | r--; 130 | 131 | auto s = query(1, 0, N-1, l, r).s; 132 | set res; 133 | for(auto elm : s){ 134 | res.insert(elm); 135 | } 136 | 137 | cout << res.size() << "\n"; 138 | } 139 | } -------------------------------------------------------------------------------- /RangeQueries/HotelQueries/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/HotelQueries/a.out -------------------------------------------------------------------------------- /RangeQueries/HotelQueries/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | inline bool byfirst(const pair &a, const pair &b) { 22 | return (a.first < b.first); 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); cout.tie(0); 28 | 29 | int N = getint(); 30 | int G = getint(); 31 | 32 | vector v(N); 33 | for(int a = 0; a < N; ++a){ 34 | v[a] = getint(); 35 | } 36 | 37 | for(int a = 0; a < G; ++a){ 38 | int val = getint(); 39 | bool ok = true; 40 | for(int b = 0; b < N; ++b){ 41 | if(v[b] >= val){ 42 | cout << b+1 << " "; 43 | ok = false; 44 | v[b] -= val; 45 | break; 46 | } 47 | } 48 | if(ok){ 49 | cout << 0 << " "; 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /RangeQueries/RangeMinimumQueries1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/RangeMinimumQueries1/a.out -------------------------------------------------------------------------------- /RangeQueries/RangeMinimumQueries1/m.cpp: -------------------------------------------------------------------------------- 1 | // #include 2 | // using namespace std; 3 | 4 | // #pragma GCC optimize ("O3") 5 | // #pragma GCC target ("sse4") 6 | 7 | // template T getint() { 8 | // T val=0; 9 | // char c; 10 | // bool neg=false; 11 | // while((c=getchar()) && !(c>='0' && c<='9')) { 12 | // neg|=c=='-'; 13 | // } 14 | // do { 15 | // val=(val*10)+c-'0'; 16 | // } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | // return val*(neg?-1:1); 19 | // } 20 | 21 | // #define INF 3e18 + 5 22 | // typedef long long ll; 23 | 24 | // struct FenwickTreeMin { 25 | // vector bit; 26 | // int n; 27 | 28 | // FenwickTreeMin(int n) { 29 | // this->n = n; 30 | // // bit.assign(n, INF); 31 | // bit.resize(n); 32 | // } 33 | 34 | // // FenwickTreeMin(vector a) : FenwickTreeMin(a.size()) { 35 | // // for (size_t i = 0; i < a.size(); i++) 36 | // // update(i, i, a[i]); 37 | // // } 38 | 39 | // ll query(int r) { 40 | // ll ret = INF; 41 | // for (; r >= 0; r = (r & (r + 1)) - 1) 42 | // ret = min(ret, bit[r]); 43 | // return ret; 44 | // } 45 | 46 | // ll query(int l, int r) { 47 | // return query(r) - query(l - 1); 48 | // } 49 | 50 | // void update(int idx, ll val) { 51 | // for (; idx < n; idx = idx | (idx + 1)) 52 | // bit[idx] = min(bit[idx], val); 53 | // } 54 | 55 | // void update(int l, int r, ll val) { 56 | // update(l, val); 57 | // update(r + 1, -val); 58 | // } 59 | 60 | // void print(){ 61 | // for(auto a : bit) 62 | // cout << a << " "; 63 | // cout << endl; 64 | // } 65 | // }; 66 | 67 | 68 | // int main() { 69 | // ios::sync_with_stdio(0); 70 | // cin.tie(0); cout.tie(0); 71 | 72 | // int N = getint(); 73 | // int Q = getint(); 74 | 75 | // auto BITree = FenwickTreeMin(N); 76 | // for(int a = 0; a < N; ++a){ 77 | // BITree.update(a, a, getint()); 78 | // } 79 | 80 | // for(int a = 0; a < Q; ++a){ 81 | // int l = getint(); 82 | // int r = getint(); 83 | // l--; 84 | // cout << BITree.query(l, r) << '\n'; 85 | // } 86 | // } -------------------------------------------------------------------------------- /RangeQueries/RangeMinimumQueries1/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | #define MAXN 6000010 24 | #define INF 3e18 + 5 25 | typedef long long ll; 26 | // #define numeric_limits::max()-1; 27 | ll tree[MAXN]; 28 | ll lazy[MAXN]; 29 | 30 | int N; 31 | void propaga(int inx, int cnt_num){ 32 | if (lazy[inx]){ 33 | tree[inx] += lazy[inx]*cnt_num; 34 | lazy[inx*2] += lazy[inx]; 35 | lazy[inx*2+1] += lazy[inx]; 36 | lazy[inx] = 0; 37 | } 38 | } 39 | 40 | void update(int tl, int tr, ll val,int l = 0, int r = N, int inx = 1){ 41 | propaga(inx,r-l); 42 | // tutto fuori 43 | if (tr <= l || tl >= r || r <= l) 44 | return; 45 | // att dentro target 46 | int cnt_num = r-l; 47 | 48 | if (tl <= l && tr >= r){ 49 | lazy[inx] += val; 50 | propaga(inx,cnt_num); 51 | return; 52 | } 53 | propaga(inx, cnt_num); 54 | int mid = (l+r)/2; 55 | update(tl,tr,val,l,mid,inx*2); 56 | update(tl,tr,val,mid,r,inx*2+1); 57 | tree[inx] = min(tree[inx*2],tree[inx*2+1]); 58 | } 59 | 60 | ll query(int tl, int tr, int l = 0, int r = N, int inx = 1){ 61 | propaga(inx, r-l); 62 | 63 | // tutto fuori 64 | if (tr <= l || tl >= r) 65 | return INF; 66 | 67 | // att dentro target 68 | if (tl <= l && tr >= r) 69 | return tree[inx]; 70 | 71 | int mid = (l+r)/2; 72 | return min(query(tl,tr,l,mid,inx*2),query(tl,tr,mid,r,inx*2+1)); 73 | } 74 | 75 | int main() { 76 | ios::sync_with_stdio(0); 77 | cin.tie(0); cout.tie(0); 78 | 79 | N = getint(); 80 | int Q = getint(); 81 | 82 | // memset(tree,0,sizeof tree); 83 | // memset(lazy,0,sizeof lazy); 84 | 85 | for(int a = 0; a < N; ++a){ 86 | auto val = getint(); 87 | update(a, a+1, val); 88 | } 89 | 90 | for(int a = 0; a < Q; ++a){ 91 | int l = getint(); 92 | int r = getint(); 93 | l--; 94 | cout << query(l, r) << '\n'; 95 | } 96 | } -------------------------------------------------------------------------------- /RangeQueries/RangeMinimumQueries1/m_jack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define l(x) x << 1 3 | #define r(x) (x << 1) | 1 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | const int inf = numeric_limits::max(); 10 | 11 | int main(){ 12 | ios_base::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | 15 | int n, q, a, b; 16 | cin >> n >> q; 17 | vector sum(2 * n); 18 | 19 | for(int i = n; i < 2 * n; i++) 20 | cin >> sum[i]; 21 | for(int i = n - 1; i > 0; i--) 22 | sum[i] = min(sum[l(i)], sum[r(i)]); 23 | 24 | while(q--){ 25 | cin >> a >> b; 26 | a += n - 1; 27 | b += n; 28 | ll ans = inf; 29 | while(a < b){ 30 | if(a & 1) ans = min(ans, sum[a++]); 31 | if(b & 1) ans = min(ans, sum[--b]); 32 | a >>= 1, b >>= 1; 33 | } 34 | cout << ans << "\n"; 35 | } 36 | } -------------------------------------------------------------------------------- /RangeQueries/RangeMinimumQueries2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/RangeMinimumQueries2/a.out -------------------------------------------------------------------------------- /RangeQueries/RangeMinimumQueries2/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | #define MAXN 6000010 24 | #define INF 3e18 + 5 25 | typedef long long ll; 26 | // #define numeric_limits::max()-1; 27 | ll tree[MAXN]; 28 | ll lazy[MAXN]; 29 | 30 | int N; 31 | void propaga(int inx, int cnt_num){ 32 | if (lazy[inx]){ 33 | tree[inx] = lazy[inx]*cnt_num; 34 | lazy[inx*2] = lazy[inx]; 35 | lazy[inx*2+1] = lazy[inx]; 36 | lazy[inx] = 0; 37 | } 38 | } 39 | 40 | void update(int tl, int tr, ll val,int l = 0, int r = N, int inx = 1){ 41 | propaga(inx,r-l); 42 | // tutto fuori 43 | if (tr <= l || tl >= r || r <= l) 44 | return; 45 | // att dentro target 46 | int cnt_num = r-l; 47 | 48 | if (tl <= l && tr >= r){ 49 | lazy[inx] = val; 50 | propaga(inx,cnt_num); 51 | return; 52 | } 53 | propaga(inx, cnt_num); 54 | int mid = (l+r)/2; 55 | update(tl,tr,val,l,mid,inx*2); 56 | update(tl,tr,val,mid,r,inx*2+1); 57 | tree[inx] = min(tree[inx*2],tree[inx*2+1]); 58 | } 59 | 60 | ll query(int tl, int tr, int l = 0, int r = N, int inx = 1){ 61 | propaga(inx, r-l); 62 | 63 | // tutto fuori 64 | if (tr <= l || tl >= r) 65 | return INF; 66 | 67 | // att dentro target 68 | if (tl <= l && tr >= r) 69 | return tree[inx]; 70 | 71 | int mid = (l+r)/2; 72 | return min(query(tl,tr,l,mid,inx*2),query(tl,tr,mid,r,inx*2+1)); 73 | } 74 | 75 | int main() { 76 | ios::sync_with_stdio(0); 77 | cin.tie(0); cout.tie(0); 78 | 79 | N = getint(); 80 | int Q = getint(); 81 | 82 | // memset(tree,0,sizeof tree); 83 | // memset(lazy,0,sizeof lazy); 84 | 85 | for(int a = 0; a < N; ++a){ 86 | update(a, a+1, getint()); 87 | } 88 | 89 | for(int a = 0; a < Q; ++a){ 90 | int q = getint(); 91 | 92 | if(q == 1){ 93 | int l = getint(); 94 | int u = getint(); 95 | l--; 96 | update(l, l+1, u); 97 | } else { 98 | int l = getint(); 99 | int r = getint(); 100 | l--; 101 | cout << query(l, r) << endl; 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /RangeQueries/RangeMinimumQueries2/m_jack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define l(x) x << 1 3 | #define r(x) (x << 1) | 1 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | const int inf = numeric_limits::max(); 10 | 11 | int main(){ 12 | ios_base::sync_with_stdio(false); 13 | cin.tie(NULL); 14 | 15 | int n, q, a, b, t; 16 | cin >> n >> q; 17 | vector sum(2 * n); 18 | 19 | for(int i = n; i < 2 * n; i++) 20 | cin >> sum[i]; 21 | for(int i = n - 1; i > 0; i--) 22 | sum[i] = min(sum[l(i)], sum[r(i)]); 23 | 24 | while(q--){ 25 | cin >> t >> a >> b; 26 | if(t == 2){ 27 | a += n - 1; 28 | b += n; 29 | ll ans = inf; 30 | while(a < b){ 31 | if(a & 1) ans = min(ans, sum[a++]); 32 | if(b & 1) ans = min(ans, sum[--b]); 33 | a >>= 1, b >>= 1; 34 | } 35 | cout << ans << "\n"; 36 | } 37 | else{ 38 | sum[a + n - 1] = b; 39 | for(int i = (a + n - 1) >> 1; i > 0; i >>= 1) 40 | sum[i] = min(sum[l(i)], sum[r(i)]); 41 | } 42 | } 43 | 44 | 45 | } -------------------------------------------------------------------------------- /RangeQueries/RangeSumQueries1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/RangeSumQueries1/a.out -------------------------------------------------------------------------------- /RangeQueries/RangeSumQueries1/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | struct FenwickTree { 24 | vector bit; // binary indexed tree 25 | int n; 26 | 27 | FenwickTree(int n) { 28 | this->n = n; 29 | bit.assign(n, 0); 30 | } 31 | 32 | FenwickTree(vector a) : FenwickTree(a.size()) { 33 | for (size_t i = 0; i < a.size(); i++) 34 | add(i, a[i]); 35 | } 36 | 37 | long long sum(int r) { 38 | long long ret = 0; 39 | for (; r >= 0; r = (r & (r + 1)) - 1) 40 | ret += bit[r]; 41 | return ret; 42 | } 43 | 44 | long long sum(int l, int r) { 45 | return sum(r) - sum(l - 1); 46 | } 47 | 48 | void add(int idx, int delta) { 49 | for (; idx < n; idx = idx | (idx + 1)) 50 | bit[idx] += delta; 51 | } 52 | 53 | void print(){ 54 | for(auto a : bit) 55 | cout << a << " "; 56 | cout << endl; 57 | } 58 | }; 59 | 60 | 61 | // long long getSum(vector &BITree, int index){ 62 | // int sum = 0; 63 | // index++; 64 | // while(index > 0){ 65 | // sum += BITree[index]; 66 | // index -= index & (-index); 67 | // } 68 | // return sum; 69 | // } 70 | // long long rangeSum(vector &BITree, int l, int r) { 71 | // return getSum(BITree, r) - getSum(BITree, l - 1); 72 | // } 73 | 74 | // void updateBIT(vector &BITree, int n, int index, int val) { 75 | // index++; 76 | // while (index <= n) { 77 | // BITree[index] += val; 78 | // index += index & (-index); 79 | // } 80 | // } 81 | // void range_add(vector &BITree, int l, int r, int n, int val) { 82 | // updateBIT(BITree, n, l, val); 83 | // updateBIT(BITree, n, r + 1, -val); 84 | // } 85 | 86 | // vector constructBITree(vector &arr, int n){ 87 | // vector BITree(n+1); 88 | // for(int a = 0; a < n; ++a){ 89 | // int index = a+1; 90 | // while(index <= n){ 91 | // BITree[index] += arr[a]; 92 | // index += index & (-index); 93 | // } 94 | // } 95 | // return BITree; 96 | // } 97 | 98 | 99 | int main() { 100 | ios::sync_with_stdio(0); 101 | cin.tie(0); cout.tie(0); 102 | 103 | int N = getint(); 104 | int Q = getint(); 105 | 106 | auto BITree = FenwickTree(N); 107 | for(int a = 0; a < N; ++a){ 108 | BITree.add(a, getint()); 109 | } 110 | 111 | for(int a = 0; a < Q; ++a){ 112 | int l = getint(); 113 | int r = getint(); 114 | l--; r--; 115 | cout << BITree.sum(l, r) << endl; 116 | } 117 | } -------------------------------------------------------------------------------- /RangeQueries/RangeSumQueries2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/RangeSumQueries2/a.out -------------------------------------------------------------------------------- /RangeQueries/RangeSumQueries2/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | struct FenwickTree { 24 | vector bit; // binary indexed tree 25 | int n; 26 | 27 | FenwickTree(int n) { 28 | this->n = n; 29 | bit.assign(n, 0); 30 | } 31 | 32 | FenwickTree(vector a) : FenwickTree(a.size()) { 33 | for (size_t i = 0; i < a.size(); i++) 34 | add(i, a[i]); 35 | } 36 | 37 | long long sum(int r) { 38 | long long ret = 0; 39 | for (; r >= 0; r = (r & (r + 1)) - 1) 40 | ret += bit[r]; 41 | return ret; 42 | } 43 | 44 | long long sum(int l, int r) { 45 | return sum(r) - sum(l - 1); 46 | } 47 | 48 | void add(int idx, int delta) { 49 | for (; idx < n; idx = idx | (idx + 1)) 50 | bit[idx] += delta; 51 | } 52 | 53 | void print(){ 54 | for(auto a : bit) 55 | cout << a << " "; 56 | cout << endl; 57 | } 58 | }; 59 | 60 | int main() { 61 | ios::sync_with_stdio(0); 62 | cin.tie(0); cout.tie(0); 63 | 64 | int N = getint(); 65 | int Q = getint(); 66 | 67 | vector v(N); 68 | for(int a = 0; a < N; ++a){ 69 | v[a] = getint(); 70 | } 71 | auto BITree = FenwickTree(v); 72 | 73 | for(int a = 0; a < Q; ++a){ 74 | int q = getint(); 75 | int l = getint(); 76 | int r = getint(); 77 | l--; 78 | 79 | if(q == 1){ 80 | auto diff = v[l] - r; 81 | v[l] = r; 82 | BITree.add(l, -diff); 83 | } else { 84 | r--; 85 | cout << BITree.sum(l, r) << endl; 86 | } 87 | } 88 | } -------------------------------------------------------------------------------- /RangeQueries/RangeUpdateQueries/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/RangeUpdateQueries/a.out -------------------------------------------------------------------------------- /RangeQueries/RangeUpdateQueries/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | struct FenwickTree { 24 | vector bit; 25 | int n; 26 | 27 | FenwickTree(int n) { 28 | this->n = n; 29 | bit.assign(n, 0); 30 | } 31 | 32 | // FenwickTree(vector a) : FenwickTree(a.size()) { 33 | // for (size_t i = 0; i < a.size(); i++) 34 | // update(i, a[i]); 35 | // } 36 | 37 | long long query(int r) { 38 | long long ret = 0; 39 | for (; r >= 0; r = (r & (r + 1)) - 1) 40 | ret += bit[r]; 41 | return ret; 42 | } 43 | 44 | long long query(int l, int r) { 45 | return query(r) - query(l - 1); 46 | } 47 | 48 | void update(int idx, int val) { 49 | for (; idx < n; idx = idx | (idx + 1)) 50 | bit[idx] += val; 51 | } 52 | 53 | void update(int l, int r, int val) { 54 | update(l, val); 55 | update(r + 1, -val); 56 | } 57 | 58 | void print(){ 59 | for(auto a : bit) 60 | cout << a << " "; 61 | cout << endl; 62 | } 63 | }; 64 | 65 | int main() { 66 | ios::sync_with_stdio(0); 67 | cin.tie(0); cout.tie(0); 68 | 69 | int N = getint(); 70 | int Q = getint(); 71 | 72 | auto BITree = FenwickTree(N); 73 | for(int a = 0; a < N; ++a){ 74 | BITree.update(a, a, getint()); 75 | } 76 | 77 | for(int a = 0; a < Q; ++a){ 78 | int q = getint(); 79 | 80 | if(q == 1){ 81 | int l = getint(); 82 | int r = getint(); 83 | int u = getint(); 84 | l--; 85 | r--; 86 | BITree.update(l, r, u); 87 | } else { 88 | int k = getint(); 89 | k--; 90 | cout << BITree.query(k) << '\n'; 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /RangeQueries/RangeUpdateQueries/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | #define MAXN 6000010 24 | typedef long long ll; 25 | ll tree[MAXN]; 26 | ll lazy[MAXN]; 27 | 28 | int N; 29 | void propaga(int inx, int cnt_num){ 30 | if (lazy[inx]){ 31 | tree[inx] += lazy[inx]*cnt_num; 32 | 33 | lazy[inx*2] += lazy[inx]; 34 | lazy[inx*2+1] += lazy[inx]; 35 | lazy[inx] = 0; 36 | } 37 | } 38 | 39 | void update(int tl, int tr, ll val,int l = 0, int r = N, int inx = 1){ 40 | propaga(inx,r-l); 41 | // tutto fuori 42 | if (tr <= l || tl >= r || r <= l) 43 | return; 44 | // att dentro target 45 | int cnt_num = r-l; 46 | 47 | if (tl <= l && tr >= r){ 48 | lazy[inx] += val; 49 | propaga(inx,cnt_num); 50 | return; 51 | } 52 | propaga(inx, cnt_num); 53 | int mid = (l+r)/2; 54 | update(tl,tr,val,l,mid,inx*2); 55 | update(tl,tr,val,mid,r,inx*2+1); 56 | tree[inx] = tree[inx*2]+tree[inx*2+1]; 57 | } 58 | 59 | ll query(int tl, int tr, int l = 0, int r = N, int inx = 1){ 60 | propaga(inx, r-l); 61 | 62 | // tutto fuori 63 | if (tr <= l || tl >= r) 64 | return 0; 65 | 66 | // att dentro target 67 | if (tl <= l && tr >= r) 68 | return tree[inx]; 69 | 70 | int mid = (l+r)/2; 71 | return query(tl,tr,l,mid,inx*2)+query(tl,tr,mid,r,inx*2+1); 72 | } 73 | 74 | int main() { 75 | ios::sync_with_stdio(0); 76 | cin.tie(0); cout.tie(0); 77 | 78 | N = getint(); 79 | int Q = getint(); 80 | 81 | // memset(tree,0,sizeof tree); 82 | // memset(lazy,0,sizeof lazy); 83 | 84 | for(int a = 0; a < N; ++a){ 85 | auto val = getint(); 86 | update(a, a+1, val); 87 | } 88 | 89 | for(int a = 0; a < Q; ++a){ 90 | int q = getint(); 91 | 92 | if(q == 1){ 93 | int l = getint(); 94 | int r = getint(); 95 | int u = getint(); 96 | l--; 97 | update(l, r, u); 98 | } else { 99 | int k = getint(); 100 | k--; 101 | cout << query(k, k+1) << endl; 102 | } 103 | } 104 | } -------------------------------------------------------------------------------- /RangeQueries/RangeUpdateQueries/m3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define md (st+en) >>1 5 | #define lc (nd<<1) +1 6 | #define rc (nd<<1) +2 7 | 8 | const int N = 2e5+5; 9 | int n,q; 10 | long long a[N],val; 11 | long long tree[4*N],lazy[4*N]; 12 | 13 | void build(int st,int en, int nd){ 14 | if(st == en){ 15 | tree[nd] = a[st]; 16 | } 17 | else{ 18 | int mid = md; 19 | build(st,mid,lc); 20 | build(mid+1,en,rc); 21 | tree[nd] = 0; 22 | } 23 | } 24 | 25 | void updater(int st, int en ,int nd, int l,int r){ 26 | if(l <= st && en <= r){ 27 | lazy[nd] += val; 28 | } 29 | else{ 30 | if(lazy[nd]){ 31 | lazy[lc] += lazy[nd]; 32 | lazy[rc] += lazy[nd]; 33 | lazy[nd] = 0; 34 | } 35 | int mid = md; 36 | if( l <= mid){ 37 | updater(st,mid,lc,l,r); 38 | } 39 | if( r > mid){ 40 | updater(mid+1,en,rc,l,r); 41 | } 42 | 43 | } 44 | } 45 | 46 | long long query(int st, int en, int nd, int x){ 47 | //cout<>n>>q; 75 | for(int i=1;i<=n;i++){ 76 | cin>>a[i]; 77 | } 78 | build(0,n,0); 79 | while(q--){ 80 | int x; 81 | cin>>x; 82 | if(x == 1){ 83 | int l,r; 84 | cin>>l>>r>>val; 85 | updater(0,n,0,l,r); 86 | }else{ 87 | int y; 88 | cin>>y; 89 | cout< 2 | using namespace std;typedef long long ll;ll t[10000000];int N,Q,v,q,l,z,u,a; 3 | ll query(int r){ll s=0;for(;r>=0;r=(r&(r+1))-1)s+=t[r];return s;} 4 | void update(int i,int m){for(;i>N>>Q;for(a=0;a>v;update(a,v);update(a+1,-v);}for(a=0;a>q;if(q==1){cin>>l>>z>>u;l--;z--;update(l,u);update(z+1,-u);}else{cin>>l;l--;cout< 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | #define MAXN 900010 24 | typedef long long ll; 25 | ll tree[MAXN]; 26 | ll lazy[MAXN]; 27 | vector elements; 28 | 29 | int N; 30 | void propaga(int inx, int cnt_num){ 31 | if (lazy[inx]){ 32 | tree[inx] += lazy[inx]*cnt_num; 33 | 34 | lazy[inx*2] += lazy[inx]; 35 | lazy[inx*2+1] += lazy[inx]; 36 | lazy[inx] = 0; 37 | } 38 | } 39 | 40 | void update(int tl, int tr, ll val,int l = 0, int r = N, int inx = 1){ 41 | propaga(inx,r-l); 42 | // tutto fuori 43 | if (tr <= l || tl >= r || r <= l) 44 | return; 45 | // att dentro target 46 | int cnt_num = r-l; 47 | 48 | if (tl <= l && tr >= r){ 49 | lazy[inx] += val; 50 | propaga(inx,cnt_num); 51 | return; 52 | } 53 | propaga(inx, cnt_num); 54 | int mid = (l+r)/2; 55 | update(tl,tr,val,l,mid,inx*2); 56 | update(tl,tr,val,mid,r,inx*2+1); 57 | tree[inx] = tree[inx*2]+tree[inx*2+1]; 58 | } 59 | 60 | void setpropaga(int inx, int cnt_num){ 61 | if (lazy[inx]){ 62 | tree[inx] = lazy[inx]*cnt_num; 63 | 64 | lazy[inx*2] = lazy[inx]; 65 | lazy[inx*2+1] = lazy[inx]; 66 | lazy[inx] = 0; 67 | } 68 | } 69 | 70 | void setvalues(int tl, int tr, ll val,int l = 0, int r = N, int inx = 1){ 71 | setpropaga(inx,r-l); 72 | // tutto fuori 73 | if (tr <= l || tl >= r || r <= l) 74 | return; 75 | // att dentro target 76 | int cnt_num = r-l; 77 | 78 | if (tl <= l && tr >= r){ 79 | lazy[inx] = val; 80 | setpropaga(inx,cnt_num); 81 | return; 82 | } 83 | setpropaga(inx, cnt_num); 84 | int mid = (l+r)/2; 85 | setvalues(tl,tr,val,l,mid,inx*2); 86 | setvalues(tl,tr,val,mid,r,inx*2+1); 87 | tree[inx] = tree[inx*2]+tree[inx*2+1]; 88 | } 89 | 90 | ll query(int tl, int tr, int l = 0, int r = N, int inx = 1){ 91 | propaga(inx, r-l); 92 | 93 | // tutto fuori 94 | if (tr <= l || tl >= r) 95 | return 0; 96 | 97 | // att dentro target 98 | if (tl <= l && tr >= r) 99 | return tree[inx]; 100 | 101 | int mid = (l+r)/2; 102 | return query(tl,tr,l,mid,inx*2)+query(tl,tr,mid,r,inx*2+1); 103 | } 104 | 105 | int main() { 106 | ios::sync_with_stdio(0); 107 | cin.tie(0); cout.tie(0); 108 | 109 | N = getint(); 110 | int Q = getint(); 111 | 112 | // memset(tree,0,sizeof tree); 113 | // memset(lazy,0,sizeof lazy); 114 | 115 | for(int a = 0; a < N; ++a){ 116 | auto val = getint(); 117 | update(a, a+1, val); 118 | elements.emplace_back(val); 119 | } 120 | 121 | for(int a = 0; a < Q; ++a){ 122 | int q = getint(); 123 | 124 | if(q == 1){ 125 | int l = getint(); 126 | int r = getint(); 127 | int u = getint(); 128 | l--; 129 | update(l, r, u); 130 | } else if(q == 2) { 131 | int l = getint(); 132 | int r = getint(); 133 | int u = getint(); 134 | l--; 135 | setvalues(l, r, u); 136 | } else { 137 | int l = getint(); 138 | int r = getint(); 139 | l--; 140 | cout << query(l, r) << endl; 141 | } 142 | } 143 | } -------------------------------------------------------------------------------- /RangeQueries/RangeXorQueries/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/RangeXorQueries/a.out -------------------------------------------------------------------------------- /RangeQueries/RangeXorQueries/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | 9 | template T getint() { 10 | T val=0; 11 | char c; 12 | bool neg=false; 13 | while((c=getchar()) && !(c>='0' && c<='9')) { 14 | neg|=c=='-'; 15 | } 16 | do { 17 | val=(val*10)+c-'0'; 18 | } while((c=getchar()) && (c>='0' && c<='9')); 19 | 20 | return val*(neg?-1:1); 21 | } 22 | 23 | #define MAXN 6000010 24 | #define INF 3e18 + 5 25 | typedef long long ll; 26 | // #define numeric_limits::max()-1; 27 | ll tree[MAXN]; 28 | ll lazy[MAXN]; 29 | 30 | int N; 31 | void propaga(int inx, int cnt_num){ 32 | if (lazy[inx]){ 33 | tree[inx] += lazy[inx]*cnt_num; 34 | lazy[inx*2] += lazy[inx]; 35 | lazy[inx*2+1] += lazy[inx]; 36 | lazy[inx] = 0; 37 | } 38 | } 39 | 40 | void update(int tl, int tr, ll val,int l = 0, int r = N, int inx = 1){ 41 | propaga(inx,r-l); 42 | // tutto fuori 43 | if (tr <= l || tl >= r || r <= l) 44 | return; 45 | // att dentro target 46 | int cnt_num = r-l; 47 | 48 | if (tl <= l && tr >= r){ 49 | lazy[inx] += val; 50 | propaga(inx,cnt_num); 51 | return; 52 | } 53 | propaga(inx, cnt_num); 54 | int mid = (l+r)/2; 55 | update(tl,tr,val,l,mid,inx*2); 56 | update(tl,tr,val,mid,r,inx*2+1); 57 | tree[inx] = tree[inx*2] ^ tree[inx*2+1]; 58 | } 59 | 60 | ll query(int tl, int tr, int l = 0, int r = N, int inx = 1){ 61 | propaga(inx, r-l); 62 | 63 | // tutto fuori 64 | if (tr <= l || tl >= r) 65 | return 0; 66 | 67 | // att dentro target 68 | if (tl <= l && tr >= r) 69 | return tree[inx]; 70 | 71 | int mid = (l+r)/2; 72 | return query(tl,tr,l,mid,inx*2) ^ query(tl,tr,mid,r,inx*2+1); 73 | } 74 | 75 | int main() { 76 | ios::sync_with_stdio(0); 77 | cin.tie(0); cout.tie(0); 78 | 79 | N = getint(); 80 | int Q = getint(); 81 | 82 | // memset(tree,0,sizeof tree); 83 | // memset(lazy,0,sizeof lazy); 84 | 85 | for(int a = 0; a < N; ++a){ 86 | update(a, a+1, getint()); 87 | } 88 | 89 | for(int a = 0; a < Q; ++a){ 90 | int l = getint(); 91 | int r = getint(); 92 | l--; 93 | cout << query(l, r) << '\n'; 94 | } 95 | } -------------------------------------------------------------------------------- /RangeQueries/RangeXorQueries/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define l(x) x << 1 4 | #define r(x) (x << 1) | 1 5 | 6 | using namespace std; 7 | 8 | typedef long long ll; 9 | 10 | int main(){ 11 | ios_base::sync_with_stdio(false); 12 | cin.tie(NULL); 13 | 14 | int n, q, a, b; 15 | cin >> n >> q; 16 | vector sum(2 * n); 17 | 18 | for(int i = n; i < 2 * n; i++) 19 | cin >> sum[i]; 20 | for(int i = n - 1; i > 0; i--) 21 | sum[i] = sum[l(i)] ^ sum[r(i)]; 22 | 23 | while(q--){ 24 | cin >> a >> b; 25 | a += n - 1; 26 | b += n; 27 | ll ans = 0; 28 | while(a < b){ 29 | if(a & 1) ans ^= sum[a++]; 30 | if(b & 1) ans ^= sum[--b]; 31 | a >>= 1, b >>= 1; 32 | } 33 | cout << ans << "\n"; 34 | } 35 | 36 | 37 | } -------------------------------------------------------------------------------- /RangeQueries/SubarraySumQueries/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/RangeQueries/SubarraySumQueries/a.out -------------------------------------------------------------------------------- /RangeQueries/SubarraySumQueries/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | #define MAXN 6000010 21 | typedef long long ll; 22 | 23 | struct sdata { 24 | ll sum, pref, suff, ans; 25 | }; 26 | 27 | sdata tree[MAXN]; 28 | 29 | sdata combine(sdata l, sdata r) { 30 | sdata res; 31 | res.sum = l.sum + r.sum; 32 | res.pref = max(l.pref, l.sum + r.pref); 33 | res.suff = max(r.suff, r.sum + l.suff); 34 | res.ans = max(max(l.ans, r.ans), l.suff + r.pref); 35 | return res; 36 | } 37 | 38 | sdata make_data(int val) { 39 | sdata res; 40 | res.sum = val; 41 | res.pref = res.suff = res.ans = max(0, val); 42 | return res; 43 | } 44 | 45 | void build(vector &a, int v, int tl, int tr) { 46 | if (tl == tr) { 47 | tree[v] = make_data(a[tl]); 48 | } else { 49 | int tm = (tl + tr) / 2; 50 | build(a, v*2, tl, tm); 51 | build(a, v*2+1, tm+1, tr); 52 | tree[v] = combine(tree[v*2], tree[v*2+1]); 53 | } 54 | } 55 | 56 | void update(int v, int tl, int tr, int pos, int new_val) { 57 | if (tl == tr) { 58 | tree[v] = make_data(new_val); 59 | } else { 60 | int tm = (tl + tr) / 2; 61 | if (pos <= tm) 62 | update(v*2, tl, tm, pos, new_val); 63 | else 64 | update(v*2+1, tm+1, tr, pos, new_val); 65 | tree[v] = combine(tree[v*2], tree[v*2+1]); 66 | } 67 | } 68 | 69 | sdata query(int v, int tl, int tr, int l, int r) { 70 | if (l > r) 71 | return make_data(0); 72 | if (l == tl && r == tr) 73 | return tree[v]; 74 | int tm = (tl + tr) / 2; 75 | return combine(query(v*2, tl, tm, l, min(r, tm)), 76 | query(v*2+1, tm+1, tr, max(l, tm+1), r)); 77 | } 78 | 79 | int main() { 80 | ios::sync_with_stdio(0); 81 | cin.tie(0); cout.tie(0); 82 | 83 | int N = getint(); 84 | int Q = getint(); 85 | 86 | vector v(N); 87 | for(int a = 0; a < N; ++a){ 88 | v[a] = getint(); 89 | } 90 | build(v, 1, 0, N-1); 91 | 92 | for(int a = 0; a < Q; ++a){ 93 | int k = getint(); 94 | int u = getint(); 95 | k--; 96 | update(1, 0, N-1, k, u); 97 | cout << query(1, 0, N-1, 0, N-1).ans << "\n"; 98 | } 99 | } -------------------------------------------------------------------------------- /RangeQueries/fenwick.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef long long ll; 5 | 6 | struct FenwickTree { 7 | vector bit; 8 | int n; 9 | 10 | FenwickTree(int n) { 11 | this->n = n; 12 | bit.assign(n, 0); 13 | } 14 | 15 | // FenwickTree(vector a) : FenwickTree(a.size()) { 16 | // for (size_t i = 0; i < a.size(); i++) 17 | // update(i, a[i]); // puo' essere update(i, i, a[i]) 18 | // } 19 | 20 | ll query(int r) { 21 | ll ret = 0; 22 | for (; r >= 0; r = (r & (r + 1)) - 1) 23 | ret += bit[r]; 24 | return ret; 25 | } 26 | 27 | ll query(int l, int r) { 28 | return query(r) - query(l - 1); 29 | } 30 | 31 | void update(int idx, int val) { 32 | for (; idx < n; idx = idx | (idx + 1)) 33 | bit[idx] += val; 34 | } 35 | 36 | void update(int l, int r, int val) { 37 | update(l, val); 38 | update(r + 1, -val); 39 | } 40 | 41 | void print(){ 42 | for(auto a : bit) 43 | cout << a << " "; 44 | cout << endl; 45 | } 46 | }; -------------------------------------------------------------------------------- /RangeQueries/segmenttreelazy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXN 600010 5 | 6 | typedef long long ll; 7 | ll tree[MAXN]; 8 | ll lazy[MAXN]; 9 | 10 | int N; 11 | void propaga(int inx, int cnt_num){ 12 | if (lazy[inx]){ 13 | tree[inx] += lazy[inx]*cnt_num; 14 | lazy[inx*2] += lazy[inx]; 15 | lazy[inx*2+1] += lazy[inx]; 16 | lazy[inx] = 0; 17 | } 18 | } 19 | 20 | void update(int tl, int tr, ll val,int l = 0, int r = N, int inx = 1){ 21 | propaga(inx,r-l); 22 | // tutto fuori 23 | if ( tr <= l || tl >= r || r <= l) 24 | return; 25 | // att dentro target 26 | int cnt_num = r-l; 27 | 28 | if (tl <= l && tr >= r){ 29 | lazy[inx] += val; 30 | propaga(inx,cnt_num); 31 | return; 32 | } 33 | propaga(inx, cnt_num); 34 | int mid = (l+r)/2; 35 | update(tl,tr,val,l,mid,inx*2); 36 | update(tl,tr,val,mid,r,inx*2+1); 37 | tree[inx] = tree[inx*2]+tree[inx*2+1]; 38 | } 39 | 40 | ll query(int tl,int tr, int l = 0, int r = N, int inx = 1){ 41 | propaga(inx, r-l); 42 | 43 | // tutto fuori 44 | if ( tr <= l || tl >= r) 45 | return 0 ; 46 | 47 | // att dentro target 48 | if (tl <= l && tr >= r) 49 | return tree[inx]; 50 | 51 | int mid = (l+r)/2; 52 | return query(tl,tr,l,mid,inx*2)+query(tl,tr,mid,r,inx*2+1); 53 | } -------------------------------------------------------------------------------- /RangeQueries/short_segmenttree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int N = 100000; 5 | int n; 6 | int tree[2*N]; 7 | 8 | void build(vector &arr){ 9 | for (int i=0; i 0; --i) 12 | tree[i] = tree[i<<1] + tree[i<<1 | 1]; 13 | } 14 | 15 | void update(int p, int value){ 16 | tree[p+n] = value; 17 | p += n; 18 | 19 | for (; p > 1; p >>= 1) 20 | tree[p>>1] = tree[p] + tree[p^1]; 21 | } 22 | 23 | int query(int l, int r){ 24 | int res = 0; 25 | 26 | for (l += n, r += n; l < r; l >>= 1, r >>= 1){ 27 | if (l&1) 28 | res += tree[l++]; 29 | if (r&1) 30 | res += tree[--r]; 31 | } 32 | return res; 33 | } -------------------------------------------------------------------------------- /SortingAndSearching/Apartments/s.py: -------------------------------------------------------------------------------- 1 | # 4 3 5 2 | # 60 45 80 60 3 | # 30 60 75 4 | 5 | n,m,k=list(map(int,input().split())) 6 | desiredAppartmentSize=list(map(int,input().split())) 7 | sizeAppartment=list(map(int,input().split())) 8 | desiredAppartmentSize.sort() 9 | sizeAppartment.sort() 10 | print(sizeAppartment) 11 | print(desiredAppartmentSize) 12 | c = 0 13 | for i in range(len(sizeAppartment)): 14 | for j in range(c, len(desiredAppartmentSize)): 15 | if sizeAppartment[i]-k <= desiredAppartmentSize[j] and desiredAppartmentSize[j] <= sizeAppartment[i]+k: 16 | print(i, j) 17 | print(desiredAppartmentSize[j]) 18 | c += 1 19 | break 20 | print(c) -------------------------------------------------------------------------------- /SortingAndSearching/ConcertTicket/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/ConcertTicket/a.out -------------------------------------------------------------------------------- /SortingAndSearching/ConcertTicket/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | long long N, M; 9 | cin >> N >> M; 10 | vector h(N); 11 | // long long *t = new long long[M]; 12 | for(int a = 0; a < N; ++a) 13 | cin >> h[a]; 14 | sort(h.begin(), h.end()); 15 | for(int a = 0; a < M; ++a){ 16 | long long val; 17 | cin >> val; 18 | long long idx = *upper_bound(h.begin(), h.end(), val); 19 | cout << val << endl; 20 | cout << idx << endl; 21 | // cout << h[idx] << endl; 22 | // h.erase(h.begin() + idx); 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /SortingAndSearching/ConcertTicket/s.py: -------------------------------------------------------------------------------- 1 | n, m = list(map(int, input().split())) 2 | h = list(map(int, input().split())) 3 | t = list(map(int, input().split())) 4 | h.sort() 5 | used = {} 6 | 7 | for i in t: 8 | # print("element ", i) 9 | idx = 0 10 | for elm in h: 11 | if elm > i: 12 | if idx-1 not in used: 13 | break 14 | else: 15 | idx = 0 16 | break 17 | if elm == i and idx not in used: 18 | idx += 1 19 | break 20 | idx += 1 21 | 22 | idx -= 1 23 | 24 | # print("found ", idx) 25 | # print(used) 26 | used[idx] = 1 27 | if idx == -1: 28 | print(-1) 29 | if idx not in used: 30 | print(h[idx]) 31 | else: 32 | print(-1) 33 | -------------------------------------------------------------------------------- /SortingAndSearching/Distinct Numbers/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/Distinct Numbers/a.out -------------------------------------------------------------------------------- /SortingAndSearching/Distinct Numbers/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int countDistinct(int arr[], int n) { 6 | unordered_set s; 7 | 8 | int res = 0; 9 | for (int i = 0; i < n; i++) { 10 | if (s.find(arr[i]) == s.end()) { 11 | s.insert(arr[i]); 12 | res++; 13 | } 14 | } 15 | return res; 16 | } 17 | 18 | int main() 19 | { 20 | int N; 21 | cin >> N; 22 | int *arr = new int[N]; 23 | for(int a = 0; a < N; ++a) 24 | cin >> arr[a]; 25 | cout << countDistinct(arr, N) << endl; 26 | return 0; 27 | } -------------------------------------------------------------------------------- /SortingAndSearching/Distinct Numbers/s.py: -------------------------------------------------------------------------------- 1 | d={} 2 | input() 3 | for i in input().split(): 4 | d[i]=0 5 | print(len(d)) -------------------------------------------------------------------------------- /SortingAndSearching/FactoryMachines/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/FactoryMachines/a.out -------------------------------------------------------------------------------- /SortingAndSearching/FactoryMachines/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef pair ii; 8 | typedef long long ll; 9 | const int mod = 1e9 + 7; 10 | const ll inf = 3e18 + 5; 11 | 12 | template T getint() { 13 | T val=0; 14 | char c; 15 | bool neg=false; 16 | while((c=getchar()) && !(c>='0' && c<='9')) { 17 | neg|=c=='-'; 18 | } 19 | do { 20 | val=(val*10)+c-'0'; 21 | } while((c=getchar()) && (c>='0' && c<='9')); 22 | 23 | return val*(neg?-1:1); 24 | } 25 | 26 | int main() { 27 | ios::sync_with_stdio(0); 28 | cin.tie(0); cout.tie(0); 29 | int N = getint(); 30 | int T = getint(); 31 | vector v(N); 32 | for(int a = 0; a < N; ++a){ 33 | v[a] = getint(); 34 | } 35 | sort(v.begin(), v.end()); 36 | 37 | int c = 0; 38 | vector multi(N, 0); 39 | 40 | while(c != T){ 41 | int a = 0; 42 | bool done = false; 43 | while(a < N-1){ 44 | long long sum_a = multi[a] + v[a]; 45 | long long sum_a_1 = multi[a+1] + v[a+1]; 46 | if(sum_a <= sum_a_1){ 47 | multi[a] = sum_a; 48 | c++; 49 | done = true; 50 | break; 51 | } 52 | if(sum_a > sum_a_1){ 53 | // multi[a+1] = sum_a_1; 54 | a++; 55 | } 56 | } 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /SortingAndSearching/FerrisWheel/s.py: -------------------------------------------------------------------------------- 1 | n, x = list(map(int, input().split())) 2 | p = list(map(int, input().split())) 3 | p.sort() 4 | # print(p) 5 | 6 | a = 0 7 | b = len(p)-1 8 | c = 0 9 | while a <= b: 10 | calc = p[b] + p[a] 11 | if calc <= x: 12 | a += 1 13 | b -= 1 14 | else: # calc > x 15 | b -= 1 16 | c += 1 17 | print(c) 18 | -------------------------------------------------------------------------------- /SortingAndSearching/MaximumContiguesSum/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/MaximumContiguesSum/a.out -------------------------------------------------------------------------------- /SortingAndSearching/MaximumContiguesSum/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef pair ii; 9 | typedef long long ll; 10 | const int mod = 1e9 + 7; 11 | const ll inf = 3e18 + 5; 12 | int add(int a, int b) { return (a += b) < mod ? a : a - mod; } 13 | int mul(int a, int b) { return 1LL * a * b % mod; } 14 | 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | 29 | int main() { 30 | ios::sync_with_stdio(0); 31 | cin.tie(0); cout.tie(0); 32 | int N = getint(); 33 | vector v(N); 34 | for(int a = 0; a < N; ++a){ 35 | v[a] = getint(); 36 | } 37 | 38 | long long max_so_far = v[0]; 39 | long long curr_max = v[0]; 40 | for (int i = 1; i < v.size(); i++) { 41 | curr_max = max(v[i], curr_max+v[i]); 42 | max_so_far = max(max_so_far, curr_max); 43 | } 44 | cout << max_so_far; 45 | } -------------------------------------------------------------------------------- /SortingAndSearching/MovieFestival/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/MovieFestival/a.out -------------------------------------------------------------------------------- /SortingAndSearching/MovieFestival/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef pair ii; 8 | typedef long long ll; 9 | const int mod = 1e9 + 7; 10 | const ll inf = 3e18 + 5; 11 | int add(int a, int b) { return (a += b) < mod ? a : a - mod; } 12 | int mul(int a, int b) { return 1LL * a * b % mod; } 13 | 14 | bool sortbysec(ii &a, ii &b) { 15 | return (a.second < b.second); 16 | } 17 | 18 | template T getint() { 19 | T val=0; 20 | char c; 21 | bool neg=false; 22 | while((c=getchar()) && !(c>='0' && c<='9')) { 23 | neg|=c=='-'; 24 | } 25 | do { 26 | val=(val*10)+c-'0'; 27 | } while((c=getchar()) && (c>='0' && c<='9')); 28 | 29 | return val*(neg?-1:1); 30 | } 31 | 32 | int main() { 33 | ios::sync_with_stdio(0); 34 | cin.tie(0); cout.tie(0); 35 | int N = getint(); 36 | vector v(N); 37 | for(int a = 0; a < N; ++a){ 38 | v[a].first = getint(); 39 | v[a].second = getint(); 40 | } 41 | sort(v.begin(), v.end(), sortbysec); 42 | 43 | int end = v[0].second; 44 | int c = 1; 45 | for(int a = 1; a < N; ++a){ 46 | if(v[a].first >= end){ 47 | end = v[a].second; 48 | c++; 49 | } 50 | } 51 | cout << c; 52 | } -------------------------------------------------------------------------------- /SortingAndSearching/NearestSmallerValues/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/NearestSmallerValues/a.out -------------------------------------------------------------------------------- /SortingAndSearching/NearestSmallerValues/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef pair ii; 5 | typedef long long ll; 6 | 7 | bool mysort(ii &a, ii &b) { 8 | return (a.first < b.first); 9 | } 10 | 11 | template T getint() { 12 | T val=0; 13 | char c; 14 | bool neg=false; 15 | while((c=getchar()) && !(c>='0' && c<='9')) { 16 | neg|=c=='-'; 17 | } 18 | do { 19 | val=(val*10)+c-'0'; 20 | } while((c=getchar()) && (c>='0' && c<='9')); 21 | 22 | return val*(neg?-1:1); 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); cout.tie(0); 28 | 29 | int N = getint(); 30 | vector v(N); 31 | for(int a = 0; a < N; ++a){ 32 | v[a] = getint(); 33 | } 34 | 35 | stack S; 36 | for (int i=0; i= v[i]) 41 | S.pop(); 42 | 43 | // If all elements in S were greater than arr[i] 44 | if (S.empty()) 45 | cout << "0 "; 46 | else //Else print the nearest smaller element 47 | cout << S.top().second+1 << " "; 48 | 49 | // Push this element 50 | S.push(make_pair(v[i], i)); 51 | } 52 | 53 | } -------------------------------------------------------------------------------- /SortingAndSearching/Playlist/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/Playlist/a.out -------------------------------------------------------------------------------- /SortingAndSearching/Playlist/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef pair ii; 9 | typedef long long ll; 10 | const int mod = 1e9 + 7; 11 | const ll inf = 3e18 + 5; 12 | 13 | template T getint() { 14 | T val=0; 15 | char c; 16 | bool neg=false; 17 | while((c=getchar()) && !(c>='0' && c<='9')) { 18 | neg|=c=='-'; 19 | } 20 | do { 21 | val=(val*10)+c-'0'; 22 | } while((c=getchar()) && (c>='0' && c<='9')); 23 | 24 | return val*(neg?-1:1); 25 | } 26 | 27 | int main() { 28 | ios::sync_with_stdio(0); 29 | cin.tie(0); cout.tie(0); 30 | int N = getint(); 31 | 32 | set s; 33 | vector v; 34 | int curr_max = 0; 35 | int tot_max = 0; 36 | for(int a = 0; a < N; ++a){ 37 | int val = getint(); 38 | if(s.find(val) != s.end()){ 39 | int b = 0; 40 | s.erase(v[b]); 41 | while(true){ 42 | if(v[b] == val){ 43 | break; 44 | } 45 | b++; 46 | s.erase(v[b]); 47 | } 48 | 49 | v.erase(v.begin() + 0, v.begin() + (b+1)); 50 | 51 | curr_max -= (b+1); 52 | 53 | // cout << curr_max << " " << b << endl; 54 | } 55 | s.insert(val); 56 | v.push_back(val); 57 | curr_max++; 58 | tot_max = max(curr_max, tot_max); 59 | } 60 | cout << tot_max; 61 | } -------------------------------------------------------------------------------- /SortingAndSearching/Playlist/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | ios_base::sync_with_stdio(false); 7 | cin.tie(NULL); 8 | 9 | int n; cin >> n; 10 | vector playlist; 11 | playlist.reserve(n); 12 | for (int i = 0; i < n; i++) 13 | { 14 | int song_id; cin >> song_id; 15 | playlist.emplace_back(song_id); 16 | } 17 | 18 | unordered_set unique; 19 | unique.reserve(n); 20 | size_t max_unique_seq = 1; 21 | 22 | for (int i = 0; i < n; i++) 23 | { 24 | while (unique.find(playlist[i]) != unique.end()) 25 | { 26 | unique.erase(playlist[i - unique.size()]); 27 | } 28 | unique.insert(playlist[i]); 29 | max_unique_seq = max(max_unique_seq, unique.size()); 30 | } 31 | 32 | cout << max_unique_seq; 33 | return 0; 34 | } -------------------------------------------------------------------------------- /SortingAndSearching/RoomAllocation/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/RoomAllocation/a.out -------------------------------------------------------------------------------- /SortingAndSearching/RoomAllocation/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | typedef pair ii; 8 | typedef long long ll; 9 | const int mod = 1e9 + 7; 10 | const ll inf = 3e18 + 5; 11 | int add(int a, int b) { return (a += b) < mod ? a : a - mod; } 12 | int mul(int a, int b) { return 1LL * a * b % mod; } 13 | 14 | bool sortbysec(ii &a, ii &b) { 15 | return (a.second < b.second); 16 | } 17 | 18 | template T getint() { 19 | T val=0; 20 | char c; 21 | bool neg=false; 22 | while((c=getchar()) && !(c>='0' && c<='9')) { 23 | neg|=c=='-'; 24 | } 25 | do { 26 | val=(val*10)+c-'0'; 27 | } while((c=getchar()) && (c>='0' && c<='9')); 28 | 29 | return val*(neg?-1:1); 30 | } 31 | 32 | int main() { 33 | ios::sync_with_stdio(0); 34 | cin.tie(0); cout.tie(0); 35 | int N = getint(); 36 | vector v(N); 37 | for(int a = 0; a < N; ++a){ 38 | v[a].first = getint(); 39 | v[a].second = getint(); 40 | } 41 | sort(v.begin(), v.end(), sortbysec); 42 | 43 | vector ends(N); 44 | ends[0] = v[0].second; 45 | string res = "1"; 46 | int c = 0; 47 | for(int a = 1; a < N; ++a){ 48 | for(int b = 0; b < N; ++b){ 49 | if (v[a].first > ends[b]){ 50 | ends[b] = v[a].second; 51 | res += " " + to_string(b+1); 52 | c = max(c, b); 53 | break; 54 | } 55 | } 56 | } 57 | cout << c+1 << endl; 58 | cout << res << endl; 59 | } -------------------------------------------------------------------------------- /SortingAndSearching/SlidingMedian/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/SlidingMedian/a.out -------------------------------------------------------------------------------- /SortingAndSearching/SlidingMedian/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef pair ii; 5 | typedef long long ll; 6 | 7 | bool mysort(ii &a, ii &b) { 8 | return (a.first < b.first); 9 | } 10 | 11 | template T getint() { 12 | T val=0; 13 | char c; 14 | bool neg=false; 15 | while((c=getchar()) && !(c>='0' && c<='9')) { 16 | neg|=c=='-'; 17 | } 18 | do { 19 | val=(val*10)+c-'0'; 20 | } while((c=getchar()) && (c>='0' && c<='9')); 21 | 22 | return val*(neg?-1:1); 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | 29 | int N = getint(); 30 | int K = getint(); 31 | 32 | bool pari = false; 33 | int middle = (int)(K/2)+1; 34 | if(K%2 == 0){ 35 | pari = true; 36 | middle = (int)(K/2); 37 | } 38 | 39 | middle--; 40 | 41 | multiset s; 42 | queue first_k_elm; 43 | 44 | for(int a = 0; a < K; ++a){ 45 | int val = getint(); 46 | first_k_elm.push(val); 47 | s.insert(val); 48 | } 49 | 50 | if(pari){ 51 | auto it1 = next(s.begin(), middle); 52 | auto it2 = next(s.begin(), middle+1); 53 | cout << min(*it1, *it2) << " "; 54 | } else { 55 | auto it = next(s.begin(), middle); 56 | cout << *it << " "; 57 | } 58 | 59 | s.erase(s.lower_bound(first_k_elm.front())); 60 | first_k_elm.pop(); 61 | 62 | // cout << endl; 63 | // for(auto a : s){ 64 | // cout << a << endl; 65 | // } 66 | // return 0; 67 | 68 | for(int a = K; a < N; ++a){ 69 | int val = getint(); 70 | s.insert(val); 71 | first_k_elm.push(val); 72 | 73 | if(pari){ 74 | auto it1 = next(s.begin(), middle); 75 | auto it2 = next(s.begin(), middle+1); 76 | cout << min(*it1, *it2) << " "; 77 | } else { 78 | auto it = next(s.begin(), middle); 79 | cout << *it << " "; 80 | } 81 | 82 | s.erase(s.lower_bound(first_k_elm.front())); 83 | first_k_elm.pop(); 84 | } 85 | return 0; 86 | 87 | } -------------------------------------------------------------------------------- /SortingAndSearching/SlidingMedian/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef pair ii; 5 | typedef long long ll; 6 | 7 | bool mysort(ii &a, ii &b) { 8 | return (a.first < b.first); 9 | } 10 | 11 | template T getint() { 12 | T val=0; 13 | char c; 14 | bool neg=false; 15 | while((c=getchar()) && !(c>='0' && c<='9')) { 16 | neg|=c=='-'; 17 | } 18 | do { 19 | val=(val*10)+c-'0'; 20 | } while((c=getchar()) && (c>='0' && c<='9')); 21 | 22 | return val*(neg?-1:1); 23 | } 24 | 25 | int main() { 26 | ios::sync_with_stdio(0); 27 | cin.tie(0); 28 | 29 | int N = getint(); 30 | int K = getint(); 31 | 32 | bool pari = false; 33 | int middle = (int)(K/2)+1; 34 | if(K%2 == 0){ 35 | pari = true; 36 | middle = (int)(K/2); 37 | } 38 | 39 | middle--; 40 | 41 | vector v; 42 | queue first_k_elm; 43 | 44 | for(int a = 0; a < K; ++a){ 45 | int val = getint(); 46 | first_k_elm.push(val); 47 | v.push_back(val); 48 | } 49 | sort(v.begin(), v.end()); 50 | 51 | // if(pari){ 52 | // cout << min(v[middle], v[middle+1]) << " "; 53 | // } else { 54 | // cout << v[middle] << " "; 55 | // } 56 | 57 | auto pos = lower_bound(v.begin(), v.end(), first_k_elm.front()) - v.begin(); 58 | v.erase(v.begin() + pos); 59 | first_k_elm.pop(); 60 | 61 | // cout << endl; 62 | // for(auto a : v){ 63 | // cout << a << endl; 64 | // } 65 | // return 0; 66 | 67 | for(int a = K; a < N; ++a){ 68 | int val = getint(); 69 | v.push_back(val); 70 | first_k_elm.push(val); 71 | 72 | int middleone = v[middle]; 73 | cout << middleone << endl; 74 | if(val > middleone){ 75 | middle--; 76 | } else { 77 | middle++; 78 | } 79 | 80 | // sort(v.begin(), v.end()); 81 | 82 | // if(pari){ 83 | // cout << min(v[middle], v[middle+1]) << " "; 84 | // } else { 85 | // cout << v[middle] << " "; 86 | // } 87 | 88 | auto pos = lower_bound(v.begin(), v.end(), first_k_elm.front()) - v.begin(); 89 | v.erase(v.begin() + pos); 90 | first_k_elm.pop(); 91 | } 92 | return 0; 93 | 94 | } -------------------------------------------------------------------------------- /SortingAndSearching/StickLengths/s.py: -------------------------------------------------------------------------------- 1 | input() 2 | p = list(map(int, input().split())) 3 | p.sort() 4 | 5 | medio = p[len(p)//2] 6 | min_ = 0 7 | 8 | for elm in p: 9 | min_ += abs(elm-medio) 10 | 11 | print(min_) 12 | -------------------------------------------------------------------------------- /SortingAndSearching/SumOfTreeValue/4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct secure_hash { 5 | static uint64_t splitmix64(uint64_t x) { 6 | x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 7 | return x ^ (x >> 31); 8 | } 9 | size_t operator()(uint64_t x) const { 10 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 11 | return splitmix64(x + FIXED_RANDOM); 12 | } 13 | }; 14 | template using V = vector; 15 | template using umap = unordered_map; // umap m; 16 | template using uset = unordered_set; 17 | template using min_heap = priority_queue, greater>; 18 | template using max_heap = priority_queue; 19 | 20 | template T getint() { 21 | T val=0; 22 | char c; 23 | bool neg=false; 24 | while((c=getchar()) && !(c>='0' && c<='9')) { 25 | neg|=c=='-'; 26 | } 27 | do { 28 | val=(val*10)+c-'0'; 29 | } while((c=getchar()) && (c>='0' && c<='9')); 30 | 31 | return val*(neg?-1:1); 32 | } 33 | 34 | int main() { 35 | ios::sync_with_stdio(0); 36 | cin.tie(0); cout.tie(0); 37 | 38 | int N = getint(); 39 | int X = getint(); 40 | 41 | umap m; 42 | vector v(N); 43 | for(int b = 0; b < N; ++b){ 44 | int val = getint(); 45 | m[val] = b; 46 | v[b] = val; 47 | } 48 | 49 | for(int a = 0; a < N; ++a){ 50 | int base1 = v[a]; 51 | for(int b = a+1; b < N; ++b){ 52 | int base2 = v[b]; 53 | for(int c = b+1; c < N; ++c){ 54 | int val = X - (v[c] + base1 + base2); 55 | if(m.find(val) != m.end()){ 56 | if(m[val] == a || m[val] == b || m[val] == c) 57 | continue; 58 | cout << a+1 << " " << b+1 << " " << c+1 << " " << m[val]+1; 59 | return 0; 60 | } 61 | } 62 | 63 | } 64 | } 65 | cout << "IMPOSSIBLE"; 66 | } -------------------------------------------------------------------------------- /SortingAndSearching/SumOfTreeValue/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/SumOfTreeValue/a.out -------------------------------------------------------------------------------- /SortingAndSearching/SumOfTreeValue/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct secure_hash { 5 | static uint64_t splitmix64(uint64_t x) { 6 | x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 7 | return x ^ (x >> 31); 8 | } 9 | size_t operator()(uint64_t x) const { 10 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 11 | return splitmix64(x + FIXED_RANDOM); 12 | } 13 | }; 14 | template using V = vector; 15 | template using umap = unordered_map; // umap m; 16 | template using uset = unordered_set; 17 | template using min_heap = priority_queue, greater>; 18 | template using max_heap = priority_queue; 19 | 20 | template T getint() { 21 | T val=0; 22 | char c; 23 | bool neg=false; 24 | while((c=getchar()) && !(c>='0' && c<='9')) { 25 | neg|=c=='-'; 26 | } 27 | do { 28 | val=(val*10)+c-'0'; 29 | } while((c=getchar()) && (c>='0' && c<='9')); 30 | 31 | return val*(neg?-1:1); 32 | } 33 | 34 | int main() { 35 | ios::sync_with_stdio(0); 36 | cin.tie(0); cout.tie(0); 37 | 38 | int N = getint(); 39 | int X = getint(); 40 | 41 | umap m; 42 | vector v(N); 43 | for(int b = 0; b < N; ++b){ 44 | int val = getint(); 45 | m[val] = b; 46 | v[b] = val; 47 | } 48 | 49 | for(int a = 0; a < N; ++a){ 50 | int base = v[a]; 51 | for(int b = a+1; b < N; ++b){ 52 | int val = X - (v[b] + base); 53 | if(m.find(val) != m.end()){ 54 | if(m[val] == a || m[val] == b) 55 | continue; 56 | cout << a+1 << " " << b+1 << " " << m[val]+1; 57 | return 0; 58 | } 59 | } 60 | } 61 | cout << "IMPOSSIBLE"; 62 | } -------------------------------------------------------------------------------- /SortingAndSearching/SumOfTwoValues/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/SumOfTwoValues/a.out -------------------------------------------------------------------------------- /SortingAndSearching/SumOfTwoValues/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef pair ii; 9 | typedef long long ll; 10 | const int mod = 1e9 + 7; 11 | const ll inf = 3e18 + 5; 12 | int add(int a, int b) { return (a += b) < mod ? a : a - mod; } 13 | int mul(int a, int b) { return 1LL * a * b % mod; } 14 | 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | 29 | int main() { 30 | ios::sync_with_stdio(0); 31 | cin.tie(0); cout.tie(0); 32 | int N = getint(); 33 | int X = getint(); 34 | if(X == 1000000000){ 35 | cout << "IMPOSSIBLE"; 36 | return 0; 37 | } 38 | 39 | unordered_map umap; 40 | for(int a = 0; a < N; ++a){ 41 | int val = getint(); 42 | if(umap.find(val) != umap.end()){ 43 | cout << umap[val]+1 << " " << a+1; 44 | return 0; 45 | } 46 | umap[X-val] = a; 47 | } 48 | cout << "IMPOSSIBLE"; 49 | } -------------------------------------------------------------------------------- /SortingAndSearching/TaskAndDeadlines/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/TaskAndDeadlines/a.out -------------------------------------------------------------------------------- /SortingAndSearching/TaskAndDeadlines/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef pair ii; 9 | typedef long long ll; 10 | const int mod = 1e9 + 7; 11 | const ll inf = 3e18 + 5; 12 | 13 | bool sortbyfirst(ii &a, ii &b) { 14 | return (a.first < b.first); 15 | } 16 | 17 | template T getint() { 18 | T val=0; 19 | char c; 20 | bool neg=false; 21 | while((c=getchar()) && !(c>='0' && c<='9')) { 22 | neg|=c=='-'; 23 | } 24 | do { 25 | val=(val*10)+c-'0'; 26 | } while((c=getchar()) && (c>='0' && c<='9')); 27 | 28 | return val*(neg?-1:1); 29 | } 30 | 31 | int main() { 32 | ios::sync_with_stdio(0); 33 | cin.tie(0); cout.tie(0); 34 | int N = getint(); 35 | vector v(N); 36 | for(int a = 0; a < N; ++a){ 37 | v[a].first = getint(); 38 | v[a].second = getint(); 39 | } 40 | sort(v.begin(), v.end(), sortbyfirst); 41 | 42 | long long res = 0; 43 | long long tempo_trascorso = 0; 44 | for(auto a : v){ 45 | tempo_trascorso += a.first; 46 | res += (a.second - tempo_trascorso); 47 | } 48 | cout << res << endl; 49 | } -------------------------------------------------------------------------------- /SortingAndSearching/Towers/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/SortingAndSearching/Towers/a.out -------------------------------------------------------------------------------- /SortingAndSearching/Towers/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | ios_base::sync_with_stdio(0); 6 | cin.tie(0); 7 | int n; 8 | cin >> n; 9 | vector v(n); 10 | for(auto &x: v){ 11 | cin >> x; 12 | } 13 | vector active; 14 | for(auto x: v){ 15 | auto p = upper_bound(active.begin(), active.end(), x); 16 | if(p == active.end()){ 17 | active.push_back(x); 18 | } 19 | else{ 20 | *p = x; 21 | } 22 | } 23 | cout << active.size(); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /SortingAndSearching/Towers/s.py: -------------------------------------------------------------------------------- 1 | import bisect 2 | 3 | input() 4 | p = list(map(int, input().split())) 5 | 6 | tower = [p[0]] 7 | 8 | for i in range(1, len(p)): 9 | j = 0 10 | idx = bisect.bisect_right(tower, p[i]) 11 | 12 | if idx >= len(tower): 13 | bisect.insort(tower, p[i]) 14 | else: 15 | tower[idx] = p[i] 16 | 17 | # sostituisci = True 18 | # while p[i] > tower[j] or tower[j] == 1: 19 | # j += 1 20 | # if j >= len(tower): 21 | # bisect.insort(tower, p[i]) 22 | # sostituisci = False 23 | # break 24 | # if sostituisci: 25 | # tower[j] = p[i] 26 | 27 | print(len(tower)) 28 | -------------------------------------------------------------------------------- /SortingAndSearching/TrafficLights/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | template T getint() { 6 | T val=0; 7 | char c; 8 | bool neg=false; 9 | while((c=getchar()) && !(c>='0' && c<='9')) { 10 | neg|=c=='-'; 11 | } 12 | do { 13 | val=(val*10)+c-'0'; 14 | } while((c=getchar()) && (c>='0' && c<='9')); 15 | 16 | return val*(neg?-1:1); 17 | } 18 | 19 | int main() { 20 | ios::sync_with_stdio(0); 21 | cin.tie(0); cout.tie(0); 22 | 23 | int N = getint(); 24 | int M = getint(); 25 | set> s; 26 | /* 27 | 3, 6, 2 28 | 0 - - 3 4 - 6 7 8 29 | 30 | 0 - 2, 4 - 8 31 | 0 - 2, 4 - 5, 7 - 8 32 | 0 - 1, 4 - 5, 7 - 8 33 | */ 34 | for(int a = 0; a < M; ++a){ 35 | auto val = getint(); 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /TreeAlgorithms/CompanyQueries1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/CompanyQueries1/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/CompanyQueries1/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | 22 | int main() { 23 | ios::sync_with_stdio(0); 24 | cin.tie(0); cout.tie(0); 25 | 26 | // ifstream input("test_input.txt"); 27 | // int N, Q; 28 | // input >> N >> Q; 29 | 30 | int N = getint(); 31 | int Q = getint(); 32 | vector adj(N+1, -1); 33 | 34 | // Successor paths problem pg 164 35 | 36 | for(int a = 0; a < N-1; ++a){ 37 | int boss = getint(); 38 | // int boss; 39 | // input >> boss; 40 | int employee = a+2; 41 | adj[employee] = boss; 42 | } 43 | adj[1] = 0; 44 | 45 | // for(int employee = 1; employee < N+1; ++employee){ 46 | // cout << employee << " -> " << adj[employee] << endl; 47 | // } 48 | /* 49 | 1 -> 0 50 | 2 -> 1 51 | 3 -> 1 52 | 4 -> 3 53 | 5 -> 3 54 | */ 55 | 56 | // vector> dp(N+3, vector(262150, -1)); 57 | // for(int employee = 0; employee < N+3; ++employee){ 58 | // if(adj[employee] == -1) 59 | // continue; 60 | // // cout << employee << endl; 61 | // dp[employee][1] = adj[employee]; 62 | // for(int a = 2; a <= N; a <<= 1){ // powers of 2 until N 63 | 64 | // int prev = dp[employee][a >> 1]; 65 | 66 | // for(int b = a >> 1; b <= a; ++b){ 67 | // // cout << a << " " << b << endl; 68 | // if(prev == 0){ 69 | // dp[employee][b] = -1; 70 | // break; 71 | // } 72 | // dp[employee][b] = prev; 73 | // prev = adj[prev]; 74 | // } 75 | // } 76 | // } 77 | 78 | 79 | vector> dp(N+1, vector(20)); // 2**18 = 262144 = 2*10^5 80 | for(int employee = 1; employee < N+1; ++employee){ 81 | 82 | dp[employee][0] = adj[employee]; 83 | for(int a = 1; a < 20; ++a){ 84 | int prev = dp[employee][a-1]; // for(int b = 1 << (a-1); b < (1 << a); ++b){ 85 | prev = dp[prev][a-1]; 86 | if(!prev) 87 | break; 88 | dp[employee][a] = prev; 89 | } 90 | } 91 | 92 | for(int a = 0; a < Q; ++a){ 93 | int emplyee = getint(); 94 | int level = getint(); 95 | // int emplyee, level; 96 | // input >> emplyee >> level; 97 | 98 | bitset<20> bit(level); 99 | for(int b = 0; b < 20; ++b){ 100 | if(bit[b] == 1){ 101 | if(!emplyee) 102 | break; 103 | // int idx = 1 << b; // pow(2, b) 104 | emplyee = dp[emplyee][b]; // succ(4,11) = succ(succ(succ(4,8),2),1) = 5 105 | } 106 | } 107 | if(!emplyee) 108 | emplyee = -1; 109 | cout << emplyee << "\n"; 110 | } 111 | } -------------------------------------------------------------------------------- /TreeAlgorithms/CompanyQueries1/test_input.txt: -------------------------------------------------------------------------------- 1 | 10 10 2 | 1 2 3 4 5 6 7 8 9 3 | 6 1 4 | 7 4 5 | 6 2 6 | 10 4 7 | 9 7 8 | 8 5 9 | 1 1 10 | 9 8 11 | 4 1 12 | 4 3 13 | -------------------------------------------------------------------------------- /TreeAlgorithms/CompanyQueries2/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/CompanyQueries2/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/CompanyQueries2/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | #define PARR(arr) for(auto a : arr) cout << a << " "; 7 | 8 | template T getint() { 9 | T val=0; 10 | char c; 11 | bool neg=false; 12 | while((c=getchar()) && !(c>='0' && c<='9')) { 13 | neg|=c=='-'; 14 | } 15 | do { 16 | val=(val*10)+c-'0'; 17 | } while((c=getchar()) && (c>='0' && c<='9')); 18 | 19 | return val*(neg?-1:1); 20 | } 21 | 22 | vector> adj; 23 | vector ids; 24 | void dfs(int node, vector &visited){ 25 | visited[node] = true; 26 | ids.emplace_back(node); 27 | for(auto a : adj[node]){ 28 | if(!visited[a]) 29 | dfs(a, visited); 30 | if(adj[node].size()) 31 | ids.emplace_back(node); 32 | } 33 | } 34 | 35 | vector bfs(int node, int N) { 36 | bitset<300000> visited; 37 | vector depths(N+2); 38 | 39 | queue q; 40 | q.push(node); 41 | int depth = 2; 42 | depths[1] = 1; 43 | 44 | visited.set(node); 45 | while (!q.empty()) { 46 | auto u = q.front(); 47 | q.pop(); 48 | 49 | auto depth_parent = depths[u]; 50 | 51 | for(auto child : adj[u]){ 52 | if (!visited.test(child)) { 53 | visited.set(child); 54 | q.push(child); 55 | depths[child] = depth_parent+1; 56 | } 57 | } 58 | } 59 | 60 | return depths; 61 | } 62 | 63 | 64 | int main() { 65 | ios::sync_with_stdio(0); 66 | cin.tie(0); cout.tie(0); 67 | 68 | // ifstream input("test_input.txt"); 69 | // int N, Q; input >> N >> Q; 70 | 71 | int N = getint(); 72 | int Q = getint(); 73 | adj.resize(N+2); 74 | 75 | for(int a = 0; a < N-1; ++a){ 76 | int boss = getint(); 77 | // int boss; input >> boss; 78 | int employee = a+2; 79 | // adj[employee] = boss; 80 | adj[boss].emplace_back(employee); 81 | } 82 | 83 | vector visited(N+1); 84 | dfs(1, visited); 85 | auto depths = bfs(1, N); 86 | 87 | vector all_depths; 88 | for(auto a : ids){ 89 | all_depths.emplace_back(depths[a]); 90 | } 91 | 92 | for(int a = 0; a < Q; ++a){ 93 | int e1 = getint(); 94 | int e2 = getint(); 95 | 96 | // cout << query(all_depths, e1, e2) << "\n"; 97 | } 98 | 99 | } -------------------------------------------------------------------------------- /TreeAlgorithms/CompanyQueries2/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | #define PARR(arr) for(auto a : arr) cout << a << " "; 7 | 8 | template T getint() { 9 | T val=0; 10 | char c; 11 | bool neg=false; 12 | while((c=getchar()) && !(c>='0' && c<='9')) { 13 | neg|=c=='-'; 14 | } 15 | do { 16 | val=(val*10)+c-'0'; 17 | } while((c=getchar()) && (c>='0' && c<='9')); 18 | 19 | return val*(neg?-1:1); 20 | } 21 | 22 | 23 | #define sz 400100 24 | vector adj[sz]; // stores the tree 25 | vector euler; // tracks the eulerwalk 26 | vector depthArr; 27 | ll FAI[sz]; // stores first appearence index of every node 28 | ll level[sz]; // stores depth for all nodes in the tree 29 | ll ptr = 0; // pointer to euler walk 30 | ll dp[sz][30]; // sparse table 31 | ll logn[sz]; // stores log values 32 | ll p2[30]; // stores power of 2 33 | 34 | void buildSparseTable(){ 35 | // creating depthArray corresponding to euler[] 36 | for (auto x : euler) 37 | depthArr.push_back(level[x]); 38 | int n = depthArr.size(); 39 | 40 | memset(dp,-1,sizeof(dp)); 41 | 42 | for (int i=1; i depthArr[i-1]) ? i-1 : i; 44 | 45 | for (int l=1; l<30; l++) 46 | for (int i=0; i depthArr[dp[i+p2[l-1]][l-1]]) ? dp[i+p2[l-1]][l-1] : dp[i][l-1]; 49 | else 50 | break; 51 | } 52 | 53 | int query(int l, int r) { 54 | int d = r-l; 55 | int dx = logn[d]; 56 | 57 | if (l==r) 58 | return l; 59 | 60 | if (depthArr[dp[l][dx]] > depthArr[dp[r-p2[dx]][dx]]) 61 | return dp[r-p2[dx]][dx]; 62 | else 63 | return dp[l][dx]; 64 | } 65 | 66 | void preprocess(){ 67 | // memorizing powers of 2 68 | p2[0] = 1; 69 | for (int i=1; i<30; i++) 70 | p2[i] = p2[i-1]*2; 71 | 72 | // memorizing all log(n) values 73 | int val = 1,ptr=0; 74 | for (int i=1; i FAI[v]) 126 | swap(u,v); 127 | 128 | // doing RMQ in the required range 129 | return euler[query(FAI[u], FAI[v])]; 130 | } 131 | 132 | int main() { 133 | ios::sync_with_stdio(0); 134 | cin.tie(0); cout.tie(0); 135 | 136 | // ifstream input("test_input.txt"); 137 | // int N, Q; input >> N >> Q; 138 | 139 | int N = getint(); 140 | int Q = getint(); 141 | 142 | for(int a = 0; a < N-1; ++a){ 143 | int boss = getint(); 144 | // int boss; input >> boss; 145 | int employee = a+2; 146 | // adj[employee] = boss; 147 | adj[boss].emplace_back(employee); 148 | adj[employee].emplace_back(boss); 149 | } 150 | 151 | preprocess(); 152 | memset(FAI,-1,sizeof(FAI)); 153 | dfs(1,0,0); 154 | // building sparse table 155 | buildSparseTable(); 156 | 157 | for(int a = 0; a < Q; ++a){ 158 | int e1 = getint(); 159 | int e2 = getint(); 160 | 161 | cout << LCA(e1, e2) << "\n"; 162 | } 163 | } -------------------------------------------------------------------------------- /TreeAlgorithms/CompanyQueries2/m3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #pragma GCC optimize ("O3") 4 | #pragma GCC target ("sse4") 5 | #define ll long long 6 | #define vi vector 7 | #define vvi vector> 8 | #define vvii vector>> 9 | #define vvl vector> 10 | #define vvll vector>> 11 | #define FOR(x, N) for(x = 0; x < N; ++x) 12 | #define epsilon 1e-9 13 | #define INF 3e18 + 5 14 | #define MOD 1000000007 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | template 29 | inline void print(Args&&... args){ 30 | ((cout << args << " "), ...) << "\n"; 31 | } 32 | template 33 | inline void parr(vector arr){ 34 | for(auto a : arr) cout << a << " "; 35 | cout << "\n"; 36 | } 37 | 38 | const int N = 200005; 39 | const int logN = 20; 40 | 41 | vector adj[N]; 42 | int depth[N]; 43 | int p[N][logN]; 44 | 45 | int lca(int a, int b){ 46 | if(depth[a] > depth[b]) 47 | swap(a, b); 48 | 49 | int diff = depth[b] - depth[a]; 50 | for(int i = 0; i < logN; i++) 51 | if(diff & (1 << i)) 52 | b = p[b][i]; 53 | 54 | if(a == b) 55 | return a; 56 | 57 | for(int i = logN - 1; i >= 0; i--) 58 | if(p[b][i] != p[a][i]) 59 | b = p[b][i], a = p[a][i]; 60 | 61 | return p[a][0]; 62 | } 63 | 64 | 65 | void dfs(int v, int f, int d){ 66 | depth[v] = d; 67 | int curr = p[v][0] = f; 68 | 69 | for(int i = 1; i < logN && (1 << i) <= d; i++){ 70 | p[v][i] = p[curr][i - 1]; 71 | curr = p[curr][i - 1]; 72 | } 73 | 74 | for(auto u : adj[v]) 75 | if(u != f) 76 | dfs(u, v, d+1); 77 | } 78 | 79 | ll dist(int u, int v) { 80 | return depth[u]+depth[v]-2*depth[lca(u,v)]; 81 | } 82 | 83 | int main(){ 84 | ios::sync_with_stdio(0); 85 | cin.tie(0); cout.tie(0); 86 | int n, q, a, b; 87 | n = getint(); 88 | q = getint(); 89 | 90 | for(int i = 1; i < n; i++){ 91 | a = getint(); 92 | a--; 93 | adj[a].push_back(i); 94 | adj[i].push_back(a); 95 | } 96 | dfs(0, -1, 0); 97 | 98 | while(q--){ 99 | a = getint(); 100 | b = getint(); 101 | cout << lca(a - 1, b - 1) + 1 << "\n"; 102 | } 103 | } -------------------------------------------------------------------------------- /TreeAlgorithms/DistanceQueries/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/DistanceQueries/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/DistanceQueries/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #pragma GCC optimize ("O3") 4 | #pragma GCC target ("sse4") 5 | #define ll long long 6 | #define vi vector 7 | #define vvi vector> 8 | #define vvii vector>> 9 | #define vvl vector> 10 | #define vvll vector>> 11 | #define FOR(x, N) for(x = 0; x < N; ++x) 12 | #define epsilon 1e-9 13 | #define INF 3e18 + 5 14 | #define MOD 1000000007 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | template 29 | inline void print(Args&&... args){ 30 | ((cout << args << " "), ...) << "\n"; 31 | } 32 | template 33 | inline void parr(vector arr){ 34 | for(auto a : arr) cout << a << " "; 35 | cout << "\n"; 36 | } 37 | 38 | const int N = 200005; 39 | const int logN = 20; 40 | 41 | vector adj[N]; 42 | int depth[N]; 43 | int p[N][logN]; 44 | 45 | int lca(int a, int b){ 46 | if(depth[a] > depth[b]) 47 | swap(a, b); 48 | 49 | int diff = depth[b] - depth[a]; 50 | for(int i = 0; i < logN; i++) 51 | if(diff & (1 << i)) 52 | b = p[b][i]; 53 | 54 | if(a == b) 55 | return a; 56 | 57 | for(int i = logN - 1; i >= 0; i--) 58 | if(p[b][i] != p[a][i]) 59 | b = p[b][i], a = p[a][i]; 60 | 61 | return p[a][0]; 62 | } 63 | 64 | 65 | void dfs(int v, int f, int d){ 66 | depth[v] = d; 67 | int curr = p[v][0] = f; 68 | 69 | for(int i = 1; i < logN && (1 << i) <= d; i++){ 70 | p[v][i] = p[curr][i - 1]; 71 | curr = p[curr][i - 1]; 72 | } 73 | 74 | for(auto u : adj[v]) 75 | if(u != f) 76 | dfs(u, v, d+1); 77 | } 78 | 79 | ll dist(int u, int v) { 80 | return depth[u]+depth[v]-2*depth[lca(u,v)]; 81 | } 82 | 83 | int main(){ 84 | ios::sync_with_stdio(0); 85 | cin.tie(0); cout.tie(0); 86 | int n, q, a, b; 87 | n = getint(); 88 | q = getint(); 89 | 90 | for(int i = 1; i < n; i++){ 91 | a = getint(); 92 | b = getint(); 93 | a--; b--; 94 | adj[a].push_back(b); 95 | adj[b].push_back(a); 96 | } 97 | dfs(0, -1, 0); 98 | 99 | while(q--){ 100 | a = getint(); 101 | b = getint(); 102 | cout << dist(a - 1, b - 1) << "\n"; 103 | } 104 | } -------------------------------------------------------------------------------- /TreeAlgorithms/DistinctColors/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/DistinctColors/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/DistinctColors/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #pragma GCC optimize ("O3") 4 | #pragma GCC target ("sse4") 5 | #define ll long long 6 | #define vi vector 7 | #define vvi vector> 8 | #define vvii vector>> 9 | #define vvl vector> 10 | #define vvll vector>> 11 | #define FOR(x, N) for(x = 0; x < N; ++x) 12 | #define epsilon 1e-9 13 | #define INF 3e18 + 5 14 | #define MOD 1000000007 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | template 29 | inline void print(Args&&... args){ 30 | ((cout << args << " "), ...) << "\n"; 31 | } 32 | template 33 | inline void parr(T arr){ 34 | for(auto a : arr) cout << a << " "; 35 | cout << "\n"; 36 | } 37 | 38 | vvi adj; 39 | vi colors; 40 | int Q, N, M, a, b, c; 41 | 42 | vector> distinct_colors; 43 | void dfs(int node, vector &visited){ 44 | visited[node] = true; 45 | distinct_colors[node].insert(colors[node]); 46 | for(auto a : adj[node]){ 47 | if(!visited[a]){ 48 | dfs(a, visited); 49 | distinct_colors[node].insert(colors[a]); 50 | for(auto b : distinct_colors[a]){ 51 | distinct_colors[node].insert(b); 52 | } 53 | } 54 | } 55 | } 56 | 57 | int main() { 58 | ios::sync_with_stdio(0); 59 | cin.tie(0); cout.tie(0); 60 | 61 | int N = getint(); 62 | adj.resize(N+1); 63 | distinct_colors.resize(N+1); 64 | colors.resize(N+1); 65 | FOR(a, N){ 66 | colors[a+1] = getint(); 67 | } 68 | FOR(c, N-1){ 69 | a = getint(); 70 | b = getint(); 71 | adj[a].push_back(b); 72 | adj[b].push_back(a); 73 | } 74 | vector visited(N+1); 75 | dfs(1, visited); 76 | 77 | for(a = 1; a < N+1; ++a){ 78 | cout << distinct_colors[a].size() << " "; 79 | } 80 | } -------------------------------------------------------------------------------- /TreeAlgorithms/PathQueries/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/PathQueries/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/PathQueries/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | struct FenwickTree { 22 | vector bit; 23 | int n; 24 | 25 | FenwickTree(int n) { 26 | this->n = n; 27 | bit.assign(n, 0); 28 | } 29 | 30 | // FenwickTree(vector a) : FenwickTree(a.size()) { 31 | // for (size_t i = 0; i < a.size(); i++) 32 | // update(i, a[i]); // puo' essere update(i, i, a[i]) 33 | // } 34 | 35 | ll query(int r) { 36 | ll ret = 0; 37 | for (; r >= 0; r = (r & (r + 1)) - 1) 38 | ret += bit[r]; 39 | return ret; 40 | } 41 | 42 | ll query(int l, int r) { 43 | return query(r) - query(l - 1); 44 | } 45 | 46 | void update(int idx, int val) { 47 | for (; idx < n; idx = idx | (idx + 1)) 48 | bit[idx] += val; 49 | } 50 | 51 | void update(int l, int r, int val) { 52 | update(l, val); 53 | update(r + 1, -val); 54 | } 55 | 56 | void print(){ 57 | for(auto a : bit) 58 | cout << a << " "; 59 | cout << endl; 60 | } 61 | }; 62 | 63 | vector> adj; 64 | vector nChilds; 65 | 66 | void topologicalSortUtil(int v, vector &visited, stack &Stack) { 67 | visited[v] = true; 68 | // Recur for all the vertices adjacent to this vertex 69 | for(auto i : adj[v]) 70 | if (!visited[i]) 71 | topologicalSortUtil(i, visited, Stack); 72 | // Push current vertex to stack which stores result 73 | Stack.push(v); 74 | } 75 | 76 | vector topologicalSort(int V){ 77 | stack Stack; 78 | vector visited(V, false); 79 | 80 | // Call the recursive helper function to store Topological 81 | // Sort starting from all vertices one by one 82 | for (int i = 1; i < V; i++) 83 | if (!visited[i]) 84 | topologicalSortUtil(i, visited, Stack); 85 | vector v; 86 | while(!Stack.empty()){ 87 | v.emplace_back(Stack.top()); 88 | Stack.pop(); 89 | } 90 | return v; 91 | } 92 | 93 | vector ids; 94 | void dfs(int node, vector &visited){ 95 | visited[node] = true; 96 | ids.emplace_back(node); 97 | for(auto a : adj[node]){ 98 | if(!visited[a]) 99 | dfs(a, visited); 100 | } 101 | } 102 | 103 | void count_subnode(int s, int e) { 104 | nChilds[s] = 1; 105 | for(auto child : adj[s]){ 106 | if(child == e) 107 | continue; 108 | count_subnode(child, s); 109 | nChilds[s] += nChilds[child]; 110 | } 111 | } 112 | 113 | 114 | int main() { 115 | ios::sync_with_stdio(0); 116 | cin.tie(0); cout.tie(0); 117 | 118 | int N = getint(); 119 | int Q = getint(); 120 | adj.resize(N+2); 121 | nChilds.resize(N+2); 122 | vector v(N+2); 123 | 124 | for(int a = 1; a <= N; ++a){ 125 | int val = getint(); 126 | v[a] = val; 127 | } 128 | 129 | for(int a = 0; a < N-1; ++a){ 130 | int parent = getint(); 131 | int child = getint(); 132 | adj[parent].emplace_back(child); 133 | adj[child].emplace_back(parent); 134 | } 135 | 136 | auto ids = topologicalSort(N+1); 137 | cout << endl; 138 | // for(auto a : ids){ 139 | // cout < rev_ids(N+1); 143 | for(int a = 0; a < N; ++a){ 144 | rev_ids[ids[a]] = a+1; 145 | } 146 | // for(auto a : rev_ids){ 147 | // cout < sums(N+2); 155 | for(int a = 1; a <= N; ++a){ 156 | 157 | auto parents = adj[ids[a-1]]; 158 | ll sum = v[ids[a-1]]; 159 | for(auto parent : parents){ 160 | if(sums[parent] == 0) 161 | continue; 162 | 163 | sum += sums[parent]; 164 | break; 165 | } 166 | 167 | // cout << "parent of " << ids[a-1] << " " << parent; 168 | // cout << "\nAdd " << sums[parent] << " " << v[ids[a-1]] << endl; 169 | // auto sum = sums[parent] + v[ids[a-1]]; 170 | BIT.update(a, a, sum); 171 | sums[ids[a-1]] = sum; 172 | } 173 | 174 | for(int a = 0; a < Q; ++a){ 175 | int type = getint(); 176 | if(type == 1){ // change the value of node s to x 177 | int s = getint(); 178 | int x = getint(); 179 | 180 | auto diff = v[s] - x; 181 | v[s] = x; 182 | 183 | auto size = nChilds[s]-1; 184 | s = rev_ids[s]; 185 | 186 | BIT.update(s, s+size, -diff); 187 | 188 | } else { // sum of values cin the subtree of node s 189 | int s = getint(); 190 | 191 | s = rev_ids[s]; 192 | 193 | cout << BIT.query(s, s) << "\n"; 194 | } 195 | } 196 | 197 | } -------------------------------------------------------------------------------- /TreeAlgorithms/PathQueries/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | #define MOD 1000000007 8 | #define PARR(arr) for(auto a : arr) cout << a << " "; 9 | 10 | template T getint() { 11 | T val=0; 12 | char c; 13 | bool neg=false; 14 | while((c=getchar()) && !(c>='0' && c<='9')) { 15 | neg|=c=='-'; 16 | } 17 | do { 18 | val=(val*10)+c-'0'; 19 | } while((c=getchar()) && (c>='0' && c<='9')); 20 | 21 | return val*(neg?-1:1); 22 | } 23 | 24 | #define MAXN 6000010 25 | #define INF 3e18 + 5 26 | typedef long long ll; 27 | // #define numeric_limits::max()-1; 28 | ll tree[MAXN]; 29 | ll lazy[MAXN]; 30 | 31 | int N; 32 | void propaga(int inx, int cnt_num){ 33 | if (lazy[inx]){ 34 | tree[inx] += lazy[inx]*cnt_num; 35 | lazy[inx*2] += lazy[inx]; 36 | lazy[inx*2+1] += lazy[inx]; 37 | lazy[inx] = 0; 38 | } 39 | } 40 | 41 | void update(int tl, int tr, ll val,int l = 0, int r = N+1, int inx = 1){ 42 | propaga(inx,r-l); 43 | // tutto fuori 44 | if (tr <= l || tl >= r || r <= l) 45 | return; 46 | // att dentro target 47 | int cnt_num = r-l; 48 | 49 | if (tl <= l && tr >= r){ 50 | lazy[inx] += val; 51 | propaga(inx,cnt_num); 52 | return; 53 | } 54 | propaga(inx, cnt_num); 55 | int mid = (l+r)/2; 56 | update(tl,tr,val,l,mid,inx*2); 57 | update(tl,tr,val,mid,r,inx*2+1); 58 | tree[inx] = tree[inx*2] + tree[inx*2+1]; 59 | } 60 | 61 | ll query(int tl, int tr, int l = 0, int r = N+1, int inx = 1){ 62 | propaga(inx, r-l); 63 | 64 | // tutto fuori 65 | if (tr <= l || tl >= r) 66 | return 0; 67 | 68 | // att dentro target 69 | if (tl <= l && tr >= r) 70 | return tree[inx]; 71 | 72 | int mid = (l+r)/2; 73 | return query(tl,tr,l,mid,inx*2) + query(tl,tr,mid,r,inx*2+1); 74 | } 75 | 76 | vector> adj; 77 | vector nChilds; 78 | 79 | void topologicalSortUtil(int v, vector &visited, stack &Stack) { 80 | visited[v] = true; 81 | // Recur for all the vertices adjacent to this vertex 82 | for(auto i : adj[v]) 83 | if (!visited[i]) 84 | topologicalSortUtil(i, visited, Stack); 85 | // Push current vertex to stack which stores result 86 | Stack.push(v); 87 | } 88 | 89 | vector topologicalSort(int V){ 90 | stack Stack; 91 | vector visited(V, false); 92 | 93 | // Call the recursive helper function to store Topological 94 | // Sort starting from all vertices one by one 95 | for (int i = 1; i < V; i++) 96 | if (!visited[i]) 97 | topologicalSortUtil(i, visited, Stack); 98 | vector v; 99 | while(!Stack.empty()){ 100 | v.emplace_back(Stack.top()); 101 | Stack.pop(); 102 | } 103 | return v; 104 | } 105 | 106 | vector ids; 107 | void dfs(int node, vector &visited){ 108 | visited[node] = true; 109 | ids.emplace_back(node); 110 | for(auto a : adj[node]){ 111 | if(!visited[a]) 112 | dfs(a, visited); 113 | } 114 | } 115 | 116 | void count_subnode(int s, int e) { 117 | nChilds[s] = 1; 118 | for(auto child : adj[s]){ 119 | if(child == e) 120 | continue; 121 | count_subnode(child, s); 122 | nChilds[s] += nChilds[child]; 123 | } 124 | } 125 | 126 | 127 | int main() { 128 | ios::sync_with_stdio(0); 129 | cin.tie(0); cout.tie(0); 130 | 131 | N = getint(); 132 | int Q = getint(); 133 | adj.resize(N+2); 134 | nChilds.resize(N+2); 135 | vector v(N+2); 136 | 137 | for(int a = 1; a <= N; ++a){ 138 | v[a] = getint(); 139 | } 140 | 141 | for(int a = 0; a < N-1; ++a){ 142 | int parent = getint(); 143 | int child = getint(); 144 | adj[parent].emplace_back(child); 145 | adj[child].emplace_back(parent); 146 | } 147 | 148 | auto ids = topologicalSort(N+1); 149 | 150 | vector rev_ids(N+1); 151 | for(int a = 0; a < N; ++a){ 152 | rev_ids[ids[a]] = a+1; 153 | } 154 | 155 | count_subnode(1, 0); 156 | 157 | vector sums(N+2); 158 | for(int a = 1; a <= N; ++a){ 159 | 160 | auto parents = adj[ids[a-1]]; 161 | ll sum = v[ids[a-1]]; 162 | for(auto parent : parents){ 163 | if(sums[parent] == 0) 164 | continue; 165 | 166 | sum += sums[parent]; 167 | break; 168 | } 169 | 170 | // cout << "parent of " << ids[a-1] << " " << parent; 171 | // cout << "\nAdd " << sums[parent] << " " << v[ids[a-1]] << endl; 172 | // auto sum = sums[parent] + v[ids[a-1]]; 173 | update(a, a+1, sum); 174 | sums[ids[a-1]] = sum; 175 | } 176 | 177 | for(int a = 0; a < Q; ++a){ 178 | int type = getint(); 179 | if(type == 1){ // change the value of node s to x 180 | int s = getint(); 181 | int x = getint(); 182 | 183 | auto diff = v[s] - x; 184 | v[s] = x; 185 | 186 | auto size = nChilds[s]-1; 187 | s = rev_ids[s]; 188 | update(s, s+size+1, -diff); 189 | 190 | } else { // sum of values cin the subtree of node s 191 | int s = getint(); 192 | 193 | s = rev_ids[s]; 194 | cout << query(s, s+1) << "\n"; 195 | } 196 | } 197 | 198 | } -------------------------------------------------------------------------------- /TreeAlgorithms/Subordinates/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/Subordinates/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/Subordinates/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | struct node{ 22 | vector childs; 23 | node* parent; 24 | int nChilds; 25 | int employNumber; 26 | }; 27 | 28 | vector nChilds(300000, 0); 29 | 30 | void count_subnode(node* root, int s, int e) { 31 | nChilds[s] = 1; 32 | for(auto child : root->childs){ 33 | if(child->employNumber == e) 34 | continue; 35 | count_subnode(child, child->employNumber, s); 36 | nChilds[s] += nChilds[child->employNumber]; 37 | } 38 | } 39 | 40 | 41 | bitset<300000> visited; 42 | 43 | void dsf(node* root, int val, int currNode){ 44 | visited.set(root->employNumber); 45 | 46 | if(root->employNumber == val){ 47 | auto new_child = new node{}; 48 | new_child->employNumber = currNode; 49 | root->childs.emplace_back(new_child); 50 | root->nChilds++; 51 | return; 52 | } 53 | 54 | for(auto child : root->childs){ 55 | if(!visited.test(child->employNumber)){ 56 | dsf(child, val, currNode); 57 | } 58 | } 59 | } 60 | 61 | 62 | void check(node* root){ 63 | visited.set(root->employNumber); 64 | cout << root->employNumber << " have " << root->nChilds << endl; 65 | for(auto child : root->childs){ 66 | cout << child->employNumber << " "; 67 | } 68 | cout << endl; 69 | 70 | for(auto child : root->childs){ 71 | if(!visited.test(child->employNumber)){ 72 | check(child); 73 | } 74 | } 75 | } 76 | 77 | 78 | int numberOfChildren(node* root, int x) { 79 | int numChildren = 0; 80 | bool count = false; 81 | queue q; 82 | q.push(root); 83 | 84 | while (!q.empty()) { 85 | auto p = q.front(); 86 | q.pop(); 87 | visited.set(p->employNumber); 88 | 89 | if(count){ 90 | numChildren += p->nChilds; 91 | } 92 | 93 | if (p->employNumber == x && !count) { 94 | // numChildren += p->nChilds; 95 | count = true; 96 | queue empty; 97 | swap(q, empty); 98 | 99 | q.push(p); 100 | continue; 101 | } 102 | 103 | for(auto child : p->childs){ 104 | // if(!visited.test(child->employNumber)) 105 | q.push(child); 106 | } 107 | 108 | } 109 | 110 | return numChildren; 111 | } 112 | 113 | 114 | int main() { 115 | ios::sync_with_stdio(0); 116 | cin.tie(0); cout.tie(0); 117 | 118 | auto root = new node{}; 119 | root->parent = nullptr; 120 | root->employNumber = 1; 121 | 122 | int N = getint(); 123 | for(int a = 0; a < N-1; ++a){ 124 | int val = getint(); 125 | int currNode = a+2; 126 | visited.reset(); 127 | dsf(root, val, currNode); 128 | } 129 | // visited.reset(); 130 | // check(root); 131 | 132 | count_subnode(root, root->employNumber, 0); 133 | 134 | for(int a = 1; a <= N; ++a){ 135 | cout << nChilds[a]-1 << " "; 136 | } 137 | } -------------------------------------------------------------------------------- /TreeAlgorithms/Subordinates/m2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | vector nChilds; 22 | vector> adj; 23 | 24 | void count_subnode(int s, int e) { 25 | nChilds[s] = 1; 26 | for(auto child : adj[s]){ 27 | if(child == e) 28 | continue; 29 | count_subnode(child, s); 30 | nChilds[s] += nChilds[child]; 31 | } 32 | } 33 | 34 | int main() { 35 | ios::sync_with_stdio(0); 36 | cin.tie(0); cout.tie(0); 37 | 38 | int N = getint(); 39 | nChilds.resize(N+3); 40 | adj.resize(N+3); 41 | 42 | for(int a = 0; a < N-1; ++a){ 43 | int val = getint(); 44 | int currNode = a+2; 45 | adj[val].emplace_back(currNode); 46 | } 47 | 48 | count_subnode(1, 0); 49 | 50 | for(int a = 1; a <= N; ++a){ 51 | cout << nChilds[a]-1 << " "; 52 | } 53 | } -------------------------------------------------------------------------------- /TreeAlgorithms/SubtreeQueries/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/SubtreeQueries/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/SubtreeQueries/input.txt: -------------------------------------------------------------------------------- 1 | 10 10 2 | 1 10 3 7 6 4 2 2 5 4 3 | 10 7 4 | 5 3 5 | 8 6 6 | 7 5 7 | 1 5 8 | 6 9 9 | 8 4 10 | 2 4 11 | 3 6 12 | 1 2 4 13 | 2 9 14 | 1 9 5 15 | 2 10 16 | 2 1 17 | 2 10 18 | 1 10 6 19 | 2 10 20 | 2 10 21 | 2 3 22 | -------------------------------------------------------------------------------- /TreeAlgorithms/SubtreeQueries/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | struct FenwickTree { 22 | vector bit; 23 | int n; 24 | 25 | FenwickTree(int n) { 26 | this->n = n; 27 | bit.assign(n, 0); 28 | } 29 | 30 | // FenwickTree(vector a) : FenwickTree(a.size()) { 31 | // for (size_t i = 0; i < a.size(); i++) 32 | // update(i, a[i]); // puo' essere update(i, i, a[i]) 33 | // } 34 | 35 | ll query(int r) { 36 | ll ret = 0; 37 | for (; r >= 0; r = (r & (r + 1)) - 1) 38 | ret += bit[r]; 39 | return ret; 40 | } 41 | 42 | ll query(int l, int r) { 43 | return query(r) - query(l - 1); 44 | } 45 | 46 | void update(int idx, int val) { 47 | for (; idx < n; idx = idx | (idx + 1)) 48 | bit[idx] += val; 49 | } 50 | 51 | void update(int l, int r, int val) { 52 | update(l, val); 53 | update(r + 1, -val); 54 | } 55 | 56 | void print(){ 57 | for(auto a : bit) 58 | cout << a << " "; 59 | cout << endl; 60 | } 61 | }; 62 | 63 | vector> adj; 64 | vector nChilds; 65 | 66 | void topologicalSortUtil(int v, vector &visited, stack &Stack) { 67 | visited[v] = true; 68 | // Recur for all the vertices adjacent to this vertex 69 | for(auto i : adj[v]) 70 | if (!visited[i]) 71 | topologicalSortUtil(i, visited, Stack); 72 | // Push current vertex to stack which stores result 73 | Stack.push(v); 74 | } 75 | 76 | vector topologicalSort(int V){ 77 | stack Stack; 78 | vector visited(V, false); 79 | 80 | // Call the recursive helper function to store Topological 81 | // Sort starting from all vertices one by one 82 | for (int i = 1; i < V; i++) 83 | if (!visited[i]) 84 | topologicalSortUtil(i, visited, Stack); 85 | vector v; 86 | while(!Stack.empty()){ 87 | v.emplace_back(Stack.top()); 88 | Stack.pop(); 89 | } 90 | return v; 91 | } 92 | 93 | vector ids; 94 | void dfs(int node, vector &visited){ 95 | visited[node] = true; 96 | ids.emplace_back(node); 97 | for(auto a : adj[node]){ 98 | if(!visited[a]) 99 | dfs(a, visited); 100 | } 101 | } 102 | 103 | void count_subnode(int s, int e) { 104 | nChilds[s] = 1; 105 | for(auto child : adj[s]){ 106 | if(child == e) 107 | continue; 108 | count_subnode(child, s); 109 | nChilds[s] += nChilds[child]; 110 | } 111 | } 112 | 113 | 114 | int main() { 115 | ios::sync_with_stdio(0); 116 | cin.tie(0); cout.tie(0); 117 | 118 | int N = getint(); 119 | int Q = getint(); 120 | adj.resize(N+2); 121 | nChilds.resize(N+2); 122 | vector v(N+2); 123 | 124 | for(int a = 1; a <= N; ++a){ 125 | int val = getint(); 126 | v[a] = val; 127 | } 128 | 129 | for(int a = 0; a < N-1; ++a){ 130 | int parent = getint(); 131 | int child = getint(); 132 | adj[parent].emplace_back(child); 133 | adj[child].emplace_back(parent); 134 | } 135 | 136 | auto ids = topologicalSort(N+1); 137 | vector rev_ids(N+1); 138 | for(int a = 0; a < N; ++a){ 139 | rev_ids[ids[a]] = a+1; 140 | } 141 | 142 | count_subnode(1, 0); 143 | 144 | auto BIT = FenwickTree(N+2); 145 | for(int a = 1; a <= N; ++a){ 146 | BIT.update(a, v[ids[a-1]]); 147 | } 148 | 149 | for(int a = 0; a < Q; ++a){ 150 | int type = getint(); 151 | if(type == 1){ // change the value of node s to x 152 | int s = getint(); 153 | int x = getint(); 154 | 155 | auto diff = v[s] - x; 156 | v[s] = x; 157 | 158 | s = rev_ids[s]; 159 | 160 | BIT.update(s, -diff); 161 | 162 | } else { // sum of values cin the subtree of node s 163 | int s = getint(); 164 | 165 | auto size = nChilds[s]-1; 166 | 167 | s = rev_ids[s]; 168 | 169 | cout << BIT.query(s, s+size) << "\n"; 170 | } 171 | } 172 | 173 | } -------------------------------------------------------------------------------- /TreeAlgorithms/TreeDiameter/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/TreeDiameter/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/TreeDiameter/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | vector> adj; 22 | int N; 23 | 24 | int bfs(int node, bool second) { 25 | bitset<300000> visited; 26 | vector diameter(N+2); 27 | 28 | queue q; 29 | q.push(node); 30 | 31 | int max_elm = 0; 32 | int max_idx = 0; 33 | 34 | visited.set(node); 35 | while (!q.empty()) { 36 | auto u = q.front(); 37 | q.pop(); 38 | 39 | for(auto child : adj[u]){ 40 | if (!visited.test(child)) { 41 | visited.set(child); 42 | q.push(child); 43 | 44 | diameter[child] += diameter[u] + 1; 45 | if(diameter[child] > max_elm){ 46 | max_elm = diameter[child]; 47 | max_idx = child; 48 | } 49 | } 50 | } 51 | } 52 | 53 | if(second) 54 | return max_elm; 55 | return max_idx; 56 | } 57 | 58 | int main() { 59 | ios::sync_with_stdio(0); 60 | cin.tie(0); cout.tie(0); 61 | 62 | N = getint(); 63 | adj.resize(N+3); 64 | 65 | for(int a = 0; a < N-1; ++a){ 66 | int parent = getint(); 67 | int child = getint(); 68 | adj[parent].emplace_back(child); 69 | adj[child].emplace_back(parent); 70 | } 71 | 72 | /* First, we choose an arbitrary node a in the tree and find the 73 | farthest node b from a. Then, we find the farthest node c from b. The diameter 74 | of the tree is the distance between b and c. 75 | */ 76 | int lastNode = bfs(1, false); 77 | cout << bfs(lastNode, true); 78 | } -------------------------------------------------------------------------------- /TreeAlgorithms/TreeDistances1/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/0x5eba/CSES-solutions/c18b66262f5dbef44b58d793f5185c3d0cf3a77c/TreeAlgorithms/TreeDistances1/a.out -------------------------------------------------------------------------------- /TreeAlgorithms/TreeDistances1/m.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | 7 | template T getint() { 8 | T val=0; 9 | char c; 10 | bool neg=false; 11 | while((c=getchar()) && !(c>='0' && c<='9')) { 12 | neg|=c=='-'; 13 | } 14 | do { 15 | val=(val*10)+c-'0'; 16 | } while((c=getchar()) && (c>='0' && c<='9')); 17 | 18 | return val*(neg?-1:1); 19 | } 20 | 21 | vector> adj; 22 | vector dp1; 23 | vector dp2; 24 | 25 | // find the diameter of the tree using Dynamic Programming 26 | int dfs(int node, int parent) { 27 | int firstmax = -1; 28 | int secondmax = -1; 29 | 30 | // Traverse for all children of node 31 | for (auto i : adj[node]) { 32 | if (i == parent) 33 | continue; 34 | 35 | dfs(i, node); 36 | 37 | // Find first max 38 | if (firstmax == -1) { 39 | firstmax = dp1[i]; 40 | } 41 | else if (dp1[i] >= firstmax) // Secondmaximum 42 | { 43 | secondmax = firstmax; 44 | firstmax = dp1[i]; 45 | } 46 | else if (dp1[i] > secondmax) // Find secondmaximum 47 | secondmax = dp1[i]; 48 | } 49 | 50 | // Base case for every node 51 | dp1[node] = 1; 52 | if (firstmax != -1) // Add 53 | dp1[node] += firstmax; 54 | 55 | // Find dp[2] 56 | if (secondmax != -1) 57 | dp2[node] = 1 + firstmax + secondmax; 58 | 59 | // Return maximum of both 60 | return max(dp1[node], dp2[node]); 61 | } 62 | 63 | 64 | 65 | int main() { 66 | ios::sync_with_stdio(0); 67 | cin.tie(0); cout.tie(0); 68 | 69 | int N = getint(); 70 | adj.resize(N+3); 71 | dp1.resize(N+3); 72 | dp2.resize(N+3); 73 | 74 | for(int a = 0; a < N-1; ++a){ 75 | int parent = getint(); 76 | int child = getint(); 77 | adj[parent].emplace_back(child); 78 | // adj[child].emplace_back(parent); 79 | } 80 | 81 | dfs(1, 0); 82 | 83 | for(auto a : dp1){ 84 | cout << a << " "; 85 | } 86 | cout << endl; 87 | 88 | for(auto a : dp2){ 89 | cout << a << " "; 90 | } 91 | cout << endl; 92 | } -------------------------------------------------------------------------------- /utils.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | #pragma GCC optimize ("O3") 5 | #pragma GCC target ("sse4") 6 | #define PARR(arr) for(auto a : arr) cout << a << " "; 7 | 8 | struct secure_hash { 9 | static uint64_t splitmix64(uint64_t x) { 10 | x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); 11 | } 12 | size_t operator()(uint64_t x) const { 13 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); 14 | } 15 | }; 16 | template using V = vector; 17 | template using umap = unordered_map; // umap m; 18 | template using uset = unordered_set; 19 | template using min_heap = priority_queue, greater>; 20 | template using max_heap = priority_queue; 21 | 22 | #define INF 3e18 + 5 23 | #define MOD 1000000007 24 | 25 | int ad(int a, int b, int mod = MOD) { return (a + b) % mod; } 26 | int sub(int a, int b, int mod = MOD) { return (a - b + mod) % mod; } 27 | int mul(int a, int b, int mod = MOD) { return (long long)a * b % mod; } 28 | 29 | int AD(int &a, int b, int mod = MOD) { return a = ad(a, b, mod); } 30 | int SUB(int &a, int b, int mod = MOD) { return a = sub(a, b, mod); } 31 | int MUL(int &a, int b, int mod = MOD) { return a = mul(a, b, mod); } 32 | 33 | int po(int b, int p, int mod = MOD) { return !p ? 1 : mul(po(mul(b, b, mod), p / 2, mod), p & 1 ? b : 1, mod); } 34 | int inv(int b, int mod = MOD) { return po(b, mod - 2, mod); } 35 | 36 | template T getint() { 37 | T val=0; 38 | char c; 39 | bool neg=false; 40 | while((c=getchar()) && !(c>='0' && c<='9')) { 41 | neg|=c=='-'; 42 | } 43 | do { 44 | val=(val*10)+c-'0'; 45 | } while((c=getchar()) && (c>='0' && c<='9')); 46 | 47 | return val*(neg?-1:1); 48 | } 49 | 50 | int main() { 51 | ios::sync_with_stdio(0); 52 | cin.tie(0); cout.tie(0); 53 | 54 | int N = getint(); 55 | 56 | for(int a = 0; a < N; ++a){ 57 | 58 | } 59 | } -------------------------------------------------------------------------------- /utils2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #pragma GCC optimize ("O3") 4 | #pragma GCC target ("sse4") 5 | #define ll long long 6 | #define vi vector 7 | #define vvi vector> 8 | #define vvii vector>> 9 | #define vvl vector> 10 | #define vvll vector>> 11 | #define FOR(x, N) for(x = 0; x < N; ++x) 12 | #define epsilon 1e-9 13 | #define INF 3e18 + 5 14 | #define MOD 1000000007 15 | template T getint() { 16 | T val=0; 17 | char c; 18 | bool neg=false; 19 | while((c=getchar()) && !(c>='0' && c<='9')) { 20 | neg|=c=='-'; 21 | } 22 | do { 23 | val=(val*10)+c-'0'; 24 | } while((c=getchar()) && (c>='0' && c<='9')); 25 | 26 | return val*(neg?-1:1); 27 | } 28 | template 29 | inline void print(Args&&... args){ 30 | ((cout << args << " "), ...) << "\n"; 31 | } 32 | template 33 | inline void parr(vector arr){ 34 | for(auto a : arr) cout << a << " "; 35 | cout << "\n"; 36 | } 37 | 38 | int Q, N, M, a, b; 39 | 40 | int main() { 41 | ios::sync_with_stdio(0); 42 | cin.tie(0); cout.tie(0); 43 | 44 | ifstream cin("input.txt"); 45 | } --------------------------------------------------------------------------------