BZOJ1452 Count

BZOJ1452 Count

题目传送门

题解

看到这题\(c\)的数据范围之后才发现这题是个水题,开100个二维树状数组记录每个颜色的个数,之后就能做到\(log^2n\)的询问和修改了。

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
#define PAUSE printf("Press Enter key to continue..."); fgetc(stdin);
#define lowbit(x) x&(-x)
const int maxn=305;
int n,m,q;
int a[maxn][maxn];
/*==================Define Area================*/
struct TreeArray {
    int t[maxn][maxn];
    void Modify(int x,int y,int d) {
        for(int i=x;i<=n;i+=lowbit(i)) {
            for(int j=y;j<=m;j+=lowbit(j)) {
                t[i][j]+=d;
            }
        }
    }
    int Sum(int x,int y) {
        int ans=0;
        for(int i=x;i;i-=lowbit(i)) {
            for(int j=y;j;j-=lowbit(j)) {
                ans+=t[i][j];
            }
        }
        return ans;
    }
}T[105];

int main() {
    read(n);read(m);
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            read(a[i][j]);
            T[a[i][j]].Modify(i,j,1);
        }
    } 
    read(q);
    for(int i=1;i<=q;i++) {
        int opt;
        read(opt);
        if(opt==1) {
            int x,y,c;
            read(x);read(y);read(c);
            T[a[x][y]].Modify(x,y,-1);
            T[c].Modify(x,y,1);
            a[x][y]=c;
        }
        else {
            int lx,ly,rx,ry,c;
            read(lx);read(rx);read(ly);read(ry);read(c);
            int ans=T[c].Sum(rx,ry)+T[c].Sum(lx-1,ly-1);
            ans-=(T[c].Sum(lx-1,ry)+T[c].Sum(rx,ly-1));
            printf("%d\n",ans);
        }
    }
    return 0;
}
/*
3 3
1 2 3
3 2 1
2 1 3
3
2 1 2 1 2 1
1 2 3 2
2 2 3 2 3 2
*/

转载于:https://www.cnblogs.com/Apocrypha/p/9435641.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值