UVA 11992 Fast Matrix Operations

来源:互联网 发布:狗听得懂人话吗 知乎 编辑:程序博客网 时间:2024/04/28 11:59

线段树区间修改,懒标记传递。


#include<bits/stdc++.h>#define INF 0x3f3f3f3f#define lson o<<1#define rson o<<1|1using namespace std;const int MAXN = 1<<17;int x, ql, qr, op;struct node {    int addv, setv, sumv, minv, maxv;};node better(node a, node b) {    node ret;    ret.sumv = a.sumv + b.sumv;    ret.minv = min(a.minv, b.minv);    ret.maxv = max(a.maxv, b.maxv);    return ret;}struct Intervaltree {    node a[MAXN];    void clear(int n) {        for(int i = 1; i <= MAXN; i++) {            a[i].addv = a[i].maxv = a[i].minv = a[i].sumv = 0;            a[i].setv = -1;        }        a[1].setv = 0;    }    void mantain(int o, int l, int r) {        if(r > l) {            a[o].sumv = a[lson].sumv + a[rson].sumv;            a[o].minv = min(a[lson].minv, a[rson].minv);            a[o].maxv = max(a[lson].maxv, a[rson].maxv);        }        if(a[o].setv >= 0) {            a[o].minv = a[o].maxv = a[o].setv;            a[o].sumv = a[o].setv*(r-l+1);        }        a[o].minv += a[o].addv, a[o].maxv += a[o].addv;        a[o].sumv += a[o].addv*(r-l+1);    }    void pushdown(int o) {        if(a[o].setv >= 0) {            a[lson].setv = a[rson].setv = a[o].setv;            a[o].setv = -1;            a[lson].addv = a[rson].addv = 0;        }        if(a[o].addv) {            a[lson].addv += a[o].addv;            a[rson].addv += a[o].addv;            a[o].addv = 0;        }    }    void update(int o, int l, int r) {        if(ql<=l && qr>=r) {            if(op == 1) a[o].addv += x;            else a[o].setv = x, a[o].addv = 0;        }        else {            pushdown(o);            int m = l+r>>1;            if(ql <= m) update(lson, l, m); else mantain(lson, l, m);            if(m+1 <= qr) update(rson, m+1, r); else mantain(rson, m+1, r);        }        mantain(o, l, r);    }    node query(int o, int l, int r, int add) {        node t;        if(a[o].setv >= 0) {            int tmp = a[o].setv + a[o].addv + add;            t.sumv = tmp*(min(qr, r)-max(l, ql)+1);            t.minv = t.maxv = tmp;            return t;        }        if(ql<=l && qr>=r) {            t.sumv = a[o].sumv + add*(r-l+1);            t.minv = a[o].minv + add;            t.maxv = a[o].maxv + add;            return t;        }        int m = l+r>>1;        if(m >= qr) return query(lson, l, m, add+a[o].addv);        if(m+1 <= ql) return query(rson, m+1, r, add+a[o].addv);        return better(query(lson, l, m, add+a[o].addv), query(rson, m+1, r, add+a[o].addv));    }}tree[25];int main() {    int n, m, q;    while(scanf("%d%d%d", &n, &m, &q) == 3) {        for(int i = 1; i <= n; i++) tree[i].clear(m);        while(q--) {            int x1, x2;            scanf("%d%d%d%d%d", &op, &x1, &ql, &x2, &qr);            if(op < 3) {                scanf("%d", &x);                for(int i = x1; i <= x2; i++) tree[i].update(1, 1, m);            }            else {                node t = tree[x1].query(1, 1, m, 0);                for(int i = x1+1; i <= x2; i++) t = better(t, tree[i].query(1, 1, m, 0));                printf("%d %d %d\n", t.sumv, t.minv, t.maxv);            }        }    }    return 0;}


0 0
原创粉丝点击