这题c++怎么办?

cin/cout老是超时....

咋办...

#include

using namespace std;

typedef struct Node

{

int size,Price,key;

Node *left,*right;

}*Treap;

int n,LimitMin,delta=0,tot=0;

Treap root,son;

void Updata(Treap tree)

{

tree -> size = tree -> left -> size + tree -> right -> size + 1;

}

Treap newnode(int key)

{

tot++;

Treap tnode = new Node;

tnode -> Price = key;

tnode -> key = rand();

tnode -> left = son;

tnode -> right = son;

Updata(tnode);

return tnode;

}

Treap Left(Treap tree)

{

Treap T = tree -> right;

tree -> right = T -> left;

T -> left = tree;

Updata(tree);

Updata(T);

return T;

}

Treap Right(Treap tree)

{

Treap T = tree -> left;

tree -> left = T -> right;

T -> right = tree;

Updata(tree);

Updata(T);

return T;

}

Treap Insert(Treap ins,Treap index)

{

if(ins -> Price Price)

{

if(index -> left == son)

index -> left = ins;

else

index -> left = Insert(ins,index -> left);

if(index -> key > index -> left -> key)

index = Right(index);

}

else

{

if(index -> right == son)

{

index -> right = ins;

}

else

index -> right = Insert(ins,index -> right);

if(index -> key > index -> right -> key)

index = Left(index);

}

Updata(index);

return index;

}

int K_th(long rank)

{

Treap temp = root -> left;

if(temp -> size < rank)

return -1;

while(rank != temp -> size - temp -> left -> size)

{

if(rank < temp -> size - temp -> left -> size)

temp=temp->right;

else

{

rank -= temp -> size - temp -> left -> size;

temp = temp -> left;

}

}

return temp->Price;

}

void Delete(int key,Treap del)

{

if(del -> left != son)

if(del -> left -> Price + delta < key)

{

del -> left = del -> left -> right;

Updata(del);

Delete(key,del);

Updata(del);

}

else

{

Delete(key,del -> left);

Updata(del);

}

else

return;

}

void work()

{

int i,k;

char op;

son = new Node;

root = new Node;

son -> key = 99999999;

son -> size = 0;

root -> key = -1;

root -> left = son;

root -> right = son;

root -> Price = 99999999;

root -> size = 1;

cin >> n >> LimitMin;

for(i = 1 ; i > op >> k;

if(op == 'I')

{

if(k >= LimitMin)

Insert(newnode(k-delta),root);

}

if(op == 'S')

{

delta -= k;

Delete(LimitMin,root);

}

if(op == 'A')

delta += k;

if(op == 'F')

{

k = K_th(k);

if(k == -1)

cout

3 条评论

  • @ 2016-08-11 10:15:18
    ios :: sync_with_stdio(false);
    
  • @ 2010-04-03 16:20:41

    不中啊

    不行的...

    scanf()用了错5个点

    cin/cout超时3个点

  • @ 2010-04-03 14:01:31

    cin超时就用cstdio的scanf啊~ 真冷~

  • 1

信息

ID
1507
难度
7
分类
数据结构 | 平衡树 点击显示
标签
递交数
2541
已通过
409
通过率
16%
被复制
4
上传者