HDU--1166--单点更新

敌兵布阵 HDU - 1166

维护一个前缀数组,耗时有点大

#include 
#include 
using namespace std;
const int maxn = 5e4 + 5;
int t, n;
int sum[maxn], num[maxn];
char str[10];
int main() {
    // freopen("in.txt", "r", stdin);
    scanf("%d", &t);
    for (int kase = 1; kase <= t; kase++) {
        memset(sum, 0, sizeof(sum));
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", num + i);
            sum[i] = sum[i - 1] + num[i];
        }
        int a, b;
        printf("Case %d:\n", kase);
        while (scanf("%s", str) && str[0] != 'E') {
            scanf("%d%d", &a, &b);
            if (str[0] == 'Q') {
                int res = sum[b] - sum[a - 1];
                printf("%d\n", res);
                continue;
            }
            else if (str[0] == 'A') {
                for (int i = a; i <= n; i++) {
                    sum[i] += b;
                }
            }
            else if (str[0] == 'S') {
                for (int i = a; i <= n; i++) {
                    sum[i] -= b;
                }
            }
        }
    }
    return 0;
}

树状数组模板

#include
#include
#include
#include
#define ms(a,b) memset(a,b, sizeof a)
#define sd(n) scanf("%d",&n)
#define fs freopen("in.txt","r",stdin)
using namespace std;
const int maxn = 1e5 + 10;
int d[maxn], n;
int lowbit(int x) {
    return x & (-x);
}
int query(int x) {
    int res = 0;
    while (x > 0) {
        res += d[x];
        x -= lowbit(x);
    }
    return res;
}
void add(int x, int y) {
    while (x <= n) {
        d[x] += y;
        x += lowbit(x);
    }
}
int main() {
    //fs; 
    int t; sd(t);
    for (int k = 1; k <= t; ++k) {
        printf("Case %d:\n", k);
        sd(n);
        ms(d, 0);
        for (int i = 1; i <= n; ++i) {
            int tmp; sd(tmp);
            add(i, tmp);
        }
        string ml = "";
        while (cin >> ml) {
            if (ml == "End") break;
            int pos, num;
            sd(pos), sd(num);
            if (ml == "Add") {
                add(pos, num);
            }
            else if (ml == "Sub") {
                add(pos, -num);
            }
            else if (ml == "Query") {
                cout << query(num) - query(pos - 1) << endl;
            }
        }
    }
}

线段树

#include 
#include 
#include 
#include 
using namespace std;

typedef long long LL;
typedef pair pii;

const int maxn = 2e5 + 5;
const int INF = 0x3f3f3f3f;

int sum[maxn * 4], A[maxn];

void PushUp(int rt) {
    sum[rt] = sum[rt * 2] + sum[rt * 2 + 1];
}
void Build(int l, int r, int rt) {
    if (l == r) {
        sum[rt] = A[l];
        return;
    }
    int m = (l + r) / 2;
    Build(l, m, rt * 2);
    Build(m + 1, r, rt * 2 + 1);
    PushUp(rt);
}
void Update(int L, int c, int l, int r, int rt) {
    if (r == l) {
        sum[rt] += c;
        return;
    }
    int m = (l + r) / 2;
    if (L <= m) Update(L, c, l, m, rt * 2);
    else Update(L, c, m + 1, r, rt * 2 + 1);
    PushUp(rt);
}
int Query(int L, int R, int l, int r, int rt) {
    if (L <= l && r <= R) {
        return sum[rt];
    }
    int m = (l + r) / 2;
    int ans = 0;
    if (L <= m) ans += Query(L, R, l, m, rt * 2);
    if (R > m) ans += Query(L, R, m + 1, r, rt * 2 + 1);
    return ans;
}

int main() {
    // freopen("in.txt", "r", stdin);
    char c;
    int T;
    int n, m, a, b;
    char str[10];
    scanf("%d", &T);
    for (int kase = 1; kase <= T; kase++) {
        printf("Case %d:\n", kase);
        scanf("%d", &n);
        memset(sum, 0, sizeof(sum));
        memset(A, 0, sizeof(A));
        for (int i = 1; i <= n; i++) scanf("%d", A + i);
        Build(1, n, 1);
        while (scanf("%s", str) && str[0] != 'E') {
            scanf("%d%d", &a, &b);
            if (str[0] == 'Q') {
                printf("%d\n", Query(a, b, 1, n, 1));
            }
            else if (str[0] == 'A') {
                Update(a, b, 1, n, 1);
            }
            else if (str[0] == 'S') {
                Update(a, -b, 1, n, 1);
            }
        }
    }
    return 0;
}

你可能感兴趣的:(HDU--1166--单点更新)