一直忘了发,现在赶快补
用权值线段树维护有人的位置,动态开点省空间
多加的人用个vector存下来就可以了
#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=300000+10;
int n,m,q,N;
std::vector<ll> V[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
#define Mid ((l+r)>>1)
#define lson l,Mid
#define rson Mid+1,r
struct Segment_Tree{
int sum[MAXN<<5],lc[MAXN<<5],rc[MAXN<<5],root[MAXN<<5],cnt;
inline void Update(int &rt,int l,int r,int ps)
{
if(!rt)rt=++cnt;
sum[rt]++;
if(l==r)return ;
else
{
if(ps<=Mid)Update(lc[rt],lson,ps);
else Update(rc[rt],rson,ps);
}
}
inline int Query(int rt,int l,int r,int k)
{
if(l==r)return l;
else
{
int t=Mid-l+1-sum[lc[rt]];
if(t>=k)return Query(lc[rt],lson,k);
else return Query(rc[rt],rson,k-t);
}
}
};
Segment_Tree T;
#undef Mid
#undef lson
#undef rson
int main()
{
freopen("phalanx.in","r",stdin);
freopen("phalanx.out","w",stdout);
read(n);read(m);read(q);N=max(n,m)+q;
while(q--)
{
int x,y;read(x);read(y);
ll now;
if(y==m)
{
T.Update(T.root[n+1],1,N,now=T.Query(T.root[n+1],1,N,x));
now=now<=n?1ll*now*m:V[n+1][now-n-1];
V[n+1].push_back(now);
write(now,'\n');
}
else
{
T.Update(T.root[x],1,N,now=T.Query(T.root[x],1,N,y));
now=now<m?1ll*(x-1)*m+now:V[x][now-m];
V[n+1].push_back(now);
write(now,'\n');
T.Update(T.root[n+1],1,N,now=T.Query(T.root[n+1],1,N,x));
now=now<=n?1ll*now*m:V[n+1][now-n-1];
V[x].push_back(now);
}
}
return 0;
}