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. 重拾C,一天一点点_2

    类型转换一般来说,如果二元运算符的两个操作数具有不同的类型,较低的类型提升为较高类型,结果为较高类型.表达式由float类型的操作数不会自动转换为double类型.使用float类型主要是为了在使用较 ...

  2. Firebird数据库相关备忘录

    Firebird数据库中有一些很特别的东西,很好用,但由于平时用的不多,记在这里,以备以后用到时查询. 1.以ADO 的OLE ODBC驱动方式访问 Firebird,可以使用如下连接串: FBCon ...

  3. windows创建桌面快捷方式的VBA脚本

    Dim wShell, oShortcut    'Dim strDesktop$ ' 为了与VBS兼容,    Dim strDesktop    ' 这里改写一下,测试通过...    Set w ...

  4. mysql基础三(视图、触发器、函数、存储过程、事务、防注入)

    一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 1.创建视图 -格式:CREATE ...

  5. Cisco IOS Basic CLI Configuration : Switch Port Command

    Cisco IOS Basic CLI Configuration : Switch Port Command 1.  Basic Switch>en Switch#conf t Enter c ...

  6. 【iis错误码】IIS 服务 这些年遇到的错误码

      II 发生错误,客户端似乎有问题.例如,客户端请求不存在的页面,客户端未提供有效的身份验证信息. 400 - 错误的请求. 401 - 访问被拒绝.   -- 暴力添加everyone用户,  i ...

  7. thinkphp中curl的使用,常用于接口

    /lib/action/PublicAction.class.php class PublicAction extends Action{ //curl,返回数组 public function ge ...

  8. How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?

    The more general extension of ViewPager would bet to create a "SetPagingEnabled" method so ...

  9. Regex.Match 方法

    Regex.Match 方法 在输入字符串中搜索正则表达式的匹配项,并将精确结果作为单个 Match 对象返回. 重载列表      (1) 在指定的输入字符串中搜索 Regex 构造函数中指定的正则 ...

  10. android开发,assets下面的资源文件不会变化/改动

    我再调试asserts下面的资源文件,发现我改动assets下面的文件内容,在真机上测试的时候还是最原先的内容,没有变,后来,卸载,重装就ok了. 原因: assets下面的资源文件,若覆盖重装,则里 ...