HDU 3911 Black and White (line segment tree)

Source: Internet
Author: User

Connection: HDU 3911 black and white

A sequence is given and M operations are performed;

  • 0 l r: indicates the maximum number of consecutive 1 s in query L and R.
  • 1 l r: returns the inverse of the number in the range of L and R.

Solution: A Question type of the Line Segment tree. The interval is merged. Because there is an inverse operation, we need to maintain six values for each node, including the length of the longest continuous sequence, the longest continuous length on the left and right. It should be noted that if the maximum value of the queried range is from R [lson] + L [rson], you must determine whether the ratio length is greater than R-l + 1. I didn't pay attention to it at the beginning, so Wa went online and searched for other people's questions. I found that many people did not pushup in the query to update the current node information. This is definitely not a problem, but Pushdown exists, you must update the current node information.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 1e5 + 5;int N, M, a[maxn];#define lson(x) ((x)<<1)#define rson(x) (((x)<<1)|1)int lc[maxn << 2], rc[maxn << 2], filp[maxn << 2];int L[maxn << 2][2], R[maxn << 2][2], S[maxn << 2][2];void maintain (int u) {    filp[u] ^= 1;    swap(L[u][0], L[u][1]);    swap(R[u][0], R[u][1]);    swap(S[u][0], S[u][1]);}void pushup(int u) {    for (int i = 0; i < 2; i++) {        S[u][i] = max(max(S[lson(u)][i], S[rson(u)][i]), R[lson(u)][i] + L[rson(u)][i]);        L[u][i] = L[lson(u)][i] + (L[lson(u)][i] == rc[lson(u)] - lc[lson(u)] + 1 ? L[rson(u)][i] : 0);        R[u][i] = R[rson(u)][i] + (R[rson(u)][i] == rc[rson(u)] - lc[rson(u)] + 1 ? R[lson(u)][i] : 0);    }}void pushdown (int u) {    if (filp[u]) {        maintain(lson(u));        maintain(rson(u));        filp[u] = 0;    }}void build (int u, int l, int r) {    lc[u] = l;    rc[u] = r;    filp[u] = 0;    if (l == r) {        int d = a[l];        L[u][d] = R[u][d] = S[u][d] = 1;        L[u][d^1] = R[u][d^1] = S[u][d^1] = 0;        return ;    }    int mid = (l + r) / 2;    build(lson(u), l, mid);    build(rson(u), mid+1, r);    pushup(u);}void modify (int u, int l, int r) {    if (l <= lc[u] && rc[u] <= r) {        maintain(u);        return;    }    pushdown(u);    int mid = (lc[u] + rc[u]) / 2;    if (l <= mid)        modify(lson(u), l, r);    if (r > mid)        modify(rson(u), l, r);    pushup(u);}int query (int u, int l, int r) {    if (l <= lc[u] && rc[u] <= r)        return S[u][1];    pushdown(u);    int mid = (lc[u] + rc[u]) / 2, ret;    if (r <= mid)        ret = query(lson(u), l, r);    else if (l > mid)        ret = query(rson(u), l, r);    else {        int ll = query(lson(u), l, r);        int rr = query(rson(u), l, r);        int a = min(L[rson(u)][1], r - mid);        int b = min(R[lson(u)][1], mid - l + 1);        ret = max( max(ll, rr), a + b);    }    pushup(u);    return ret;}int main () {    int x, l, r;    while (scanf("%d", &N) == 1) {        for (int i = 1; i <= N; i++)            scanf("%d", &a[i]);        build (1, 1, N);        scanf("%d", &M);        while (M--) {            scanf("%d%d%d", &x, &l, &r);            if (x)                modify(1, l, r);            else                printf("%d\n", query(1, l, r));        }    }    return 0;}

HDU 3911 Black and White (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.