• 二维前缀和与差分


    二维前缀和

    #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;
    }
  • 相关阅读:
    莫比乌斯反演
    CDQ 分治
    二分图的最大匹配、完美匹配和匈牙利算法
    网络流简介
    BSGS && EXBSGS
    fhq-treap
    炸鱼w咕咕咕
    路由器配置——静态路由
    路由器配置——静态路由-回环地址测试
    路由器配置——单臂路由实现VLAN间通信
  • 原文地址:https://www.cnblogs.com/sugewud/p/9937514.html
Copyright © 2020-2023  润新知