BZOJ-3531-travel
When I got this question, I found that the religion that can change the faith, that is, the path that can be changed, thought it was a question of a dynamic tree (not a dynamic tree), and later I found that the tree chains used were broken down, creates a line tree for each religion. it is amazing. number of religions <= 10 ^ 5, each religion creates a line tree, and the line tree is generally up to four times the space... The method of writing a line segment tree as I normally do not work. as a result, I learned a dynamic line segment tree from the HZWER blog. I need to assign a number to it when I access a node, and record the numbers of the left and right subnodes of the node instead
o<<1
Represents the left node of o,
o<<1^1
Indicates the right node of o. A little pointer. at first, I got stuck in not knowing how to modify the religious belief. Seeing HZWER found that it was very simple to change the value of the node in the line tree of the original religious belief to 0, then, change the value to the original value in the line tree of the religion after the change. I have been calling this question for a long time until I finally find that the most critical error lies in the query.
Taking query and as an example, I wrote this at the beginning:
int ask_sum(int x, int y){ int ret = 0; while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); // top ret += query_sum(roots[c[x]], 1, n, tid[top[x]], tid[x]); x = fa[top[x]]; }if(dep[x] > dep[y]) swap(x, y); ret += query_sum(roots[c[x]], 1, n, tid[x], tid[y]); return ret;}
Here c [x] indicates the religion of the city of x www.bkjia.com. When x changes, c [x] is bound to change, but the line tree we are looking for remains unchanged. therefore, you must record the root node of the original line segment tree of x, instead of using it each time.
roots[c[x]]
To obtain.
After modification:
int ask_sum(int x, int y){ int ret = 0, cx = c[x]; // here while(top[x] != top[y]) { if(dep[top[x]] < dep[top[y]]) swap(x, y); // top ret += query_sum(roots[cx], 1, n, tid[top[x]], tid[x]); x = fa[top[x]]; } if(dep[x] > dep[y]) swap(x, y); ret += query_sum(roots[cx], 1, n, tid[x], tid[y]); return ret;}
It is found that the vector graph does not have a good adjacent table, which requires both time and space. Therefore, we will use an array to simulate the adjacent table.