[wikioi]线段树练习 2
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的更多相关文章
- [wikioi]线段树练习
http://codevs.cn/problem/1080/ #include <vector> #include <iostream> #include <string ...
- 【wikioi】2216 行星序列(线段树)
http://wikioi.com/problem/2216/ 这题太让我感动了QAQ,让我找到了我一直以来写线段树的错误!!!! 就是,pushdown一定要放在最前面!要不然顺序会错.也就是说,当 ...
- 【wikioi】1191 数轴染色(线段树+水题)
http://wikioi.com/problem/1191/ 太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉. #include <cstdio> #inc ...
- wikioi 1080 线段树练习 树状数组
1080 线段树练习 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现 ...
- Wikioi 1081 线段树成段更新单点查询
线段树练习飘逸的写法,自从自己改成这样的写法之后,线段树就没再练过,如今最终练得上了. 由于这里查询仅仅是查询了叶子结点,所以pushUp函数就用不上了,只是我没去掉之前是3ms.去掉之后反而变成4m ...
- [wikioi]线段覆盖
http://wikioi.com/problem/1214/ 这道题也归为贪心了.我也不是很能分辨,但想法确实是:1.有阶段最优化性:2.前一状态和后一状态有关系. 想法:1.排个序是很自然的想法, ...
- 小结:线段树 & 主席树 & 树状数组
概要: 就是用来维护区间信息,然后各种秀智商游戏. 技巧及注意: 一定要注意标记的下放的顺序及影响!考虑是否有叠加或相互影响的可能! 和平衡树相同,在操作每一个节点时,必须保证祖先的tag已经完全下放 ...
- bzoj3932--可持久化线段树
题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
随机推荐
- Berkeley DB
最近用BDB写点东西,写了挺多个测试工程.列下表,也理清楚最近的思路 1.测试BDB程序,包括打开增加记录,查询记录,获取所有记录.将数据转存mysql 程序的不足,增加记录仅仅只有key和value ...
- Python开发【第一篇】Python基础之正则表达式补充
正则表达式 一简介:就其本质而言,正则表达式(或RE)是一种小型的.高度专业化的标称语言,(在Python中)它内嵌在Python中,并通过re模块实现.正则表达式模式被编译成一系列的字节码,然后由用 ...
- 1096. Consecutive Factors (20)
Among all the factors of a positive integer N, there may exist several consecutive numbers. For exam ...
- yii2.0 控制器加载不同的user组件
Yii::$app->user->id Yii::$app->user2->id Yii::$app->admin->id identityC ...
- matlab实现高斯牛顿法、Levenberg–Marquardt方法
高斯牛顿法: function [ x_ans ] = GaussNewton( xi, yi, ri) % input : x = the x vector of 3 points % y = th ...
- netbeans设置字体
选择 monospaced 字体 摘抄自:http://blog.sina.com.cn/s/blog_4b6047bc01000boz.html 今天看该文档时,突然意识到通过修改JRE的字体配置文 ...
- 输入一个链表,输出该链表中倒数第k个结点。
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- JS中关于JS文件的引用以及问题
问题描述: 由于JSP中JS函数比较多,因此打算新建一个JS文件在JSP中引用JS文件,现在出现如下问题,JS如何引用时正确的,JS引用之后出现乱码如何解决? 问题解决: (1)JS ...
- 【HDOJ】【2089】不要62
数位DP cxlove基础数位DP第一题 用容斥把所有的不吉利数字去掉就得到吉利数字的数量= =(满足区间减法) //HDOJ 2089 #include<cmath> #include& ...
- 【UVA】【11021】麻球繁衍
数序期望 刘汝佳老师的白书上的例题……参见白书 //UVA 11021 #include<cmath> #include<cstdio> #define rep(i,n) fo ...