0% found this document useful (0 votes)
7 views16 pages

PU BruteForceTroop

Uploaded by

imran188566
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

PU BruteForceTroop

Uploaded by

imran188566
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1

Prime University dfs(u, dist + w);


PU_BruteForceBABA }

#Template: # Finding gcd: __gcd(m, n)


# Finding lcm: lcm(m, n)
To compile: g++ -o mycode mycode.cpp # To sorting an array A from index a to b: sort(A + a, A + b + 1)
# To find the lower bound of an in an array A from 0 to n: lower_bound(A, A+n+1, a) - A;
//lower_bound() will give the pointer of the value so we need to subtract A
# Sieve of Eratosthenes
#include "bits/stdc++.h"
const int N = 1e5+9;
using namespace std;
bool primes[N];
void sieveOfEratosthenes() {
//#define int long long
memset(primes, true, N);
#define endl "\n"
primes[1] = false;
#define Faster ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
for (int p = 2; p * p < N; p++) {
#define mem(a, b) memset(a, b ,sizeof(a))
if (primes[p] == true) {
#define len(a) sizeof(a) / sizeof(int)
for (int i = p * p; i < N; i += p)
#define all(x) (x).begin(), (x).end()
primes[i] = false;
}
int32_t main() {
}
Faster;
}

return 0;
#find last 4 bits of a number n: bitset<4>(n);
}
#Comparator func in sort function:
bool cmp(pair < int, int > a, pair < int, int > b) {
if (a.first != b.first) {
#Some Useful functions:
# Find permutaion: next_permutation(arr, arr+4); return a.first < b.first;
# Fill array with value a: memset(arr, a, sizeof(arr)); }
# greater<T>() that changes the functionality of a function. return a.second < b.second;
Exp : sort(vec.begin(),=vec.end(),greater<int>()); greater<int>());
}
# max and min value: INT_MAX, INT_MIN,LLONG_MAX, LLONG_MIN,
# Filling with 0’s at the end to make the size of array power of 2: __builtin_popcount(n) int main() {
while (__builtin_popcount(n) != 1) { sort(all(v), cmp);
arr.push_back(0); }
n++; #Array compression:
} for (int i = 0; i < n; i++) {
int x = input[i];
#Find leading zeros in a number n:__builtin_clz(n); //use __builtinll_clz(n) ->ll if (mymap.find(x) == mymap.end()) { //x not yet compressed
#Find tailing zeros in a number n:__builtin_ctz(n); mymap[x] = assign;
#printing digits after the decimal point: cout << fixed << setprecision(4) << val
printf("Mapping %d with %d\n", x, assign);
# Traversing first and second values of a pair of a node: assign++;
for (auto [u, w] : v[node]) { // here v[node] is denoting a vector }

1
2
x = mymap[x]; if (k == m)
compressed[c++] = x; return i + 1 - m;
} }
return -1;
}
# Rounding:
float round(float var) #LongestIncreasingSubsequence
{ typedef vector<int> VI;
// 37.66666 * 100 =3766.66 typedef pair<int,int> PII;
// 3766.66 + .5 =3767.16 for rounding off value
typedef vector<PII> VPII;
// then type cast to int so value is 3767
// then divided by 100 so the value converted into 37.67
float value = (int)(var * 100 + .5); #define STRICTLY_INCREASNG
return (float)value / 100;
}
VI LongestIncreasingSubsequence(VI v) {
#KMP implementation VPII best;
vector<int> prefix_function(string s) {
VI dad(v.size(), -1);
vector<int> p(s.length());
int k = 0;
for (int i = 0; i < v.size(); i++) {
for (int i = 1; i < s.length(); i++) {
#ifdef STRICTLY_INCREASNG
while (k > 0 && s[k] != s[i])
PII item = make_pair(v[i], 0);
k = p[k - 1];
VPII::iterator it = lower_bound(best.begin(), best.end(), item);
if (s[k] == s[i])
item.second = i;
++k;
#else
p[i] = k;
PII item = make_pair(v[i], i);
}
VPII::iterator it = upper_bound(best.begin(), best.end(), item);
return p;
#endif
}
if (it == best.end()) {
dad[i] = (best.size() == 0 ? -1 : best.back().second);
best.push_back(item);
} else {
int find_substring(string haystack, string needle) { // try to find sub string dad[i] = it == best.begin() ? -1 : prev(it)->second;
needle in haystack *it = item;
int m = needle.length(); }
if (m == 0) }
return 0; VI ret;
vector<int> p = prefix_function(needle); for (int i = best.back().second; i >= 0; i = dad[i])
for (int i = 0, k = 0; i < haystack.length(); i++) { ret.push_back(v[i]);
while (k > 0 && needle[k] != haystack[i]) reverse(ret.begin(), ret.end());
k = p[k - 1]; return ret;
if (needle[k] == haystack[i]) }
++k;
2
3
} else if (color[v] == 1) {
#Basic DFS: return v;
void dfs(int v) { }
// Take action on v after entering the v }
vis[v] = true; color[u] = 2;
for (auto child : adj[v]) { return -1;
if (vis[child]) continue; }
// Take action on child before entering the child node vector<int> find_cycle(const vector<vector<int>> &graph) {
dfs(child); int n = graph.size();
// Take action on child after exiting childe node vector<int> color(n);
} vector<int> next(n);
// Take action on v before exiting the v for (int u = 0; u < n; u++) {
} if (color[u] != 0)
continue;
#Detect Graph Cycle: int cycleStart = dfs(graph, u, color, next);
if (cycleStart != -1) {
vector<int> cycle;
cycle.push_back(cycleStart);
for (int i = next[cycleStart]; i != cycleStart; i = next[i]) {
cycle.push_back(i);
}
cycle.push_back(cycleStart);
return cycle;
}
}
return {};
}
int32_t main() {
Output: 4 IOS;
8 6 7 8 vector<vector<int>> graph{{1}, {2}, {3}, {4}, {5}, {8}, {7}, {8}, {6}, {8}};
int dfs(const vector<vector<int>> &graph, int u, vector<int> &color, vector<int> auto cycle = find_cycle(graph);
&next) { cout << cycle.size() << endl;
color[u] = 1; for (int x : cycle)
for (int v : graph[u]) { cout << x << " ";
next[u] = v; cout << endl;
if (color[v] == 0) { return 0;
int cycleStart = dfs(graph, v, color, next); }
if (cycleStart != -1) {
return cycleStart; #Finding prime factors:
} // returns prime_divisor -> power

3
4
// O(sqrt(n)) complexity
map<long long, int> factorize(long long n) { int binExp(int a, int b) {
map<long long, int> factors; a %= MOD;
for (int d = 2; (long long)d * d <= n; d++) { int result = 1;
while (n % d == 0) { if (a < 0) a += MOD;
++factors[d]; while (b) {
n /= d; if (b&1) result = binMultiply(result, a);
} a = binMultiply(a, a);
} b >>= 1;
if (n > 1) { }
++factors[n]; return result;
} }
return factors;
} # Combination and Permutaion
int main() { Built-in Permutation func: next_permutation(arr, arr + 4)
map<long long int, int> factors = factorize(10 * 24 * 3 * 1000000000039); Built-in Combination func: next_combination(comb, n)
for (auto e : factors) { int main() {
cout << e.first << "^" << e.second << " " << endl; int arr[] = { 1, 2, 3, 4 };
} //sort(arr, arr + 3);
} do {
cout << arr[0] << " " << arr[1] << " " << arr[2] << " " << arr[3] << "\n";

Output: } while (next_permutation(arr, arr + 4));


2^4 }
3^2
5^1 bool next_combination(vector<int> &comb, int n) {
1000000000039^1 int k = comb.size();
for (int i = k - 1; i >= 0; i--) {
#Binary Exponentiation: if (comb[i] < n - k + i) {
const int MOD = 1e9+7; ++comb[i];
int binMultiply(int a, int b) { while (++i < k) {
a %= MOD; comb[i] = comb[i - 1] + 1;
int result = 0; }
if (a < 0) a += MOD; return true;
while (b) { }
if (b&1) result = (result + a) % MOD; }
a = (a + a) % MOD; return false;
b >>= 1; }
} void permute(string str, string out) {
return result; if (str.size() == 0) {
} cout << out << endl;

4
5
return; vector<pair<int, int>> dec(sq);
} for (int i = 1; i <= sq; ++i)
for (int i = 0; i < str.size(); i++) { dec[i - 1] = {pow_mod(g, (long long)i * sq * b % (m - 1), m), i};
permute(str.substr(1), out + str[0]); sort(dec.begin(), dec.end());
rotate(str.begin(), str.begin() + 1, str.end()); for (int i = 0; i < sq; ++i) {
} int my = pow_mod(g, (long long)i * b % (m - 1), m) * (long long)a % m;
} auto it = lower_bound(dec.begin(), dec.end(), make_pair(my, 0));
int main() { if (it != dec.end() && it->first == my) {
ISO; int x = it->second * sq - i;
vector<int> comb{0, 1, 2, 3}; int delta = (m - 1) / __gcd(b, m - 1);
do { return pow_mod(g, x % delta, m);
for (int v : comb) }
cout << v + 1 << " "; }
cout << endl; return -1;
} while (next_combination(comb, 6)); }
// usage example
string str = "ABCD"; int main() {
permute(str, ""); cout << discrete_root(3, 3, 5) << endl;
} }

# return any x such that x^a = b (mod m) #Divisors/factors:


Precondition = m is prime const int N = 1e6+2;
#include <bits/stdc++.h>
vector < int > factors[N];
void divisors(int n) { // it works O(nlogn)
using namespace std;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j+=i)
// https://cp-algorithms.com/algebra/discrete-root.html
factors[j].push_back(i);
}
int pow_mod(int x, int n, int mod) {
int res = 1; # How many smaller than n numbers have which are coprime with n (Euler’s totien
for (long long p = x; n > 0; n >>= 1, p = (p * p) % mod) function):
if ((n & 1) != 0) int phi[N], mark[N];
void sieve_phi(int n) {
for (int i = 1; i <= n; i++) phi[i] = i;
// precondition: m is prime phi[1] = 1;
int discrete_root(int a, int b, int m) { mark[1] = 1;
if (a == 0) for (int i = 2; i <= n; i++) {
return -1; if (!mark[i]) { // if i is prime
int g = generator(m); for (int j = i; j <= n; j+=i) {
int sq = (int)sqrt(m) + 1; mark[j] = 1;

5
6
phi[j] = phi[j] / i * (i - 1); //cout << v << " ";
} cout << "parent node of " << v << " is " << p[v] << endl;
} cout << "distance is " << d[v] << endl;
} q.pop();
} for (int u : adj[v]) {
if (!used[u]) {
#using stack: used[u] = true;
int32_t main() { q.push(u);
ios_base::sync_with_stdio(0); d[u] = d[v] + 1;
cin.tie(0); p[u] = v;
string s; cin >> s; }
int cnt = 0; }
stack<char> a; }
for(int i = 0; s[i]; i++) { }
if(!a.empty() && s[i] == a.top()){
cnt++; #If we have to restore and display the shortest path from the source to some
a.pop(); vertex u, it can be done in the following manner:
} if (!used[u]) {
else{ cout << "No path!";
a.push(s[i]); } else {
} vector<int> path;
} for (int v = u; v != -1; v = p[v])
if(cnt % 2 == 1) cout << "Yes" << endl; path.push_back(v);
else cout << "No" << endl; reverse(path.begin(), path.end());
return 0; cout << "Path: ";
} for (int v : path)
cout << v << " ";
#BFS }
vector< int > adj[100]; // adjacency list representation
vector<bool> used(100); #Some Math tricks
void bfs(int source_node, int nodes) *if we want to find how many prime numbers have in n! Then divide result += n /
{ p^x until n / p ^ x == 0. And x = 1, 2, 3, ..
*Digits have in n! = log(1) + log(2) + …..+ log(n);
queue<int> q;
*if abc a number and a+b+c is divisible by 3 then 3|abc
vector<int> d(nodes), p(nodes);
*last two digits of 2308 is divisible by 4 then 4|2308
// d is path length *a is divisible by both 2 and 3 are divisible then 6|a
// p, which stores for each vertex the vertex from which we reached it *1073 ->
q.push(source_node); ● From the rule stated remove 3 from the number and double it, which becomes 6.
used[source_node] = true;
p[source_node] = -1; ● Remaining number becomes 107, so 107-6 = 101.
● Repeating the process one more time, we have 1 x 2 = 2.
while (!q.empty()) {
● Remaining number 10 – 2 = 8.
int v = q.front();
6
7
● As 8 is not divisible by 7, hence the number 1073 is not divisible by 7.

*If the last three digits of a number are divisible by 8, then the number is
completely divisible by 8
*The rule for divisibility by 9 is similar to the divisibility rule for 3
*if the number is divisible by both 3 and 4, then the number is divisible by 12
exactly
*For any given number, to check if it is divisible by 13, we have to add four
times the last digit of the number to the remaining number and repeat the process
until we get a two-digit number. Now check if that two-digit number is divisible
by 13 or not. If it is divisible, then the given number is divisible by 13.
For example: 2795 → 279 + (5 x 4)
→ 279 + (20)
→ 299
→ 29 + (9 x 4)
→ 29 + 36
→65
Number 65 is divisible by 13, 13 x 5 = 65.

*(a-b) % m = (((a-b) % m) + m) % m
If (((a-b) % m) + m) % m it works slow then use this condition…
int ans = (a-b) % m;
If (ans < 0) ans += m;
*(a/b) % m = ((a%m) * (b^-1)%m) % m

*Xor of two same number is zero.

#Segment Tree:
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
const int N = 1e5 + 9;
ll a[N];
ll t[4 * N];
struct ST{
# #define lc (n * 2)
# 2+4+6+...+2n = n(n+1) #define rc ((n * 2) + 1)
# 1+2+3+....+n = n/2{2a+(n-1)d} or inline void pull(int n){
n/2(a+p) if we know the last term that is p. t[n] = t[lc] + t[rc];
}
inline ll combine(ll a, ll b){
ll c = a + b;
return c;
}

7
8
const int N = 5e5 + 9;
void build(int n, int b, int e){ struct Node
if(b == e){ {
t[n] = a[b]; int one, two, zero;
Return; };
} int a[N];
int mid = (b + e) / 2; struct ST
build(lc, b, mid); {
build(rc, mid+1, e); #define lc (n << 1)
pull(n); #define rc ((n << 1) + 1)
} Node t[4 * N];
void update(int n, int b, int e, int i, ll x){ long long lazy[4 * N];
if(e<i || i<b) return; ST()
if(i==b && e==i){ {
t[n] = x; memset(t, 0, sizeof t);
Return; memset(lazy, 0, sizeof lazy);
} }
int mid = (b + e) / 2; inline void push(int n, int b, int e) // change this
update(lc, b, mid, i, x); {
update(rc, mid+1, e, i, x); if (lazy[n] == 0) return;
pull(n); if(lazy[n] == 1)
} {
ll query(int n, int b, int e, int i, int j){ int one = t[n].zero;
if(e<i || j<b) return 0; int two = t[n].one;
if(i<=b && e<=j) return t[n]; int zero = t[n].two;
int mid = (b + e) / 2; t[n].one = one;
ll temp = combine(query(lc, b, mid, i, j),query(rc, mid+1, e, i, j)); t[n].two = two;
return temp; t[n].zero = zero;
} }
}s; else if(lazy[n] == 2)
int main(){ {
int n, m; cin >> n >>m; int one = t[n].two;
for(int i = 1; i <= n; i++) cin >> a[i]; int two = t[n].zero;
s.build(1, 1, n); int zero = t[n].one;
while(m--){ t[n].one = one;
int sign, l, r; t[n].two = two;
cin >> sign >> l >> r; t[n].zero = zero;
l++; }
if(sign == 1) s.update(1, 1, n, l, r); if (b != e)
else cout<< s. query(1, 1, n, l, r)<<endl; {
} lazy[lc] = (lazy[lc] + lazy[n]) % 3;
// cout<< s. query(1, 1, n, 1, 3) << endl; lazy[rc] = (lazy[rc] + lazy[n]) % 3;
} }
lazy[n] = 0;
#Structure: }
#include<bits/stdc++.h> inline Node combine(Node a,Node b) // change this
using namespace std; {

8
9
Node c; c.one = 0;
c.one = a.one + b.one; c.two = 0;
c.two = a.two + b.two; c.zero = 0;
c.zero = a.zero + b.zero; return c;
return c; }; //return null
} if (i <= b && e <= j) return t[n];
inline void pull(int n) // change this int mid = (b + e) >> 1;
{ return combine(query(lc, b, mid, i, j), query(rc, mid + 1, e, i, j));
t[n].one = t[lc].one + t[rc].one; }
t[n].two = t[lc].two + t[rc].two; } t;
t[n].zero = t[lc].zero + t[rc].zero; void solve()
} {
int n, q;
void build(int n, int b, int e) cin >> n >> q;
{ t.build(1, 1, n);
lazy[n] = 0; // change this while(q--)
if (b == e) {
{ int sign, l, r;
t[n]= {0, 0, 1}; cin >> sign >> l >> r;
return; if(sign == 0)
} t.upd(1, 1, n, ++l, ++r, 1);
int mid = (b + e) >> 1; else
build(lc, b, mid); {
build(rc, mid + 1, e); Node x = t.query(1, 1, n, ++l, ++r);
pull(n); cout << x.zero << endl;
} }
void upd(int n, int b, int e, int i, int j, long long v) }
{ }
push(n, b, e); int32_t main()
if (j < b || e < i) return; {
if (i <= b && e <= j) int t;
{ cin >> t;
lazy[n] = (lazy[n] + v) % 3; //set lazy for(int test = 1; test <= t; test++)
push(n, b, e); {
return; cout << "Case " << test <<":\n";
} solve();
int mid = (b + e) >> 1; }
upd(lc, b, mid, i, j, v); }
upd(rc, mid + 1, e, i, j, v);
pull(n);
Vector
● To resize vector : vec.resize(a); // {0, 0, 0,...}
}
● To assign value in a vector : for (auto &e : vec) cin >> e;
Node query(int n, int b, int e, int i, int j)
● To find out upper bound of value a : upper_bound(v.begin(), v.end(), a) –
{ v.begin();
push(n, b, e); ● To find out lower bound of value a : lower_bound(v.begin(), v.end(), a) –
if (i > e || b > j) v.begin();
{ ● 2D vector : vector < vector < int > > v;
Node c; ● To remove a element from the end : vec.pop_back();

9
10
● To add a value v in index i : vec.insert(vec.begin()+i, v); 2. find_by_order(k)
● To remove a value v in index i : vec.erase(vec.begin()+i); Modified Template:
● To remove all value from vector : vec.clear();
● Passing vector to a function with reference: func(vector<int> &vec) {code…};
● To add new values replacing olds: vec.assign(int size, int value);
● Declaring an array of vectors: vector< ll > v[N]; template <typename T>
● Clearing array of vectors: for (auto& u : v) u.clear(); class orderset {
● Assigning a value v to a vector: vector<int> vec(n, v); public:
● To find a value in a vector : void insert(T x) {
vector < int > :: iterator it = find(v.begin(), v.end(), val);
if (it != v.end()) //have if (!binary_search(x)) {
● To erase duplicate numbers in vector: vals.erase(unique(vals.begin(), auto it = lower_bound(x);
vals.end()), vals.end()); v.insert(it, x);
s.insert(x);
Set }
# Compair Funtion: }
struct DescendingOrder {
bool operator()(const pair < int, int >& a, const pair < int, int >& b) const {
void erase(T x) {
return a > b;
if (binary_search(x)) {
}
auto it = lower_bound(x);
};
v.erase(it);
struct node {
s.erase(x);
set < pair < int, int >, DescendingOrder > st;
}
};
}

a. To iterate the value of a set s : for(auto i=s.begin(); i!=s.end(); i++) cout << *i << ‘ T& operator[](int i) {
‘; return v[i];
To find the upper bound or lower bound }
auto it = s.upper_bound(2);
cout << *it << endl; int size() const {
Copy of a set to a Vector: return v.size();
vector < int > v(all(s)); }

Ordered Set
bool empty() const {
● combines the features of a set and a vector.
#include <ext/pb_ds/assoc_container.hpp> return v.empty();
#include <ext/pb_ds/tree_policy.hpp> }
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, typename std::vector<T>::iterator begin() {
rb_tree_tag,tree_order_statistics_node_update> return v.begin();
#define ordered_set tree<pair<int, int>, null_type,less<pair<int, int>>, }
rb_tree_tag,tree_order_statistics_node_update> // for pair
1. order_of_key (k) typename std::vector<T>::iterator end() {

10
11
return v.end();
} // Check if a key is present
if (myMap.count("Charlie")) {
private: cout << "Charlie is in the map" << std::endl;
std::vector<T> v; }
std::set<T> s;
// Update a value
bool binary_search(T x) const { myMap["Bob"] = 31;
return s.find(x) != s.end();
} // Iterate over the key-value pairs in the map
cout << "All key-value pairs in the map:" << std::endl;
typename std::vector<T>::iterator lower_bound(T x) { for (const auto& kv : myMap) {
return std::lower_bound(v.begin(), v.end(), x); cout << kv.first << ": " << kv.second << std::endl;
} }
};
return 0;
Unordered Map }
// Iterating the map and
// printing unordered values Map
for (auto i = order.begin();i != order.end(); i++) #include "bits/stdc++.h"
{
cout << i->first << " : " << i->second << '\n'; using namespace std;

} int main()
{
#include "bits/stdc++.h" // Initialize a map with some key-value pairs
map<string, int> myMap = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 35}};
using namespace std;
// Access the value associated with a key
int main() { cout << "Alice's age is " << myMap["Alice"] << endl;
// Create an unordered_map with string keys and int values
std::unordered_map<std::string, int> myMap; // Insert a new key-value pair into the map
myMap.insert({"Dave", 40});
// Insert some key-value pairs
myMap.insert({"Alice", 25}); // Print all key-value pairs in the map
myMap.insert({"Bob", 30}); for (auto it = myMap.begin(); it != myMap.end(); it++)
myMap.insert({"Charlie", 35}); {
cout << it->first << " is " << it->second << " years old." << endl;
// Access values using their keys }
cout << "Alice's age is " << myMap["Alice"] << std::endl;

11
12
// Check if a key exists in the map
string name = "Charlie";
if (myMap.count(name) > 0)
{
cout << name << " is in the map." << endl;
}
else
{
cout << name << " is not in the map." << endl;
}

// Erase a key-value pair from the map


myMap.erase("Bob");

// Print all key-value pairs in the map again


for (auto [name, age] : myMap)
{
cout << name << " is " << age << " years old." << endl;
}

if (myMap.find("kiwi") == myMap.end())
{
std::cout << "kiwi not found" << std::endl;
}

// This function is used to remove all elements from the map.


myMap.clear();

return 0;
}

12
13

// Print the number of elements in the set


cout << "Number of elements: " << s.size() << endl;

// Find the order statistics of some values


cout << "Order statistics: " << endl;
cout << "The rank of 5 is " << s.order_of_key(5) << endl;
#include <ext/pb_ds/assoc_container.hpp> cout << "The rank of 9 is " << s.order_of_key(9) << endl;
#include <ext/pb_ds/tree_policy.hpp> cout << "The element at rank 2 is " << *s.find_by_order(2) << endl;
#include <iostream>
using namespace std;
using namespace __gnu_pbds; // Remove an element from the set
typedef tree<int, null_type, less<int>, rb_tree_tag, s.erase(5);
tree_order_statistics_node_update> indexed_set;

//typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, // Print the remaining elements in the set
tree_order_statistics_node_update> indexed_set; cout << "Set elements after erasing 5: ";
for (auto x : s) {
int main() { cout << x << " ";
// Define an indexed set with some initial elements }
indexed_set s; cout << endl;
s.insert(3);
s.insert(1);
s.insert(4); return 0;
s.insert(1); }
s.insert(5);
s.insert(9);
s.insert(2); #include <bits/stdc++.h>
s.insert(6); #include <ext/pb_ds/assoc_container.hpp>
s.insert(5); #include <ext/pb_ds/tree_policy.hpp>

using namespace std;


// Print the elements in the set using namespace __gnu_pbds;
cout << "Set elements: ";
for (auto x : s) { typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
cout << x << " "; tree_order_statistics_node_update> indexed_set;
}
cout << endl; int main() {
indexed_set s;

13
14
s.insert({1, 2}); };
s.insert({2, 3});
s.insert({3, 4});
int main()
cout << s.order_of_key({2, 0}) << endl; // 1 {
cout << s.find_by_order(1)->second << endl; // 3 // i want to sort in descending order then use
std::multiset<int, GreaterInt> numbers;
return 0;
} // Insert elements into the multiset
numbers.insert(10);
numbers.insert(20);
numbers.insert(30);
numbers.insert(10);
numbers.insert(40);

// Iterate over the multiset and print its elements


cout << "Multiset elements: ";
for (int n : numbers)
{
cout << n << " ";
}
cout << std::endl;

// Count the number of occurrences of an element in the multiset


int count = numbers.count(10);
cout << "Number of occurrences of 10: " << count << std::endl;

// Search for an element in the multiset and erase it


std::multiset<int>::iterator it = numbers.find(20);
if (it != numbers.end())
{
#include <iostream> numbers.erase(it);
#include <set> }

using namespace std; // Iterate over the multiset and print its elements again
cout << "Multiset elements after erasing 20: ";
struct GreaterInt { for (int n : numbers)
bool operator()(int a, int b) const { {
return a > b; cout << n << " ";
} }

14
15
cout << std::endl;
#String:
// Find the smallest and largest elements in the multiset a.To sort a String lexicographically: sort(string.begin(), string.end(), greater<char>());
b.string.emtpy() returns true if string is empty.
int smallest = *numbers.begin();
c.Take last character of a string: string.back();
int largest = *std::prev(numbers.end());
d.Adding a character in last to a string: string.push_back(char);
e.Removing last character of a string: string.pop_back();
cout << "Smallest element: " << smallest << std::endl;
f.Find the first position of a character of a substring: string.find();
cout << "Largest element: " << largest << std::endl; g.Substring of a string: string.substr(x,y);
H.take string as input with white spaces: getline(cin, s);
return 0;
} h.To compare two string in many way:

i.To swap a string with another string: string1.swap(string2);


j.To insert a string in another string in position ith index: str1.insert(i, str2, x, n); where x
begins index of str2 and n is character number.
k.To replace of a string from index x up to n1 characters with a substring of another string from index y
up to n2: str1.replace(x, n1, str2, y, n2);
l.To add a string in the last: str1.append(str2, x, n);
m.Find length of a string: str.length();

1. string::size(): Returns the number of characters in the string.


2. string::substr(start, length): Returns a new string that is a substring of
the original string, starting at the start index and including length
characters.

15
16
3. string::find(str): Searches for the first occurrence of the specified
substring str in the string, and returns the index of the first character
of the match.
4. string::replace(start, length, str): Replaces length characters in the
string, starting at the start index, with the specified string str.
5. string::insert(index, str): Inserts the specified string str into the
string at the specified index.
6. string::erase(start, length): Removes length characters from the string,
starting at the start index.
7. string::compare(str): Compares the string to the specified string str, and
returns 0 if they are equal, a negative value if the first string is
lexicographically smaller, and a positive value if the first string is
lexicographically larger.
8. string::at(index): Returns the character at the specified index.
9. string::back(): Returns the last character in the string.
10. string::front(): Returns the first character in the string.
11. string::empty(): Returns true if the string is empty, and false otherwise.
12. string::clear(): Removes all characters from the string.
13. string::c_str(): Returns a C-style string (i.e., a null-terminated
character array) that is equivalent to the string.
14. string::push_back(ch): Appends the specified character ch to the end of
the string.
15. string::pop_back(): Removes the last character from the string.
16. getline(cin, str): Reads a line of input from the standard input stream
(cin) and stores it in the specified string str.
17. stoi(str): Converts the string str to an integer value.
18. stol(str): Converts the string str to a long integer value.
19. stoll(str): Converts the string str to a long long integer value.
20. stof(str): Converts the string str to a float value.
21. stod(str): Converts the string str to a double value.
22. to_string(val): Converts the specified value val to a string.

16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy