Census

Time Limit: 8 sec

Description

This year, there have been many problems with population calculations, since in some cities, there are many emigrants, or the population growth is very high. Every year the ACM (for Association for Counting Members) conducts a census in each region. The country is divided into N^2 regions, consisting of an N x N grid of regions. Your task is to find the least, and the greatest population in some set of regions. Since in a single year there is no significant change in the populations, the ACM modifies the population counts by some number of inhabitants.

Input

In the first line you will find N (0 <= N <= 500), in following the N lines you will be given N numbers, which represent, the initial population of city C [i, j]. In the following line is the number Q (Q <= 40000), followed by Q lines with queries:

There are two possible queries:

  • “x1 y1 x2 y2” which represent the coordinates of the upper left and lower right of where you must calculate the maximum and minimum change in population.

  • “x y v” indicating a change of the population of city C [x, y] by value v.

Output

For each query, “x1 y1 x2 y2” print in a single line the greatest and least amount of current population. Separated each output by a space.

Notice: There is only a single test case.

Sample Input

5 5

1 2 3 4 5

0 9 2 1 3

0 2 3 4 1

0 1 2 4 5

8 5 3 1 4

4

q 1 1 2 3

c 2 3 10

q 1 1 5 5

q 1 2 2 2

Sample Output

9 0

10 0

9 2


解题心得:

  1. 题意很简单,就是给你一个矩阵,多次询问,每次询问一个子矩阵,输出子矩阵里面的最大值和最小值,也可以改变矩阵中某个点的值。
  2. 一看就是一个线段树,不过是一个矩阵,但是也很简单啊,把线段树的每一行拆出来建一个树,询问的时候就一个树一个树的找,时间给你8s,其实300ms就过了。

/*其实代码差不多就是一个线段树的模板*/
#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
struct NODE
{
int Max,Min;
}node[maxn][maxn<<2];//第一维表示是用矩阵的第几行建立的线段树
int n,m,ans_Max,ans_Min; void updata(int c,int root)
{
node[c][root].Max = max(node[c][root<<1|1].Max,node[c][root<<1].Max);
node[c][root].Min = min(node[c][root<<1|1].Min,node[c][root<<1].Min);
} void build_tree(int c,int root,int l,int r)
{
if(l == r)
{
int temp;
scanf("%d",&temp);
node[c][root].Max = node[c][root].Min = temp;
return ;
}
int mid = (l+r)>>1;
build_tree(c,root<<1,l,mid);
build_tree(c,root<<1|1,mid+1,r);
updata(c,root);
} void init()
{
for(int i=1;i<=n;i++)
build_tree(i,1,1,n);
} void get_ans(int c,int root,int l,int r,int L,int R)
{
if(l == L && r == R)
{
ans_Max = max(ans_Max,node[c][root].Max);
ans_Min = min(ans_Min,node[c][root].Min);
return ;
}
int mid = (L+R)>>1;
if(mid >= r)
get_ans(c,root<<1,l,r,L,mid);
else if(mid < l)
get_ans(c,root<<1|1,l,r,mid+1,R);
else
{
get_ans(c,root<<1,l,mid,L,mid);
get_ans(c,root<<1|1,mid+1,r,mid+1,R);
}
} void change(int c,int va,int root,int pos,int l,int r)
{
if(l == r && l == pos)
{
node[c][root].Max = va;
node[c][root].Min = va;
return ;
}
int mid = (l+r)>>1;
if(mid >= pos)
change(c,va,root<<1,pos,l,mid);
else if(mid < pos)
change(c,va,root<<1|1,pos,mid+1,r);
updata(c,root);
} void query()
{
int m;
scanf("%d",&m);
while(m--)
{
char s[10];
scanf("%s",s);
if(s[0] == 'q')
{
ans_Max = -INF;
ans_Min = INF;
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(int i=x1;i<=x2;i++)
get_ans(i,1,y1,y2,1,n);
printf("%d %d\n",ans_Max,ans_Min);
}
if(s[0] == 'c')
{
int x,y,va;
scanf("%d%d%d",&x,&y,&va);
change(x,va,1,y,1,n);
}
}
} int main()
{
while(scanf("%d",&n) != EOF)
{
init();
query();
}
return 0;
}

最新文章

  1. Python高手之路【四】python函数装饰器
  2. 学习python函数笔记之一
  3. Linux相关指令
  4. (转) c# ExecuteNonQuery() 返回值 -1
  5. javascript对象的理解
  6. 基于visual Studio2013解决算法导论之045斐波那契堆
  7. Unobtrusive Ajax
  8. mtk硬件项目开始关闭蓝牙功能:mtk 硬件ScanCode和keycode应用演示示例
  9. HttpWebResponse远程服务器返回错误: (500) 内部服务器错误
  10. Java字节码浅析(二)
  11. CSS3 的calc()方法的使用
  12. node.js+express+mongodb
  13. Parallel Decision Tree
  14. wpf(怎么跨线程访问wpf控件)
  15. js eval深入
  16. c++ static笔记
  17. 记一次使用MemoryCache不能Get的问题
  18. Java使用TCP聊天程序
  19. 监控Coherence成员的加入和离开集群事件
  20. Spring Boot发布和调用RESTful web service

热门文章

  1. linux笔记学习大全,包括相关软件
  2. Quartz.NET实现作业调度(3.0版本实现)定时执行一个任务
  3. java中过滤器、监听器、拦截器的区别
  4. 北航oo作业第二单元小结
  5. 创建一个自己的GitHub,创建自己的开源项目
  6. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:10.项目介绍之架构(2)
  7. 消除ImageButton背景图片
  8. Windows系统HTTP身份验证方法
  9. hadoop2.4 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
  10. VMware-Ubuntu16.04LTS-安装ssh