UVa 1402 Runtime Error 伸展树
Runtime Error 到现在连样例也跑不出来!!!
调试了一晚上快要死了……
知道错在哪里但是不会改,代码先扔在这里吧。看来不能太依赖模板啊orz……
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; struct Node
{
Node *ch[];
int v; //节点编号
int s; //节点域
int flip;
Node( int v ):v(v)
{
ch[] = ch[] = NULL;
s = ;
flip = ;
}
int cmp( int x ) const
{
int t = ( ch[] == NULL ) ? : ch[]->s;
if ( t >= x ) return ;
if ( t + == x ) return -;
return ;
}
void maintain()
{
s = ;
if ( ch[] != NULL ) s += ch[]->s;
if ( ch[] != NULL ) s += ch[]->s;
return;
}
void pushDown()
{
if ( flip )
{
flip = ;
swap( ch[], ch[] );
if ( ch[] != NULL ) ch[]->flip = !ch[]->flip;
if ( ch[] != NULL ) ch[]->flip = !ch[]->flip;
}
}
}; struct number
{
int val;
int i;
}; const int MAXN = ; int n;
number num[MAXN];
int SA[MAXN]; void Rotate( Node* &o, int d ) //d=0 左旋 d=1 右旋
{
Node *k = o->ch[ d ^ ];
o->ch[ d ^ ] = k->ch[d];
k->ch[d] = o;
o = k;
o->ch[d]->maintain();
o->maintain();
return;
} void splay( Node* &o, int k )
{
o->pushDown();
int d = o->cmp(k);
if ( d == )
{
if ( o->ch[] != NULL )
k -= o->ch[]->s;
--k;
}
if ( d != - )
{
Node *p = o->ch[d];
p->pushDown();
int d2 = p->cmp(k);
int k2 = k;
if ( d2 == )
{
if ( p->ch[] != NULL )
k2 -= p->ch[]->s;
--k2;
}
if ( d2 != - )
{
splay( p->ch[d2], k2 );
if ( d == d2 ) Rotate( o, d ^ );
else Rotate( o->ch[d], d );
}
Rotate( o, d ^ );
}
return;
} void build( Node* &o, int l, int r )
{
int m = ( l + r ) >> ; o = new Node( m );
if ( l < m ) build( o->ch[], l, m - );
if ( r > m ) build( o->ch[], m + , r );
o->maintain(); return;
} void DFS( Node *cur )
{
cur->pushDown();
if ( cur->ch[] ) DFS( cur->ch[] );
//if ( cur->v && cur->v != n + 1 )
printf( "num=%d id=%d\n", SA[cur->v], cur->v );
if ( cur->ch[] ) DFS( cur->ch[] );
return;
} bool cmp( number a, number b )
{
if ( a.val == b.val ) return a.i < b.i;
return a.val < b.val;
} int Search( Node *o, int x )
{
int res = ;
while ( o != NULL )
{
o->pushDown();
int d;
if ( x == o->v ) d = -;
else if ( x < o->v ) d = ;
else d = ; printf("search=%d\n", o->v ); if ( d == - )
{
if ( o->ch[] != NULL )
res += o->ch[]->s;
++res;
return res;
}
else
{
if ( o->v == && x == )
{
printf("%d\n", o->ch[]->v );
puts("@@@");
}
if ( d == )
{
if ( o->ch[] != NULL )
res += o->ch[]->s;
++res;
}
o = o->ch[d];
}
}
return -;
} Node *root; Node *Merge( Node *left, Node *right )
{
splay( left, left->s );
left->ch[] = right;
left->maintain();
return left;
} void RemoveRoot()
{
Node *tmp = root;
root = Merge( root->ch[], root->ch[] );
delete tmp;
return;
} int main()
{
while ( scanf( "%d", &n ), n )
{
for ( int i = ; i <= n; ++i )
{
scanf("%d", &num[i].val );
num[i].i = i;
} root = NULL;
build( root, , n );
sort( num + , num + n + , cmp ); for ( int i = ; i <= n; ++i ) SA[ num[i].i ] = num[i].val;
//DFS( root );
for ( int i = ; i <= n; ++i )
{
int id = Search( root, num[i].i );
printf( "num = %d %d %d\n", num[i].val, num[i].i, id );
splay( root, id );
DFS(root);
if ( i ) putchar(' '); int tmp;
if ( root->ch[] ) tmp = i + root->ch[]->s;
else tmp = i;
printf( "%d\n\n", tmp ); root->ch[]->flip ^= ; RemoveRoot();
}
puts("");
}
return ;
}
UVa 1402 Runtime Error 伸展树的更多相关文章
- UVa 11922 - Permutation Transformer 伸展树
第一棵伸展树,各种调试模板……TVT 对于 1 n 这种查询我处理的不太好,之前序列前后没有添加冗余节点,一直Runtime Error. 后来加上冗余节点之后又出了别的状况,因为多了 0 和 n+1 ...
- UVA 11922 Permutation Transformer —— splay伸展树
题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...
- runtime error 的原因
1. 数组访问越界 2. 分母为 0 3. 括号 做题时偶然发现的! (详见 UVA 10158 War) 代码中出现了这种东西 else if( arefriends(x,y==-1) ) ...
- AVL树和伸展树 -数据结构(C语言实现)
读数据结构与算法分析 AVL树 带有平衡条件的二叉树,通常要求每颗树的左右子树深度差<=1 可以将破坏平衡的插入操作分为四种,最后通过旋转恢复平衡 破坏平衡的插入方式 描述 恢复平衡旋转方式 L ...
- com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Cannot assign requested address: bind
在发布 web service 时报错: Endpoint.publish(publishAddress, hl7MessageReveiver); com.sun.xml.internal.ws.s ...
- Arcmap 安装完后使用出现visual fortran run-time error的解决方法
今天将ArcGIS安装到自己的XP笔记本上,安装过程一帆风顺,但打开Arcmap使用的时候,出现了visual fortran run-time error. 下面是解决方法: 下载个Dforrt.d ...
- Qt Sqlite qwt 发布过程中碰到的问题runtime error
qt版本:4.8.0 qwt版本:6.1.2 使用dll show检测缺少的dll,或者笨一点的方法,点击运行差什么找什么放进去: 左上显示exe调用哪些dll,右边是dll又再次调用啦哪些dll: ...
- Splay伸展树学习笔记
Splay伸展树 有篇Splay入门必看文章 —— CSDN链接 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 创造者:Daniel Sleator 和 Robert Ta ...
- Codeforce - Runtime Error
Bahosain was trying to solve this simple problem, but he got a Runtime Error on one of the test case ...
随机推荐
- ActiveMQ.xml文件的主要配置
ActiveMQ.xml文件默认位置位于 activemq/conf/目录下,主要的配置及解析如下:<beans xmlns="http://www.springframework.o ...
- swap分区添加
首先你需要使用命令:dd 来创建一个swapfile,然后你需要使用mkswap命令在设备或者文件中创建一个Linux swap分区a) 使用root用户登陆b) 使用下面的命令创建一个2G的 Swa ...
- SSH时不需输入密码
我这里有2台机器,一台装了Teradata数据库,ip是192.168.184.128,称它为teradata-pc:另一台装了Oracle数据库,ip地址是192.168.184.129,称它为 ...
- centos 6.5安装vncserver 并开启远程桌面
vnc是一款使用广泛的服务器管理软件,可以实现图形化管理,下面简单介绍一下如何在centos6.5下安装vnc. 1.下载vncserver yum install tigervnc tige ...
- Scrumworks乱码
要搞敏捷,先找个工具.选了scrumworks5.1.安装完后发现录入汉字乱码. 环境: server:CentOS linux db:mysql5.0 appserver:jboss(scrumwo ...
- 基于FPGA的线阵CCD图像测量系统研究——笔记
本文是对基于FPGA的线阵CCD图像测量系统研究(作者:高尚)的阅读笔记 第一章绪论 1. 读读看 读了前面的摘要依然没有看懂作者要做什么.接着往下读....终于看到了一个字眼“基于机器视觉的图像测量 ...
- JAVA类与对象(一)----基础概念理解
面向对象基本概念 面向对象是一种新兴的程序设计方法,或者说是一种新的程序设计规范,其基本思想是使用对象.类.继承.封装.消息等基本概念来进行程序设计.它是从现实世界客观存在的事物(即对象)出发来构造软 ...
- Java 8 VM GC Tunning Guild Charter 9-b
第九章 G1 GC The Garbage-First (G1) garbage collector is a server-style garbage collector, targeted for ...
- Framework 类库的事件编程
http://msdn.microsoft.com/zh-cn/library/aa663632.aspx 本页内容 EventHandler 委托 自定义的事件参数 参数化自定义事件 小结 本月的内 ...
- SQL Server 创建表 添加主键 添加列常用SQL语句【转】
--删除主键alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段名 ...