SGU 263. Towers
各种操作:
- put x c:表示在第 x 列上增加 c 个积木(c>0)。
- tput t x c:表示在塔 t 的第 x 列上增加 c 个积木(c>0)。
- towers:询问共有几座塔。
- cubes t:询问第 t 座塔共有几个积木。
- length t:询问第 t 座塔的长度。
- tcubes t x:询问第 t 座塔的第 x 列有几个积木。
对应做法:
- towers: 由于要查询tower的数目,以及删除一个tower,加入一个tower,考虑使用平衡树维护。
- put: 首先加积木这一件事情只会使得tower发生合并操作对吧,所以可以用并查集维护点属于那个tower。关于合并积木:先查找这个位置是否本来就有积木,如果有,加上就行拉,更新一下BST中的端点的cubes就行拉;否则加上之后插入BST内,如果其左边有towers,或者右边有towers,或者两边都有删除靠右边的节点,将信息传递到左边的节点,然后在并查集内把父亲指向左边节点的左端点,没拉。
- tput: 在某个tower内加积木,如果我知道某个tower的左端点,直接加就行了,之后更新BST内节点即可。
- cubes: 直接把大小放到tower的最左边的端点中,查询直接输出即可。
- lengths: 同上。
- tcubes: 同tput。
由于要求支持k大操作,set不适用,只好手写平衡树拉>_<!!。
notice:
- 删除节点旋转时要记录方向 int d = t->ch[]->fix < t->ch[]->fix; ,千万不要在这里图方便 else rot(t, t->ch[]->fix < t->ch[]->fix), del(t->ch[t->ch[]->fix < t->ch[]->fix], pos); ,因为已经把儿子转走了,如上定RE。
- 新建指针节点一定将其指向NULL,无论之后会不会给它赋值。
- 手残打错变量名。。。为什么每次敲数据结构题都会敲错变量名。。。
CODE:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + ;
const int maxp = 1e6; int n;
int fa[maxn], num[maxn]; int test_on; int find_fa(int x) {
if(fa[x] != x) fa[x] = find_fa(fa[x]);
return fa[x];
} class BST {
private:
struct treap_node {
treap_node *ch[];
int fix, size, pos, cubes, length;
treap_node() {
ch[] = ch[] = NULL;
}
treap_node(int _pos, int _cubes, int _lengh) {
pos = _pos, cubes = _cubes, length = _lengh;
ch[] = ch[] = NULL;
size = ;
fix = rand();
}
} *T;
void maintain(treap_node *x) {
if(x) {
x->size = ;
if(x->ch[]) x->size += x->ch[]->size;
if(x->ch[]) x->size += x->ch[]->size;
}
}
void rot(treap_node *&x, int d) {
treap_node *y = x->ch[d ^ ];
x->ch[d ^ ] = y->ch[d];
y->ch[d] = x;
maintain(x), maintain(y);
x = y;
}
void insert(treap_node *&t, int p, int c, int l) {
if(t == NULL) {
t = new treap_node(p, c, l);
} else {
if(p < t->pos) {
insert(t->ch[], p, c, l);
if(t->ch[]->fix < t->fix) rot(t, );
} else {
insert(t->ch[], p, c, l);
if(t->ch[]->fix < t->fix) rot(t, );
}
}
maintain(t);
}
void del(treap_node *&t, int pos) {
if(t->pos == pos) {
if(!(t->ch[]) || !(t->ch[])) {
treap_node *p = t;
if(t->ch[]) p = t->ch[];
else p = t->ch[];
t = NULL;
delete t;
t = p;
} else {
int d = t->ch[]->fix < t->ch[]->fix;
// notice
rot(t, d);
del(t->ch[d], pos);
}
} else del(t->ch[t->pos < pos], pos);
if(t) maintain(t);
}
treap_node *select(int k) {
treap_node *t = T;
int size;
while() {
size = t->ch[] ? t->ch[]->size : ;
if(k <= size) {
t = t->ch[];
} else if(k > size + ) {
k -= size + ;
t = t->ch[];
} else break;
}
return t;
}
treap_node *find(treap_node *t, int pos) {
if(t == NULL || t->pos == pos) return t;
return find(t->ch[t->pos < pos], pos);
} public: void print(treap_node *node) {
if(node == NULL) return ;
print(node->ch[]);
printf("address :%d size :%d fix :%d\n pos :%d cubes :%d length :%d\n", node, node->size, node->fix, node->pos, node->cubes, node->length);
printf(" ch[0] :%d ch[1] :%d\n", node->ch[], node->ch[]);
print(node->ch[]);
} int f, t, x, c;
treap_node *r;
void put() {
scanf("%d%d", &x, &c);
f = find_fa(x);
num[x] += c;
r = find(T, f);
if(r == NULL) {
insert(T, x, num[x], );
treap_node *p, *q;
p = q = NULL;
// notice
r = find(T, f);
if( < x - ) p = find(T, find_fa(x - ));
if(x + <= maxp) q = find(T, find_fa(x + ));
if(p != NULL || q != NULL) {
if(p != NULL) {
fa[r->pos] = p->pos;
p->cubes += r->cubes;
p->length += r->length;
del(T, r->pos);
r = p;
}
if(q != NULL) {
fa[q->pos] = r->pos;
r->cubes += q->cubes;
r->length += q->length;
// notice
del(T, q->pos);
}
}
} else {
r->cubes += c;
}
/*
r = select(1);
printf(" %d\n", r->length);
printf("%d\n", test_on);
print(T);
puts("");
*/
}
void tcubes() {
scanf("%d%d", &t, &x);
r = select(t);
printf("%d cubes in %dth column of %dth tower\n", num[r->pos + x - ], x, t);
}
void tput() {
scanf("%d%d%d", &t, &x, &c);
r = select(t);
num[r->pos + x - ] += c;
r->cubes += c;
}
void length() {
scanf("%d", &t);
r = select(t);
printf("length of %dth tower is %d\n", t, r->length);
}
void towers() {
printf("%d towers\n", T ? T->size : );
}
void cubes() {
scanf("%d", &t);
r = select(t);
printf("%d cubes in %dth tower\n", r->cubes, t);
}
} T; char ope[];
int main() {
#ifdef ONLINE_JUDGE
srand((int)time(NULL));
#endif for(int i = ; i < maxn; ++i) fa[i] = i;
scanf("%d", &n);
for(int i = ; i <= n; ++i) {
// test_on = i;
// printf("%d ", test_on);
scanf("%s", ope);
if(ope[] == 'p') {
T.put();
} else if(ope[] == 't' && ope[] == 'o') {
T.towers();
} else if(ope[] == 't' && ope[] == 'p') {
// puts("");
T.tput();
} else if(ope[] == 'c') {
T.cubes();
} else if(ope[] == 'l') {
T.length();
} else if(ope[] == 't' && ope[] == 'c') {
T.tcubes();
}
} return ;
}
SGU 263. Towers的更多相关文章
- SGU 202 The Towers of Hanoi Revisited (DP+递归)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意 :n个圆盘,m个柱子的汉诺塔输出步骤. ht ...
- SGU 202. The Towers of Hanoi Revisited
多柱汉诺塔问题. 引用自wiki百科 多塔汉诺塔问题 在有3个柱子时,所需步数的公式较简单,但对于4个以上柱子的汉诺塔尚未得到通用公式,但有一递归公式(未得到证明,但目前为止没有找到反例): 令为在有 ...
- LightOJ1126 Building Twin Towers(DP)
题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1126 Description Professor Sofdor Al ...
- SGU 495. Kids and Prizes
水概率....SGU里难得的水题.... 495. Kids and Prizes Time limit per test: 0.5 second(s)Memory limit: 262144 kil ...
- UVA 10066 The Twin Towers
裸最长公共子序列 #include<time.h> #include <cstdio> #include <iostream> #include<algori ...
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- 【SGU】495. Kids and Prizes
http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:N个箱子M个人,初始N个箱子都有一个礼物,M个人依次等概率取一个箱子,如果有礼物则 ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
随机推荐
- 项目压力测试软件 -- LoadRunner 11.0 的安装、汉化和破解
重要说明: LoadRunner 11.0 只支持Win7,32位系统:不支持Win7,64位系统[ Win7,64位 我反复安装都没有成功!] 一.下载安装.汉化.破解文件: 我的下 ...
- JQuery学习五
获取样式attr("myclass")移除样式removeClass("myclass")增加样式addClass("myclass")to ...
- Qt ------ 内存回收机制、new对象的回收
写在前面的总结: 建议:对于不能指定父对象的对象(对象通过moveToThread()移入其他线程.没有继承QObject的类产生的对象),在其他线程通过deleteLater()内存回收,其他通过指 ...
- ARM汇编程序闪烁灯与其反汇编代码比较
/* *LED闪烁 *led.s */ #define GPJ0CON 0xE0200240 #define GPJ0DAT 0xE0200244 .global _start //把 _start ...
- 前后端分离中,gulp实现头尾等公共页面的复用 前言
前言 通常我们所做的一些页面,我们可以从设计图里面看出有一些地方是相同的.例如:头部,底部,侧边栏等等.如果前后端分离时,制作静态页面的同学,对于这些重复的部分只能够通过复制粘贴到新的页面来,如果页面 ...
- 【Foreign】Game [博弈论][DP]
Game Time Limit: 20 Sec Memory Limit: 512 MB Description 从前有个游戏.游戏分为 k 轮. 给定一个由小写英文字母组成的字符串的集合 S, 在 ...
- python学习笔记(三)之变量和字符串
在其他语言中,变量就是有名字的存储区,可以将值存储在变量中,也即内存中.在Python中略有不同,python并不是将值存储在变量中,更像是把名字贴在值上边.所以,有些python程序员会说pytho ...
- js_判断当前页面是否有网络和网络连接超时
2018-04-12 方法一:通过navigator.onLine属性判断,返回true为有联网状态,false为断网状态. //方法一 if(navigator.onLine) { console. ...
- deepin安装metasploit
[1]安装metasploit 1.curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/tem ...
- Python学习笔记 - day12 - Python操作NoSQL
NoSQL(非关系型数据库) NoSQL,指的是非关系型的数据库.NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称.用于超大规模数据的存储.(例如 ...