uva - 11992 (下推法改进版: 1.246 秒)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,(rt<<1|1)
#define INF  1000000100

const int maxn = 1000010;
int sum[21][maxn<<2],MIN[21][maxn<<2],MAX[21][maxn<<2],add[21][maxn<<2],val[21][maxn<<2];
void pushUP(int rt,int i){
sum[i][rt]=sum[i][rt<<1]+sum[i][rt<<1|1];
MAX[i][rt]=max(MAX[i][rt<<1],MAX[i][rt<<1|1]);
MIN[i][rt]=min(MIN[i][rt<<1],MIN[i][rt<<1|1]);
}
void pushDOWN(int l,int r,int rt,int m,int i){
if(l==r) {
add[i][rt]=0; val[i][rt]=-1;
return ;
}
if(val[i][rt]>=0){
add[i][rt<<1]=add[i][rt<<1|1]=0;
val[i][rt<<1]=val[i][rt<<1|1]=val[i][rt];
sum[i][rt<<1]=(m-(m>>1))*val[i][rt];
sum[i][rt<<1|1]=(m>>1)*val[i][rt];
MAX[i][rt<<1]=val[i][rt];
MIN[i][rt<<1]=val[i][rt];
MAX[i][rt<<1|1]=val[i][rt];
MIN[i][rt<<1|1]=val[i][rt];
val[i][rt]=-1;
}
if(add[i][rt]){
add[i][rt<<1]+=add[i][rt];
add[i][rt<<1|1]+=add[i][rt];
sum[i][rt<<1]+=(m-(m>>1))*add[i][rt];
sum[i][rt<<1|1]+=(m>>1)*add[i][rt];
MAX[i][rt<<1]+=add[i][rt];
MIN[i][rt<<1]+=add[i][rt];
MAX[i][rt<<1|1]+=add[i][rt];
MIN[i][rt<<1|1]+=add[i][rt];
add[i][rt]=0;
}
}
void build(int l,int r,int rt,int i){
add[i][rt]=0;
val[i][rt]=-1;
if(l==r){
   MAX[i][rt]=MIN[i][rt]=sum[i][rt]=0;
   return ;
}
int m=(l+r)>>1;
build(lson,i);
build(rson,i);
pushUP(rt,i);
}
bool flag;
int ql,qr,addv,value;
void update(int l,int r,int rt,int i ){
if(ql<=l&&r<=qr){
   if(flag){ add[i][rt]+=addv;
   sum[i][rt]+=addv*(r-l+1);
   MIN[i][rt]+=addv;
   MAX[i][rt]+=addv;
   }
   else {
   add[i][rt]=0; val[i][rt]=value;
   sum[i][rt]=value*(r-l+1);
   MIN[i][rt]=value;
   MAX[i][rt]=value;
   }
   return ;
}
pushDOWN(l,r,rt,r-l+1,i);
int m=(l+r)>>1;
if(ql<=m) update(lson,i);
if(qr >m) update(rson,i);
pushUP(rt,i);
}
int sum_,min_,max_;
void query(int l,int r,int rt,int i ){
if(ql<=l&&r<=qr){
   sum_+=sum[i][rt];
   min_=min(min_,MIN[i][rt]);
   max_=max(max_,MAX[i][rt]);
   return ;
}
pushDOWN(l,r,rt,r-l+1,i);
int m=(l+r)>>1;
if(ql<=m) query(lson,i);
if(qr >m) query(rson,i);
}
int r,c,Q;
int read(){
for(int i=1;i<=r;i++) build(1,c,1,i);
}
int main()
{
  while(scanf("%d %d %d",&r,&c,&Q)!=EOF){
       read();
       while(Q--){
        int cmd;
        int x1,x2,y1,y2;
        scanf("%d %d %d %d %d",&cmd,&x1,&y1,&x2,&y2);
        if(cmd==1){
            scanf("%d",&addv);  flag=true;
            ql=y1; qr=y2;
            for(int i=x1;i<=x2;i++)
            update(1,c,1,i);
        }
        else if(cmd==2){
            scanf("%d",&value);
            ql=y1; qr=y2;  flag=false;
            for(int i=x1;i<=x2;i++)
            update(1,c,1,i);
        }
        else {
            sum_=0; min_=INF; max_=-INF;
            ql=y1; qr=y2;
            for(int i=x1;i<=x2;i++)
            query(1,c,1,i);
            printf("%d %d %d\n",sum_,min_,max_);
        }
       }
  }
return 0;
}


根据你提供的错误信息,`()-索引必须显示在索引表达式的最后`,这表明在 MATLAB 代码中可能存在语问题,通常是由于尝试对矩阵或数组使用不正确的索引方式导致的。 以下是一个典型的场景和解决方案: --- ### 错误场景 假设你在 MATLAB 中有如下代码: ```matlab A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; result = (A(1, :) + 1)(2); % 错误:试图对结果再次索引 ``` 上述代码会导致类似 `()-索引必须显示在索引表达式的最后` 的错误。原因是 `(A(1, :) + 1)` 的结果是一个行向量,而 MATLAB 不允许直接对其再次使用 `()` 索引。 --- ### 解决方案 #### 方 1: 使用临时变量存储中间结果 将中间结果存储到一个临时变量中,然后再进行索引操作。例如: ```matlab A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; temp = A(1, :) + 1; % 先计算中间结果 result = temp(2); % 再对中间结果进行索引 ``` #### 方 2: 使用逻辑运算符重新组织代码 如果需要对复杂表达式的结果进行索引,可以通过逻辑运算符重新组织代码。例如: ```matlab A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; result = (A(1, :) + 1)(2); % 错误写 % 改为: result = A(1, 2) + 1; % 直接索引并计算 ``` #### 方 3: 检查是否需要多维数组索引 如果代码中涉及多维数组索引,确保索引顺序正确。例如: ```matlab B = rand(3, 3, 3); % 创建一个3x3x3的随机数组 value = B(1, :, 2)(2); % 错误写 % 改为: temp = B(1, :, 2); % 提取第1行、第2页的数据 value = temp(2); % 对提取的数据进行索引 ``` --- ### 示例代码 以下是一个完整的示例,展示如何避免类似的索引错误: ```matlab % MATLAB 示例代码:避免索引错误 % 创建一个3x3矩阵 A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % 错误写:尝试对复杂表达式直接索引 % result = (A(1, :) + 1)(2); % 正确写1:使用临时变量 temp = A(1, :) + 1; % 计算中间结果 result1 = temp(2); % 对中间结果进行索引 % 正确写2:直接索引并计算 result2 = A(1, 2) + 1; % 输出结果 disp('Result using temporary variable:'); disp(result1); disp('Result using direct indexing:'); disp(result2); ``` --- ### 解释 1. **错误原因**: - MATLAB 不支持对复杂表达式(如 `(A(1, :) + 1)`)直接使用 `()` 索引。 - 必须先将中间结果存储到一个变量中,然后再对其进行索引。 2. **解决方**: - 使用临时变量存储中间结果。 - 重新组织代码逻辑,避免复杂的嵌套索引。 3. **代码运行结果**: - `result1` 和 `result2` 都会输出 `3`,因为 `A(1, 2) + 1` 的值为 `2 + 1 = 3`。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值