簡單的線段樹應用

qingyezhu發表於2013-10-07

hdu 1754

 

/*
 * c.cpp
 *
 *  Created on: 2013-10-6
 *      Author: wangzhu
 */

#include<cstdio>
#include<iostream>
using namespace std;
#define MAX -1
struct Node {
    int left, right, max;
    Node *leftChild, *rightChild;
} node[800010];
int getMax(int a, int b) {
    return a > b ? a : b;
}
int pos;
void build(Node *root, int left, int right) {
    root->left = left;
    root->right = right;
    root->max = MAX;
    if (left != right) {
        pos++;
        root->leftChild = node + pos;
        pos++;
        root->rightChild = node + pos;

        int mid = left + (right - left) / 2;
        build(root->leftChild, left, mid);
        build(root->rightChild, mid + 1, right);
    }
}

int result;

void query(Node *root, int left, int right) {
    if (root->max <= result) {
        return;
    }
    if (left == root->left && right == root->right) {
        result = root->max;
        return;
    } else {
        int mid = root->left + (root->right - root->left) / 2;
        if (right <= mid) {
            query(root->leftChild, left, right);
        } else if (left > mid) {
            query(root->rightChild, left, right);
        } else {
            query(root->leftChild, left, mid);
            query(root->rightChild, mid + 1, right);
        }
    }
}
void update(Node *root, int index, int value) {
    if (index == root->left && index == root->right) {
        root->max = value;
        return;
    }
    root->max = getMax(root->max, value);
    int mid = root->left + (root->right - root->left) / 2;
    if (index <= mid) {
        update(root->leftChild, index, value);
    } else {
        update(root->rightChild, index, value);
    }
}
int main() {
    int n, m, left, right,value;
    char c[2];
    while (~scanf("%d %d", &n, &m)) {
        pos = 0;
        build(node,1, n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &value);
            update(node,i,value);
        }
        for (int i = 0; i < m; i++) {
            scanf("%s %d %d", c, &left, &right);
            if (c[0] == 'Q') {
                result = MAX;
                query(node, left, right);
                printf("%d\n", result);
            } else {
                update(node, left, right);
            }
        }
    }
    return 0;
}

 

九度題目1544:數字序列區間最小值

 

/*
 * c.cpp
 *
 *  Created on: 2013-10-6
 *      Author: wangzhu
 */

#include<cstdio>
#include<iostream>
using namespace std;
struct Node {
    int left, right, min;
    Node *L, *R;
};
int arr[100010];

int getMin(int a, int b) {
    return a > b ? b : a;
}
Node *build(int left, int right) {
    Node *root = new Node;
    root->left = left;
    root->right = right;
    if (left != right) {
        int mid = left + (right - left) / 2;
        root->L = build(left, mid);
        root->R = build(mid + 1, right);
        root->min = getMin(root->L->min, root->R->min);
    } else {
        root->L = NULL;
        root->R = NULL;
        root->min = getMin(arr[left - 1], arr[right - 1]);
    }
    return root;
}

int query(Node *root, int left, int right) {
    if (left == root->left && right == root->right) {
        return root->min;
    } else {
        int ret = -1;
        int mid = root->left + (root->right - root->left) / 2;
        if (right <= mid) {
            ret = query(root->L, left, right);
        } else if (left > mid) {
            ret = query(root->R, left, right);
        } else {
            ret = getMin(query(root->L, left, mid),
                    query(root->R, mid + 1, right));
        }
        return ret;
    }
}
int main() {
    int n, m, left, right;
    while (~scanf("%d", &n)) {
        for (int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }
        Node *root;
        root = build(1, n);
        scanf("%d", &m);
        for (int i = 0; i < m; i++) {
            scanf("%d %d", &left, &right);
            if (left == right) {
                printf("%d\n", arr[left - 1]);
            } else {
                printf("%d\n", query(root, left, right));
            }
        }
    }
    return 0;
}

 

相關文章