zoukankan      html  css  js  c++  java
  • 二维前缀和与差分

    二维前缀和

    #include<bits/stdc++.h>
    #define REP(i, a, b) for(register int i = (a); i < (b); i++)
    #define _for(i, a, b) for(register int i = (a); i <= (b); i++)
    using namespace std;
    
    const int N = 100;
    int a[N][N], n;
    
    int main()
    {
        scanf("%d", &n);
        _for(i, 1, n)
            _for(j, 1, n)
            {
                int x; scanf("%d", &x);
                a[i][j] = x + a[i-1][j] + a[i][j-1] - a[i-1][j-1];
            }
        
        int x1, y1, x2, y2;
        while(~scanf("%d%d%d%d", &x1, &y1, &x2, &y2))
            printf("%d
    ", a[x2][y2] - a[x1-1][y2] - a[x2][y1-1] + a[x1-1][y1-1]);
     
        return 0;
    }

     二维差分

    #include<bits/stdc++.h>
    #define REP(i, a, b) for(register int i = (a); i < (b); i++)
    #define _for(i, a, b) for(register int i = (a); i <= (b); i++)
    using namespace std;
    
    const int N = 100;
    int a[N][N], n, m;
    
    int main()
    {
        scanf("%d%d", &n, &m);
        _for(i, 1, m)
        {
            int x1, y1, x2, y2, p;
            scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &p);
            a[x1][y1] += p; a[x2+1][y2+1] += p;
            a[x2+1][y1] -= p; a[x1][y2+1] -= p;
        } 
    
        _for(i, 1, n)
            _for(j, 1, n)
                a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
        
        _for(i, 1, n)
        {
            _for(j, 1, n)
                printf("%d ", a[i][j]);
            puts(""); 
        } 
            
        return 0;
    }
  • 相关阅读:
    C#.NET Winform 快速开发平台
    .Net C/S系统开发框架(楚楚原创)
    C# Winform 开发框架
    php导出excel表格超链接
    tp3使用PHPExcel 导出excel
    tp文件上传、表格转数组
    BUG修复记录
    tp3切库问题记录
    个人总结
    初识爬虫(番外篇-python)
  • 原文地址:https://www.cnblogs.com/sugewud/p/9937514.html
Copyright © 2011-2022 走看看