http://codevs.cn/problem/1081/

#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;
cin >> c;
if (c == 1) {
int a, b, x;
cin >> a >> b >> x;
add(a, b, x, 1);
} else if (c == 2) {
int i;
cin >> i;
int res = query(i, i, 1);
cout << res << endl;
}
}
}

  

[wikioi]线段树练习 2的更多相关文章

  1. [wikioi]线段树练习

    http://codevs.cn/problem/1080/ #include <vector> #include <iostream> #include <string ...

  2. 【wikioi】2216 行星序列(线段树)

    http://wikioi.com/problem/2216/ 这题太让我感动了QAQ,让我找到了我一直以来写线段树的错误!!!! 就是,pushdown一定要放在最前面!要不然顺序会错.也就是说,当 ...

  3. 【wikioi】1191 数轴染色(线段树+水题)

    http://wikioi.com/problem/1191/ 太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉. #include <cstdio> #inc ...

  4. wikioi 1080 线段树练习 树状数组

    1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现 ...

  5. Wikioi 1081 线段树成段更新单点查询

    线段树练习飘逸的写法,自从自己改成这样的写法之后,线段树就没再练过,如今最终练得上了. 由于这里查询仅仅是查询了叶子结点,所以pushUp函数就用不上了,只是我没去掉之前是3ms.去掉之后反而变成4m ...

  6. [wikioi]线段覆盖

    http://wikioi.com/problem/1214/ 这道题也归为贪心了.我也不是很能分辨,但想法确实是:1.有阶段最优化性:2.前一状态和后一状态有关系. 想法:1.排个序是很自然的想法, ...

  7. 小结:线段树 & 主席树 & 树状数组

    概要: 就是用来维护区间信息,然后各种秀智商游戏. 技巧及注意: 一定要注意标记的下放的顺序及影响!考虑是否有叠加或相互影响的可能! 和平衡树相同,在操作每一个节点时,必须保证祖先的tag已经完全下放 ...

  8. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  9. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

随机推荐

  1. Geoserver 相关学习

    参考资料: http://geoserver.org/ http://docs.geoserver.org/ 相关文档 http://docs.geoserver.org/stable/en/user ...

  2. Configuration python CGI in XAMPP in win-7

    1.After install XAMPP,we need add the path of the Mysql just find the path and add it to your sys-pa ...

  3. Oracle 动态视图3 V$SESSION

    每一个连接到数据库实例中的session都拥有一条记录.包括用户session及后台进程如DBWR,LGWR,arcchiver等 Column Datatype Description SADDR ...

  4. How to begin Python learning?

    如何开始Python语言学习? 1. 先了解它,Wiki百科:http://zh.wikipedia.org/zh-cn/Python 2. Python, Ruby等语言来自开源社区,社区的学法是V ...

  5. linux 进程控制笔记

    进程创建 普通函数调用完成后,最多返回(return)一次,但fork/vfork会返回二次,一次返回给父进程,一次返回给子进程 父进程的返回值为子进程的进程ID,子进程的返回值为0 1.pid_t ...

  6. ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

    测试库一条update语句报错:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> ...

  7. docker 感性介绍

    Docker 允许开发者们将他们的应用打包放在云端的“容器”中,无需再修改就可以发布到任何流行的 Linux 机器上.由于采用沙盒机制,各应用之间没有任何接口,所以不用担心它们会相互干扰.也同样因为这 ...

  8. StackExchange.Redis的使用

    StackExchange.Redis介绍 有需要了解的和基础的使用可以参考:http://www.cnblogs.com/bnbqian/p/4962855.html StackExchange.R ...

  9. MVC中的几个问题汇总

    1.The model backing the 'XXXXDBContext' context has changed since the database was created. Either m ...

  10. js 截取某个字符前面或者后面的字符串

    /* string 字符串; str 指定字符; split(),用于把一个字符串分割成字符串数组; split(str)[0],读取数组中索引为0的值(第一个值),所有数组索引默认从0开始; */ ...