BZOJ 1452: [JSOI2009]Count 二维树状数组

1452: [JSOI2009]Count

Description

Input

Output

Sample Input



Sample Output

1
2

HINT



Source

 

题解:设定C[101][N][N] 树状数组上价值为val的lowbit数组

//meek
///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//****************************************

const int N=305;
const ll INF = 1ll<<61;
const int inf = 1<<31;
const int mod= 1000000007;

int C[111][315][315],a[514][505],n,m;

void update(int x,int y,int c,int val) {
    for(int i=x;i<=N;i+=i&(-i)) {
        for(int j=y;j<=N;j+=j&(-j)) {
            C[c][i][j]+=val;
        }
    }
}
int ask(int x,int y,int c) {
    int sum=0;
    for(int i=x;i>=1;i-=i&(-i)) {
        for(int j=y;j>=1;j-=j&(-j)) {
            sum+=C[c][i][j];
        }
    }
    return sum;
}
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) scanf("%d",&a[i][j]),update(i,j,a[i][j],1);
    }
    int q,x,y,t,c,x1,x2,y1,y2;
    scanf("%d",&q);
    for(int i=1;i<=q;i++) {
        scanf("%d",&t);
        if(t==1) {
            scanf("%d%d%d",&x,&y,&c);
            update(x,y,a[x][y],-1);
            a[x][y]=c;
            update(x,y,c,1);
        }
        else {
            scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&c);
            printf("%d\n",(ask(x2,y2,c)-ask(x1-1,y2,c)-ask(x2,y1-1,c)+ask(x1-1,y1-1,c)));
        }
    }
    return 0;
}
代码

 

posted @ 2015-12-11 23:10  meekyan  阅读(182)  评论(0编辑  收藏  举报