【题解】【UVA 11297】Census

来源:互联网 发布:软件授权使用说明书 编辑:程序博客网 时间:2024/06/08 10:50

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 ×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, wich 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:
• ‘q 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. • ‘c x y v’ indicating a change of the population of city C[x,y] by value v.
Output
For each query, ‘q 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 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


题解:

其实就是RMQ的二维版本,线段树即可。

注意由于是二维的,就要用上树套树(线段树套线段树),即主线段树的每一个节点都保存着一棵小线段树。

如果不想写线段树套线段树,也可以用四叉树。。。。

代码:

#include <bits/stdc++.h>using namespace std;int n,m,a[501][501],maxn,minn;struct node{int maxv,minv;}p[2001][2001];void build2(int mo,int ml,int mr,int o,int l,int r){if(l!=r){int m=(l+r)>>1;build2(mo,ml,mr,o<<1,l,m);build2(mo,ml,mr,(o<<1)+1,m+1,r);p[mo][o].maxv=max(p[mo][o<<1].maxv,p[mo][(o<<1)+1].maxv);p[mo][o].minv=min(p[mo][o<<1].minv,p[mo][(o<<1)+1].minv);}else{if(ml==mr)p[mo][o].maxv=p[mo][o].minv=a[ml][l];else{p[mo][o].maxv=max(p[mo<<1][o].maxv,p[(mo<<1)+1][o].maxv);p[mo][o].minv=min(p[mo<<1][o].minv,p[(mo<<1)+1][o].minv);}}}void build1(int o,int l,int r){if(l!=r){int m=(l+r)>>1;build1(o<<1,l,m);build1((o<<1)+1,m+1,r);}build2(o,l,r,1,1,n);}void query2(int o,int l,int r,int ql,int qr,int mo){if(l>=ql && r<=qr){maxn=max(maxn,p[mo][o].maxv);minn=min(minn,p[mo][o].minv);}else{int m=(l+r)>>1;if(ql<=m)query2(o<<1,l,m,ql,qr,mo);if(qr>m)query2((o<<1)+1,m+1,r,ql,qr,mo);}}void query1(int o,int l,int r,int ql,int qr,int l2,int r2){if(l>=ql && r<=qr){query2(1,1,n,l2,r2,o);}else{int m=(l+r)>>1;if(ql<=m)query1(o<<1,l,m,ql,qr,l2,r2);if(qr>m)query1((o<<1)+1,m+1,r,ql,qr,l2,r2);}}void update2(int o,int l,int r,int q,int val,int mo){if(l==r){if(val==-1){p[mo][o].maxv=max(p[mo<<1][o].maxv,p[(mo<<1)+1][o].maxv);p[mo][o].minv=min(p[mo<<1][o].minv,p[(mo<<1)+1][o].minv);return;}p[mo][o].maxv=p[mo][o].minv=val;return;}int m=(l+r)>>1;if(q<=m)update2(o<<1,l,m,q,val,mo);else update2((o<<1)+1,m+1,r,q,val,mo);p[mo][o].maxv=max(p[mo][o<<1].maxv,p[mo][(o<<1)+1].maxv);p[mo][o].minv=min(p[mo][o<<1].minv,p[mo][(o<<1)+1].minv);}void update1(int o,int l,int r,int qx,int qy,int val){if(l==r){update2(1,1,n,qy,val,o);}else{int m=(l+r)>>1;if(qx<=m)update1(o<<1,l,m,qx,qy,val);else update1((o<<1)+1,m+1,r,qx,qy,val);update2(1,1,n,qy,-1,o);}}int main(){scanf("%d\n",&n);for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)scanf("%d",&a[i][j]);build1(1,1,n);scanf("%d\n",&m);char s[5];for(int i=1;i<=m;++i){scanf("%s",s);if(s[0]=='q'){int x,y,xx,yy;scanf("%d%d%d%d",&x,&y,&xx,&yy);minn=2147483647;maxn=0;query1(1,1,n,x,xx,y,yy);printf("%d %d\n",maxn,minn);}else{int x,y,c;scanf("%d%d%d",&x,&y,&c);update1(1,1,n,x,y,c);}}}


原创粉丝点击