Census(二维线段树)

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

题目思路:传说中的树套树,对矩阵的每一个竖线建一个线段树维护区间的最大数,然后对x轴建一个线段树,其中他的每一个节点都是一个线段树。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 2010;
const int maxn2= 50005;
const int inf =0x3f3f3f3f;
int n;
int w[maxn][maxn],ma[maxn][maxn],mi[maxn][maxn];
int m=0,minn=inf;char a;int b,c,e,f;int flag=0;
void push_up1(int ro){
    for(int i=1;i<=n*4;i++){
        ma[ro][i]=max(ma[ro<<1][i],ma[ro<<1|1][i]);
        mi[ro][i]=min(mi[ro<<1][i],mi[ro<<1|1][i]);
    }
}
void push_up2(int rx,int ro){
    ma[rx][ro]=max(ma[rx][ro<<1],ma[rx][ro<<1|1]);
    mi[rx][ro]=min(mi[rx][ro<<1],mi[rx][ro<<1|1]);
}
void build_tree2(int rx,int x,int ro,int l,int r){
    if(l==r){
        ma[rx][ro]=mi[rx][ro]=w[x][l]; return ;
    }
    int mid=(l+r)>>1;
    build_tree2(rx,x,ro<<1,l,mid);
    build_tree2(rx,x,ro<<1|1,mid+1,r);
    push_up2(rx,ro);
}
void build_tree(int ro,int l,int r){
    if(l==r){
        build_tree2(ro,l,1,1,n); return;
    }
    int mid=(l+r)>>1;
    build_tree(ro<<1,l,mid);
    build_tree(ro<<1|1,mid+1,r);
    push_up1(ro);
}
void update2(int rx,int ro,int l,int r){
    if(l==r&&c==l){
        if(flag){
            ma[rx][ro]=mi[rx][ro]=e; return ;
        }
        ma[rx][ro]=max(ma[rx<<1][ro],ma[rx<<1|1][ro]); //注意要把对x轴线段树的子节点的影响向上传递
        mi[rx][ro]=min(mi[rx<<1][ro],mi[rx<<1|1][ro]);
        return;
    }
    int mid=(l+r)>>1;
    if(c<=mid) update2(rx,ro<<1,l,mid);
    else update2(rx,ro<<1|1,mid+1,r);
    push_up2(rx,ro);
}
void update1(int ro,int l,int r,int pos){
    if(l==r&&r==pos){
        flag=1;update2(ro,1,1,n);return;
    }
    int mid=(l+r)>>1;
    if(pos<=mid) update1(ro<<1,l,mid,pos);
    else update1(ro<<1|1,mid+1,r,pos);
    flag=0; update2(ro,1,1,n);
}
void query2(int rx,int ro,int l,int r,int nl,int nr){
    if(l==nl&&r==nr){
        m=max(m,ma[rx][ro]);minn=min(mi[rx][ro],minn);
        return;
    }
    int mid=(l+r)>>1;
    if(mid>=nr) query2(rx,ro<<1,l,mid,nl,nr);
    else if(mid<nl) query2(rx,ro<<1|1,mid+1,r,nl,nr);
    else {
        query2(rx,ro<<1,l,mid,nl,mid);
        query2(rx,ro<<1|1,mid+1,r,mid+1,nr);
    }
}
void query1(int ro,int l,int r,int nl,int nr){
    if(l==nl&&r==nr){
        query2(ro,1,1,n,c,f); return;
    }
    int mid=(l+r)>>1;
    if(nr<=mid) query1(ro<<1,l,mid,nl,nr);
    else if(nl>mid) query1(ro<<1|1,mid+1,r,nl,nr);
    else {
        query1(ro<<1,l,mid,nl,mid);
        query1(ro<<1|1,mid+1,r,mid+1,nr);
    }
}
int main()
{
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&w[i][j]);
        build_tree(1,1,n);
        int q;scanf("%d",&q);
        while(q--){
            scanf(" %c",&a);
            if(a=='q'){
                scanf("%d%d%d%d",&b,&c,&e,&f);
                m=0,minn=inf;
                query1(1,1,n,b,e);
                printf("%d %d\n",m,minn);
            }else{
                scanf("%d%d%d",&b,&c,&e);
                update1(1,1,n,b);
            }
        }
    }
    return 0;
}

发布了37 篇原创文章 · 获赞 27 · 访问量 1084
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览