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 ;
}