暂且叫它空位模型= =|||,我觉得这个模型往线段树上想的话有点儿牵强...往二分上想更简单点儿,因为这个模型的更新查询操作分不太清楚...
POJ 2828 :倒序操作...
POJ 2886 :简单的模型..涉及到了反素数的知识...(借鉴了wff学长的代码,copy了一份反素数表),百度百科里有段求反素数的程序..DFS实现的...
代码:
POJ2828:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 200005
#define lson rt<<1
#define rson rt<<1|1
int Blk[N<<2];
int Val[N],Pos[N],Ans[N];
void Build(int l,int r,int rt)
{
Blk[rt]=r-l+1;
if(l==r)
return ;
int mid=(l+r)>>1;
Build(l,mid,lson);
Build(mid+1,r,rson);
}
void Find(int n,int l,int r,int rt)
{
Blk[rt]--;
if(l==r)
{
Ans[l]=Val[n];
return ;
}
int mid=(l+r)>>1;
if(Blk[lson]>=Pos[n])
Find(n,l,mid,lson);
else
{
Pos[n]-=Blk[lson];
Find(n,mid+1,r,rson);
}
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
Build(1,n,1);
for(i=0;i<n;++i)
scanf("%d %d",&Pos[i],&Val[i]);
for(i=0;i<n;++i)
Pos[i]++;
for(i=n-1;i>=0;--i)
Find(i,1,n,1);
for(i=1;i<=n;++i)
{
printf("%d%c",Ans[i],i==n?'\n':' ');
}
}
return 0;
}
POJ2886:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 500005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int a[37]={1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560,10080,15120,20160,25200,27720,45360,50400,
55440,83160,110880,166320,221760,277200,332640,498960,500001};
int b[37]={1,2,3,4,6,8,9,10,12,16,18,20,24,30,32,36,40,48,60,64,72,80,84,90,96,100,108,120,128,144,160,168,180,192,200,1314521};
int Blk[N<<2],Remain,A[N];
char Name[N][15];
void Build(int l,int r,int rt)
{
Blk[rt]=r-l+1;
if(l==r)
return ;
int mid=(l+r)>>1;
Build(lson);
Build(rson);
}
int Find(int n,int l,int r,int rt)
{
Blk[rt]--;
if(l==r)
{
return l;
}
int mid=(l+r)>>1;
if(Blk[rt<<1]>=n)
return Find(n,lson);
else
{
n-=Blk[rt<<1];
return Find(n,rson);
}
}
int main()
{
int n,k,i,pos,move,anti,num,ans;
while(scanf("%d %d",&n,&k)!=EOF)
{
for(i=1;i<=n;++i)
{
scanf("%s",Name[i]);
scanf("%d",&A[i]);
}
i=0;
while(a[i]<=n)
i++;
anti=a[i-1],num=b[i-1];
Build(1,n,1);
pos=k,Remain=n;
for(i=1;i<=anti;++i)
{
ans=Find(pos,1,n,1);
move=A[ans];
Remain--;
if(Remain==0)
break;
if(move<0)
{
pos=((pos+move-1)%Remain+Remain)%Remain+1;
}
else
{
pos=(pos-1+move-1)%Remain+1;
}
}
printf("%s %d\n",Name[ans],num);
}
return 0;
}