[Bzoj 1018] [SHOI2008] Blocked traffic traffic "segment Tree Maintenance Connectivity"

Source: Internet
Author: User
Tags strcmp

Title Link: BZOJ-1018

Problem analysis

This problem shows that the brush problem is less, the game is easy to kneel. Sdoi Round1 Day2 T3 is similar to this problem. However, I have not done this problem.

This problem is a classical model for maintaining the connectivity of line-segment trees.

One node of our segment tree represents the connectivity of an interval, with 6 bool values representing the connectivity between points on the 4 corners of the interval.

Then the connectivity of the whole interval is combined with the connectivity between two sub-intervals and two sub-zones.

When you modify an edge, modify it in an array of edges, and then Update it upward from the segment tree leaf at the point where the edge is located.

When asked about the connectivity between two points, if their columns are Y1, y2, then we ask for [1, Y1] [Y1, y2] [y2, n] connectivity.

Then use the three connectivity combination to manually enumerate the various circumstances to determine whether two points are connected.

Because respectively in the Y1, y2 two points, may not just through [Y1, Y2] between the edge of the connection, but need to use the sides of the side connected together.

Code
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath > #include <algorithm>using namespace std;inline int gmin (int a, int b) {return a < b? A:b;} inline int gmax (int a, int b) {return a > b a:b;} const int MAXN = 100000 + 5;int N;char str[15];bool a[maxn][3];struct es{bool A1, A2, B1, B2, C1, C2;} T[MAXN * 4]; Inline es Plus (es e1, es E2, int m) {es ret;ret.a1 = e1.a1 | | (e1.b1 && e1.b2 && a[m][1] && a[m][2] && e2.a1); ret.a2 = E2.a2 | | (e2.b1 && e2.b2 && a[m][1] && a[m][2] && e1.a2) ret.b1 = (e1.b1 && a[m][1] &AMP;&A mp E2.B1) | | (E1.c1 && a[m][2] && e2.c2); ret.b2 = (e1.b2 && a[m][2] && e2.b2) | | (E1.c2 && a[m][1] && e2.c1); ret.c1 = (e1.b1 && a[m][1] && e2.c1) | | (E1.c1 && a[m][2] && e2.b2); ret.c2 = (e1.b2 && a[m][2] && e2.c2) | | (E1.c2 &&Amp A[M][1] && e2.b1); return ret;} void Build (int x, int s, int t) {if (s = = t) {t[x].a1 = T[x].a2 = T[x].c1 = T[x].c2 = false; T[X].B1 = T[X].B2 = True;return;} int m = (s + t) >> 1; Build (x << 1, S, m); Build (x << 1 | 1, M + 1, t); T[X] = Plus (t[x << 1], t[x << 1 | 1], m);} void Modify (int x, int s, int t, int Pos) {if (s = = t) {t[x].a1 = T[x].a2 = T[x].c1 = T[X].C2 = A[pos][0]; T[X].B1 = T[X].B2 = True;return;} int m = (s + t) >> 1;if (pos <= m) Modify (x << 1, S, M, POS); else Modify (x << 1 | 1, M + 1, T, POS); t [x] = Plus (t[x << 1], t[x << 1 | 1], m);} ES Get (int x, int s, int t, int l, int r) {if (L <= s && r >= T) return t[x];es ret;int m = (s + t) >> 1;if (R <= m) ret = Get (x << 1, S, M, L, R), else if (l >= m + 1) ret = Get (x << 1 | 1, M + 1, T, L, R); e LSE RET = Plus (Get (x << 1, S, M, L, R), get (x << 1 | 1, M + 1, T, L, R), m); return ret;} int main () {scanf ("%d", &n), int x, Y, XX, Yy;bool F, Ans;es El, Ex, Er; Build (1, 1, N), while (true) {scanf ("%s", str), if (strcmp (str, "Exit") = = 0) break;scanf ("%d%d%d%d", &x, &y, &xx , &yy), if (strcmp (Str, "Ask") = = 0) {if (Y > yy) {swap (x, XX); swap (y, yy);} El = Get (1, 1, N, 1, y); Ex = Get (1, 1, n, y, yy); Er = Get (1, 1, N, yy, n); if (x = = xx) {if (x = = 1) Ans = EX.B1 | | (A[y-1][1] && a[y-1][2] && el.a2 && ex.c2) | | (A[yy][1] && a[yy][2] && er.a1 && ex.c1) | | (A[y-1][1] && a[y-1][2] && el.a2 && a[yy][1] && a[yy][2] && er.a1 && E X.B2); else Ans = EX.B2 | | (A[y-1][1] && a[y-1][2] && el.a2 && ex.c1) | | (A[yy][1] && a[yy][2] && er.a1 && ex.c2) | | (A[y-1][1] && a[y-1][2] && el.a2 && a[yy][1] && a[yy][2] && er.a1 && E X.B1);} Else{if (x < xx) Ans = Ex.c1 | | (A[y-1][1] && a[y-1][2] && EL.A2 && ex.b2) | | (Ex.b1 && a[yy][1] && a[yy][2] && er.a1); else Ans = Ex.c2 | | (A[y-1][1] && a[y-1][2] && el.a2 && ex.b1) | | (Ex.b2 && a[yy][1] && a[yy][2] && er.a1);} if (Ans) printf ("y\n"), Else printf ("n\n");} Else{if (strcmp (Str, "Open") = = 0) F = true;else F = false;if (x = = xx) {a[gmin (Y, yy)][x] = f; Modify (1, 1, N, gmin (y, yy));} Else{a[y][0] = f; Modify (1, 1, n, y);}}} return 0;}

  

[Bzoj 1018] [SHOI2008] Blocked traffic traffic "segment Tree Maintenance Connectivity"

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.