3
14
2016
0

BZOJ 1503: [NOI2004]郁闷的出纳员

Time Limit: 5 Sec  Memory Limit: 64 MB

Description

OIER公司是一家大型专业化软件公司,有着数以万计的员工。作为一名出纳员,我的任务之一便是统计每位员工的工资。这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资。如果他心情好,就可能把每位员工的工资加上一个相同的量。反之,如果心情不好,就可能把他们的工资扣除一个相同的量。我真不知道除了调工资他还做什么其它事情。工资的频繁调整很让员工反感,尤其是集体扣除工资的时候,一旦某位员工发现自己的工资已经低于了合同规定的工资下界,他就会立刻气愤地离开公司,并且再也不会回来了。每位员工的工资下界都是统一规定的。每当一个人离开公司,我就要从电脑中把他的工资档案删去,同样,每当公司招聘了一位新员工,我就得为他新建一个工资档案。老板经常到我这边来询问工资情况,他并不问具体某位员工的工资情况,而是问现在工资第k多的员工拿多少工资。每当这时,我就不得不对数万个员工进行一次漫长的排序,然后告诉他答案。好了,现在你已经对我的工作了解不少了。正如你猜的那样,我想请你编一个工资统计程序。怎么样,不是很困难吧?

Input

Output

输出文件的行数为F命令的条数加一。对于每条F命令,你的程序要输出一行,仅包含一个整数,为当前工资第k多的员工所拿的工资数,如果k大于目前员工的数目,则输出-1。输出文件的最后一行包含一个整数,为离开公司的员工的总数。

Sample Input

9 10
I 60
I 70
S 50
F 2
I 30
S 15
A 5
F 1
F 2

 

Sample Output

10
20
-1
2

 

HINT

I命令的条数不超过100000 A命令和S命令的总条数不超过100 F命令的条数不超过100000 每次工资调整的调整量不超过1000 新员工的工资不超过100000

这里是跳表~好开森。。居然写对了呢

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <time.h>

const int maxl=50,maxlongint=0x7fffffff;

struct node
{
	int data,pnum;
	int l;
	node* next[maxl+10];
	int sum[maxl+10];
};
typedef node* Point;
struct list
{
	int level;
	Point head,tail;
};

int totnum[maxl+10];
Point update[maxl+10];
list skip;
int n,min,totp,ans,delta;

void Init()
{
	srand(time(NULL));
	scanf("%d%d\n",&n,&min);
	delta=ans=totp=0;
	skip.level=1;
	skip.head=new node;
	skip.head->data=-maxlongint; 
	skip.head->pnum=0; 
	skip.head->l=1; 
	memset(skip.head->sum,0,sizeof(skip.head->sum));
	skip.tail=new node;
	skip.tail->data=maxlongint; 
	skip.tail->pnum=0; 
	memset(skip.tail->next,0,sizeof(skip.tail->next));
	for(int i=1;i<=maxl;++i)
		skip.head->next[i]=skip.tail;
}

int Rand()
{
	int tot=1;
	while(rand()%2)
	{
		++tot;
		if(tot==maxl||tot==skip.level+1)break;
	}
	return tot;
}

Point Find(int k)
{
	Point p=skip.head;
	for(int i=skip.level;i;--i)
	{
		totnum[i]=0;
		while(p->next[i]->data<k)
		{
			totnum[i]+=p->sum[i];
			p=p->next[i];
		}
		update[i]=p;
	}
	return p;
}

void Insert(int k)
{
	Point p,t;
	int tot;
	p=Find(k);
	if(p->next[1]->data==k)
	{
		p=p->next[1]; p->pnum++;
		for(int i=skip.level;i;--i)update[i]->sum[i]++;
	}
	else
	{
		t=new node;
		t->data=k; t->pnum=1; t->l=Rand();
		if(t->l>=skip.level)
		{
			for(int i=skip.level+1;i<=t->l+1;++i)
			{
				update[i]=skip.head; update[i]->sum[i]=totp;
				skip.head->next[i]=skip.tail;
				totnum[i]=0;
			}
			skip.level=t->l+1;
			skip.head->l=skip.level;
			skip.tail->l=skip.level;
		}
		memset(t->next,0,sizeof(t->next));
		tot=0;
		for(int i=1;i<=skip.level;++i)
		{
			if(i<=t->l)
			{
				t->sum[i]=update[i]->sum[i]-tot;
				t->next[i]=update[i]->next[i];
				update[i]->next[i]=t;
				update[i]->sum[i]=tot+t->pnum;
				tot+=totnum[i];
			}
			else update[i]->sum[i]=update[i]->sum[i]+t->pnum;
		}
	}
	++totp;
}

void Delete()
{
	int tot;
	Find(min+delta);
	tot=0;
	for(int i=1;i<=skip.level;++i)
	{
		skip.head->next[i]=update[i]->next[i];
		skip.head->sum[i]=update[i]->sum[i]-tot;
		tot+=totnum[i];
	}
	while(skip.level>1 && skip.head->next[skip.head->l-1]==skip.tail)
	{
		skip.head->sum[skip.level]=0;
		skip.level--;
		skip.head->l--;
		skip.tail->l--;
	}
	ans+=tot; totp-=tot;
}

void Findnum(int k)
{
	Point p;
	k=totp-k+1;
	if(k<=0)puts("-1");
	else 
	{
		p=skip.head;
		for(int i=skip.level;i;--i)
		{
			while(p!=skip.tail && p->sum[i]<k && k>0)
			{
				k-=p->sum[i];;
				p=p->next[i];
			}
		}
		p=p->next[1];
		printf("%d\n",(p->data)-delta);
	}
}

void Make()
{
	int k;
	char c;
	for(int i=1;i<=n;++i)
	{
		scanf("%c%d\n",&c,&k);
		if(c=='I')
		{
			if(k<min)continue;
			k+=delta; 
			Insert(k);
		}
		if(c=='A')delta-=k;
		if(c=='S'){delta+=k;Delete();};
		if(c=='F')Findnum(k);
	}
	printf("%d\n",ans);
}

int main()
{
	Init();
	Make();
	return 0;
}
const maxl=50;
type
  point=^node;
  node=record
         data,pnum:longint;
         l:integer;
         next:array[1..maxl]of point;
         sum:array[1..maxl]of longint;
  end;
  list=record
         level:integer;
         head,tail:point;
  end;
var
  totnum:array[1..maxl]of longint;
  updata:array[1..maxl]of point;
  skip:list;
  n,min,totp,ans,delta:longint;
procedure Init();
  var i:integer;
  begin
    randomize;
    readln(n,min);
    delta:=0; ans:=0; totp:=0;
    skip.level:=1;
    new(skip.head);
    with(skip.head^)do
    begin
      data:=-maxlongint;
      pnum:=0;
      l:=1;
	  fillchar(sum,sizeof(sum),0);
    end;
    new(skip.tail);
    with(skip.tail^)do
      begin
        data:=maxlongint;
        pnum:=0;
		fillchar(next,sizeof(next),0);
      end;
    for i:=1 to maxl do skip.head^.next[i]:=skip.tail;
  end;

function rand():integer;
  begin
    rand:=1;
    while(random(2)=1)do 
	begin 
	  inc(rand);
	  if(rand=skip.level+1) or (rand=maxl)then break;
	end;
  end;

function Find(k:longint):point;
  var
    p:point;
    i:integer;
  begin
    p:=skip.head;
    for i:=skip.level downto 1 do
    begin
      totnum[i]:=0;
      while p^.next[i]^.data<k do 
	  begin 
	    totnum[i]:=totnum[i]+p^.sum[i]; 
		p:=p^.next[i]; 
	  end;
      updata[i]:=p;
    end;
    Find:=p;
  end;

procedure Insert(k:longint);
  var
    p,t:point;
    i:integer;
    tot:longint;
  begin
    p:=Find(k);
    if p^.next[1]^.data=k then
    begin
      p:=p^.next[1];
      inc(p^.pnum);
      for i:=skip.level downto 1 do inc(updata[i]^.sum[i]);
    end
    else
    begin
      new(t);
      t^.data:=k;
      t^.pnum:=1;
      t^.l:=rand();
      if(t^.l>=skip.level)then
      begin
        for i:=skip.level+1 to t^.l+1 do
        begin
          updata[i]:=skip.head; updata[i]^.sum[i]:=totp;
          skip.head^.next[i]:=skip.tail;
          totnum[i]:=0;
        end;
        skip.level:=t^.l+1; skip.head^.l:=skip.level;
        skip.tail^.l:=skip.level;
      end;
	  fillchar(t^.next,sizeof(t^.next),0);
      tot:=0;
      for i:=1 to skip.level do
      begin
        if i<=t^.l then
        begin
          t^.sum[i]:=updata[i]^.sum[i]-tot;
          t^.next[i]:=updata[i]^.next[i];
          updata[i]^.next[i]:=t;
          updata[i]^.sum[i]:=tot+t^.pnum;
          tot:=tot+totnum[i]
        end else updata[i]^.sum[i]:=updata[i]^.sum[i]+t^.pnum;
      end;
    end;
    inc(totp);
  end;

procedure Delete();
  var
    i:integer;
    tot:longint;
  begin
    Find(min+delta);
    tot:=0;
    for i:=1 to skip.level do
    begin
      skip.head^.next[i]:=updata[i]^.next[i];
      skip.head^.sum[i]:=updata[i]^.sum[i]-tot;
      tot:=tot+totnum[i];
    end;
    while (skip.level>1) and (skip.head^.next[skip.head^.l-1]=skip.tail)do
      begin
        skip.head^.sum[skip.level]:=0;
        dec(skip.level); dec(skip.head^.l); dec(skip.tail^.l)
      end;
    inc(ans,tot);
    dec(totp,tot);
  end;

procedure Findnum(k:longint);
  var
    p:point;
    i:integer;
  begin
    k:=totp-k+1;
    if k<=0 then writeln(-1)
    else
    begin
      p:=skip.head;
      for i:=skip.level downto 1 do
      begin
        while(p<>skip.tail)and(p^.sum[i]<k)and(k>0)do
          begin
            dec(k,p^.sum[i]);
            p:=p^.next[i];
          end;
      end;
      p:=p^.next[1]; writeln(p^.data-delta);
    end;
  end;

procedure Make();
  var
    i,k:longint;
    c:char;
  begin
    for i:=1 to n do
      begin
        readln(c,k);
        if c='I' then
          begin
            if k<min then continue;
            k:=k+delta;
            Insert(k);
          end;
        if c='A' then delta:=delta-k;
        if c='S' then begin delta:=delta+k; Delete();end;
        if c='F' then Findnum(k);
      end;
    writeln(ans);
  end;

begin
  Init();
  Make();
end.
Category: 程序 | Tags: bzoj 跳表 | Read Count: 182

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com