NOIP2017列隊(phalanx)解題報告
阿新 • • 發佈:2017-11-30
htm else 區間 temp 平衡數 turn esp etc rdo
列隊作為NOIP2017最後一道題,其實並不難,只是相對於其它題目,有點小小的工業
首先,這道題我用splay維護的,如果你不會splay,又想學一下splay,可以來[這裏](http://www.cnblogs.com/dengyixuan/p/7895910.html)學一學,接下來步入正題
首先這道題和往年一樣,特殊數據會給你極大的啟發,在考試時,看到x=1,只有一行的數據時,我就想到,可以維護一顆平衡數,每次只查詢(x,y)由於x=1,所以只要查詢隊列中第y大即可,那麽你再想一想,對於每一行和最後一列維護都分別維護一顆splay即可
但是這樣空間並不允許n*m個點,我們發現,大部分點自始至終都是連續的,所以,對於每一個點,我們可以令它表示一排連續的點,當要查詢的點在連續的區間中,我們就把這個點拆開,拆成三段,(原來的點可以保留,修改一下信息,再加一個點即可)
所以我們只需要在普通splay上加一個spilt來分裂點即可
inline ll spilt(int now,ll k)//要分裂編號為now的點,其中第k個是我們要的數字
{
splay(now,root);
k=k+lef[now]-1;//計算出k的數值
int temp=newnode(k+1,rig[now]);//申請一個新點,區間為k+1至rig[now]
rig[now]=k-1;//舊點區間右端點改為為k-1
if(!ch[now][1])//如果沒有右兒子就接上
{
ch[now][1]=temp;
fa[temp]=now;
}
else //要麽就把原來root的右兒子接到新點右兒子上
{
int pos=nex(now);
ch[pos][0]=temp;fa[temp]=pos;
splay(temp,root);
}
return k;
}
下面是完整代碼
#include<bit/stdc++.h>
using namespace std;
typedef int sign;
typedef long long ll;
#define For(i,a,b) for(register sign i=(sign)a;i<=(sign)b;++i)
#define Fordown(i,a,b) for(register sign i=(sign)a;i>=(sign)b;--i)
const int N=3e6+5;
bool cmax(sign &a,sign b){return (a<b)?a=b,1:0;}
bool cmin(sign &a,sign b){return (a>b)?a=b,1:0;}
template<typename T>T read()
{
T ans=0,f=1;
char ch=getchar();
while(!isdigit(ch)&&ch!=‘-‘)ch=getchar();
if(ch==‘-‘)f=-1,ch=getchar();
while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch-‘0‘),ch=getchar();
return ans*f;
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("splay.in","r",stdin);
freopen("splay.out","w",stdout);
#endif
}
int fa[N],ch[N][2],sz;
ll lef[N],rig[N],size[N];
struct splay_tree
{
int root;
inline void clear(int x){fa[x]=ch[x][0]=ch[x][1]=size[x]=lef[x]=rig[x]=0;}
inline int get(int x){return x==ch[fa[x]][1];}
inline void push_up(int x){size[x]=size[ch[x][0]]+size[ch[x][1]]+rig[x]-lef[x]+1;}
inline void rotate(int x,int &k)
{
int old=fa[x],oldfa=fa[old],o=get(x);
if(old==k)k=x;
else ch[oldfa][old==ch[oldfa][1]]=x;
fa[x]=oldfa;fa[old]=x;fa[ch[x][o^1]]=old;
ch[old][o]=ch[x][o^1];ch[x][o^1]=old;
push_up(old);push_up(x);
}
inline void splay(int x,int &k)
{
while(x^k)
{
if(fa[x]^k)rotate(get(x)^get(fa[x])?x:fa[x],k);
rotate(x,k);
}
push_up(x);
}
inline int find_last()//因為插到最後面,所以一直往右兒子走
{
int now=root;
while(ch[now][1])now=ch[now][1];
return now;
}
inline int newnode(ll l,ll r)
{
++sz;lef[sz]=l;rig[sz]=r;size[sz]=r-l+1;
return sz;
}
inline void init(ll l,ll r){root=newnode(l,r);}
inline void insert(ll v)
{
int now=newnode(v,v);
int pos=find_last();
ch[pos][1]=now;fa[now]=pos;
splay(now,root);
}
inline int pre(int x)
{
int now=ch[x][0];
while(ch[now][1])now=ch[now][1];
return now;
}
inline int nex(int x)
{
int now=ch[x][1];
while(ch[now][0])now=ch[now][0];
return now;
}
inline ll spilt(int now,ll k)
{
splay(now,root);
k=k+lef[now]-1;
int temp=newnode(k+1,rig[now]);
rig[now]=k-1;
if(!ch[now][1])
{
ch[now][1]=temp;
fa[temp]=now;
}
else
{
int pos=nex(now);
ch[pos][0]=temp;fa[temp]=pos;
splay(temp,root);
}
return k;
}
inline ll find_ans(ll x)//找第k大
{
int now=root;ll len;
while(1)
{
if(x<=size[ch[now][0]])now=ch[now][0];
else
{
x-=size[ch[now][0]];
len=rig[now]-lef[now]+1;
if(x<=len)//要找的點在這個點的區間中
{
//ps:如果這個點左區間等於右區間,不必刪掉它(很慢),把左端點++,這樣這個點的長度就變成0了,不影響這棵樹
if(x==1)
{
ll res=lef[now];
lef[now]++;
splay(now,root);
push_up(now);
return res;
}
if(x==len)
{
ll res=rig[now];
rig[now]--;
splay(now,root);
push_up(now);
return res;
}
else return spilt(now,x);
}
x=x-(rig[now]-lef[now]+1);
now=ch[now][1];
}
}
}
}s[N];
ll n,m,q;
inline void input(){n=read<ll>();m=read<ll>();q=read<ll>();}
inline void init()
{
For(i,1,n)s[i].init(m*(i-1)+1,i*m-1);
s[0].init(m,m);
For(i,2,n)s[0].insert(i*m);
}
inline void work()
{
int x,y;
ll v;
while(q--)
{
x=read<int>(),y=read<int>();
if(y==m)
{
printf("%lld\n",v=s[0].find_ans(x));
s[0].insert(v);
}
else
{
printf("%lld\n",v=s[x].find_ans(y));
s[x].insert(s[0].find_ans(x));
s[0].insert(v);
}
}
}
int main()
{
file();
input();
init();
work();
return 0;
}
NOIP2017列隊(phalanx)解題報告