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 的时间检查整个打出来的字符串,并且 ...
随机推荐
- SQL Server 2008设置主键为自增
环境:SQL Server 2008 问题:设置主键,将主键设为自增. 解决:点击table->选中表->design->选中需要设置主键的字段,单击右键"设置主键&quo ...
- C语言函数的变参实用与分析
实现变参传递的关键是: 传入参数在内存中是连续分布的. #define va_list void* #define va_arg(arg, type) *(type*)arg; arg = (char ...
- 使用kubeadm安装Kubernetes 1.12
使用kubeadm安装Kubernetes 1.12 https://blog.frognew.com/2018/10/kubeadm-install-kubernetes-1.12.html 测试环 ...
- poj 2541 Binary Witch
Binary Witch http://poj.org/problem?id=2541 Time Limit: 1000MS Memory Limit: 65536K Descript ...
- [Luogu 4092] HEOI/TJOI2016 树
[Luogu 4092] HEOI/TJOI2016 树 搜了树剖标签不知道怎么就跳出了个暴搜题啊! 管他既然做了就发上来吧- 有修改标签就向下搜并修改,遇到标签即停止. 这题是真的真的短. #inc ...
- vue-route-transition路由前进后退动画
vue-route-transition vue router 切换动画 特性 模拟前进后退动画 基于css3流畅动画 基于sessionStorage,页面刷新不影响路由记录 路由懒加载,返回可记录 ...
- [cerc2012][Gym100624C]20181013
题意:用元素符号表示字符串 题解:签到题 简单dp 难点在于把元素符号都改成小写qaq #include<cstdio> #include<cstdlib> #include& ...
- 51nod 1806 wangyurzee的树
基准时间限制:1 秒 空间限制:131072 KB wangyurzee有n个各不相同的节点,编号从1到n.wangyurzee想在它们之间连n-1条边,从而使它们成为一棵树.可是wangyur ...
- 【BZOJ】1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
[算法]DP+数学优化 [题意]把n个1~m的数字分成k段,每段的价值为段内不同数字个数的平方,求最小总价值.n,m,ai<=40000 [题解] 参考自:WerKeyTom_FTD 令f[i] ...
- 人人都能掌握的Java服务端性能优化方案
作为一个Java后端开发,我们写出的大部分代码都决定着用户的使用体验.如果我们的后端代码性能不好,那么用户在访问我们的网站时就要浪费一些时间等待服务器的响应.这就可能导致用户投诉甚至用户的流失. 关于 ...