DAY 3
data structure
1. Heap
Priority_queue large root heap
Priority_queue<int , vector<int> , greater<int> > 小根堆
Support insert a value, delete the Max / Min
It overloads the operator or function class
Sort P1090 merger fruit heap Huffman
2.LCA (common ancestor)
Limited to trees, doubling Dafa is good.
We have a tree, the ancestors of the definition of a point to this point to all points on the path root
For point A, B, they have some common ancestor
Once the LCA find all of their common ancestor can be found, LCA must be the deepest common ancestor
Uses : Shortest find any two points, AàLCA + BàLCA
step:
- If the depth is less than the depth B of A, put them exchange (just for ease of handling, deep jump)
If( depA<depB) swap(A,B)
2. A, B transferred to the same depth
3. A, B while raising until the A = B, find LCA
Complexity is O (dep), but if your tree is a chain, it might as well not
Consider optimization
P [x] [i], x up of 2 ^ i ancestor
P[x][i]= P[P[x][i-1]][i-1]
(1) Optimization of: A, B transferred to the same depth
We found that deep difference between A and B
Such as d [A] -d [B] = 19
Two decomposition system 19
19=10011
AàP[A][4] 16
AàP[A][1] 2
AàP[A][0] 1
(2) Optimization: A, B while raising
Beginning ancestor A, B has not the same, until a certain point, on the back are the same as the
We not determine the earliest point in the same ancestors, but we can find the last two are not the same ancestor of x, y, then the time, their father would be LCA
For(i=16~0)
If(f[A][i]!=f[B][i])
A, B while the jump 2 ^ i
Complexity of O (logn)
LCA applications:
The difference can process information tree
A tree is used to find the shortest path B: Aàgen + Bàgen - 2 * LCAàgen
3.ST table
Only the maximum and minimum range queries, not to modify, static
Defined mx [i] [j] is maximum iài + 2 ^ j -1 of
If the maximum required interval [L, R] of
For example the interval [1946]
First seek length interval 46-19 + 1 = 28
It found that it can be covered with two 16
PS: seeking overlap interval maximum value does not affect
Covering the interval length P = Floor (log2 (L))
The [L, R] is split into two intervals [L, P], and [R-2 ^ P, R], were treated in two halves
For example, recursive solution max
st[i][j] = max(st[i][j-1], st[i + (1 << j-1)][j-1])
4.HASH
It is a function
HASH is usually say: Design an F (string) à int function, that is, into a digital string,
map function based on a comparison of red-black tree
If you put the string in the map, the complexity of comparing two strings O (string length)
So to develop a new approach
Suppose you give a string ababb, 1 represents a, 0 b represents
10100
ababb
Binary memories beginner, calculated in a similar manner a value
b*2^0+b*2^1+a*2^2+b*2^3+a*2^4
HASH allow the conflict, we must try to avoid conflict, rather than radical conflict
Give you a garbled yy1926
We now construct HASH
First determine P few hexadecimal, a large prime number, is larger than the set of strings
Then calculate its value is 6 * p ^ 0 + 2 * p ^ 1 + 9 * p ^ 2 + 1 * p ^ 3 + y * p ^ 4 + y * p ^ 5
Earlier figures also good, direct int calculated, then for the characters? ?
Whichever is the ASCII code, mod 998244353 (a large prime number)
For convenience, however, it can directly open unsigned ll
Unsigned ll range of 0 to 2 ^ 64-1
You surprisingly found that 2 ^ 64-1 is a prime
NATURAL allow overflow, overflow on a modulo Nature
Unsigned ll with memory, it is calculated by, a modulo operation to get the province
To avoid conflicts, you can modulo two numbers
How to calculate substring HASH hash?
We want to design an algorithm that meets at least delete the string concatenation
For example, now we have a string damengshen and p
d d
d * p ^ 1 + i
of d * p ^ i * p ^ 2 + 1 + n
ding d*p^3+i*p^2+n*p^1+g
If you require
ing is the hash hash [ding] - hash [d] * p ^ 3
ng is the hash hash [ding] - hash [d] * p ^ 2
We maintain a string of hash values for each prefix
Found difficult to determine the index p, in fact, the length of p is the difference between the index of the string
Order H [i] represents 1 ~ hash value of i
h[i]=h[i-1]*p+s[i]
hash[i][j]=h[j] - h[i-1]*p^(j-i+1)
5. disjoint-set
No need to keep the tree structure, a father can point directly to its representative source
Path compression
- int father(int x){
- return fa[x]==x? x: fa[x]=father(fa[x]);
- }
Father [x] à denotes a source
Disjoint-set P3367 [template] disjoint-set
6. Fenwick tree
int lowbit(int x){ return x&(-x); } void modify(int x,int y){ // add y to a[x] for(int i=x;i<=n;i+=lowbit(i)) c[i]+=y; } int query(int x){ // sum of a[1]...a[x] int ret=0; for(int i=x;i;i-=lowbit(i)) ret+=c[i]; return ret; } int query(int l,int r){ return query(r)-query(l-1); }
7. segment tree
example:
1. P4281 [AHOI2008] emergency collection / gathering
- Seeking three nodes to a node and the minimum distance and the distance of the node and
- Obtaining twenty-two LCA, wherein there are two identical, compared with the other answer, it will be appreciated that FIG paint
2. P1168 median
For a given number of columns
We maintain two stacks
Large piles of top maintenance roots median
Consider two numbers each time put into the heap, smaller than the large piles of root left in the top of the heap, large roots into small stacks
Once the reactor explosion, and put large piles of top root root into small heap ah
K-ary Huffman
With a heap maintenance
4. P3101 [USACO14JAN] for skiers
You consider all the points taken out, and even around the edge point, the right side is the altitude difference between these two points, then consider a sequence row
Removing individual edges, the edge is not even, then added to a Wang Libian a rim, a merger of the two set points, when there is a set number of edges> = T, it is determined that the level of the entire set of
5. The P5043 [template] Tree isomorphism ([BJOI2015] homogeneous tree)
For each point of the root, seek hash
If the two hash set the same, then they are isomorphic
- Tree HASH, if two hash set the same, then they isomorphic
- For an unrooted trees, the number of its center of gravity does not exceed 2.
- Each center of gravity enumeration to determine the center of gravity as the root tree rooted tree represent the minimum, maximum and then to take lexicographically.
- Represents the minimum rooted tree, the sequence can be seen as brackets, each bracket according to the sequence of the sub-tree lexicographically sorted order to link up.
Data structures subject Awards (do not pile title)