Segment tree partition summary

First, the requirement can be offline .
There are two segment tree partition.

Type One

Inquiry-based operating range, a single point.

Sometimes An operation can be done quickly, however, to realize the inverse of this operation more difficult.
Because, in general, we need to implement the inverse operations are carried out a long time ago.
However, if only undo the last operation, it would be much simpler.
For example, maintain some connectivity, or diameter, problems such as a linear group.
Such problems plus side to do well, but the border erase difficult to achieve.
We can sweep it again operating, effective range of each operation.
Then, each of the addition operation valid according to section line segment tree and traversing the tree line Zheke marker to simultaneously processed.
In some ways, as will be appreciated permanent marker.
In this way, it will be revoked at any time revoke becomes only the last. (Or to be revoked)
requires: maintaining a data structure to support Undo the last action, not shared equally complexity (due to the withdrawal)
time complexity: a more than normal log.

Example 1: eight vertical and eight horizontal

Topic links: [HAOI2017] eight vertical and eight horizontal

Divide and Conquer & linear segment tree base template title.

To a tree, support plus side, border erase, modify the right side, and ask largest XOR and rings.
Xor and similar path, the exclusive OR result of the inquiry is the greatest and, using linear group all rings.
Modify delete + insert can be seen. Since the linear base does not support removing, so use divide and conquer tree line.
Disjoint-set can be used to maintain the tree.

Code:

#include <stdio.h> 
#include <bitset> 
#include <string.h> 
#include <vector> 
using namespace std;
int fr[503],ne[1003],v[1003],w[1003],bs = 0,len,ff[503];
bool bk[1003],ca[1003];
bitset < 1005 > bi[2003],jl[503],ji[1003],ans[1003];
void addb(int a, int b, int c) {
    v[bs] = b;
    w[bs] = c;
    ne[bs] = fr[a];
    fr[a] = bs++;
}
void dfs1(int u, int f) {
    for (int i = fr[u]; i != -1; i = ne[i]) {
        if (v[i] == f) continue;
        jl[v[i]] = jl[u] ^ bi[w[i]];
        dfs1(v[i], u);
    }
}
int getv(int x) {
    if (x == ff[x]) return x;
    ff[x] = getv(ff[x]);
    return ff[x];
}
bool merge(int x, int y) {
    x = getv(x);
    y = getv(y);
    if (x == y) return false;
    ff[x] = y;
    return true;
}
void fuz(bitset < 1005 > &x, char zf[1005]) {
    x = 0;
    int n = strlen(zf);
    if (n > len) len = n;
    for (int i = 0; i < n; i++) {
        if (zf[i] == '1') x[n - 1 - i] = 1;
    }
}
void getans(bitset < 1005 > &x) {
    x = 0;
    for (int i = len - 1; i >= 0; i--) {
        if (x[i] == 0 && bk[i]) x ^= ji[i];
    }
}
int st[1005],tp = 0;
void insert(bitset < 1005 > x) {
    for (int i = len - 1; i >= 0; i--) {
        if (x[i]) {
            if (!bk[i]) {
                bk[i] = true;
                ji[i] = x;
                st[tp++] = i;
                break;
            } else x ^= ji[i];
        }
    }
}
struct SJd {
    int x,y,z;
    SJd() {}
    SJd(int X, int Y, int Z) {
        x = X;y = Y;z = Z;
    }
};
vector < SJd > ve[8005];
void xiugai(int i, int l, int r, int L, int R, SJd x) {
    if (R <= l || r <= L) return;
    if (L <= l && r <= R) {
        ve[i].push_back(x);
        return;
    }
    int m = (l + r) >> 1;
    xiugai(i << 1, l, m, L, R, x);
    xiugai((i << 1) | 1, m, r, L, R, x);
}
int wz[2003];
void dfs3(int i, int l, int r) {
    int la = tp;
    for (int j = 0; j < ve[i].size(); j++) insert(bi[ve[i][j].z] ^ jl[ve[i][j].x] ^ jl[ve[i][j].y]);
    if (l + 1 == r) {
        if (wz[l] != -1) getans(ans[wz[l]]);
    } else {
        int m = (l + r) >> 1;
        dfs3(i << 1, l, m);
        dfs3((i << 1) | 1, m, r);
    }
    for (int i = la; i < tp; i++) bk[st[i]] = false;
    tp = la;
}
char zf[1003],ch[20];
int tx[1003],ty[1003],la[1003],tm[1003];
int ll[2003],rr[2003],X[2003],Y[2003],Z[2003];
SJd xg[2003];
int main() {
    int n,m,q;
    scanf("%d%d%d", &n, &m, &q);
    for (int i = 1; i <= n; i++) {
        fr[i] = -1;ff[i] = i;
    }
    for (int i = 0; i < m; i++) {
        int x,y;
        scanf("%d%d%s", &x, &y, zf);
        fuz(bi[i], zf);
        if (merge(x, y)) {
            addb(x, y, i);addb(y, x, i);
        } else tx[i] = x,
        ty[i] = y;
    }
    dfs1(1, 0);
    int ss = 0,ks = 0,xs = 0;
    for (int i = 1; i <= q; i++) {
        scanf("%s", ch);
        if (ch[0] == 'A') {
            int x,y;
            scanf("%d%d%s", &x, &y, zf);
            ks += 1;ss += 1;
            wz[ss] = i;
            fuz(bi[m + i], zf);
            la[ks] = ss;tm[ks] = i;
            X[ks] = x;Y[ks] = y;
        } else if (ch[0] == 'C' && ch[1] == 'a') {
            int k;
            scanf("%d", &k);
            ss += 1;wz[ss] = i;
            xg[xs] = SJd(X[k], Y[k], m + tm[k]);
            ll[xs] = la[k];rr[xs] = ss;
            xs += 1;
            ca[k] = true;
        } else {
            int k;
            scanf("%d%s", &k, zf);
            fuz(bi[m + i], zf);
            ss += 1;wz[ss] = -1;
            xg[xs] = SJd(X[k], Y[k], m + tm[k]);
            ll[xs] = la[k];rr[xs] = ss;
            xs += 1;wz[ss] = i;
            tm[k] = i;la[k] = ss;
        }
    }
    for (int i = 0; i < m; i++) {
        if (tx[i]) insert(bi[i] ^ jl[tx[i]] ^ jl[ty[i]]);
    }
    tp = 0;
    for (int k = 1; k <= ks; k++) {
        if (ca[k]) continue;
        xg[xs] = SJd(X[k], Y[k], m + tm[k]);
        ll[xs] = la[k];
        rr[xs] = ss + 1;
        xs += 1;
    }
    for (int i = 0; i < xs; i++) xiugai(1, 0, ss + 1, ll[i], rr[i], xg[i]);
    dfs3(1, 0, ss + 1);
    for (int i = 0; i <= q; i++) {
        bool zz = false;
        for (int j = len - 1; j >= 0; j--) {
            if (ans[i][j] == 1) {
                printf("1");
                zz = true;
            } else if (zz) printf("0");
        }
        printf("\n");
    }
    return 0;
}

Example 2: Time Travel

Topic links: [CTSC2016] Time Travel

Meaning of the questions:
In a tree, each node represents a collection of some of the elements are present in this collection,
the collection on each node, is the father of copying down first, then add or remove an element from becoming a new collection.
Each element has \ ((x, y, z , c) \) four values, \ ((Y, Z) \) is useless, that is, two \ ((X, C) \) .
Each time a point is given the tree, and a X, the requirement that all the elements of the node \ (min ((X--x_i) ^ 2 + C_i) \) .
Complexity Requirements \ (O (nlogn) \) .

First, see \ (min ((X--x_i) ^ 2 + C_i) \) , it is natural to think slope optimization.
\ (x_i ^ 2 = y_i + C_i, y_i = x_i, K = 2X, B-Y = KX, X-ANS = B ^ 2 + \) .
So, the equivalent, the convex hull of each node is down by the father of a copy and then add or delete a point, become a new convex hull.

Can be found, this is a version tree, traverse it, it becomes a sequence of. But also a single point of inquiry.

Guess you like

Origin www.cnblogs.com/lnzwz/p/11614311.html