UVa-11992 Fast Matrix Operations 快速矩阵运算

题目链接
因为矩阵最多就20行,所以可以建立多棵线段树。对于标记set和add要注意优先级。

//Time:309ms
#include <bits/stdc++.h>
#define fre freopen("in.txt", "r", stdin)
#define MAXN 500005
#define inf 0x3f3f3f3f
using namespace std;

int row, column, q;
struct node
{
    int l, r;
    int sum, max, min;
    int set, add;
} nd[21][MAXN * 4];

//简单初始化
void build(int l, int r, int root = 1)
{
    for (int i = 1; i <= row; i++)
    {
        nd[i][root] = {l, r, 0, 0, 0, 0, 0};
    }
    if (l == r)
    {
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, root << 1);
    build(mid + 1, r, root << 1 | 1);
}

//单点更新set,会将add置0
inline void change_set(int row, int root, int set)
{
    nd[row][root].add = 0;
    nd[row][root].set = set;
    nd[row][root].max = set;
    nd[row][root].min = set;
    nd[row][root].sum = (nd[row][root].r - nd[row][root].l + 1) * set;
}

//单点更新add
inline void change_add(int row, int root, int add)
{
    nd[row][root].add += add;
    nd[row][root].max += add;
    nd[row][root].min += add;
    nd[row][root].sum += (nd[row][root].r - nd[row][root].l + 1) * add;
    //printf("l=%d r=%d sum=%d\n", nd[row][root].l, nd[row][root].r, nd[row][root].sum);
}

//更新单点信息
inline void update_node(int row, int root)
{
    nd[row][root].max = max(nd[row][root << 1].max, nd[row][root << 1 | 1].max);
    nd[row][root].min = min(nd[row][root << 1].min, nd[row][root << 1 | 1].min);
    nd[row][root].sum = nd[row][root << 1].sum + nd[row][root << 1 | 1].sum;
}

inline void pushdown(int row, int root)
{
    //如果set被设置过,降add的值加到set上下传,并将add赋值为0
    if (nd[row][root].set)
    {
        change_set(row, root << 1, nd[row][root].set + nd[row][root].add);
        change_set(row, root << 1 | 1, nd[row][root].set + nd[row][root].add);
        nd[row][root].set = 0;
        nd[row][root].add = 0;
    }
    else if (nd[row][root].add)
    {
        change_add(row, root << 1, nd[row][root].add);
        change_add(row, root << 1 | 1, nd[row][root].add);
        nd[row][root].add = 0;
    }
}

int query_sum(int row, int ql, int qr, int root = 1)
{
    if (ql <= nd[row][root].l && qr >= nd[row][root].r)
    {
        return nd[row][root].sum;
    }
    pushdown(row, root);
    int mid = (nd[row][root].l + nd[row][root].r) >> 1;
    if (ql > mid)
        return query_sum(row, ql, qr, root << 1 | 1);
    if (qr <= mid)
        return query_sum(row, ql, qr, root << 1);
    return query_sum(row, ql, qr, root << 1) + query_sum(row, ql, qr, root << 1 | 1);
}

int query_max(int row, int ql, int qr, int root = 1)
{
    if (ql <= nd[row][root].l && qr >= nd[row][root].r)
        return nd[row][root].max;
    pushdown(row, root);
    int mid = (nd[row][root].l + nd[row][root].r) >> 1;
    if (ql > mid)
        return query_max(row, ql, qr, root << 1 | 1);
    if (qr <= mid)
        return query_max(row, ql, qr, root << 1);
    return max(query_max(row, ql, qr, root << 1), query_max(row, ql, qr, root << 1 | 1));
}

int query_min(int row, int ql, int qr, int root = 1)
{
    if (ql <= nd[row][root].l && qr >= nd[row][root].r)
        return nd[row][root].min;
    pushdown(row, root);
    int mid = (nd[row][root].l + nd[row][root].r) >> 1;
    if (ql > mid)
        return query_min(row, ql, qr, root << 1 | 1);
    if (qr <= mid)
        return query_min(row, ql, qr, root << 1);
    return min(query_min(row, ql, qr, root << 1), query_min(row, ql, qr, root << 1 | 1));
}

void update_add(int row, int ql, int qr, int add, int root = 1)
{
    if (ql <= nd[row][root].l && qr >= nd[row][root].r)
    {
        change_add(row, root, add);
        return;
    }
    pushdown(row, root);
    int mid = (nd[row][root].l + nd[row][root].r) >> 1;
    if (ql > mid)
        update_add(row, ql, qr, add, root << 1 | 1);
    else if (qr <= mid)
        update_add(row, ql, qr, add, root << 1);
    else
    {
        update_add(row, ql, qr, add, root << 1);
        update_add(row, ql, qr, add, root << 1 | 1);
    }
    update_node(row, root);
}

void update_set(int row, int ql, int qr, int set, int root = 1)
{
    if (ql <= nd[row][root].l && qr >= nd[row][root].r)
    {
        change_set(row, root, set);
        return;
    }
    pushdown(row, root);
    int mid = (nd[row][root].l + nd[row][root].r) >> 1;
    if (ql > mid)
        update_set(row, ql, qr, set, root << 1 | 1);
    else if (qr <= mid)
        update_set(row, ql, qr, set, root << 1);
    else
    {
        update_set(row, ql, qr, set, root << 1);
        update_set(row, ql, qr, set, root << 1 | 1);
    }
    update_node(row, root);
}

int main()
{
    while (cin >> row >> column >> q)
    {
        build(1, column);
        int select, x1, x2, y1, y2, val;
        while (q--)
        {
            scanf("%d", &select);
            if (select == 1)
            {
                scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &val);
                for (int i = x1; i <= x2; i++)
                    update_add(i, y1, y2, val);
            }
            else if (select == 2)
            {
                scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &val);
                for (int i = x1; i <= x2; i++)
                    update_set(i, y1, y2, val);
            }
            else
            {
                scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
                int sum = 0, maxn = -inf, minn = inf;
                for (int i = x1; i <= x2; i++)
                {
                    sum += query_sum(i, y1, y2);
                    maxn = max(query_max(i, y1, y2), maxn);
                    minn = min(query_min(i, y1, y2), minn);
                }
                printf("%d %d %d\n", sum, minn, maxn);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值