POJ3468--A Simple Problem with Integers(Splay Tree)
虽然有点难,但是这套题都挂了一个月了啊喂……
网上模板好多……最后还是抄了kuangbin聚聚的,毕竟好多模板都是抄他的,比较习惯……
POJ 3468
题意:给n个数,两种操作,区间整体加一个数,或者区间求和。
题解:把区间的前一个数挪到根,区间后一个数挪到根的右子树,根的右子树的左子树就是要处理的区间。。。
SplayTree是一个二叉排序树,它所保存的顺序是数字的编号,所以无论怎样旋转,编号的顺序都不会变。。。
在首尾各插入一个结点,这样求整个区间的时候也可以找到前一个数和后一个数。。。
照了别人的博客写了两遍,自己又裸敲一遍,还是错了好多细节,不过大概理解了。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = ;
int sz[N], ch[N][], pre[N];
int a[N];
ll sum[N], add[N], key[N];
int root, tot; void new_node(int &o, int fa, int k) {
o = ++tot;
sz[o] = ;
ch[o][] = ch[o][] = ;
pre[o] = fa;
sum[o] = key[o] = k;
add[o] = ;
} void push_up(int o) {
sum[o] = sum[ch[o][]] + sum[ch[o][]] + key[o];
sz[o] = sz[ch[o][]] + sz[ch[o][]] + ; // +1 !!
} void update(int o, int v) {
if (!o) return ;
add[o] += v;
key[o] += v;
sum[o] += (ll)sz[o]*v;
} void push_down(int o) {
if (add[o]) {
update(ch[o][], add[o]);
update(ch[o][], add[o]);
add[o] = ;
}
} void build(int &o, int l, int r, int fa) {
if (l > r) return;
int mid = (l+r) >> ;
new_node(o, fa, a[mid]);
build(ch[o][], l, mid-, o);
build(ch[o][], mid+, r, o);
push_up(o);
} void init(int n) {
root = tot = ;
sz[] = ch[][] = ch[][] = pre[] = ;
sum[] = add[] = key[] = ;
new_node(root, , -);
new_node(ch[root][], root, -);
build(ch[ch[root][]][], , n, ch[root][]);
push_up(ch[root][]);
push_up(root);
} void rotate(int o, int d) { // 0:left 1:right
int fa = pre[o];
push_down(fa);
push_down(o);
ch[fa][!d] = ch[o][d];
pre[ch[o][d]] = fa;
if (pre[fa]) ch[pre[fa]][ch[pre[fa]][]==fa] = o;
pre[o] = pre[fa];
ch[o][d] = fa;
pre[fa] = o;
push_up(fa);
} void splay(int o, int goal) {
push_down(o);
while (pre[o] != goal) {
if (pre[pre[o]] == goal) {
rotate(o, ch[pre[o]][] == o);
} else {
int fa = pre[o];
int d = (ch[pre[fa]][] == fa);
if (ch[fa][d] == o) {
rotate(o, !d);
rotate(o, d);
} else {
rotate(fa, d);
rotate(o, d);
}
}
}
push_up(o);
if (goal == ) root = o;
} int get_kth(int o, int k) {
push_down(o); //!!
int t = sz[ch[o][]] + ;
if (t == k) return o;
if (t > k) return get_kth(ch[o][], k);
return get_kth(ch[o][], k-t);
} ll query(int l, int r) {
splay(get_kth(root, l), );
splay(get_kth(root, r+), root);
return sum[ ch[ch[root][]][] ];
} void Add(int l, int r, int v) {
splay(get_kth(root, l), );
splay(get_kth(root, r+), root);
update(ch[ch[root][]][], v);
push_up(ch[root][]);
push_up(root);
} int main() {
//freopen("in.txt", "r", stdin);
int n, q;
while (~scanf("%d%d", &n, &q) && n) {
for (int i = ; i <= n; ++i) scanf("%d", a+i);
init(n);
char op[];
int x, y, z;
while (q--) {
scanf("%s%d%d", op, &x, &y);
if (*op == 'Q') {
printf("%lld\n", query(x, y));
} else {
scanf("%d", &z);
Add(x, y, z);
}
}
}
return ;
}
POJ3468--A Simple Problem with Integers(Splay Tree)的更多相关文章
- ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- A - 敌兵布阵 ——B - I Hate It——C - A Simple Problem with Integers(线段树)
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- Simple Problem with Integers(POJ 3486)
A Simple Problem with Integers Time Li ...
- poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...
- POJ3468 A Simple Problem with Integers(数状数组||区间修改的RMQ问题)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- poj3468 A Simple Problem with Integers(线段树/树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
随机推荐
- 1030-ACM程序设计之马拉松竞赛
描述 校ACM协会近四个月举行了为期100天ACM程序设计之马拉松竞赛,竞赛题总数为1000,同学们反响热烈,先后有许多ACM程序设计竞赛爱好者开始先后编号,成功解答的题目数为选手的成绩. 今天进行成 ...
- EL表达式对数组、集合操作
el表达式是通过${key}的方式获取对象中的值.在el表达式中有如下几个隐含的对象,pageScope,requestSope,sessionScope,applicationScope,如果要取$ ...
- http://jingyan.baidu.com/article/a378c960630e61b329283045.html
http://jingyan.baidu.com/article/a378c960630e61b329283045.html
- java List 去重(两种方式)
方法一: 通过Iterator 的remove方法 Java代码 public void testList() { List<Integer> list=new ArrayList< ...
- sizeof运算符来获取各种数据类型在内存中所占字节数--gyy整理
C++并没有规定各种数据类型在内存中的存储大小,依赖于不同的编译器的不同而不同,要想获知当前编译器对各种数据类型分配的大小,可以通过sizeof运算符来获取. 使用方法1: sizeof(数据类型) ...
- PHP 的面向方面编程
面向方面编程(AOP)对于PHP来说是一个新的概念.现在PHP对于 AOP 并没有官方支持,但有很多扩展和库实现了这个特性.本课中,我们将使用 Go! PHP library 来学习 PHP 如何进行 ...
- Linux上程序执行的入口--Main
main()函数,想必大家都不陌生了,从刚开始写程序的时候,大家便开始写main(),我们都知道main是程序的入口.那main作为一个函数,又是谁调用的它,它是怎么被调用的,返回给谁,返回的又是什么 ...
- Android开发UI之手动显示和隐藏软键盘
1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Contex ...
- poj 2253 Frogger(最短路 floyd)
题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...
- bzoj4028
一眼分块题…… 分块,维护每个块的总的gcd和xor和 先思考我们应该怎么查询,考虑到gcd是一个神奇的东西,因为它最多变化logX次 于是我们从前往后扫描每个块,如果一个块内总的gcd是当前扫描的前 ...