BZOJ-3531-travel

Source: Internet
Author: User

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<<1Represents the left node of o, o<<1^1Indicates 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.