Winter vacation training notes

Posted by outsidaz on Wed, 26 Jan 2022 23:51:33 +0100

Precautions (incomplete):
(1)i-type sieve for prime numbers (use bool type array, the large number int will explode MLE)
(2) for (int j = I; J < = n / I; j + +) do not multiply what can be divided (i*j may explode int)
(3) don't forget long long
1, Memset (4 - > 600 million)
  1.int
0x7f a large number (slightly less than 0x7fffff)
0xaf a very small number
  2.double
127 is a big number
3. Hex: 0xff
  4. unsigned int
Ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa very large number
2, Shortest circuit (no shortest circuit in negative weight circuit)
  1.Floyd
The simplest shortest path algorithm
The shortest path between any two points in the graph can be calculated
The time complexity of Floyd algorithm is n^3
It is suitable for the case of negative weight edge
The code implementation is the simplest
  2.Dijstra
Time stable algorithm for calculating the shortest path from one point to all other points
It cannot be used to calculate graphs with negative weight, and heap optimization is generally used
  3.Bellman ford
It can handle the shortest path with negative weight edge
However, the shortest circuit with negative ring cannot be handled (there is no shortest circuit at this time)
Due to the high complexity, it is generally not recommended
  4.Spfa
Complexity O (metaphysics)
It is generally used when the dijstra algorithm of actual heap optimization cannot pass
It may be faster or slower and can be stuck to O(n*m)
/ / PS: heap optimization stores nodes, not edges; Don't forget to take CS [i] when using adjacent linked list to
3, Joint query set (tree data structure)
  1. operation
(1) initialization
    for(int i = 1;i <= n;i++) father[i] = i;
(2) query: find the root node
(3) path compression

1 (Find and compress paths)
2 int find(int x){
3   if(father[x] != x) father[x] = find(father[x]);
4   return father[x];
5 }

(4) merge: the set of two elements is merged into one set

1 void unionn(int x,int y){
2   x = find(x);
3   y = find(y);
4   father[y] = x;
5 }

  2. Use the root node of the tree where an element is located to represent the collection where the point is located
  3. Find father for (int i = 1; I < = n; I + +) if (find (I) = = I) sum + +;
4, Discretization
  1.map

1 #include<map>
2 map<string,int> mp;
3 string a;
4 cin >> a;
5 mp[a] = 0;
6 mp["Hello"] = 50;//mapping
7 mp.find(a);//lookup
8 mp.clear()//empty

  2. STL (example: automatic program analysis)
5, Minimum spanning tree
  1.Prim
min[v] represents the minimum edge weight of the blue dot V connected to the white dot
  2. Kruskal
Using parallel search set to achieve
6, vector

 1 #include <vector>
 2 vector<int> a;
 3 push_back Add a data at the end of the array
 4 pop_back Remove the last data of the array
 5 at Get the data of the number position
 6 begin Get the pointer to the array header
 7 end Get the last cell of the array+1 Pointer to
 8 front Get the reference of array header
 9 back Get the reference of the last cell of the array
10 max_size obtain vector How big can it be
11 capacity current vector Allocated size
12 size Size of data currently in use
13 resize Change the size of the currently used data,If it is larger than the one currently in use,Default values for the filler
14 reserve Change current vecotr Size of allocated space
15 erase Delete the data item pointed to by the pointer
16 clear Clear current vector
17 rbegin take vector The inverted start pointer returns(It's actually the original end-1)
18 rend take vector Returns the end pointer of the inversion construct(It's actually the original begin-1)
19 empty judge vector Is it empty
20 swap With another vector Exchange data

7, Tree array
(single point modification, interval query - > ordinary tree array)
(single point query, interval modification - > difference)
  1. The binary of negative number is the complement of inverse code

  Original code Complement Inverse code
1 0000 0001 0000 0001

0000 0001

-1 1000 0001 1111 1110 1111 1111

 

 

 

 

 

  2. X & (- x) returns the position of the last 1 after X is converted to binary

  3. Construction method

(1) the elements of each tree array are formed by adding 2^k initial array elements

(2) the subscript of the parent node of a node i in the tree array is i+lowbit(i)
(3) the subscript of the sibling (left) of a node i in the tree array is i-lowbit(i)

1 int lowbit(int x){
2     return x & (-x);
3 }

  4. Single point modification (time complexity O(lgn))
To add a val value to an element of the original array
The points of all tree arrays involving this point should be modified
That is, go up along the parent node to the rightmost end of the interval

1 void update(int x,int val){
2     while(x <= n){
3         c[x] += val;
4         x += lowbit(x);
5     }
6 }

  5. Query prefix and (time complexity O(lgn))
For the interval of inquiry, start from the right endpoint
The tree array value of the brothers moved to the left each time
That is, each subscript subtracts the lowbit value

1 int query(int x){
2     int sum = 0;
3     while(x){
4         sum += c[x];
5         x -= lowbit(x);
6     }
7 }

  6. Interval query

1 int getsum(int x){
2     int s = 0;
3     while(x > 0){
4         s += c[x];
5         x -= lowbit(x);
6     }
7     return s;
8 }

  7. Optimization (card constant tips)
(1) add inline before non recursive function to optimize the reading speed when calling repeatedly
(2) fast reading and fast input

 1 //Read quickly
 2 inline int read(){
 3     int x = 0,f = 1;
 4     char c = getchar();
 5     while(c > '9'||c < '0'){
 6         if(c == '-') f = -1;
 7         c = getchar();
 8     }
 9     while(c >= '0'&&c <= '9'){
10         x = (x << 3) + (x << 1) + (c ^ 48);
11         c = getchar();
12     }
13     return x*f;
14 }

  8. Difference
The difference between each number and its previous number is used to build a tree array
The number originally represented by a position is the sum of the interval from the beginning to this position
Add the interval [l,r] to x
Only d[l] += x;d[r+1] -= x;
8, Two dimensional tree array
  1. law
Whether abscissa or ordinate
Take them out separately, and they all meet x += lowbit(x)
  2. modify

1 void updata(int x,int y,int key){
2     for(int i = x;i <= n;i += lowbit(i)){//Pay attention to the end position,n still maxn,maxn May be less than n,Cannot modify all
3         for(int j = y;j <= n;j += lowbit(j))
4         c[i][j] += key;
5     }
6 } 

  3. query

1 int sum(int x,int y){
2   int result = 0;
3   for(int i = x;i > 0;i -= lowbit(i)){
4     for(int j = y;j > 0;j -= lowbit(j))
5       result += c[i][j];
6   }
7   return result;
8 }

  4. 2D prefix and

1           x1,y2
2 ________________ ____________ x2,y2
3 |                 |
4 |                 |
5 |_______________x1,y1     |x2,y1
6 |         |       |
7 |         |       |
8 |         |        |
9 |_______________|____________|

Sum in rectangle:
  query(x2,y2)-query(x1-1,y2)-query(x2,y1-1)+query(x1-1,y1-1);
9, XOR
Properties: satisfy commutative law and associative law
Commutative law: A ^ B = B ^ A;
Binding law: A ^ (B ^ C) = (A ^ B) ^ C;
Identity Law: X ^ 0 = X;
Zeroing Law: X ^ X = 0;
Reflexivity: A ^ B ^ B = A ^ 0 = A;
For any X:X ^ (-1) = -X-1;
If A ^ B = C holds, then A ^ B = C,B ^ C = A;