• [一本通学习笔记] 树状数组


    没什么可说的。
    注意下标从0还是从1,不然可能会T掉。

    10114. 「一本通 4.1 例 2」数星星 Stars

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 100005;
    int a[N], ans[N];
    inline int lowbit(int t) { return t & (-t); }
    void add(int i, int v) {
        for (; i < N; i += lowbit(i)) a[i] += v;
    }
    int sum(int i) {
        int s = 0;
        for (; i > 0; i -= lowbit(i)) s += a[i];
        return s;
    }
    struct point {
        int x, y;
        bool operator<(const point &b) { return (x == b.x) ? (y < b.y) : (x < b.x); }
    } p[N];
    int n;
    int main() {
        ios::sync_with_stdio(false);
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y, p[i].x++, p[i].y++;
        sort(p + 1, p + n + 1);
        for (int i = 1; i <= n; i++) {
            ans[sum(p[i].y)]++;
            add(p[i].y, 1);
        }
        for (int i = 0; i < n; i++) cout << ans[i] << endl;
        return 0;
    }
    

    10115. 「一本通 4.1 例 3」校门外的树


    维护几个差分序列,询问时用树状数组前缀和即可。

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 100005;
    struct BIT {
        int a[N];
        BIT() { memset(a, 0, sizeof a); }
        inline int lowbit(int x) { return x & (-x); }
        void add(int i, int v) {
            for (; i < N; i += lowbit(i)) a[i] += v;
        }
        int sum(int i) {
            int s = 0;
            for (; i; i -= lowbit(i)) s += a[i];
            return s;
        }
    } a, b;
    
    int n, m, t1, t2, t3;
    
    int main() {
        ios::sync_with_stdio(false);
        cin >> n >> m;
        for (int i = 1; i <= m; i++) {
            cin >> t1 >> t2 >> t3;
            if (t1 == 1) {
                a.add(t2, +1);
                b.add(t3, +1);
            } else {
                cout << a.sum(t3) - b.sum(t2 - 1) << endl;
            }
        }
    }
    

    10116. 「一本通 4.1 练习 1」清点人数

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 100005;
    int a[N];
    inline int lowbit(int x) { return x & (-x); }
    void add(int i, int x) {
        for (; i < N; i += lowbit(i)) a[i] += x;
    }
    int sum(int i) {
        int s = 0;
        for (; i; i -= lowbit(i)) s += a[i];
        return s;
    }
    int main() {
        int n, m, t1, t2, t3;
        cin >> n >> m;
        char op;
        for (int i = 1; i <= m; i++) {
            cin >> op;
            if (op == 'A') {
                cin >> t1;
                cout << sum(t1) << endl;
            }
            if (op == 'B') {
                cin >> t1 >> t2;
                add(t1, t2);
            }
            if (op == 'C') {
                cin >> t1 >> t2;
                add(t1, -t2);
            }
        }
    }
    

    10117. 「一本通 4.1 练习 2」简单题

    #include <bits/stdc++.h>
    using namespace std;
    
    int n, m;
    const int N = 100005;
    int a[N], t1, t2, t3;
    
    inline int lowbit(int x) { return x & (-x); }
    void add(int i, int v) {
        for (; i < N; i += lowbit(i)) a[i] += v;
    }
    int sum(int i) {
        int s = 0;
        for (; i; i -= lowbit(i)) s += a[i];
        return s;
    }
    
    int main() {
        cin >> n >> m;
        for (int i = 1; i <= m; i++) {
            cin >> t1 >> t2;
            if (t1 == 1) {
                cin >> t3;
                add(t2, +1);
                add(t3 + 1, -1);
            } else {
                cout << (sum(t2) & 1) << endl;
            }
        }
    }
    
  • 相关阅读:
    POJ 2955 Brackets 区间DP
    POJ 3311 Hie with the Pie 最短路+状压DP
    POJ 3615 Cow Hurdles(最短路径flyod)
    hdu 3790 最短路径dijkstra(多重权值)
    poj 3254 Corn Fields 状压DP
    状压DP
    poj2411 Mondriaan's Dream 状压DP
    M: Mysterious Conch 字符串哈希
    哈希(hash)理解
    域渗透:GPP(Group Policy Preferences)漏洞
  • 原文地址:https://www.cnblogs.com/mollnn/p/11616086.html
Copyright © 2020-2023  润新知