BZOJ 4066: 简单题

Description

你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令参数限制内容1 x y A1<=x,y<=N,A是正整数将格子x,y里的数字加上A2 x1 y1 x2 y21<=x1<= x2<=N

1<=y1<= y2<=N输出x1 y1 x2 y2这个矩形内的数字和3无终止程序

Input

输入文件第一行一个正整数N。

接下来每行一个操作。每条命令除第一个数字之外,

均要异或上一次输出的答案last_ans,初始时last_ans=0。

Output

对于每个2操作,输出一个对应的答案。

Sample Input

4

1 2 3 3

2 1 1 3 3

1 1 1 1

2 1 1 0 7

3

Sample Output

3

5

HINT

数据规模和约定

1<=N<=500000,操作数不超过200000个,内存限制20M,保证答案在int范围内并且解码之后数据仍合法。

样例解释见OJ2683

新加数据一组,但未重测—-2015.05.24

Solution

KD-Tree

KD-Tree裸题,注意要像替罪羊树那样维护树高来保证复杂度。

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <cstdio>
#include <algorithm>
using namespace std;
typedef int LL;
const double alpha = 0.56;
const int MAXNODE = 200005;
int getint() {
    int r = 0, k = 1; char c = getchar();
    for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1;
    for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
    return r * k;
}
LL ans;
int ScapeGoat, L[2], R[2], dst[MAXNODE], o, n, tol, tot, root, D;
struct Node {
    int mi[2], mx[2], a[2], D, siz, lc, rc;
    LL val, sum;
    inline void PushUp();
    inline bool banlance();
    int & operator [] (int x) { return a[x]; }
} tree[MAXNODE], tmp[MAXNODE];
inline void Node::PushUp() {
        mx[0] = mi[0] = a[0];
        mx[1] = mi[1] = a[1];
        siz = 1; sum = val;
        if (lc) {
            siz += tree[lc].siz;
            sum += tree[lc].sum;
            mx[0] = max(mx[0], tree[lc].mx[0]);
            mx[1] = max(mx[1], tree[lc].mx[1]);
            mi[0] = min(mi[0], tree[lc].mi[0]);
            mi[1] = min(mi[1], tree[lc].mi[1]);
        }
        if (rc) {
            siz += tree[rc].siz;
            sum += tree[rc].sum;
            mx[0] = max(mx[0], tree[rc].mx[0]);
            mx[1] = max(mx[1], tree[rc].mx[1]);
            mi[0] = min(mi[0], tree[rc].mi[0]);
            mi[1] = min(mi[1], tree[rc].mi[1]);
        }
    }
inline bool Node::banlance() { return alpha * siz >= double(max(tree[lc].siz, tree[rc].siz)); }
inline bool operator < (Node a, Node b) { return a[D] < b[D]; }
void DFS(const int &now) {
    if (!now) return;
    DFS(tree[now].lc);
    ++tol;
    dst[tol] = now;
    tmp[tol][0] = tree[now][0];
    tmp[tol][1] = tree[now][1];
    tmp[tol].val = tree[now].val;
    DFS(tree[now].rc);
}
int Build(const int &l, const int &r) {
    if (l > r) return 0;
    int mid = (l + r) >> 1, ret = dst[mid];
    D ^= 1;
    nth_element(tmp+l, tmp+mid, tmp+r+1);
    tree[ret][0] = tmp[mid][0];
    tree[ret][1] = tmp[mid][1];
    tree[ret].val = tmp[mid].val;
    tree[ret].D = D;
    tree[ret].lc = Build(l, mid-1);
    tree[ret].rc = Build(mid+1, r);
    tree[ret].PushUp();
    return ret;
}
inline int Rebuild(const int &root) {
    tol = 0;
    DFS(root);
    return Build(1, tol);
}
void Change(int &now, const LL &val) {
    if (!now) {
        D ^= 1;
        now = ++tot;
        tree[now][0] = L[0];
        tree[now][1] = L[1];
        tree[now].val = val;
        tree[now].D = D;
        tree[now].PushUp();
    } else {
        if (L[0] == tree[now][0] && L[1] == tree[now][1]) {
            tree[now].val += val;
            tree[now].sum += val;
        } else {
            if (L[tree[now].D] < tree[now][tree[now].D]) Change(tree[now].lc, val);
            else Change(tree[now].rc, val);
            tree[now].PushUp();
            if (tree[now].banlance()) {
                if (ScapeGoat) {
                    if (ScapeGoat == tree[now].lc) tree[now].lc = Rebuild(tree[now].lc);
                    else tree[now].rc = Rebuild(tree[now].rc);
                    ScapeGoat = 0;
                }
            } else ScapeGoat = now;
        }
    }
}
void Query(const int &now) {
    if (!now) return;
    if (tree[now].mi[0] > R[0] || tree[now].mx[0] < L[0] || tree[now].mi[1] > R[1] || tree[now].mx[1] < L[1]) return;
    if (L[0] <= tree[now].mi[0] && tree[now].mx[0] <= R[0] && L[1] <= tree[now].mi[1] && tree[now].mx[1] <= R[1]) {
        ans += tree[now].sum;
        return;
    }
    if (L[0] <= tree[now][0] && tree[now][0] <= R[0] && L[1] <= tree[now][1] && tree[now][1] <= R[1])
        ans += tree[now].val;
    Query(tree[now].lc);
    Query(tree[now].rc);
}
int main() {
    n = getint();
    while (true) {
        o = getint();
        if (o == 3) break;
        if (o == 1) {
            L[0] = getint() ^ ans;
            L[1] = getint() ^ ans;
            Change(root, getint() ^ ans);
            if (ScapeGoat) {
                root = Rebuild(root);
                ScapeGoat = 0;
            }
        } else {
            L[0] = getint() ^ ans; L[1] = getint() ^ ans;
            R[0] = getint() ^ ans; R[1] = getint() ^ ans;
            ans = 0; Query(root);
            printf("%d\n", ans);
        }
    }
}