zoukankan      html  css  js  c++  java
  • 简单的线段树应用

    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
    ", 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
    ", arr[left - 1]);
                } else {
                    printf("%d
    ", query(root, left, right));
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    为什么在SqlServer流水模式下,事务无法启动?
    默认web站点被删除,如何设置新的默认站点?
    用C#实现基于TCP协议的网络通讯
    如何通过DataRelation关联两个DataGrid,实现主从表。
    如何设置网站的会话时间?
    性能测试基本概念释疑
    C#中如何获取服务器IP,名称,操作系统,客户端IP,名称!
    DataGridComboBoxColumn控件
    端口基础知识
    P2P之UDP穿透NAT的原理与实现
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/3354854.html
Copyright © 2011-2022 走看看