Write a data structure, support the following operations:
I command a new payroll file with the initial salary of K. If an employee's initial salary is below the lower wage, he will leave the company immediately.
A orders the wages of each employee plus K
s order to deduct every employee's salary from K
F order to inquire about the wages of K-much
Analysis: Naked treap
But I write the find function when the unequal number of the reverse, and in the rotation when I forget to assign Y to X, resulting in my adjustment for half a day to adjust the ~ ~
Code:
Const maxn=101000;
var delta,root,a1,i,n,min,x,all:longint;
C:char;
T:ARRAY[0..MAXN] of record s,k,l,r,num:longint;
End
Procedure Rttl (var x:longint);
var y:longint;
Begin Y:=T[X].R;
T[X].R:=T[Y].L;
T[y].l:=x;
t[x].s:=t[t[x].l].s+t[t[x].r].s+1;
t[y].s:=t[t[y].l].s+t[t[y].r].s+1;
X:=y;
End
Procedure Rttr (var x:longint);
var y:longint;
Begin Y:=T[X].L;
T[X].L:=T[Y].R;
T[y].r:=x;
t[x].s:=t[t[x].l].s+t[t[x].r].s+1;
t[y].s:=t[t[y].l].s+t[t[y].r].s+1;
X:=y;
End
Procedure Insert (Var d:longint;x:longint);
Begin if D=0 THEN BEGIN Inc (A1);
T[a1].s:=1;
T[a1].num:=random (65536) +1;
T[a1].k:=x;
t[a1].l:=0;
t[a1].r:=0;
D:=A1;
Exit
End
Inc (T[D].S);
If X<T[D].K then begin insert (T[D].L,X);
If T[t[d].l].num>t[d].num then rttr (d);
End ELSE begin insert (T[D].R,X);
If T[t[d].r].num>t[d].num then Rttl (d);
End
End function Delete (VAR D:longint): Boolean;
Begin if D=0 then exit (true);
If T[d].k+delta<min then begin all:=all+t[t[d].l].s+1;
D:=T[D].R;
Delete:=delete (d);
End Else Delete:=delete (T[D].L);
If D>0 then t[d].s:=t[t[d].l].s+t[t[d].r].s+1;
End
function Find (d,x:longint): Longint;
Begin if T[t[d].l].s+1=x then exit (T[d].k+delta);
if (t[t[d].l].s>=x) and (t[d].l>0) then exit (Find (T[D].L,X)) Else exit (Find (t[d].r,x-t[t[d].l].s-1));
End
Begin Randomize;
READLN (n,min);
For I:=1 to n do begin read (c);
READLN (x);
Case C of ' I ': if X>=min then insert (Root,x-delta);
' A ':d elta:=delta+x;
' S ': Begin Delta:=delta-x;
Repeat if root=0 then break;
Until delete (root);
End
' F ': if (x=0) or (X>T[ROOT].S) then Writeln ( -1) Else Writeln (Find (root,t[root].s-x+1)) end;
End
Writeln (All);
End.
C + + code:
#include <iostream> #include <cstdio> #include <ctime> #include <cstdlib> using namespace std;
struct leaf {int k,s,l,r,num;};
int n,m,delta=0,size=0,root=0,tot=0;
Leaf t[100005]={0};
void Rttl (int &x) {int y=t[x].r;
T[X].R=T[Y].L;
T[y].l=x;
t[x].s=t[t[x].l].s+t[t[x].r].s+1;
t[y].s=t[t[y].l].s+t[t[y].r].s+1;
X=y;
} void rttr (int &x) {int y=t[x].l;
T[X].L=T[Y].R;
T[y].r=x;
t[x].s=t[t[x].l].s+t[t[x].r].s+1;
t[y].s=t[t[y].l].s+t[t[y].r].s+1;
X=y;
} void Insert (int &d,int x) {if (d==0) {size++;
t[size].l=t[size].r=0;
T[size].k=x;
T[size].s=1;
T[size].num=rand ();
D=size;
Return
} if (X>T[D].K) {insert (t[d].r,x);
if (t[t[d].r].num>t[d].num) Rttl (d);
}else {Insert (t[d].l,x);
if (t[t[d].l].num>t[d].num) rttr (d);
} t[d].s=t[t[d].l].s+t[t[d].r].s+1;
} void del (int &d) {if (d==0) return;
if (t[d].k+delta<m) {tot+=t[t[d].l].s+1;
D=T[D].R;
Del (d);
Return
} del (T[D].L); t[d].s=t[t[d].l].s+t[t[d].r].s+1;
int find (int d,int k) {if (t[t[d].l].s==k-1) {return t[d].k;
} if (t[t[d].l].s<k-1) {return find (t[d].r,k-t[t[d].l].s-1);
}else {return find (t[d].l,k);
the int main () {scanf ("%d%d", &n,&m);
while (n--) {char w[1];
int x;
scanf ("%s%d", w,&x);
if (w[0]== ' I ') {if (x>=m) insert (Root,x-delta);
}else if (w[0]== ' A ') {delta+=x;
}else if (w[0]== ' S ') {delta-=x;
del (root); }else {if (x>t[root].s| |
x==0) {printf ("%d\n",-1);
}else {printf ("%d\n", Find (root,t[root].s-x+1) +delta);
} printf ("%d\n", tot);
return 0; }