acwing795,796,797,798 前缀和,二维前缀和,差分,差分矩阵


前缀和

输入一个长度为 n的整数序列。接下来再输入 m个询问, 每个询问输入一对 l,r。
对于每个询问,输出原序列中从第 l个数到第 r个数的和。

输入格式 第一行包含两个整数 n和 m。 第二行包含 n个整数,表示整数数列。 
接下来 m行,每行包含两个整数 l 和 r, 表示一个询问的区间范围。

输出格式 共 m行,每行输出一个询问的结果。

数据范围 1≤l≤r≤n,1≤n,m≤100000,1000≤数列中元素的值≤1000

输入样例: 
5 
3 2 1 3 6 4 
1 2 
1 3
2 4

输出样例: 
3 
6 
10
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
const int N=100005;
int value[N],sum[N];
int main()
{
    int m, n;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>value[i];
        sum[i]=sum[i-1]+value[i];
    }
    while(m--)
    {
        int r,l;
        cin>>r>>l;
        cout<<sum[l]-sum[r-1]<<endl;
        
        
    }
    return 0;
 
}

前缀和矩阵

输入一个n行m列的整数矩阵,再输入q个询问,每个询问包含四个整数x1, y1, x2, y2,表示一个子矩阵的左上角坐标和右下角坐标。

对于每个询问输出子矩阵中所有数的和。

输入格式
第一行包含三个整数n,m,q。

接下来n行,每行包含m个整数,表示整数矩阵。

接下来q行,每行包含四个整数x1, y1, x2, y2,表示一组询问。

输出格式
共q行,每行输出一个询问的结果。

数据范围
1≤n,m≤1000,
1≤q≤200000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,1000≤矩阵内元素的值≤1000
输入样例
3 4 3
1 7 2 4
3 6 2 8
2 1 2 3
1 1 2 2
2 1 3 4
1 3 3 4
输出样例:
17
27
21
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
const int N=1005;
int value[N][N],sum[N][N];
int main()
{
    int m, n, q;
    cin>>n>>m>>q;
    for(int i=1;i<=n;i++)
       for(int j=1;j<=m;j++)
    {
        cin>>value[i][j];
        sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+value[i][j];
    }
    while(q--)
    {
        int x1,x2,y1,y2;
        cin>>x1>>y1>>x2>>y2;
        cout<<sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]<<endl;
    }
    return 0;
 
}

差分

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
const int N=1005;
int value[N],cha[N];
int main()
{
    int m, n, q;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>value[i];
        cha[i]+=value[i];
        cha[i+1]-=value[i];
    }
    while(m--)
    {
        int l,r,c;
        cin>>l>>r>>c;
        cha[l]+=c;
        cha[r+1]-=c;
    }
    for(int i=1;i<=n;i++)
    {
        cha[i]=cha[i-1]+cha[i];
        cout<<cha[i]<<" ";
        
    }

    return 0;
 
}

差分矩阵

输入一个n行m列的整数矩阵,再输入q个操作,每个操作包含五个整数x1, y1, x2, y2, c,其中(x1, y1)(x2, y2)表示一个子矩阵的左上角坐标和右下角坐标。

每个操作都要将选中的子矩阵中的每个元素的值加上c。

请你将进行完所有操作后的矩阵输出。

输入格式

第一行包含整数n,m,q。

接下来n行,每行包含m个整数,表示整数矩阵。

接下来q行,每行包含5个整数x1, y1, x2, y2, c,表示一个操作。

输出格式

共 n 行,每行 m 个整数,表示所有操作进行完毕后的最终矩阵。

数据范围

1≤n,m≤10001≤n,m≤1000,
1≤q≤1000001≤q≤100000,
1≤x1≤x2≤n1≤x1≤x2≤n,
1≤y1≤y2≤m1≤y1≤y2≤m,1000≤c≤10001000≤c≤1000,1000≤矩阵内元素的值≤10001000≤矩阵内元素的值≤1000

输入样例:

3 4 3
1 2 2 1
3 2 2 1
1 1 1 1
1 1 2 2 1
1 3 2 3 2
3 1 3 4 1
输出样例:

2 3 4 1
4 3 4 1
2 2 2 2
3 4 3
1 2 2 1
3 2 2 1
1 1 1 1
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cmath>
#include <map>
using namespace std;
const int N=1005;
int value[N][N],cha[N][N];
int main()
{
    int m, n, q, c;
    cin>>n>>m>>q;
    for(int i=1;i<=n;i++)
     for(int j=1;j<=m;j++)
    {
        cin>>value[i][j];
        cha[i][j]+=value[i][j];
        cha[i+1][j]-=value[i][j];
        cha[i][j+1]-=value[i][j];
        cha[i+1][j+1]=value[i][j];
    }
    while(q--)
    {
        int x1,x2,y1,y2;
        cin>>x1>>y1>>x2>>y2>>c;
        cha[x1][y1]+=c;
        cha[x2+1][y1]-=c;
        cha[x1][y2+1]-=c;
        cha[x2+1][y2+1]+=c;
    }
    for(int i=1;i<=n;i++)
    {
       for(int j=1;j<=m;j++)
        {
         cha[i][j]+=cha[i-1][j]+cha[i][j-1]-cha[i-1][j-1];
         cout<<cha[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值