HDU 1166 敵兵佈陣——線段樹,樹狀數組

這題本來我打算用前綴數組實現源數據的處理,並把更變用map

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

// 線段樹

struct Node {
    int lp;
    int rp;
    int mid;
    int sum;
    Node* left;
    Node* right;
    Node(int l, int r): lp(l), rp(r), mid((l + r) / 2), left(NULL), right(NULL) {}//new初始化的內存不一定自動填0
};

int list[50005];//從1開始

int T, N, a, b;

Node* buildTree(int l, int r) {
    Node* p = new Node(l, r);
    if (l == r) {
        p->sum = list[l];
        return p;
    }
    p->left = buildTree(l, p->mid);
    p->right = buildTree(p->mid + 1, r);

    p->sum = p->left->sum + p->right->sum;
    return p;
}

void treeAdd(Node *which, int where, int what) {

    if (which->left != NULL && which->right != NULL) {
        treeAdd((where <= which->mid) ? which->left : which->right, where, what);
    }
    which->sum += what;
}

int calSum(Node * which, int from, int to) {

    if (from == which->lp && to == which->rp) {
        return which->sum;
    }

    if (from <= which->mid) {
        if (to > which->mid) {//橫跨
            return calSum(which->left, from, which->mid) + calSum(which->right, which->mid + 1, to);
        } else {//全在左邊
            return calSum(which->left, from, to);
        }
    } else {//全在右邊
        return calSum(which->right, from, to);
    }

    return 0;
}

void del(Node * which) {
    if (which->left) {
        del(which->left);
    }
    if (which->right) {
        del(which->right);
    }

    delete which;
}

int main(int argc, char const *argv[])
{

    char cmd[10];
    scanf("%d", &T);

    for (int t = 1; t <= T; t++) {

        memset(list, 0, sizeof(list));

        scanf("%d", &N);

        for (int i = 0; i < N; i++) {
            scanf("%d", list + i);
        }

        Node* root = buildTree(0, N - 1);

        printf("Case %d:\n", t);
        while (1) {
            scanf("%s", cmd);
            if ('E' == cmd[0]) {
                break;
            }
            scanf("%d%d", &a, &b);
            switch (cmd[0]) {
            case 'Q':
                printf("%d\n", calSum(root, a - 1, b - 1));
                break;
            case 'A':
                list[a - 1] += b;
                treeAdd(root, a - 1, b);
                break;
            case 'S':
                list[a - 1] -= b;
                treeAdd(root, a - 1, -b);
                break;
            }
        }

        del(root);
        root = NULL;

    }

    return 0;
}

自定義結構體組成的數組實現線段樹:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

// 線段樹

struct Node {
    int lp;
    int rp;
    int mid;
    int sum;
} nodes[50005 << 2];//空間爲原數組的四倍長,1儲存根元素,對於第k個節點,K<<1表示左支,(k<<1)|1表示右支

int list[50005];//從0開始

int T, N, a, b;

int buildTree(int which, int l, int r) {

    nodes[which].lp = l;
    nodes[which].rp = r;
    nodes[which].mid = ((l + r) >> 1);

    if (l == r) {
        nodes[which].sum = list[l];

    } else {
        nodes[which].sum += buildTree(which << 1, l, (l + r) >> 1);
        nodes[which].sum += buildTree((which << 1) | 1, ((l + r) >> 1) + 1, r);
    }

    return nodes[which].sum;
}

void treeAdd(int which, int where, int what) {

    if (nodes[which].lp != nodes[which].rp) {
        treeAdd((where <= nodes[which].mid) ? which << 1 : (which << 1) | 1, where, what);
    }
    nodes[which].sum += what;
}

int calSum(int which, int from, int to) {

    // printf("from_%d,to_%d,and_now_is_%d,%d\n", from, to, nodes[which].lp, nodes[which].rp);
    if (from == nodes[which].lp && to == nodes[which].rp) {
        return nodes[which].sum;
    }

    if (from <= nodes[which].mid) {
        if (to > nodes[which].mid) {//橫跨
            return calSum(which << 1, from, nodes[which].mid) + calSum((which << 1) | 1, nodes[which].mid + 1, to);
        } else {//全在左邊
            return calSum(which << 1, from, to);
        }
    } else {//全在右邊
        return calSum((which << 1) | 1, from, to);
    }

    return 0;
}

int main(int argc, char const *argv[])
{

    char cmd[10];
    scanf("%d", &T);

    for (int t = 1; t <= T; t++) {

        memset(list, 0, sizeof(list));
        memset(nodes, 0, sizeof(nodes));

        scanf("%d", &N);

        for (int i = 0; i < N; i++) {
            scanf("%d", list + i);
        }

        buildTree(1, 0, N - 1);

        printf("Case %d:\n", t);
        while (1) {
            scanf("%s", cmd);
            if ('E' == cmd[0]) {
                break;
            }
            scanf("%d%d", &a, &b);
            switch (cmd[0]) {
            case 'Q':
                printf("%d\n", calSum(1, a - 1, b - 1));
                break;
            case 'A':
                list[a - 1] += b;
                treeAdd(1, a - 1, b);
                break;
            case 'S':
                list[a - 1] -= b;
                treeAdd(1, a - 1, -b);
                break;
            }
        }

    }

    return 0;
}

樹狀數組實現:代碼量超少!!!

#include <iostream>
#include <cstdio>
#include <cstring>

//Accept

#define lowbit(x) (x&(-x))

//lowbit(x) 其實代表了第x號節點最底層代表的區間長度

using namespace std;

/**
*   c[x]
*                                                           1000
*                              /————————————————————————————[8]
*                             /                              |
*                           100                              |
*              /————————————[4]                /————————————[ ]
*             /              |                /              |
*           010              |              110              |
*      /————[2]        /————[ ]        /————[6]        /————[ ]
*     /      |        /      |        /      |        /      |
*   001      |      011      |      101      |      111      |
*   [1]     [ ]     [3]     [ ]     [5]     [ ]     [7]     [ ]
*/
int c[50005];//樹狀數組,從1開始
// c[i] = data[i - 2 ^ k + 1 ... i];

int data[50005];//存儲原始數據,從1開始

int s[50005];//前綴數組,在init時用到,從1開始

int T, N, a, b;

int calSum(int where) {//返回從data[1...where]
    int su = 0;
    while (where) {
        su += c[where];
        where -= lowbit(where);
    }
    return su;
}

void add(int where, int what) {
    while (where <= N) {
        c[where] += what;
        where += lowbit(where);
    }
}

int init() {
    int sum = 0;
    // for (int i = 1; i <= N; i++) {
    //  for (int x = i - lowbit(i) + 1; x <= i; x++) {
    //      c[i] += data[x];
    //  }
    // }
    // 用前綴數組來進行優化:
    for (int i = 1; i <= N; i++) {
        c[i] = s[i] - s[i - lowbit(i)];
    }

}

int main(int argc, char const *argv[])
{

    char cmd[10];
    scanf("%d", &T);

    for (int t = 1; t <= T; t++) {

        memset(c, 0, sizeof(c));
        memset(data, 0, sizeof(data));
        memset(s, 0, sizeof(s));

        scanf("%d", &N);

        for (int i = 1; i <= N; i++) {
            scanf("%d", data + i);
            s[i] = s[i - 1] + data[i];
        }

        init();

        printf("Case %d:\n", t);
        for (;;) {
            scanf("%s", cmd);
            if ('E' == cmd[0]) {
                break;
            }
            scanf("%d%d", &a, &b);
            switch (cmd[0]) {
            case 'Q':
                printf("%d\n", calSum(b) - calSum(a - 1));
                break;
            case 'A':
                add(a, b);
                break;
            case 'S':
                add(a, -b);
                break;
            }
        }
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章