二维树状数组(单点更新+区间查询)

http://poj.org/problem?id=1195

题意:对矩阵进行操作。

 

 https://www.cnblogs.com/aininot260/p/9336527.html

//#include<bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
typedef long long ll ;
#define int ll
#define mod 1000000007
#define gcd __gcd
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//ll lcm(ll a , ll b){return a*b/gcd(a,b);}
//ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define SC scanf
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
using namespace std;
const int N = 1e6+100;
const int maxn = 2e3+9;
int c[maxn][maxn];
int n ;

int lowerbit(int x){
    return x&(-x);
}


void add(int x , int y , int val){
    while(x <= n){
        int j = y ;
        while(j <= n){
            c[x][j] += val;
            j += lowerbit(j);
        }
        x += lowerbit(x);
    }
}
int getsum(int x , int y){
    int ans = 0 ;
    while(x){
        int j = y ;
        while(j){
            ans += c[x][j] ;
            j -= lowerbit(j);
        }
        x -= lowerbit(x);
    }
    return ans ;
}


void solve(){
    int t ;
    while(~scanf("%lld" , &t) && t <= 2){
        if(t == 0){
            cin >> n ;
        }else if(t == 1){
            int x , y , val;
            scanf("%lld%lld%lld" , &x , &y , &val);
            x++ , y++;
            add(x , y , val);
        }else{
            int x1 , x2 , y1 , y2 ;
            scanf("%lld%lld%lld%lld" , &x1 , &y1 , &x2 , &y2);
            x1++ , y1++ , x2++ , y2++;
            cout << getsum(x2 , y2) - getsum(x1-1 , y2) - getsum(x2 , y1-1) + getsum(x1-1 , y1-1) << endl;
        }
    }
}

signed main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0); cout.tie(0);
    //cnt = 0;
    //int t ;
    //scanf("%lld" , &t);
    //while(t--){
        solve();
    //}
}

 

posted @ 2020-03-01 18:39  无名菜鸟1  阅读(176)  评论(0编辑  收藏  举报