description
http://www.lydsy.com/JudgeOnline/problem.php?id=3531
Analysis
- When I first got this problem, I saw that we could change the religion of belief, that is, you can change the path, think it is the topic of the dynamic tree (dynamic tree does not), and later found that all the tree chains are used to create a line segment tree for each religion. That's amazing.
- Religion number <= 10^5, each religion establishes a line segment tree, the line segment tree also generally opens to four times times the space ... According to my usual method of writing line segment tree is certainly not. So from Hzwer's blog to learn the dynamic segment tree, need to access a node to assign a number to it, record the node's left and right sub-node number instead of the left side of the
o<<1
expression O, o<<1^1
representing O's point of view of the method. A little bit of a pointer feeling.
- At first I don't know how to change the religion, see Hzwer found very simple, the node in the first faith of the religious line in the tree of the value of the change to 0, and then in the changed faith in the religious line of the tree to change the value to the original value.
- I tuned this question for a long time until I finally found a most critical error on the query.
To query and for example, I wrote it from the beginning:
int ask_sum(intint y){ int0; while(top[x] != top[y]) { if// top 1, n, tid[top[x]], tid[x]); x = fa[top[x]]; }if(dep[x] > dep[y]) swap(x, y); 1, n, tid[x], tid[y]); return ret;}
- Here C[x] represents the religion of the X-City religion, and when x changes, c[x] is bound to change, but the line tree we are looking for is always constant. So record the root node of the original X segment tree and not use it every time
roots[c[x]]
.
After the change:
int ask_sum(intint y){ int0// here while(top[x] != top[y]) { if// top 1, n, tid[top[x]], tid[x]); x = fa[top[x]]; } if(dep[x] > dep[y]) swap(x, y); 1, n, tid[x], tid[y]); return ret;}
- It is found that vector storage diagram is not a good adjacency table, it takes time and space, so we use array to simulate adjacency table to save it later.
Code
https://code.csdn.net/snippets/611472
Bzoj-3531-Travel