1. 程式人生 > >Mosaic 【HDU - 4819】【二維線段樹】

Mosaic 【HDU - 4819】【二維線段樹】

題目連結


  這道題難就只是難在題目難讀,題意讀懂後就是一道普通的二維線段樹更新查詢問題。

  題意:給你一個N*N的矩陣,並且已經建立了初始值,然後給你個點以及L,很多人不解其義,其實就是給你個點,然後查的是以(x, y)為基礎的點,在以左上角(x-L/2, y-L/2)為其中一個端點,另一個端點右下角(x+L/2, y+L/2)的所覆蓋區間內的最大值與最小值的平均值賦給(x, y)這個點,並輸出新值。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define INF 0x3f3f3f3f
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 805;
int N, Q, a[maxN][maxN];
struct node
{
    int maxx, minn;
    node(int a=0, int b=0):maxx(a), minn(b) {}
}tree[maxN<<2][maxN<<2];
void pushup(int rt, int fa)
{
    tree[fa][rt].maxx = max(tree[fa][rt<<1].maxx, tree[fa][rt<<1|1].maxx);
    tree[fa][rt].minn = min(tree[fa][rt<<1].minn, tree[fa][rt<<1|1].minn);
}
void build_In(int rt, int fa, int l, int r, bool flag, int pos)
{
    if(l == r)
    {
        if(flag) tree[fa][rt] = node(a[pos][l], a[pos][l]);
        else
        {
            tree[fa][rt].maxx = max(tree[fa<<1][rt].maxx, tree[fa<<1|1][rt].maxx);
            tree[fa][rt].minn = min(tree[fa<<1][rt].minn, tree[fa<<1|1][rt].minn);
        }
        return;
    }
    int mid = (l + r)>>1;
    build_In(rt<<1, fa, l, mid, flag, pos);
    build_In(rt<<1|1, fa, mid+1, r, flag, pos);
    pushup(rt, fa);
}
void build_Out(int rt, int l, int r)
{
    if(l == r)
    {
        build_In(1, rt, 1, N, true, l);
        return;
    }
    int mid = (l + r)>>1;
    build_Out(rt<<1, l, mid);
    build_Out(rt<<1|1, mid+1, r);
    build_In(1, rt, 1, N, false, 0);
}
void update_In(int rt, int fa, int l, int r, int qy, int val, bool flag)
{
    if(l == r)
    {
        if(flag) tree[fa][rt] = node(val, val);
        else
        {
            tree[fa][rt].maxx = max(tree[fa<<1][rt].maxx, tree[fa<<1|1][rt].maxx);
            tree[fa][rt].minn = min(tree[fa<<1][rt].minn, tree[fa<<1|1][rt].minn);
        }
        return;
    }
    int mid = (l + r)>>1;
    if(qy>mid) update_In(rt<<1|1, fa, mid+1, r, qy, val, flag);
    else update_In(rt<<1, fa, l, mid, qy, val, flag);
    pushup(rt, fa);
}
void update_Out(int rt, int l, int r, int qx, int qy, int val)
{
    if(l == r)
    {
        update_In(1, rt, 1, N, qy, val, true);
        return;
    }
    int mid = (l + r)>>1;
    if(qx>mid) update_Out(rt<<1|1, mid+1, r, qx, qy, val);
    else update_Out(rt<<1, l, mid, qx, qy, val);
    update_In(1, rt, 1, N, qy, val, false);
}
void query_In(int rt, int fa, int l, int r, int ql, int qr, int &maxx, int &minn)
{
    if(ql<=l && qr>=r)
    {
        maxx = max(maxx ,tree[fa][rt].maxx);
        minn = min(minn, tree[fa][rt].minn);
        return;
    }
    int mid = (l + r)>>1;
    if(ql>mid) query_In(rt<<1|1, fa, mid+1, r, ql, qr, maxx, minn);
    else if(qr<=mid) query_In(rt<<1, fa, l, mid, ql, qr, maxx, minn);
    else
    {
        query_In(rt<<1, fa, l, mid, ql, qr, maxx, minn);
        query_In(rt<<1|1, fa, mid+1, r, ql, qr, maxx, minn);
    }
}
void query_Out(int rt, int l, int r, int qlx, int qly, int qrx, int qry, int &maxx, int &minn)
{
    if(qlx<=l && qrx>=r)
    {
        query_In(1, rt, 1, N, qly, qry, maxx, minn);
        return;
    }
    int mid = (l + r)>>1;
    if(qlx>mid) query_Out(rt<<1|1, mid+1, r, qlx, qly, qrx, qry, maxx, minn);
    else if(qrx<=mid) query_Out(rt<<1, l, mid, qlx, qly, qrx, qry, maxx, minn);
    else
    {
        query_Out(rt<<1|1, mid+1, r, qlx, qly, qrx, qry, maxx, minn);
        query_Out(rt<<1, l, mid, qlx, qly, qrx, qry, maxx, minn);
    }
}
int main()
{
    int T;  scanf("%d", &T);
    for(int Cas=1; Cas<=T; Cas++)
    {
        scanf("%d", &N);
        for(int i=1; i<=N; i++) for(int j=1; j<=N; j++) scanf("%d", &a[i][j]);
        build_Out(1, 1, N);
        scanf("%d", &Q);
        printf("Case #%d:\n", Cas);
        while(Q--)
        {
            int x, y, l, lx, ly, rx, ry, mid_d, ans;
            scanf("%d%d%d", &x, &y, &l);
            int MAXX = 0, MINN = INF;
            mid_d = l>>1;
            lx = x - mid_d; ly = y - mid_d; rx = x + mid_d; ry = y + mid_d;
            query_Out(1, 1, N, lx, ly, rx, ry, MAXX, MINN);
            ans = (MAXX + MINN)/2;
            update_Out(1, 1, N, x, y, ans);
            printf("%d\n", ans);
        }
    }
    return 0;
}