求助!!!

本人是用SBT做的,不知哪写疵了无限TLE,求各位大神赐教!!!

评测结果
编译成功

测试数据 #0: Accepted, time = 906 ms, mem = 2464 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 2124 KiB, score = 10
测试数据 #2: Accepted, time = 15 ms, mem = 2124 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 2124 KiB, score = 10
测试数据 #4: Accepted, time = 156 ms, mem = 2204 KiB, score = 10
测试数据 #5: Accepted, time = 734 ms, mem = 2464 KiB, score = 10
测试数据 #6: TimeLimitExceeded, time = 1156 ms, mem = 2460 KiB, score = 0
测试数据 #7: Accepted, time = 31 ms, mem = 2120 KiB, score = 10
测试数据 #8: Accepted, time = 125 ms, mem = 2124 KiB, score = 10
测试数据 #9: Accepted, time = 1000 ms, mem = 2464 KiB, score = 10
TimeLimitExceeded, time = 4123 ms, mem = 2464 KiB, score = 90

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,delta,ans=0,k,mem=0;
int root=0,cnt=0;
char op;

struct node
{
  int l,r,siz,key;
  void newnode()
  {
    l=r=0;
    siz=1;
  }
}tree[100010];

void right_rot(int &v)
{
  int x=tree[v].l;
  tree[v].l=tree[x].r;
  tree[x].r=v;
  tree[x].siz=tree[v].siz;
  tree[v].siz=tree[tree[v].l].siz+tree[tree[v].r].siz+1;
  v=x;
}

void left_rot(int &v)
{
  int x=tree[v].r;
  tree[v].r=tree[x].l;
  tree[x].l=v;
  tree[x].siz=tree[v].siz;
  tree[v].siz=tree[tree[v].l].siz+tree[tree[v].r].siz+1;
  v=x;
}

void maintain(int &v,bool flag)
{
  if (!flag)
  {
    if (tree[tree[tree[v].l].l].siz>tree[tree[v].r].siz)
      right_rot(v);
    else if(tree[tree[tree[v].l].r].siz>tree[tree[v].r].siz)
    {
      left_rot(tree[v].l);
      right_rot(v);
    }
    else return;
  }
  else
  {
    if (tree[tree[tree[v].r].r].siz>tree[tree[v].l].siz)
      left_rot(v);
    else if(tree[tree[tree[v].r].l].siz>tree[tree[v].l].siz)
    {
      right_rot(tree[v].r);
      left_rot(v);
    }
    else return;
  }
  maintain(tree[v].l,0);
  maintain(tree[v].r,1);
  maintain(v,1);
  maintain(v,0);
}

void insert(int &v,int p)
{
  if (v==0)
  {
    v=++cnt;
    tree[cnt].key=p;
    tree[cnt].newnode();
  }
  else
  {
    tree[v].siz++;
    if (p<tree[v].key) insert(tree[v].l,p);
    else insert(tree[v].r,p);
    maintain(root,p>=tree[v].key);
  }
}

void delete_min()
{
  int temp=root;
  if (!tree[temp].l) {root=tree[root].r;return;}
  while(tree[tree[temp].l].l)
  {
    tree[temp].siz--;
    temp=tree[temp].l;
  }
  tree[temp].siz--;
  tree[temp].l=tree[tree[temp].l].r;
}

int get_Kth(int &v,int k)
{
  int x=tree[tree[v].l].siz+1;
  if (x>k) return get_Kth(tree[v].l,k);
  if (x<k) return get_Kth(tree[v].r,k-x);
  return tree[v].key;
}

int get_min(int v)
{
  while(tree[v].l) v=tree[v].l;
  return tree[v].key;
}

int main()
{
  freopen("cashier.in","r",stdin);
  freopen("cashier.out","w",stdout);
  
  scanf("%d%d",&n,&m);getchar();
  delta=0;
  while(n--)
  {
    scanf("%c%d",&op,&k);getchar();
    if (op=='I')
    {
      if (k>=m) {insert(root,k-delta);mem++;}
    }
    if (op=='A') delta+=k;
    if (op=='S')
    {
      delta-=k;
      while(mem>0&&get_min(root)<m-delta)
      {
        delete_min();
        ans++;mem--;
      }
    }
    if (op=='F')
    {
      if (k>mem) printf("-1\n");
      else printf("%d\n",get_Kth(root,mem-k+1)+delta);
    }
  }
  printf("%d\n",ans);
  
  return 0;
}

我对着写的题解就过了:
```c++
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN=100010;

struct SBT
{
int left,right,size,key;
void Init()
{
left=right=0;
size=1;
}
}tree[MAXN];
int tot,root;
void left_rotate(int &x)
{
int y=tree[x].right;
tree[x].right=tree[y].left;
tree[y].left=x;
tree[y].size=tree[x].size;
tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;
x=y;
}
void right_rotate(int &x)
{
int y=tree[x].left;
tree[x].left=tree[y].right;
tree[y].right=x;
tree[y].size=tree[x].size;
tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;
x=y;
}
void maintain(int &x,int flag)
{
if(flag==0)
{
if(tree[tree[tree[x].left].left].size > tree[tree[x].right].size)
right_rotate(x);
else if(tree[tree[tree[x].left].right].size > tree[tree[x].right].size)
left_rotate(tree[x].left),right_rotate(x);
else return;
}
else
{
if(tree[tree[tree[x].right].right].size > tree[tree[x].left].size)
left_rotate(x);
else if(tree[tree[tree[x].right].left].size > tree[tree[x].left].size)
right_rotate(tree[x].right),left_rotate(x);
else return;
}
maintain(tree[x].left,0);
maintain(tree[x].right,1);
maintain(x,0);
maintain(x,1);
}
void insert(int &x,int key)
{
if(x==0)
{
x=++tot;
tree[x].Init();
tree[x].key=key;
}
else
{
tree[x].size++;
if(key<tree[x].key)insert(tree[x].left,key);
else insert(tree[x].right,key);
maintain(x,key>=tree[x].key);
}
}
int get_Kth(int &x,int k)
{
if(k<=tree[tree[x].left].size)
return get_Kth(tree[x].left,k);
if(k>tree[tree[x].left].size+1)
return get_Kth(tree[x].right,k-tree[tree[x].left].size-1);
return tree[x].key;
}
int get_min(int r)
{
while(tree[r].left)
r=tree[r].left;
return r;
}
int DeleteMin()
{
int temp=root;
if(tree[temp].left==0)
{
root=tree[root].right;
return tree[temp].key;
}
while(tree[tree[temp].left].left)
{
tree[temp].size--;
temp=tree[temp].left;
}
tree[temp].size--;
int ret=tree[tree[temp].left].key;
tree[temp].left=tree[tree[temp].left].right;
return ret;
}
int main()
{
char op[10];
int u;
int n,Min;
int temp=0;
while(scanf("%d%d",&n,&Min)==2)
{
tot=root=0;
temp=0;
int ans=0;
while(n--)
{
scanf("%s",&op);
if(op[0]=='I')
{
scanf("%d",&u);
if(u>=Min)
{
insert(root,u-temp);
}
}
else if(op[0]=='A')
{
scanf("%d",&u);
temp+=u;
}
else if(op[0]=='S')
{
scanf("%d",&u);
temp-=u;
while(root!=0 && tree[get_min(root)].key+temp<Min)
{
DeleteMin();
ans++;
}
}
else
{
scanf("%d",&u);
if(root==0 || u>tree[root].size)printf("-1\n");
else
{
printf("%d\n",get_Kth(root,tree[root].size+1-u)+temp);
}
}
}
printf("%d\n",ans);return 0;
}
return 0;
}
```

0 条评论

目前还没有评论...

信息

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