Practice for KD Tree

鏈接:https://ac.nowcoder.com/acm/problem/201927
來源:牛客網

題目描述
It’s time to have a break and implement something.
You are given an n×nn\times n matrix A{A}. All the elements are zero initially.
First, you need to perform m1m_1 range addition operations. For each operations, you are given x1,y1,x2,y2,w (1x1x2n,1y1y2n)x_1, y_1, x_2, y_2, w~(1\leq x_1\leq x_2 \leq n, 1\leq y_1 \leq y_2\leq n).You need to add ww to all the elements Ai,jA_{i,j} where x1ix2x_1\leq i\leq x_2 and y1jy2y_1\leq j \leq y_2.
Then you need to perform m2m_2 range maximum queries. For each operations, you are given x1,y1,x2,y2 (1x1x2n,1y1y2n)x_1, y_1, x_2, y_2~(1\leq x_1\leq x_2 \leq n, 1\leq y_1 \leq y_2\leq n). You need to answer the maximum element among the elements Ai,jA_{i,j} that satisify x1ix2x_1\leq i\leq x_2 and y1jy2y_1\leq j \leq y_2.
輸入描述:
The first line contains three integers
n,m1,m2 (1n2000,1m15×104,1m25×105)n, m_1, m_2~(1\leq n\leq 2000, 1\leq m_1 \leq 5 \times 10^4, 1\leq m_2 \leq 5 \times 10^5)
Each of the following m1m_1 lines contains five integers
x1,y1,x2,y2,w (1x1x2n,1y1y2n,1w109)x_1, y_1, x_2, y_2, w~(1\leq x_1\leq x_2\leq n, 1\leq y_1 \leq y_2 \leq n, 1\leq w \leq 10^9)
Each of the following m2m_2 lines contains four integers
x1,y1,x2,y2 (1x1x2n,1y1y2n)x_1, y_1, x_2, y_2~(1\leq x_1\leq x_2\leq n, 1\leq y_1 \leq y_2 \leq n)
輸出描述:
Output m2m_2 lines, each of which contains one integer.
示例1

輸入
5 5 5
1 1 4 5 4
4 1 4 1 10
1 3 3 3 3
1 1 5 5 8
2 4 4 5 8
2 1 2 1
4 1 5 4
1 2 3 5
2 1 5 3
1 3 5 5
輸出
12
22
20
22
20

看起來像是線段樹維護區間最值,不過是二維的。對着線段樹的板子yy了一個二維線段樹,沒想到真就有這個東西。
不過需要加一個剪枝優化,不然過不了。

#include<bits/stdc++.h>
 
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
 
inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}
 
const int N = 2e3 + 10;
ll a[N][N];
 
struct Tree {
#define son(x) (p*4-2+x)
    struct {
        int l1, r1, l2, r2;
        ll dat;
    } t[N * N * 8];
 
    void build(int p, int l1, int r1, int l2, int r2) {
        if (r1 < l1 || r2 < l2)return;
        t[p].l1 = l1, t[p].r1 = r1, t[p].l2 = l2, t[p].r2 = r2;
        if (l1 == r1 && l2 == r2) {
            t[p].dat = a[l1][l2];
            return;
        }
        int mid1 = (l1 + r1) >> 1, mid2 = (l2 + r2) >> 1;
        build(son(0), l1, mid1, l2, mid2);
        build(son(1), l1, mid1, mid2 + 1, r2);
        build(son(2), mid1 + 1, r1, l2, mid2);
        build(son(3), mid1 + 1, r1, mid2 + 1, r2);
        t[p].dat = max(max(t[son(0)].dat, t[son(1)].dat), max(t[son(2)].dat, t[son(3)].dat));
    }
 
    void change(int p, int x, int y, ll v) {
        if (t[p].l1 == t[p].r1 && t[p].l2 == t[p].r2) {
            t[p].dat = v;
            return;
        }
        int mid1 = (t[p].l1 + t[p].r1) >> 1, mid2 = (t[p].l2 + t[p].r2) >> 1;
        if (x <= mid1) {
            if (y <= mid2)change(son(0), x, y, v);
            else change(son(1), x, y, v);
        } else {
            if (y <= mid2)change(son(2), x, y, v);
            else change(son(3), x, y, v);
        }
        t[p].dat = max(max(t[son(0)].dat, t[son(1)].dat), max(t[son(2)].dat, t[son(3)].dat));
    }
    ll ans;
    ll ask(int p, int l1, int r1, int l2, int r2) {
        if(t[p].dat<=ans)return ans;
        if (l1 <= t[p].l1 && r1 >= t[p].r1 && l2 <= t[p].l2 && r2 >= t[p].r2)return ans=t[p].dat;
        int mid1 = (t[p].l1 + t[p].r1) >> 1, mid2 = (t[p].l2 + t[p].r2) >> 1;
        if (l1 <= mid1) {
            if (l2 <= mid2)ans = max(ans, ask(son(0), l1, r1, l2, r2));
            if (r2 > mid2 && mid2 < t[p].r2)ans = max(ans, ask(son(1), l1, r1, l2, r2));
        }
        if (r1 > mid1 && mid1 < t[p].r1) {
            if (l2 <= mid2)ans = max(ans, ask(son(2), l1, r1, l2, r2));
            if (r2 > mid2 && mid2 < t[p].r2)ans = max(ans, ask(son(3), l1, r1, l2, r2));
        }
        return ans;
    }
} tr;
 
int n, m1, m2;
 
 
int main() {
    n = qr(), m1 = qr(), m2 = qr();
    while (m1--) {
        int x1 = qr(), y1 = qr(), x2 = qr(), y2 = qr(), w = qr();
        a[x1][y1] += w;
        a[x1][y2 + 1] -= w;
        a[x2 + 1][y1] -= w;
        a[x2 + 1][y2 + 1] += w;
    }
    repi(i, 1, n) repi(j, 1, n)a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
    tr.build(1, 1, n, 1, n);
    while (m2--) {
        int x1 = qr(), y1 = qr(), x2 = qr(), y2 = qr();
        tr.ans=0;
        pl(tr.ask(1, x1, x2, y1, y2));
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章