二维数点 | 树状数组

· · 个人记录

1.【UR #12】实验室外的攻防战

题解

const int N = 1e5 + 10;

int n, m, w[N], a[N], b[N], tr[N];

void add(int x, int c) {for (; x <= n; x += x & -x) chkmax(tr[x], c);}
int pre(int x) {int s = 0; for (; x; x -= x & -x) chkmax(s, tr[x]); return s;}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++ ) read(m), a[m] = i;
    for (int i = 1; i <= n; i ++ ) read(m), b[m] = i;
    for (int i = 1; i <= n; i ++ ) {
        if (pre(a[i]) > b[i]) return puts("NO"), 0;
        add(a[i], b[i]);
    }   
    puts("YES");
    return 0;
}

2.P6958 [NEERC2017]The Great Wall

题解

注意sum时下标可能会越界

#define pi pair<long long, int>

const int N = 3e4 + 10;

int n, m, r, k;
LL I, A[3][N];
pi f[N], g[N], h[N];

namespace BIT {
    int tr[N];

    void clear() {memset(tr, 0, (m + 1) << 2);}
    void add(int x) {for (; x <= m; x += x & -x) tr[x] ++ ;}
    int sum(int x) { int s = 0; for (; x >= 1; x &= x - 1) s += tr[x]; return s;}
}

int Count(LL s) {
    using namespace BIT;
    int res = 0;
    clear();
    for (int j = m, i = 1; j >= 1; j -- ) {
        while (f[j].fi + f[i].fi <= s && i <= m) add(f[i].se), i ++ ;
        res += sum(f[j].se - r);
    }   
    clear();
    for (int j = m, i = 1; j >= 1; j -- ) {
        while (g[i].fi + h[j].fi <= s && i <= m) add(g[i].se), i ++ ;
        res += sum(h[j].se - 1) - sum(h[j].se - r);
    }
    return res;
}

int main() {
    cin >> n >> r >> k; m = n - r + 1;
    for (int i = 1; i <= n; i ++ ) read(A[0][i]), I += A[0][i];
    for (int i = 1; i <= 2; i ++ )
        for (int j = 1; j <= n; j ++ )
            read(A[i][j]), A[i][j] += A[i][j - 1] - A[0][j];
    for (int i = 1; i <= m; i ++ )
        f[i] = {A[1][i + r - 1] - A[1][i - 1], i},
        g[i] = {A[2][i + r - 1] - A[1][i + r - 1] - A[1][i - 1], i},
        h[i] = {A[1][i - 1] + A[1][i + r - 1] - A[2][i - 1], i};
    sort(f + 1, f + 1 + m), sort(g + 1, g + 1 + m), sort(h + 1, h + 1 + m);
    LL l = 0, r = A[2][n] + I;
    while (l < r) {
        LL mid = l + r >> 1;
        if (Count(mid) >= k) r = mid;
        else l = mid + 1;
    }
    cout << l + I << "\n";
    return 0;
}

3.P3369 【模板】普通平衡树

const int N = 1e5 + 54;

int n, m, A[N], tot;
int tr[N];
pi q[N];

inline void add(int x, int c) {for (; x <= tot; x += x & -x) tr[x] += c;}
inline int sum(int x) {int s = 0; for (; x; x -= x & -x) s += tr[x]; return s;}

int kth(int k) {
    int l = 0, r;
    for (int i = 17; i >= 0; i -- ) {
        if ((r = l + (1 << i)) <= tot && tr[r] < k)
            k -= tr[l = r];
    }
    return A[l + 1];
}

inline int prev(int x) {int k = sum(x - 1); return kth(k);}

inline int succ(int x) {int k = sum(x) + 1; return kth(k);}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++ ) {
        int a, b; read(a), read(b);
        A[i] = b; q[i] = {a, b};
    }
    A[n + 1] = INT_MIN, A[n + 2] = INT_MAX;
    sort(A + 1, A + 1 + (n + 2));
    tot = unique(A + 1, A + 1 + (n + 2)) - A - 1;
    for (int i = 1; i <= n; i ++ ) {
        int op = q[i].fi;
        int x = op != 4 ? lower_bound(A + 1, A + tot, q[i].se) - A : q[i].se;
        switch (op) {
            case 1: add(x, 1); break; 
            case 2: add(x, -1); break;
            case 3: cout << sum(x - 1) + 1 << "\n"; break;
            case 4: cout << kth(x) << "\n"; break;
            case 5: cout << prev(x) << "\n"; break;
            case 6: cout << succ(x) << "\n"; break;
        }
    }
    return 0;
}

4.AT2436 城壁 (Rampart)

const int N = 4e3 + 54;

int n, m, L[N][N], R[N][N], U[N][N], D[N][N], C, P, H;
char A[N][N];
int tr[N << 1], cnt, idx;
vector<pi> dg[N << 1];
vi q[N << 1];

inline void add(int x, int v) {for (; x >= 1; x -= x & -x) tr[x] += v;}
inline int sum(int x) {int s = 0; for (; x <= H; x += x & -x) s += tr[x]; return s;}

int main() {
    cin >> n >> m >> C >> P; H = max(n, m) * 2;
    memset(A, 1, sizeof A);
    for (int i = 1; i <= P; i ++ ) {
        int a, b; read(a), read(b);
        A[a][b] = 0;
    }
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ )
            if (A[i][j]) L[i][j] = L[i][j - 1] + 1, U[i][j] = U[i - 1][j] + 1;
    for (int i = n; i >= 1; i -- )
        for (int j = m; j >= 1; j -- )
            if (A[i][j]) R[i][j] = R[i][j + 1] + 1, D[i][j] = D[i + 1][j] + 1;
    for (int i = 1; i <= n; i ++ ) 
        for (int j = 1; j <= m; j ++ ) {
            chkmin(R[i][j], U[i][j]), chkmin(L[i][j], D[i][j]);
            dg[i + j].emplace_back(i, j);
        }
    LL res = 0;
    for (int k = 2; k <= n + m; k ++ ) {
        memset(tr, 0, (H + 1) << 2);
        for (auto t : dg[k]) {
            int r = t.fi, c = t.se, r1 = -R[r][c] + r, r2 = -C + r + 1;
            if (A[r][c] && R[r][c] >= C) {
                if (r1 > 0) q[r1].emplace_back(-r);
                if (r2 > 0) q[r2].emplace_back(r);
            }
        }
        for (auto t : dg[k]) {
            int r = t.fi, c = t.se;
            if (A[r][c]) {
                add(r + L[r][c] - 1, 1);
            } 
            for (auto v : q[r]) {
                v < 0 ? res -= sum(-v) : res += sum(v);
            }
        }
        for (int i = 1; i <= n; i ++ ) if (q[i].size()) q[i].clear();
    }
    cout << res << "\n";
    return 0;
}

5.P6619 [省选联考 2020 A/B 卷] 冰火战士

const int N = 2e6 + 54;

int n, m, A[N], idx, tot, tr1[N], tr2[N], ln, B[N];
int offset;

struct Q {
    int t, x, y;
} q[N];

inline void add(int x, int v, int *tr) {for (; x <= tot; x += x & -x) tr[x] += v;}
inline int sum(int x, int *tr) {int s = 0; for (; x >= 1; x &= x - 1) s += tr[x]; return s;}

int main() {
    cin >> m;
    for (int i = 1; i <= m; i ++ ) {
        int op, a, b, c; read(op), read(a);
        if (op == 1) {
            read(b), read(c);
            q[i] = {a, b, c};
            A[ ++ idx] = b;
        } else {
            q[i] = q[a];
            q[i].y *= -1;
        }
    }
    sort(A + 1, A + 1 + idx);
    tot = unique(A + 1, A + 1 + idx) - 1 - A; ln = __lg(tot + 1);
    for (int i = 1; i <= m; i ++ ) {
        int t = q[i].t, z = q[i].x, y = q[i].y, pos = -1;
        int x = lower_bound(A + 1, A + 1 + tot, z) - A;
        // assert(x >= 1 && x <= tot);
        if (t == 0) {
            add(x, y, tr1); //ice
        } else {
            add(x + 1, -y, tr2); //fire
            B[x + 1] -= y;
            offset += y;
        }
        int l = 0, r, s1 = 0, s2 = offset, res = -1;
        for (int b = ln; b >= 0; b -- ) {
            if ((r = (1 << b) + l) > tot) continue;
            if (s1 + tr1[r] <= s2 + tr2[r]) s1 += tr1[r], s2 += tr2[l = r];
        }
        if (s1 && s2) res = s1, pos = l;
        if (l < tot) {
            s2 += B[ ++ l]; //tr2表示的是一段区间的值 此时只需要单点的值
            if (~res && s2 < res) {printf("%d %lld\n", A[pos], res * 2ll); continue;}
            else if (!s2) {puts("Peace"); continue;}
            res = s2, l = 0, s2 = offset;
            for (int b = ln; b >= 0; b -- ) {
                if ((r = (1 << b) + l) > tot) continue;
                if (s2 + tr2[r] >= res) s2 += tr2[l = r];
            }
            pos = l;
        }
        if (!pos || !~res) puts("Peace"); 
        else printf("%d %lld\n", A[pos], res * 2ll);
    }
    return 0;
}

二维树状数组

1.P4514 上帝造题的七分钟

板子题

const int N = 2e3 + 54;

int n, m;
int tr[4][N][N];
char op[2];

inline void add(int x, int y, int v, int o) {
    for (int i = x; i <= n; i += i & -i)
        for (int j = y; j <= m; j += j & -j) 
            tr[o][i][j] += v;   
}

inline int sum(int x, int y, int o) {
    int s = 0;
    for (int i = x; i >= 1; i -= i & -i)
        for (int j = y; j >= 1; j -= j & -j)
            s += tr[o][i][j];
    return s;
}

inline void adj(int x, int y, int v) {
    add(x, y, v, 0);
    add(x, y, v * x, 1);
    add(x, y, v * y, 2);
    add(x, y, v * x * y, 3);    
}

inline LL Sum(int x, int y) {
    return sum(x, y, 0) * (x + 1) * (y + 1) - sum(x, y, 1) * (y + 1) - sum(x, y, 2) * (x + 1) + sum(x, y, 3);
}

int main() {
    cin >> op >> n >> m;
    while (cin >> op) {
        int a, b, c, d, v;
        read(a), read(b), read(c), read(d);
        if (a > c) swap(a, c), swap(b, d);
        if (*op == 'L') {
            read(v);
            adj(c + 1, d + 1, v), adj(a, b, v);
            adj(c + 1, b, -v), adj(a, d + 1, -v);
        } else {
            printf("%d\n", Sum(c, d) + Sum(a - 1, b - 1) - Sum(a - 1, d) - Sum(c, b - 1));
        }
    }
    return 0;
}

2.CF575I Robots protection

const int N = 5e3 + 54, M = N << 1;

int n, m, C, q;
int X[2][N][M], o[N][N];

inline void add(int x, int y, int tr[N][M], int v, int m) {
    for (int i = x; i <= n; i += i & -i)
        for (int j = y; j <= m; j += j & -j)
            tr[i][j] += v;
}

inline void add(int x, int y, int tr[N][N], int v, int m) {
    for (int i = x; i <= n; i += i & -i)
        for (int j = y; j <= n; j += j & -j)
            tr[i][j] += v;
}

inline int sum(int x, int y, int tr[N][M]) {
    int s = 0;
    for (int i = x; i >= 1; i &= i - 1)
        for (int j = y; j >= 1; j &= j - 1)
            s += tr[i][j];
    return s;
}

inline int sum(int x, int y, int tr[N][N]) {
    int s = 0;
    for (int i = x; i >= 1; i &= i - 1)
        for (int j = y; j >= 1; j &= j - 1)
            s += tr[i][j];
    return s;
}


int main() {
    cin >> n >> q;
    C = n * 2 - 1;
    while (q -- ) {
        int op, d, x, y, a;
        if (read(op), op == 1) {
            read(d), read(x), read(y), read(a);
            switch (d) {
                case 1: 
                    add(x, y, o, 1, n), add(x + a + 1, y, o, -1, n);
                    add(x, x + y + a, X[0], -1, C), add(x + a + 1, x + y + a, X[0], 1, C);
                    break;
                case 2: 
                    add(x, y + 1, o, -1, n), add(x + a + 1, y + 1, o, 1, n);
                    add(x, n - x + y - a, X[1], 1, C), add(x + a + 1, n - x + y - a, X[1], -1, C);
                    break;
                case 3: 
                    add(x - a, y, o, 1, n), add(x + 1, y, o, -1, n);
                    add(x - a, n - x + y + a + 1, X[1], -1, C), add(x + 1, n - x + y + a + 1, X[1], 1, C);
                    break;
                case 4: 
                    add(x + 1, y + 1, o, 1, n), add(x - a, y + 1, o, -1, n);
                    add(x - a, x + y - a - 1, X[0], 1, C), add(x + 1, x + y - a - 1, X[0], -1, C);
                    break;
            }
        } else {
            read(x), read(y);
            cout << sum(x, y, o) + sum(x, x + y - 1, X[0]) + sum(x, n - x + y, X[1]) << "\n";
        }
    }
    return 0;
}