zoukankan      html  css  js  c++  java
  • 差分

    一维

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int N=1e5+5;
     4 int n,m,l,r,c;
     5 int a[N],d[N];
     6 int main()
     7 {
     8     cin>>n>>m;
     9     for(int i=0;i<n;i++) cin>>a[i];
    10     for(int i=0;i<n;i++){
    11         if(i==0) d[i]=a[i];
    12         else d[i]=a[i]-a[i-1];
    13     }
    14     while(m--){
    15         cin>>l>>r>>c;
    16         d[l-1]+=c;
    17         d[r]-=c;
    18     }
    19     for(int i=0;i<n;i++){
    20         if(i==0) cout<<d[i]<<" ";
    21         else{
    22             d[i]+=d[i-1];
    23             cout<<d[i]<<" ";
    24         }
    25     }
    26     return 0;
    27 }

    二维

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int N=1e3+5;
     4 int a[N][N],d[N][N];
     5 int n,m,q;
     6 void deal(int x1,int y1,int x2,int y2,int c)
     7 {
     8     d[x1][y1]+=c;
     9     d[x1][y2+1]-=c;
    10     d[x2+1][y1]-=c;
    11     d[x2+1][y2+1]+=c;
    12 }
    13 int main()
    14 {
    15     cin>>n>>m>>q;
    16     for(int i=1;i<=n;i++){
    17         for(int j=1;j<=m;j++){
    18             cin>>a[i][j];
    19         }
    20     }
    21     for(int i=1;i<=n;i++){
    22         for(int j=1;j<=m;j++){
    23             deal(i,j,i,j,a[i][j]);
    24         }
    25     }
    26     while(q--){
    27         int x1,x2,y1,y2,c;
    28         cin>>x1>>y1>>x2>>y2>>c;
    29         deal(x1,y1,x2,y2,c);
    30     }
    31     for(int i=1;i<=n;i++){
    32         for(int j=1;j<=m;j++){
    33             d[i][j]+=d[i-1][j]+d[i][j-1]-d[i-1][j-1];
    34         }
    35     }
    36     for(int i=1;i<=n;i++){
    37         for(int j=1;j<=m;j++){
    38             cout<<d[i][j]<<" ";
    39         }
    40         cout<<endl;
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    Git 命令使用小笔记
    一个关于if else容易迷惑的问题
    浏览器与Node环境下的Event Loop
    镜面反射
    Socket通信原理
    Git
    vba工具
    为什么要用prototype
    Oracle 列转换为行, 逗号拼接.
    JS中的phototype
  • 原文地址:https://www.cnblogs.com/wydxry/p/15786532.html
Copyright © 2011-2022 走看看