线段树--lazy思想

这篇博客介绍了如何使用线段树结合懒惰更新的思想来处理矩阵中单元格值的批量修改问题。HH通过一系列操作将矩阵的行或列设置为特定值,最终输出经过这些操作后的矩阵状态。博客内容包括了测试用例的处理和具体实现的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Today HH is palying with a n×n matrix.

 All the numbers of the matrix is 0 initial, and every time HH will do one of the following things:

  1. make all the numbers in the k row become v
  2. make all the numbers in the k column become v

Now HH wants to know what's the final matrix after q options. 

The first line contains an positive integer T(1T10), represents there are T test cases.

For each test case:

The first line contains two positive integers n,q(1n500,1q1000) - the size of the matrix and the times of the options.

Then q lines following, for each line contains three integers op,k,v(1op2,1kn,1v100).
if op=1, then HH will change all the numbers in the k row into v
if op=2, then HH will change all the numbers in the k column into v

关于lazy,以及计时的应用。

#include <iostream>

#include <cstdio>

#include <cstring>

using namespacestd;

const int maxn =505;


int h[maxn][2],l[maxn][2];

int main()

{

    int T;

    scanf("%d",&T);

    while (T --) {

        int n,q;

        scanf("%d%d",&n,&q);

        memset(h,0,sizeof(h));

        memset(l,0,sizeof(l));

        int op,k,v;

        for (int i =1; i <= q; i ++) {

            scanf("%d%d%d",&op,&k,&v);

            if(op ==1)

            {

                h[k][0] = v;

                h[k][1] = i;

            }

            elseif(op ==2){

                l[k][0] = v;

                l[k][1] = i;

            }

        }

        for (int i =1; i <= n; i ++) {

            for (int j =1; j <= n; j ++) {

                if (j !=1) {

                    printf(" ");

                }

                if (h[i][1] >l[j][1]) {

                    printf("%d",h[i][0]);

                }

                elseif(h[i][1] <l[j][1])

                {

                    printf("%d",l[j][0]);

                }

                elseprintf("0");

            }

            printf("\n");

        }

    }

    return0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值