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 ...
随机推荐
- Google-Gson使用
转自:http://my.oschina.net/itblog/blog/204120 这几天,因为项目的需要,接触了Google的Gson库,发现这个东西很好用,遂记下简单的笔记,供以后参考.至于G ...
- Qt: 访问容器(三种方法,加上for循环就四种了)good
#include <iostream>#include <QString>#include <QList>#include <QListIterator> ...
- 使用QGridLayout布局实现翻页效果
http://blog.csdn.net/u013704336/article/details/51474942
- nchar 和 nvarchar
字符数据类型(nchar 长度固定,nvarchar 长度可变)和 Unicode 数据使用 UNICODE UCS-2 字符集. nchar [ ( n ) ] n 个字符的固定长度的 Unicod ...
- 算法总结之欧拉函数&中国剩余定理
算法总结之欧拉函数&中国剩余定理 1.欧拉函数 概念:在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)( ...
- Android隐藏输入法键盘(hideSoftInputFromInputMethod没有效果)
在个别时候,需要强制隐藏Android输入法键盘,如当前键盘正在显示,这个时候点击了侧滑面板,就要强制隐藏输入法键盘.网上常见的方法有: 1. InputMethodManager imm = (In ...
- “段寄存器”的故事[转](彻底搞清内存段/elf段/实模式保护模式以及段寄存器)
http://blog.csdn.net/michael2012zhao/article/details/5554023 一. 段寄存器的产生 段寄存器的产生源于Intel 8086 CPU体系结构中 ...
- 24.allegro中光绘gerber[原创]
光绘 一,基本设置 或者: ------------------------------------- ------- ------- 二,需要设置内容: 1)顶层和底层: 2)内部布线层: 3)OU ...
- C#中的线程(三) 使用多线程
第三部分:使用多线程 1. 单元模式和Windows Forms 单元模式线程是一个自动线程安全机制, 非常贴近于COM——Microsoft的遗留下的组件对象模型.尽管.NET最大地放弃摆脱了遗留 ...
- 通过ngx-lua来统计Nginx上的虚拟主机性能数据
Web server调研分析 Filed under: Web Server — cmpan @ 2012-10-29 20:38:34 摘要 简单可依赖的架构首先需要有一个简单可依赖的前端WebSe ...