Description
思路:
先想一下当x=1的时候该怎么做,发现只有一条链维护删除一个元素添加到后面,其实就是一个很长的数组,只是后面一开始空了一截,删除的话就空在那里,再放到后面最后面,要找第k个元素的话用线段树表示这个点存在不存在然后树上二分即可。
然后考虑满分做法,只是将一条链换成了n条链,显然,不管是线段树还是存值的数组都开不下,但是发现我们不知道的只是后面的点,前面点可以通过坐标求出来,于是后面的点要开数组的话就用vector。线段树的空间不够的话,就用线段树动态开点(虽然我也没有听说过),因为线段树是不断二分的,所以开的点也是log的。
要注意一开始你假设的空间就是开满的,即开maxn+maxq。(这里一开始没想明白)。
/*==============================
* Auhtor : ylsoi
* Problem : phalanx
* Algorithm : Segment_Tree
* Time : 2018.5.18
* ===========================*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<climits>
#include<vector>
using namespace std;
void File(){
freopen("[noip2017]phalanx.in","r",stdin);
freopen("[noip2017]phalanx.out","w",stdout);
}
#define REP(i,a,b) for(register int i=a;i<=b;++i)
#define DREP(i,a,b) for(register int i=a;i>=n;--i)
#define MREP(i,x) for(register int i=beg[x];i;i=E[i].last)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define inf INT_MAX
const int maxn=3e5+10;
int n,m,q;
struct Segment_Tree{
#define mid ((l+r)>>1)
struct node{
node *lc,*rc;
int sum;
node(){lc=rc=NULL;sum=0;}
}root;
void build(node *rt,int l,int r,int up){
rt->sum=up-l+1;
if(mid>up){
rt->lc=new node;
build(rt->lc,l,mid,up);
}
if(mid+1<=up){
rt->rc=new node;
build(rt->rc,mid+1,r,up);
}
}
void update(node *rt,int l,int r,int pos,int x,int up){
rt->sum+=x;
if(l==r)return;
else{
if(pos<=mid){
if(rt->lc==NULL){
rt->lc=new node;
rt->lc->sum= mid<=up ? mid-l+1 : 0;
}
update(rt->lc,l,mid,pos,x,up);
}
else{
if(rt->rc==NULL){
rt->rc=new node;
rt->rc->sum= r<=up ? r-mid : 0;
}
update(rt->rc,mid+1,r,pos,x,up);
}
}
}
int find(node *rt,int l,int r,int x){
if(rt==NULL)return l+x-1;
int lenl= rt->lc==NULL ? mid-l+1 : rt->lc->sum;
if(lenl<x)return find(rt->rc,mid+1,r,x-lenl);
else return find(rt->lc,l,mid,x);
}
}T[maxn];
int cnt[maxn];
vector<ll>a[maxn];
ll cal(int x,int y){
++cnt[x];
ll va;
int pos=T[x].find(&T[x].root,1,m-1+maxn,y);
T[x].update(&T[x].root,1,m-1+maxn,pos,-1,m-1);
if(pos<=m-1)va=(ll)(x-1)*m+pos;
else va=a[x][pos-m];
return va;
}
ll move(int x,int y,ll va){
++cnt[0];
ll w;
int pos=T[0].find(&T[0].root,1,n+maxn,x);
T[0].update(&T[0].root,1,n+maxn,pos,-1,n);
if(pos<=n)w=(ll)pos*m;
else w=a[0][pos-n-1];
if(y!=m){
T[x].update(&T[x].root,1,m-1+maxn,m-1+cnt[x],1,m-1);
a[x].push_back(w);
}
else va=w;
T[0].update(&T[0].root,1,n+maxn,n+cnt[0],1,n);
a[0].push_back(va);
return w;
}
int main(){
File();
scanf("%d%d%d",&n,&m,&q);
REP(i,1,n)T[i].build(&T[i].root,1,m-1+maxn,m-1);
T[0].build(&T[0].root,1,n+maxn,n);
REP(i,1,q){
int x,y;
scanf("%d%d",&x,&y);
if(y==m)printf("%lld\n",move(x,y,0));
else{
ll va=cal(x,y);
printf("%lld\n",va);
move(x,y,va);
}
}
return 0;
}