zoukankan      html  css  js  c++  java
  • [wikioi]线段树练习

    http://codevs.cn/problem/1080/

    #include <vector>
    #include <iostream>
    #include <string.h>
    
    using namespace std;
    
    const int MAXN = 100000;
    
    struct Line {
        int left, right;
        int n;
    };
    
    Line tree[MAXN * 3];
    
    void buildtr(int left, int right, int k) {
        tree[k].left = left;
        tree[k].right = right;
        tree[k].n = 0;
        if (left == right) return;
        int mid = (left + right) / 2;
        buildtr(left, mid, k * 2);
        buildtr(mid + 1, right, k * 2 + 1);
    }
    
    int query(int x, int k) {
        if (tree[k].left == x && tree[k].right == x) {
            return tree[k].n;
        }
        int mid = (tree[k].left + tree[k].right) / 2;
        if (x <= mid) return query(x, k * 2);
        else return query(x, k * 2 + 1);
    }
    
    int query(int l, int r, int k) {
        if (tree[k].left == l && tree[k].right == r) {
            //cout << "k:" << k << ",l" << l << "r," << r << ",n:" << tree[k].n << endl;
            return tree[k].n;
        }
        int mid = (tree[k].left + tree[k].right) / 2;
        if (r <= mid) {
            return query(l, r, k * 2);
        } else if (l > mid) {
            return query(l, r, k * 2 + 1);
        } else {
            return query(l, mid, k * 2) + query(mid + 1, r, k * 2 + 1);
        }
    }
    
    int update(int x, int y, int k) {
        int diff = 0;
        if (tree[k].left == x && tree[k].right == x) {
            diff = y - tree[k].n;
            tree[k].n = y;
            return diff;
        }
        int mid = (tree[k].left + tree[k].right) / 2;
        if (x <= mid) {
            diff = update(x, y, k * 2);
        } else {
            diff = update(x, y, k * 2 + 1);
        }
        tree[k].n += diff;
        return diff;
    }
    
    int add(int l, int r, int x, int k) {
        int diff = 0;
        if (tree[k].left == tree[k].right) {
            diff = x;
            tree[k].n += x;
            //cout << "diff:" << x << endl;
            return x;
        }
        int mid = (tree[k].left + tree[k].right) / 2;
        if (mid >= r) {
            diff = add(l, r, x, k * 2);
        } else if (mid < l) {
            diff = add(l, r, x, k * 2 + 1);
        } else {
            diff += add(l, mid, x, k * 2);
            diff += add(mid + 1, r, x, k * 2 + 1);
        }
        tree[k].n += diff;
        return diff;
    }
    
    int main() {
        int n;
        cin >> n;
        memset(tree, sizeof(tree), 0);
        buildtr(1, n, 1);
        for (int i = 1; i <= n; i++) {
            int x;
            cin >> x;
            update(i, x, 1);
        }
        int m;
        cin >> m;
        while (m--) {
            int c, x, y;
            cin >> c >> x >> y;
            if (c == 1) {
                add(x, x, y, 1);
            } else if (c == 2) {
                int res = query(x, y, 1);
                cout << res << endl;
            }
        }
    }
    

      

  • 相关阅读:
    Hibernate Criteria用法大全
    hibernate数据库操作基础
    Hibernate中Session.get()方法和load()方法的详细比较
    3hibernate核心对象关系映射 xxx.hbm.xml
    2.Hibernate的主配置文件hibernate.cfg.xml
    PHP Switch 语句判断成绩
    PHP页面显示中文字符出现乱码
    windows7下GithubDesktop和极域学生客户端冲突导致无法正常打开解决方案
    关于js特效轮播图练习
    Docker报错总结
  • 原文地址:https://www.cnblogs.com/lautsie/p/4034372.html
Copyright © 2011-2022 走看看