今天我刷题了吗
  • 2U's GitBook
  • 我的模板
    • I/O 及其他
      • 快速读写
      • 关闭同步流
      • 文件重定向
      • 随机数
      • 运行时间
      • Lambda
      • 万能头文件
    • 字符串
      • 字符串分割
      • KMP 算法
      • Manacher 算法
      • AC 自动机
      • 高精度
    • 数学
      • 快速幂
      • 矩阵快速幂
      • 筛法
      • 欧拉函数
      • Polya 定理
      • 逆元
      • 组合数
    • 图论
      • Dijkstra 算法
      • SPFA 算法
      • Kruskal 算法
      • Prim 算法
      • 倍增
      • 离线 Tarjan 算法
      • Tarjan 算法
      • Hungary 算法
      • A* 算法
      • EK 算法
      • Dinic 算法
    • 数据结构
      • 线段树
      • 树状数组
      • 可持久化数组
      • 主席树
      • 树堆
      • 无旋树堆
      • 伸展树
      • 树套树
      • 树链剖分
      • 点分治
      • 动态树
  • 我的题单
由 GitBook 提供支持
在本页

这有帮助吗?

  1. 我的模板
  2. 数据结构

树状数组

洛谷 P3374 【模板】树状数组 1

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const int maxn(5e5 + 10);
int c[maxn];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

inline int lowbit(int x)
{
    return x & -x;
}

void update(int p, int n, int v)
{
    for (int i = p; i <= n; i += lowbit(i)) {
        c[i] += v;
    }
}

ll getSum(int p)
{
    ll sum = 0;
    while (p) {
        sum += c[p];
        p -= lowbit(p);
    }
    return sum;
}

ll query(int l, int r)
{
    return getSum(r) - getSum(l - 1);
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    int n = read(), m = read();
    for (int i = 1; i <= n; ++i) {
        update(i, n, read());
    }
    while (m--) {
        int op = read(), a = read(), b = read();
        if (op == 1) {
            update(a, n, b);
        } else {
            write(query(a, b), true);
        }
    }
    return 0;
}

洛谷 P3368 【模板】树状数组 2

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const int maxn(5e5 + 10);
int c[maxn];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

inline int lowbit(int x)
{
    return x & -x;
}

void update(int p, int n, int v)
{
    for (int i = p; i <= n; i += lowbit(i)) {
        c[i] += v;
    }
}

ll getSum(int p)
{
    ll sum = 0;
    while (p) {
        sum += c[p];
        p -= lowbit(p);
    }
    return sum;
}

ll query(int l, int r)
{
    return getSum(r) - getSum(l - 1);
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    int n = read(), m = read();
    for (int i = 1; i <= n; ++i) {
        update(i, n, read());
    }
    while (m--) {
        int op = read(), a = read(), b = read();
        if (op == 1) {
            update(a, n, b);
        } else {
            write(query(a, b), true);
        }
    }
    return 0;
}

洛谷 P3372 【模板】线段树 1

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const int maxn(1e5 + 10);
int a[maxn];
ll c[maxn][2];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

inline int lowbit(int x)
{
    return x & -x;
}

void add(int t, int p, int n, ll v)
{
    for (int i = p; i <= n; i += lowbit(i)) {
        c[i][t] += v;
    }
}

ll getSum(int t, int p)
{
    ll sum = 0;
    for (int i = p; i; i -= lowbit(i)) {
        sum += c[i][t];
    }
    return sum;
}

void update(int l, int r, int n, int v)
{
    add(0, l, n, v);
    add(0, r + 1, n, -v);
    add(1, l, n, 1ll * v * (l - 1));
    add(1, r + 1, n, -1ll * v * r);
}

ll query(int l, int r)
{
    ll sum_r = 1ll * r * getSum(0, r) - getSum(1, r);
    ll sum_l = 1ll * (l - 1) * getSum(0, l - 1) - getSum(1, l - 1);
    return sum_r - sum_l;
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    int n = read(), m = read();
    for (int i = 1; i <= n; ++i) {
        a[i] = read();
        add(0, i, n, a[i] - a[i - 1]);
        add(1, i, n, 1ll * (a[i] - a[i - 1]) * (i - 1));
    }
    while (m--) {
        int op = read(), x = read(), y = read();
        if (op == 1) {
            int k = read();
            update(x, y, n, k);
        } else {
            write(query(x, y), true);
        }
    }
    return 0;
}
上一页线段树下一页可持久化数组

最后更新于4年前

这有帮助吗?