zoukankan      html  css  js  c++  java
  • UVA 11297 Census(二维线段树)

    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.

    The 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.

    The 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. 

    题目大意:一个n*m的矩阵上有些数,单点修改,区域查询。

    思路:二维线段树裸题。抄个代码体验一下。

    代码(502MS):

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int INF = 0x7fffffff;
     8 const int MAXN = 2010;
     9 struct IntervaTree2D {
    10     int Max[MAXN][MAXN], Min[MAXN][MAXN], n, m;
    11     int xo, xleaf, x1, y1, x2, y2, x, y, v, vmax, vmin;
    12 
    13     void query1D(int o, int L, int R) {
    14         if(y1 <= L && R <= y2) {
    15             vmax = max(vmax, Max[xo][o]); vmin = min(vmin, Min[xo][o]);
    16         }
    17         else {
    18             int M = (L + R) >> 1;
    19             if(y1 <= M) query1D(o * 2, L, M);
    20             if(M < y2) query1D(o * 2 + 1, M + 1, R);
    21         }
    22     }
    23 
    24     void query2D(int o, int L, int R) {
    25         if(x1 <= L && R <= x2) {xo = o; query1D(1, 1, m);}
    26         else {
    27             int M = (L + R) >> 1;
    28             if(x1 <= M) query2D(o * 2, L, M);
    29             if(M < x2) query2D(o * 2 + 1, M + 1, R);
    30         }
    31     }
    32 
    33     void modify1D(int o, int L, int R) {
    34         if(L == R) {
    35             if(xleaf) {Max[xo][o] = Min[xo][o] = v; return ;}
    36             Max[xo][o] = max(Max[xo * 2][o], Max[xo * 2 + 1][o]);
    37             Min[xo][o] = min(Min[xo * 2][o], Min[xo * 2 + 1][o]);
    38         }
    39         else {
    40             int M = (L + R) >> 1;
    41             if(y <= M) modify1D(o * 2, L, M);
    42             else modify1D(o * 2 + 1, M + 1, R);
    43             Max[xo][o] = max(Max[xo][o * 2], Max[xo][o * 2 + 1]);
    44             Min[xo][o] = min(Min[xo][o * 2], Min[xo][o * 2 + 1]);
    45         }
    46     }
    47 
    48     void modify2D(int o, int L, int R) {
    49         if(L == R) {xo = o; xleaf = 1; modify1D(1, 1, m);}
    50         else {
    51             int M = (L + R) / 2;
    52             if(x <= M) modify2D(o * 2, L, M);
    53             else modify2D(o * 2 + 1, M + 1, R);
    54             xo = o; xleaf = 0; modify1D(1, 1, m);
    55         }
    56     }
    57 
    58     void query() {vmax = -INF; vmin = INF; query2D(1, 1, n);}
    59     void modify() {modify2D(1, 1, n);}
    60 } t;
    61 
    62 int main() {
    63     int n, m, Q;
    64     char op[10];
    65     scanf("%d%d", &n, &m);
    66     t.n = n; t.m = m;
    67     for(int i = 1; i <= n; ++i)
    68         for(int j = 1; j <= m; ++j) {
    69             scanf("%d", &t.v);
    70             t.x = i, t.y = j;
    71             t.modify();
    72         }
    73     scanf("%d", &Q);
    74     while(Q--) {
    75         scanf("%s", op);
    76         if(*op == 'q') {
    77             scanf("%d%d%d%d", &t.x1, &t.y1, &t.x2, &t.y2);
    78             t.query();
    79             printf("%d %d
    ", t.vmax, t.vmin);
    80         } else {
    81             scanf("%d%d%d", &t.x, &t.y, &t.v);
    82             t.modify();
    83         }
    84     }
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    spring data实现自定义的repository实现类,实现跟jpa联通
    SQLYog快捷键大全
    JSP 中EL表达式用法详解
    java中的标记接口
    单元测试中快速执行某个方法
    Spring Boot 以 jar 包方式运行在后台
    Spring Data JPA
    扩展spring data jpa的数据更新方法时注意事项
    在Spring Data JPA 中使用Update Query更新实体类
    高速创建和mysql表相应的java domain实体类
  • 原文地址:https://www.cnblogs.com/oyking/p/3320517.html
Copyright © 2011-2022 走看看