UVA 11992 线段树区间更新 + 两个lazy(二维线段树)

传送门:UVA 11992

题解

最多有20行, 可以建20棵线段树, 然后更新查询时按维数维护
线段树区间合并, 两个lazy有主次之分(set比add优先)
pushDown()和pushUp();

/* adrui's submission Language : C++ Result : Accepted Love : ll Favorite : Dragon Balls Standing in the Hall of Fame */


#include
#include
#include
#include
#include
using namespace std;

#define debug 0
#define inf 0x7f7f7f7f
#define mid ((l + r) >> 1)
#define ls rt << 1, l, mid
#define rs rt << 1|1, mid + 1, r
#define M(a, b) memset(a, b, sizeof(a))

int row, col, q;

const int maxn(1e6 + 5);

struct{
    int mx, mn, sum, addLayz, setLazy;
}node[21][maxn << 2];

void pushUp(int i, int rt) {
    node[i][rt].sum = node[i][rt << 1].sum + node[i][rt << 1 | 1].sum;
    node[i][rt].mx = max(node[i][rt << 1].mx, node[i][rt << 1 | 1].mx);
    node[i][rt].mn = min(node[i][rt << 1].mn, node[i][rt << 1 | 1].mn);
}

void build(int i, int rt, int l, int r) {
    node[i][rt].setLazy = -1;
    if (l == r) {
        return;
    }

    build(i, ls);
    build(i, rs);
    //pushUp(i, rt);
}

void pushDown(int i, int rt, int m) {
    if (node[i][rt].setLazy != -1) {
        node[i][rt << 1].sum = (m - (m >> 1)) * node[i][rt].setLazy;
        node[i][rt << 1 | 1].sum = (m >> 1) * node[i][rt].setLazy;

        node[i][rt << 1].mx = node[i][rt].setLazy;
        node[i][rt << 1 | 1].mx = node[i][rt].setLazy;
        node[i][rt << 1].mn = node[i][rt].setLazy;
        node[i][rt << 1 | 1].mn = node[i][rt].setLazy;

        node[i][rt << 1].addLayz = node[i][rt << 1 | 1].addLayz = 0;//set优先, 孩子节点的addLazy设为0
        node[i][rt << 1].setLazy = node[i][rt].setLazy;
        node[i][rt << 1 | 1].setLazy = node[i][rt].setLazy;

        node[i][rt].setLazy = -1;
    }

    if (node[i][rt].addLayz != 0) {
        node[i][rt << 1].sum += (m - (m >> 1)) * node[i][rt].addLayz;
        node[i][rt << 1 | 1].sum += (m >> 1) * node[i][rt].addLayz;

        node[i][rt << 1].mx += node[i][rt].addLayz;
        node[i][rt << 1 | 1].mx += node[i][rt].addLayz;
        node[i][rt << 1].mn += node[i][rt].addLayz;
        node[i][rt << 1 | 1].mn += node[i][rt].addLayz;

        node[i][rt << 1].addLayz += node[i][rt].addLayz;
        node[i][rt << 1 | 1].addLayz += node[i][rt].addLayz;

        node[i][rt].addLayz = 0;
    }


}

void addVal(int i, int rt, int l, int r, int ul, int ur, int add) {     //add
    if (ul <= l && ur >= r) {
        node[i][rt].sum += (r - l + 1) * add;
        node[i][rt].mx += add;
        node[i][rt].mn += add;
        //if (node[i][rt].setLazy != -1) node[i][rt].setLazy += add;
        node[i][rt].addLayz += add;
        return;
    }

    pushDown(i, rt, r - l + 1);

    if(ul <= mid)   addVal(i, ls, ul, ur, add);
    if (ur > mid)   addVal(i, rs, ul, ur, add);

    pushUp(i, rt);
}

void setVal(int i, int rt, int l, int r, int ul, int ur, int v) {       //set
    if (ul <= l && ur >= r) {
        node[i][rt].sum = (r - l + 1) * v;
        node[i][rt].mx = v;
        node[i][rt].mn = v;
        node[i][rt].setLazy = v;
        node[i][rt].addLayz = 0;//优先
        return;
    }

    pushDown(i, rt, r - l + 1);

    if (ul <= mid)  setVal(i, ls, ul, ur, v);
    if (ur > mid)   setVal(i, rs, ul, ur, v);

    pushUp(i, rt);
}

void query(int i, int rt, int l, int r, int ql, int qr, int &sum, int &mn, int &mx) {//query
    if (ql <= l && qr >= r) {
        sum += node[i][rt].sum;
        mx = max(mx, node[i][rt].mx);
        mn = min(mn, node[i][rt].mn);
        return;
    }

    pushDown(i, rt, r - l + 1);

    if (ql <= mid) query(i, ls, ql, qr, sum, mn, mx);
    if (qr > mid)  query(i, rs, ql, qr, sum, mn, mx);
    pushUp(i, rt);
}
int main() {
#if debug
    freopen("in.txt", "r", stdin);
#endif //debug

    cin.tie(0);
    cin.sync_with_stdio(false);

    int k, x1, y1, x2, y2, v;
    while (cin >> row >> col >> q) {

        M(node, 0);
        for (int i = 1; i <= row; i++)                          //建树
            build(i, 1, 1, col);

        while (q--) {
            cin >> k;

            if (k == 3) {
                cin >> x1 >> y1 >> x2 >> y2;
                int sum = 0;
                int mn = inf, mx = -inf;

                for (int i = x1; i <= x2; i++) {
                    query(i, 1, 1, col, y1, y2, sum, mn, mx);
                }

                cout << sum << " " << mn << " " << mx << endl;
            }
            else {
                cin >> x1 >> y1 >> x2 >> y2 >> v;
                if (k == 1) {
                    for (int i = x1; i <= x2; i++)
                        addVal(i, 1, 1, col, y1, y2, v);
                }
                else {
                    for (int i = x1; i <= x2; i++)
                        setVal(i, 1, 1, col, y1, y2, v);
                }
            }
        }
    }
    return 0;
}

你可能感兴趣的:(UVA,tree),线段树区间合并)