[A through study notes] Fenwick tree

nothing to say.
Note from the index from 0 or 1, or may be T out.

10114. "4.1 cases through a 2" Number the Stars 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 cases through a 3" tree outside the school


Maintaining several difference sequence, when asked Fenwick tree and you can use the prefix.

#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. "one through 4.1 Exercise 1" head count

#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. "one through 4.1 Exercise 2" simple questions

#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;
        }
    }
}

Guess you like

Origin www.cnblogs.com/mollnn/p/11616086.html
Recommended