码迷,mamicode.com
首页 > 其他好文 > 详细

[wikioi]线段树练习

时间:2014-10-19 11:31:33      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   for   sp   div   on   log   

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;
        }
    }
}

  

[wikioi]线段树练习

标签:blog   http   io   os   for   sp   div   on   log   

原文地址:http://www.cnblogs.com/lautsie/p/4034372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!