[UVA][二段] 11297 - Census@Morris' Blog|PChome Online 人新台
2012-05-12 15:23:33| 人553| 回0 | 上一篇 | 下一篇

[UVA][二段] 11297 - Census

0 收藏 0 0 站台


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

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.

Notice: There is only a single test case.

Sample Input Sample Output
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
9 0
10 0
9 2


依照自己的想法去做,
//C++ 0.648 Rank 33
#include <stdio.h>
#include <string.h>
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
struct subTree {
    int l, r;
    int maxv, minv;
};
struct segmentTree {
    int l, r;
    subTree subtree[1025];
} mainTree[1025];
void subbuild(int fk, int nk, int l, int r) {
    mainTree[fk].subtree[nk].l = l;
    mainTree[fk].subtree[n].r = r;
    if(l >= r)
        return;
    int m = (l+r)>>1;
    subbuild(fk, nk<<1, l, m);
    subbuild(fk, nk<<1|1, m+1, r);
}
void build(int k, int lr, int rr, int lc, int rc) {
    mainTree[k].l = lr;
    mainTree[k].r = rr;
    subbuild(k, 1, lc, rc);
    if(lr >= rr)
        return;
    int m = (lr+rr)>>1;
    build(k<<1, lr, m, lc, rc);
    build(k<<1|1, m+1, rr, lc, rc);
}
void submodify(int fk, int nk, int x, int y, int v) {
    if(mainTree[fk].subtree[nk].l == y && mainTree[fk].subtree[nk].r == y) {
        if(mainTree[fk].l == x && mainTree[fk].r == x) {
            mainTree[fk].subtree[nk].maxv = v;
            mainTree[fk].subtree[nk].minv = v;
        } else {
            mainTree[fk].subtree[nk].maxv = max(mainTree[fk<<1].subtree[nk].maxv, mainTree[fk<<1|1].subtree[nk].maxv);
            mainTree[fk].subtree[nk].minv = min(mainTree[fk<<1].subtree[nk].minv, mainTree[fk<<1|1].subtree[nk].minv);
        }
    }
    if(mainTree[fk].subtree[nk].l >= mainTree[fk].subtree[nk].r)
        return;
    int m = (mainTree[fk].subtree[nk].l+mainTree[fk].subtree[nk].r)>>1;
    if(y <= m)
        submodify(fk, nk<<1, x, y, v);
    else
        submodify(fk, (nk<<1)+1, x, y, v);
    if(mainTree[fk].l != mainTree[fk].r) {
        mainTree[fk].subtree[nk].maxv = max(mainTree[fk<<1].subtree[nk].maxv, mainTree[fk<<1|1].subtree[nk].maxv);
        mainTree[fk].subtree[nk].minv = min(mainTree[fk<<1].subtree[nk].minv, mainTree[fk<<1|1].subtree[nk].minv);
    } else {
        mainTree[fk].subtree[nk].maxv = max(mainTree[fk].subtree[nk<<1].maxv, mainTree[fk].subtree[nk<<1|1].maxv);
        mainTree[fk].subtree[nk].minv = min(mainTree[fk].subtree[nk<<1].minv, mainTree[fk].subtree[nk<<1|1].minv);
    }
}
void modify(int k, int x, int y, int v) {
    if(mainTree[k].l == x && mainTree[k].r == x) {
        submodify(k, 1, x, y, v);
    }
    if(mainTree[k].l >= mainTree[k].r)
        return;
    int m = (mainTree[k].l+mainTree[k].r)>>1;
    if(x <= m)
        modify(k<<1, x, y, v);
    else
        modify(k<<1|1, x, y, v);
    submodify(k, 1, x, y, v);
}
int ansMax, ansMin;
void subquery(int fk, int nk, int lc, int rc) {
    if(mainTree[fk].subtree[nk].l == lc && mainTree[fk].subtree[nk].r == rc) {
        ansMax = max(ansMax, mainTree[fk].subtree[nk].maxv);
        ansMin = min(ansMin, mainTree[fk].subtree[nk].minv);
        return;
    }
    if(mainTree[fk].subtree[nk].l >= mainTree[fk].subtree[nk].r)
        return;
    int m = (mainTree[fk].subtree[nk].l+mainTree[fk].subtree[nk].r)>>1;
    if(rc <= m)
        subquery(fk, nk<<1, lc, rc);
    else if(lc > m)
        subquery(fk, nk<<1|1, lc, rc);
    else {
        subquery(fk, nk<<1, lc, m);
        subquery(fk, nk<<1|1, m+1, rc);
    }

}
void query(int k, int lr, int rr, int lc, int rc) {
    if(mainTree[k].l == lr && mainTree[k].r == rr) {
        subquery(k, 1, lc, rc);
        return;
    }
    if(mainTree[k].l >= mainTree[k].r)
        return;
    int m = (mainTree[k].l+mainTree[k].r)>>1;
    if(rr <= m)
        query(k<<1, lr, rr, lc, rc);
    else if(lr > m)
        query(k<<1|1, lr, rr, lc, rc);
    else {
        query(k<<1, lr, m, lc, rc);
        query(k<<1|1, m+1, rr,lc, rc);
    }
}
int main() {
    int n, m, q, x, y, v;
    int i, j, x1, x2, y1, y2;
    while(scanf("%d %d", &n, &m) == 2) {
        build(1, 1, n, 1, m);
        for(i = 1; i <= n; i++) {
            for(j = 1; j <= m; j++) {
                scanf("%d", &x);
                modify(1, i, j, x);
            }
        }
        scanf("%d", &q);
        char cmd[3];
        while(q--) {
            scanf("%s", cmd);
            if(cmd[0] == 'q') {
                scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
                ansMax = 0, ansMin = 0xfffffff;
                query(1, x1, x2, y1, y2);
                printf("%d %d\n", ansMax, ansMin);
            } else {
                scanf("%d %d %d %d", &x, &y, &v);
                modify(1, x, y, v);
            }
        }
    }
    return 0;
}

台: Morris
人(553) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][zkw式二段] 11297 - Census
此分上一篇:[UVA][path] 11015 - 05-2 Rendezvous

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86