BZOJ1452 Count

mac2022-06-30  164

目录

BZOJ1452 Count题解code

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+'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

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)