POJ 1195 Mobile phones 二维线段树
来源:互联网 发布:云服务器绑定域名 编辑:程序博客网 时间:2024/05/17 00:50
题目连接:http://poj.org/problem?id=1195
二维线段树 单点更新 区间查询
代码:
#include <cstdio>#include <cstring>using namespace std;const int maxn = 1000 + 50;int sum[4 * maxn][4 * maxn];#define lson rt << 1,l,mid#define rson rt << 1 | 1,mid + 1,rint n;//向上更新void PushUp(int rt,int prt){ sum[prt][rt] = sum[prt][rt << 1] + sum[prt][rt << 1 | 1];}//构造二级线段树void SubBuild(int rt,int l,int r,int prt){ sum[prt][rt] = 0; if(l == r) return; int mid = l + r >> 1; SubBuild(lson,prt),SubBuild(rson,prt);}//构造一级线段树void Build(int rt,int l,int r){ SubBuild(1,1,n,rt); if(l == r) return; int mid = l + r >> 1; Build(lson),Build(rson);}//更新二级线段树void SubUpdate(int rt,int l,int r,int x,int v,int prt){ if(l == r){ sum[prt][rt] += v; } else{ int mid = l + r >> 1; if(x <= mid) SubUpdate(lson,x,v,prt); else SubUpdate(rson,x,v,prt); PushUp(rt,prt); }}//更新一级线段树void Update(int rt,int l,int r,int x,int y,int v){ SubUpdate(1,1,n,x,v,rt); if(l == r) return; int mid = l + r >> 1; if(y <= mid) Update(lson,x,y,v); else Update(rson,x,y,v);}//查询二级线段树 区间查询int SubQuery(int rt,int l,int r,int x1,int x2,int prt){ if(x1 <= l && r <= x2) return sum[prt][rt]; else{ int mid = l + r >> 1; int ret = 0; if(x1 <= mid) ret += SubQuery(lson,x1,x2,prt); if(x2 > mid) ret += SubQuery(rson,x1,x2,prt); return ret; }}//查询一级线段树 单点查询int Query(int rt,int l,int r,int x1,int y1,int x2,int y2){ if(y1 <= l && r <= y2) return SubQuery(1,1,n,x1,x2,rt); else{ int mid = l + r >> 1,ret = 0; if(y1 <= mid) ret += Query(lson,x1,y1,x2,y2); if(y2 > mid) ret += Query(rson,x1,y1,x2,y2); return ret; }}int main(){ int op,a,b,c,d; while(scanf("%d",&op)){ if(op == 0){ scanf("%d",&n); Build(1,1,n); } else if(op == 1){ scanf("%d %d %d",&a,&b,&c); a++,b++; Update(1,1,n,a,b,c); } else if(op == 2){ scanf("%d %d %d %d",&a,&b,&c,&d); a++,b++,c++,d++; printf("%d\n",Query(1,1,n,a,b,c,d)); } else if(op == 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 (二维树状数组或线段树)
- Mobile phones poj1195 二维线段树
- POJ1195 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 二维树状数组
- Mobile Phones poj 1195--二维树状数组
- POJ 1195 Mobile phones【二维树状数组】
- 微机个人笔记-随机存取存储器(RAM)
- DataFrame转化成RDD-[1]反射法
- UCGUI FONT解析
- 类型信息之Class对象
- mongodo window 使用入门
- POJ 1195 Mobile phones 二维线段树
- VS2013上编译并使用Tesseract3.0.3.中文识别技术
- Java 位 运 算 符
- 自定义横向进度条及圆形进度条
- CSS中的div和span标签
- 421. Maximum XOR of Two Numbers in an Array
- android权限大全
- 使用fiddle进行接口测试
- 深入了解mybatis参数