Bzoj 1095 hide-and-seek (line segment tree)

Source: Internet
Author: User
Digression recently, the course is not very tight. We plan to switch to the BZ version based on the AC rate and strive for more than one question per day. Then I would like to hear that the rest of the questions are basically data structures> _ <. It hurts... Description given a tree, each node is either black or white. Two operations can be performed: to reverse a certain vertex, return the farthest Black Point. Solution this question seems to be a chain of sub-governance, edge sub-governance can be done, and then found the problem of the island. I found a more advanced approach, and read Cao Qinxiang's "Data Structure Extraction and compression", which is broken... This topic uses the nature of the DFS sequence, that is, the sequence of parentheses ..

Defines a bracket code for a tree. This encoding method is intuitive. Therefore, we do not provide a strict definition here. Use the following tree as an example:
(Picture brain supplement ...)
You can traverse the data in sequence and write it as follows: [A [B [E] [f [H] [I] [C] [d [g]
Remove the letter string: [[[] [] [] [] [] [] []
It is called the brackets of this tree. (This encoding is essentially obtained through depth-first traversal)
Test two nodes, such as E and G,
Extract the brackets between them:] [[] [] [] [] [] [
Remove the matching brackets and get:] [[
We can see two] and two [, that is to say, in the tree, we climb two steps from E to G, and then go two steps down to G.
Note that the information required in the question is only the distance between the midpoint and the vertex of the tree. Therefore, the matching brackets in the storage code are meaningless.
Therefore, for a bracket encoded in s between two nodes, you can use a Binary Group (a, B) to describe it, that is to say, after removing the matching brackets, there will be a] and B [.
Therefore, for the two vertices PQ, if the Code s between two vertices PQ can be expressed as (A, B), and the distance between PQ is a + B.
That is to say, the question only needs to be dynamically maintained: max {A + B | s '(a, B) is a substring of S, and s' is between two black spots },
Here s is the brackets of the entire tree. We record this amount as DIS (s ).
Now, if we can get the full-segment encoding statistics through the left half of the statistics and the right half of the statistics, we can solve this problem with the familiar line segment tree.
This requires the following analysis.
Consider the two parentheses encoded S1 (A1, B1) and S2 (A2, B2) if they are connected to Form S (A, B ).
Note that the pairs of minutes {B, c} are formed when S1 and S2 are connected, and they will be offset after merging. (?.. Here, B and c should refer to B1 and A2 respectively...
Therefore:
When A2 <B1, the first segment [is deleted, and the two segments] are connected together. For example:
] [[+] [[=] [[
When A2> = b1, the second segment is completely deleted. The two segments are [connected together, for example:
] [[[+] [[=] [[[(? .. Reversed ?...
In this way, a very useful conclusion is obtained:
When A2 <B1, (a, B) = (a1-b1 + A2, B2 ),
When A2> = b1, (a, B) = (A1, b1-a2 + B2 ).
As a result, several simple inferences are obtained:
(I) A + B = A1 + B2 + | a2-b1 | = max {(a1-b1) + (A2 + b2), (A1 + B1) + (b2-a2 )}
(Ii) a-B = a1-b1 + a2-b2
(Iii) B-A = b2-a2 + b1-a1
In the (I) type, we can find that to maintain DIS (s), we must maintain the following four volumes of sub-strings:
Right_plus: max {A + B | s' (A, B) is a suffix of S, and S' is immediately after a black spot}
Right_minus: max {a-B | s' (A, B) is a suffix of S, and S' is immediately after a black spot}
Left_plus: max {A + B | s' (A, B) is a prefix of S, and there is a black spot following s}
Left_minus: max {B-A | s '(a, B) is a prefix of S, and there is a black spot following s}
In this way, for S = S1 + S2, where S1 (a, B), S2 (c, d), S (E, F), there is
(E, f) = B <C? (A-B + C, D): (A, B-C + D)
DIS (S) = max {DIS (S1), left_minus (S2) + right_plus (S1), left_plus (S2) + right_minus (S1), DIS (S2 )}
Is it enough to add these four parameters?
Yes, because:
Right_plus (S) = max {right_plus (S1)-C + D, right_minus (S1) + C + D, right_plus (S2 )}
Right_minus (S) = max {right_minus (S1) + c-d, right_minus (S2 )}
Left_plus (S) = max {left_plus (S2)-B + A, left_minus (S2) + B + A, left_plus (S1 )}
Left_minus (S) = max {left_minus (S2) + B-a, left_minus (S1 )}
In this way, you can use the line segment tree to process the encoded string. In actual implementation, adding the node label to the encoding string is more convenient. For the underlying node, if the corresponding character is a bracket or a white point, the values of right_plus, right_minus, left_plus, left_minus, and DIS are all-maxlongint. If the corresponding characters are black spots, the values of right_plus, right_minus, left_plus, dis is-maxlongint.
Now, this question has been successfully solved. Looking back on this process, we can find that it is critical to express the information of the entire tree in a string. This "pressure" makes the line segment tree a powerful tool...

Then the question is done... Code
#include <bits/stdc++.h>#define ls (rt << 1)#define rs (rt << 1 | 1)using namespace std;const int N = 100010, inf = 1e9;int n, q, tot, cnt, dfn[N * 3], to[N << 1], nxt[N << 1], head[N], pos[N], cc[N];struct Node {    int l, r, l1, r1, l2, r2, c1, c2, dis;    void init(int x) {        dis = -inf;        c1 = c2 = 0;        if (dfn[x] == -2)   c2 = 1;//(        if (dfn[x] == -5)   c1 = 1;//)        if (dfn[x] > 0 && !cc[dfn[x]])  l1 = r1 = l2 = r2 = 0;        else l1 = r1 = l2 = r2 = -inf;    }}a[N * 12];inline int read(int &t) {    int f = 1;char c;    while (c = getchar(), c < ‘0‘ || c > ‘9‘) if (c == ‘-‘) f = -1;    t = c - ‘0‘;    while (c = getchar(), c >= ‘0‘ && c <= ‘9‘) t = t * 10 + c - ‘0‘;    t *= f;}void add(int u, int v) {    to[tot] = v, nxt[tot] = head[u], head[u] = tot++;    to[tot] = u, nxt[tot] = head[v], head[v] = tot++;}void dfs(int u, int fa) {    dfn[++cnt] = -2;//    dfn[++cnt] = u;     pos[u] = cnt;    for (int i = head[u], v; ~i; i = nxt[i]) {        v = to[i];        if (v != fa)    dfs(v, u);    }    dfn[++cnt] = -5;//)}Node merge(Node a, Node b) {    Node c;    c.l = a.l, c.r = b.r;    c.dis = max(a.dis, b.dis);    c.dis = max(c.dis, max(a.r2 + b.l1, a.r1 + b.l2));    if (b.c1 < a.c2)    c.c1 = a.c1, c.c2 = a.c2 - b.c1 + b.c2;    else c.c1 = a.c1 + b.c1 - a.c2, c.c2 = b.c2;    c.l1 = max(a.l1, max(b.l1 + a.c1 - a.c2, b.l2 + a.c1 + a.c2));    c.r1 = max(b.r1, max(a.r1 - b.c1 + b.c2, a.r2 + b.c1 + b.c2));    c.l2 = max(a.l2, b.l2 + a.c2 - a.c1);    c.r2 = max(b.r2, a.r2 + b.c1 - b.c2);    return c;}void change(int rt, int p) {    if (a[rt].l == a[rt].r) {        a[rt].init(a[rt].l);        return;    }    int mid = a[rt].l + a[rt].r >> 1;    if (p <= mid)   change(ls, p);    else change(rs, p);    a[rt] = merge(a[ls], a[rs]);}void build(int rt, int l, int r) {    if (l == r) {        a[rt].l = l, a[rt].r = r;        a[rt].init(l);        return;    }    int mid = l + r >> 1;    build(ls, l, mid), build(rs, mid + 1, r);    a[rt] = merge(a[ls], a[rs]);}int main() {    memset(head, -1, sizeof(head));    read(n);    int now = n;    for (int i = 1, x, y; i < n; ++i) {        read(x), read(y);        add(x, y);    }    dfs(1, 0);    build(1, 1, cnt);    read(q);    while (q--) {        char s[10];        int x;        scanf("%s", s);        if (s[0] == ‘C‘) {            read(x);            if (!cc[x]) --now;            else ++now;            cc[x] ^= 1;            change(1, pos[x]);        }           else {            if (!now)   puts("-1");            else if (now == 1)  puts("0");            else printf("%d\n", a[1].dis);        }    }    return 0;}

Bzoj 1095 hide-and-seek (line segment tree)

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.