POJ 1195 Mobile phones 题解
来源:互联网 发布:软件存储权限 编辑:程序博客网 时间:2024/06/05 22:40
题意
刚开始有一个S*S的全0矩阵,每一次操作是给某一位置增加一个数(可能是负数)并保持非负(如果为负数就清0)或者询问某个矩形区间的数字之和
思路
用二维树状数组维护区间和,注意输入的坐标范围从0开始所以要先加1,以及保持每个位置的非负性
代码
#include <cstdio>int BIT[1025][1025],cnt[1025][1025];int Ins,S,X,Y,A,L,B,R,T;int lowbit(int x){ return x&(-x);}void add(int x,int y,int d){ for(int i=x;i<=S;i+=lowbit(i)) for(int j=y;j<=S;j+=lowbit(j)) BIT[i][j]+=d;}int sum(int x,int y){ int ans=0; for(int i=x;i>0;i-=lowbit(i)) for(int j=y;j>0;j-=lowbit(j)) ans+=BIT[i][j]; return ans;}int main(){ while(1) { scanf("%d",&Ins); if(Ins==0) scanf("%d",&S); else if(Ins==1) { scanf("%d%d%d",&X,&Y,&A); X++; Y++; if(A<-cnt[X][Y]) A=-cnt[X][Y]; add(X,Y,A); cnt[X][Y]+=A; } else if(Ins==2) { scanf("%d%d%d%d",&L,&B,&R,&T); L++; B++; R++; T++; printf("%d\n",sum(R,T)-sum(L-1,T)-sum(R,B-1)+sum(L-1,B-1)); } else if(Ins==3) break; } return 0;}
阅读全文
0 0
- Poj 1195 Mobile phones 题解
- POJ 1195 Mobile phones 题解
- POJ 1195 Mobile phones
- poj 1195 Mobile phones
- poj 1195 Mobile phones
- poj 1195 mobile phones
- POJ 1195 Mobile phones
- Poj 1195 Mobile phones
- poj 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile phones
- poj 1195 Mobile phones
- poj 1195 Mobile phones
- POJ 1195 Mobile phones
- POJ 1195 Mobile Phones
- python scrapy 之 随机选择user-agent
- 提升Android比于iOS吸引力,谷歌欲借力人工智能
- Oracle 12.2新特性掌上手册
- WebService
- Kotlin在Android上的运用(二)
- POJ 1195 Mobile phones 题解
- 20150519-jQuery ajax()
- SDN学习日记—基于RYU的hub开发1
- Windows/Android/IOS WebRTC音视频总结(二)
- mapreduce编程练习(一)简单的练习 WordCount
- Lowest Common Ancestor of a Binary Tree
- 如何看待 Google 最新的系统 Fuchsia?
- 对LOWORD, HIWORD, LOBYTE, HIBYTE的理解
- Unity在导入官方Assets出现“提示上下文不存在SessionState”问题的解决办法