UVA 11992 Fast Matrix Operations

本文介绍了一种高效的矩阵操作方法,支持矩阵元素的增量更新、赋值及子矩阵的求和、极值查询等操作,适用于处理大规模矩阵数据结构问题。

Fast Matrix Operations

There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operations:

1 x1 y1 x2 y2 v

Increment each element (x,y) in submatrix (x1,y1,x2,y2) by v (v>0)

2 x1 y1 x2 y2 v

Set each element (x,y) in submatrix (x1,y1,x2,y2) to v

3 x1 y1 x2 y2

Output the summation, min value and max value of submatrix (x1,y1,x2,y2)

In the above descriptions, submatrix (x1,y1,x2,y2) means all the elements (x,y) satisfying x1<=x<=x2 and y1<=x<=y2. It is guaranteed that 1<=x1<=x2<=r, 1<=y1<=y2<=c. After any operation, the sum of all the elements in the matrix does not exceed 109.

Input

There are several test cases. The first line of each case contains three positive integers r, c, m, where m (1<=m<=20,000) is the number of operations. Each of the next m lines contains a query. There will be at most twenty rows in the matrix. The input is terminated by end-of-file (EOF). The size of input file does not exceed 500KB.

Output

For each type-3 query, print the summation, min and max.

Sample Input

4 4 8
1 1 2 4 4 5
3 2 1 4 4
1 1 1 3 4 2
3 1 2 4 4
3 1 1 3 4
2 2 1 4 4 2
3 1 2 4 4
1 1 1 4 3 3

Output for the Sample Input

45 0 5
78 5 7
69 2 7
39 2 7

Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yeji Shen, Dun Liang

Note: Please make sure to test your program with the gift I/O files before submitting!

题目大意:有一个r行c列的全0矩阵,支持三种操作:

1 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素增加v(v > 0)。

2 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素设为v(v > 0)。

3 x1 y1 x2 y2    查询子矩阵(x1,y1,x2,y2)的元素和、最小值和最大值。

子矩阵(x1,y1,x2,y2)是指满足x1 <= x <= x2,y1 <= y <= y2,的所有元素(x,y)。输入保证任何时刻矩阵内的所有元素之和不超过10 ^ 9。

其中,矩阵不超过20行,元素总数不超过10 ^ 6,且操作数不超过20000。

每一行建一个线段树 有意思
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_N = 100010;
const int INF = 0x3f3f3f3f;
int s[30][MAX_N<<2],MAX[30][MAX_N<<2],MIN[30][MAX_N<<2],addv[30][MAX_N<<2],setv[30][MAX_N<<2];
void up(int i,int p){
        s[i][p]=s[i][p*2]+s[i][p*2+1];
        MIN[i][p]=min(MIN[i][p*2],MIN[i][p*2+1]);
        MAX[i][p]=max(MAX[i][p*2],MAX[i][p*2+1]);
}
void down(int i,int p,int l,int r){
    int mid = (l+r)>>1;
    if(setv[i][p]){
        setv[i][p*2] = setv[i][p];
        setv[i][p*2+1] = setv[i][p];
        addv[i][p*2]=addv[i][p*2+1]=0;
        s[i][p*2]=setv[i][p*2]*(mid-l+1);
        s[i][p*2+1]=setv[i][p*2+1]*(r-mid);
        MIN[i][p*2]=MIN[i][p*2+1]=setv[i][p];
        MAX[i][p*2]= MAX[i][p*2+1]=setv[i][p];
        setv[i][p] = 0;
    }
    if(addv[i][p]){
        addv[i][p*2] += addv[i][p];
        addv[i][p*2+1] += addv[i][p];
        s[i][p*2]+=addv[i][p]*(mid-l+1);
        s[i][p*2+1]+=addv[i][p]*(r-mid);
        MIN[i][p*2]+=addv[i][p*2];
        MIN[i][p*2+1]+=addv[i][p*2+1];
        MAX[i][p*2]+=addv[i][p];
        MAX[i][p*2+1]+=addv[i][p];
        addv[i][p] = 0;
    }
}
void build(int i,int p,int l,int r){
    addv[i][p]=0;
    setv[i][p]=0;
    if(l==r){
        MIN[i][p]=MAX[i][p]=s[i][p]=0;
        return;
    }
    int mid =(l+r)>>1;
    build(i,p*2,l,mid);
    build(i,p*2+1,mid+1,r);
    up(i,p);
}
void change(int a,int i,int p,int l,int r,int x,int y,int v){
    if(x<=l&&r<=y){
        if(a==1){
            addv[i][p]+=v;
            s[i][p]+=v*(r-l+1);
            MIN[i][p]+=v;
            MAX[i][p]+=v;
        }
        else if(a==2){
            addv[i][p]=0;
            setv[i][p]=v;
            s[i][p]=v*(r-l+1);
            MIN[i][p]=MAX[i][p]=v;
        }
        return;
    }
    down(i,p,l,r);
    int mid=(l+r)>>1;
    if(x<=mid) change(a,i,p*2,l,mid,x,y,v);
    if(y>mid) change(a,i,p*2+1,mid+1,r,x,y,v);
    up(i,p);
}
void query(int i,int p,int l,int r,int x,int y,int& sum,int& maxx,int& minn){
  if(x<=l&&r<=y){
    sum=s[i][p];
    maxx=MAX[i][p];
    minn=MIN[i][p];
    return;
  }
down(i,p,l,r);
int mid=(l+r)>>1;
int ts,tmax,tmin;
  sum=0;maxx=-1;minn=INF;
  if(x<=mid) {
    query(i,p*2,l,mid,x,y,ts,tmax,tmin);
    sum+=ts;
    maxx=max(maxx,tmax);
    minn=min(minn,tmin);
  }
  if(y>mid){
    query(i,p*2+1,mid+1,r,x,y,ts,tmax,tmin);
    sum+=ts;
    maxx=max(maxx,tmax);
    minn=min(minn,tmin);
  }
}


int main(){
    int r,c,m;
    while(~scanf("%d%d%d",&r,&c,&m)){
        for(int i=i;i<=r;++i)
           build(i,1,1,c);
    while(m--){
        int a,x1,y1,x2,y2,v;
        scanf("%d",&a);
        if(a==1||a==2){
                scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v);
            for(int i=x1;i<=x2;++i)
               change(a,i,1,1,c,y1,y2,v);
        }
        else if(a==3){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
           int sum = 0,minn=INF,maxx=-1;
           int ts,tmin,tmax;
           for(int i=x1;i<=x2;i++){
            query(i,1,1,c,y1,y2,ts,tmax,tmin);
            sum+=ts;
            maxx=max(maxx,tmax);
            minn=min(minn,tmin);
           }
           printf("%d %d %d\n",sum,minn,maxx);
        }
    }
    }
    return 0;
}

基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值