博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mobile phones·POJ1195
阅读量:5888 次
发布时间:2019-06-19

本文共 4291 字,大约阅读时间需要 14 分钟。

Mobile phones

Time Limit: 5000MS   Memory Limit: 65536K

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 
Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 
The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 
Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 41 1 2 32 0 0 2 2 1 1 1 21 1 2 -12 1 1 2 3 3

Sample Output

34

Source

 
好像没有标记下传的东西= = 不过想想也知道lazy肯定是打在y轴上的。。
 
Codes:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using namespace std;10 const int N = 1025;11 #define Ch1 ((i)<<1)12 #define Ch2 ((Ch1)|1)13 #define For(i,n) for(int i=1;i<=n;i++)14 #define Rep(i,l,r) for(int i=l;i<=r;i++)15 16 struct tnodey{17 short l,r,mid;18 int sum;19 };20 21 struct tnodex{22 short l,r,mid;23 tnodey y[N*3];24 }T[N*3];25 26 int op,n,A;27 int x1,y1,x2,y2,x,y;28 29 void BuildY(int root,int i,int l,int r){30 T[root].y[i].l = l;T[root].y[i].r = r; T[root].y[i].mid = (l+r)>>1;31 T[root].y[i].sum = 0;32 if(l==r) return;33 BuildY(root,Ch1,l,T[root].y[i].mid); BuildY(root,Ch2,T[root].y[i].mid+1,r);34 }35 36 void BuildX(int i,int l,int r){37 BuildY(i,1,0,n);38 T[i].l = l; T[i].r = r; T[i].mid = (l+r)>>1;39 if(l==r) return;40 BuildX(Ch1,l,T[i].mid);BuildX(Ch2,T[i].mid+1,r);41 }42 43 void ModifyY(int root,int i,int x,int delta){44 if(T[root].y[i].l==T[root].y[i].r) {T[root].y[i].sum += delta;return;}45 if(x<=T[root].y[i].mid) ModifyY(root,Ch1,x,delta);46 else ModifyY(root,Ch2,x,delta);47 T[root].y[i].sum = T[root].y[Ch1].sum + T[root].y[Ch2].sum;48 }49 50 void ModifyX(int i,int x,int delta){51 ModifyY(i,1,y,delta);52 if(T[i].l==T[i].r) return;53 if(x<=T[i].mid) ModifyX(Ch1,x,delta);54 else ModifyX(Ch2,x,delta);55 }56 57 int queryY(int root,int i,int l,int r){58 if(l<=T[root].y[i].l&&T[root].y[i].r<=r) return T[root].y[i].sum;59 if(r<=T[root].y[i].mid) return queryY(root,Ch1,l,r);else60 if(l>T[root].y[i].mid) return queryY(root,Ch2,l,r);else61 return queryY(root,Ch1,l,T[root].y[i].mid) + queryY(root,Ch2,T[root].y[i].mid+1,r);62 }63 64 int queryX(int i,int l,int r){65 if(l<=T[i].l&&T[i].r<=r) return queryY(i,1,y1,y2);66 if(r<=T[i].mid) return queryX(Ch1,l,r);else67 if(l>T[i].mid) return queryX(Ch2,l,r);else68 return queryX(Ch1,l,T[i].mid) + queryX(Ch2,T[i].mid+1,r);69 }70 71 void init(){72 scanf("%d%d",&op,&n);n--;73 BuildX(1,0,n);74 while(op!=3){75 scanf("%d",&op);76 if(op==1){77 scanf("%d%d%d",&x,&y,&A);78 ModifyX(1,x,A);79 }80 if(op==2){81 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);82 printf("%d\n",queryX(1,x1,x2));83 }84 }85 }86 87 int main(){88 init();89 return 0;90 }

 

转载于:https://www.cnblogs.com/zjdx1998/p/3818637.html

你可能感兴趣的文章
curl 向远程服务器传输file文件
查看>>
[Java]读取文件方法大全
查看>>
【NopCommerce源码架构学习-二】单例模式实现代码分析
查看>>
[知识点]线段树
查看>>
动态规划大合集II
查看>>
MySQL忘记密码后重置密码(Mac )
查看>>
网站访问量统计案例
查看>>
web.xml中的url-pattern映射规则
查看>>
图像的下采样Subsampling 与 上采样 Upsampling
查看>>
SQL 数据类型
查看>>
支付宝接口调用,支付操作
查看>>
如何在ashx页面获取Session值 (仅供个人参考)
查看>>
cookie与session
查看>>
Linux经常用到的命令以及快捷键
查看>>
计算题:挣值、预测、沟通、盈亏平衡点、
查看>>
ios一个自定义的下拉多选菜单
查看>>
存在性问题
查看>>
js 实现 aop
查看>>
AES加密在windows与linux平台下显示结果不同,解决方案
查看>>
别让持续交付自动化交付bug
查看>>