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

本文提供了一道 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;
}
代码

 

转载于:https://www.cnblogs.com/zxhl/p/5040497.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值