题意
https://www.luogu.org/problemnew/show/P3960
思路
30
30
30分暴力送温暖,但其实正解就从这里拓展出来。
最本质的想法无疑是优化纯模拟,先分析操作会对列队带来的变化。首先出去上厕所的人从它这一行删除,行末从最后一行补一个人。最后一行也对应的删除那个人,并且加入上厕所的人,都是删除并从后面加入。
显然线段树模拟删除操作,加入的人用
vector
\text{vector}
vector存着,但是线段树的空间貌似开不下,这个时候就需要动态开点了。对于每一次删除操作,如果没有这棵子树,那就添加这棵子树,每次最多开
log
n
\log_n
logn 层,空间复杂度就压缩到了
q
log
n
q\log_n
qlogn。
代码
#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define FOR(i,x,y) for(int i=(x),i##END=(y);i<=i##END;++i)
#define DOR(i,x,y) for(int i=(x),i##END=(y);i>=i##END;--i)
typedef long long LL;
using namespace std;
const int N=3e5+3;
const int NlogN=6e6+3;
struct node{int lson,rson,sum;};
struct SegmentTree
{
node nd[NlogN];
int rt[N],tot;
void build(){memset(rt,0,sizeof(rt));tot=0;}
void create(int &k,int sum){nd[k=++tot]=(node){0,0,sum};}
int query(int x,int y,int n){return query(rt[x],1,n,y);}
int query(int &k,int l,int r,int K)
{
if(!k)create(k,r-l+1);
nd[k].sum--;
if(l==r)return l;
int mid=l+r>>1,res;
if(!nd[k].lson)res=mid-l+1;
else res=nd[nd[k].lson].sum;
if(K<=res)return query(nd[k].lson,l,mid,K);
else return query(nd[k].rson,mid+1,r,K-res);
}
}ST;
vector<LL>h[N];
int n,m,q;
int main()
{
scanf("%d%d%d",&n,&m,&q);
ST.build();
FOR(i,1,q)
{
int x,y,t;LL las,tmp;
scanf("%d%d",&x,&y);
t=ST.query(0,x,n+q);
if(t<=n)las=1ll*t*m;
else las=h[0][t-n-1];
if(y!=m)
{
h[x].push_back(las);
t=ST.query(x,y,m+q);
if(t<m)tmp=1ll*(x-1)*m+t;
else tmp=h[x][t-m];
}
else tmp=las;
h[0].push_back(tmp);
printf("%lld\n",tmp);
}
return 0;
}