HDU 2642 树状数组
来源:互联网 发布:mac如何卸载java 编辑:程序博客网 时间:2024/06/05 04:35
点击打开链接
题意:给个二维矩阵,矩阵有0或者1两个值,然后有三个操作,Q问区间和,剩下两个是更新点的值
思路:更新点的值直接更新就行了,然后询问区间和的时候就处理一下,每次问的是X1,Y1到X2,Y2的区间和,而树状数组的和是从1,1开始的,所以总的减去多于的在加上多减去的就OK了
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll INF=0x3f3f3f3f3f3f3f3fll;const int maxn=1010;int sum[maxn][maxn],n;bool vis[maxn][maxn];int lowbit(int x){return x&(-x);}void update(int x,int y,int val){ for(int i=x;i<maxn;i+=lowbit(i)){ for(int j=y;j<maxn;j+=lowbit(j)){ sum[i][j]+=val; } }}int msum(int x,int y){ int res=0; for(int i=x;i>0;i-=lowbit(i)){ for(int j=y;j>0;j-=lowbit(j)){ res+=sum[i][j]; } } return res;}int main(){ int x1,x2,y1,y2; char ch[10]; while(scanf("%d",&n)!=-1){ memset(sum,0,sizeof(sum)); memset(vis,0,sizeof(vis)); while(n--){ scanf("%s",ch); if(ch[0]=='B'){ scanf("%d%d",&x1,&y1); x1++;y1++; if(vis[x1][y1]) continue; vis[x1][y1]=1;update(x1,y1,1); }else if(ch[0]=='D'){ scanf("%d%d",&x1,&y1); x1++;y1++; if(vis[x1][y1]) update(x1,y1,-1); vis[x1][y1]=0; }else if(ch[0]=='Q'){ scanf("%d%d%d%d",&x1,&x2,&y1,&y2); x1++;x2++;y1++;y2++; if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2); int ans=msum(x2,y2)-msum(x1-1,y2)-msum(x2,y1-1)+msum(x1-1,y1-1); printf("%d\n",ans); } } } return 0;}
1 0
- HDU 2642 树状数组
- HDU 2642 二维树状数组
- hdu 2642 二维树状数组
- HDU 2642 二维树状数组
- hdu 2642stars(二维树状数组)
- HDU 2642 stars 二维树状数组
- HDU 2642 二维树状数组问题
- hdu 2642 stars (二维树状数组)
- HDU 2642 Stars (二维树状数组)
- HDU 2642 Stars(二维树状数组)
- hdu 2642 Stars(二维树状数组)
- hdu- 2642 Stars 二维树状数组
- HDU 2642 Stars 二维树状数组应用
- HDU 2642(树状数组模板二维)
- HDU 2642 Stars 二维树状数组
- HDU 2642 Stars [二维树状数组]【数据结构】
- hdu 2642 star(二维树状数组)
- hdu 2642 (summerIII J) 二维树状数组
- 设计模式与架构的核心概念乃是抽象
- 用url请求数据并用pull解析
- 神经网络优化算法选择
- imageview 自定义
- Android系统中自定义按键的短按、双击、长按事件
- HDU 2642 树状数组
- Div span 标签详解
- AssetBundle资料整理
- Spring源码深度解析(八)解析及注册BeanDefinitions
- 用NSURLSession发送网络请求
- IDEA 16 JRebel Spring Tomcat热部署
- 只获取城市名称定位(简单)
- Servlet基本接口与类
- SlidingMenu ViewPager Fragment GSON 网络解析 网络请求 ListView