分块做法 fhq Orz
LCT做法 rzz Orz
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int,int> abcd;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
#define ND 300005
struct Splay{
struct node{
int size,val,sum; int rev;
node *p,*ch[2],*fat;
inline void setc(node *c,int d) { ch[d]=c; c->p=this; }
inline bool dir() { return p->ch[1]==this; }
inline void update() {
size=ch[0]->size+ch[1]->size+1;
sum=ch[0]->sum+ch[1]->sum+val;
}
inline void reverse() { rev^=1; swap(ch[0],ch[1]); }
inline void pushdown(node *null){
if (rev){
if (ch[0]!=null) ch[0]->reverse();
if (ch[1]!=null) ch[1]->reverse();
rev=0;
}
}
}*null,Mem[ND];
Splay() { null=Mem; null->p=null->ch[0]=null->ch[1]=null->fat=null; null->size=null->val=null->sum=0; }
inline void rot(node *x){
if (x==null || x->p==null) return ;
bool d=x->dir(); node *p=x->p;
if (p->p!=null) p->p->setc(x,p->dir()); else x->p=null;
p->setc(x->ch[d^1],d); x->setc(p,d^1); p->update(); x->update(); swap(x->fat,p->fat);
}
node *sta[1200005];
inline void splay(node *x){
int pnt=0; node *y=x;
while (y!=null) sta[++pnt]=y,y=y->p;
for (int i=pnt;i;i--) sta[i]->pushdown(null);
while (x->p!=null)
if (x->p->p==null)
rot(x);
else
x->dir()==x->p->dir()?(rot(x->p),rot(x)):(rot(x),rot(x));
}
inline node *Access(node *x){
node *y=null;
while (x!=null){
splay(x);
x->ch[1]->p=null; x->ch[1]->fat=x;
x->setc(y,1); y->fat=null;
x->update();
y=x; x=x->fat;
}
return y;
}
inline void Link(node *x,node *y){
if (Jud(x,y)) return;
Access(x)->reverse();
splay(x);
x->fat=y;
Access(x);
}
inline void Cut(node *x){
Access(x); splay(x);
x->ch[0]->p=null; x->ch[0]=null;
x->fat=null; x->update();
}
inline void Cut(node *x,node *y){
Access(x)->reverse();
Cut(y);
}
inline node *Root(node *x){
Access(x); splay(x); node *y=x;
while (y->ch[0]!=null) y->pushdown(null),y=y->ch[0];
return y;
}
inline bool Jud(node *x,node *y){
return Root(x)==Root(y);
}
inline int Query(node *x,node *y){
Access(x)->reverse();
return Access(y)->sum;
}
inline void Change(node *x,int v){
Access(x); splay(x);
x->val=v; x->update();
}
}LCT;
const int N=150005;
int icnt,sx[N<<1];
int cnt[N<<1];
inline int Bin(int x){
return lower_bound(sx+1,sx+icnt+1,x)-sx;
}
int n,L,a[N],b[N];
int Q,x[N],y[N];
Splay::node *pos[ND];
inline void Init(){
for (int i=1;i<=icnt;i++){
pos[i]=LCT.Mem+i;
pos[i]->p=pos[i]->ch[0]=pos[i]->ch[1]=pos[i]->fat=LCT.null;
pos[i]->val=pos[i]->sum=0; pos[i]->size=1;
}
}
int main()
{
int next,ans;
freopen("elephants.in","r",stdin);
freopen("elephants.out","w",stdout);
read(n); read(L); read(Q);
for (int i=1;i<=n;i++) read(a[i]),sx[++icnt]=a[i];
for (int i=1;i<=Q;i++) read(x[i]),x[i]++,read(y[i]),sx[++icnt]=y[i]; sx[++icnt]=1<<30;
sort(sx+1,sx+icnt+1); icnt=unique(sx+1,sx+icnt+1)-sx-1;
Init();
for (int i=1;i<=n;i++) cnt[Bin(a[i])]++;
for (int i=1;i<icnt;i++)
if (cnt[i])
{
next=upper_bound(sx+1,sx+icnt+1,sx[i]+L)-sx;
LCT.Change(pos[i],1),LCT.Link(pos[i],pos[next]);
}
else
LCT.Link(pos[i],pos[i+1]);
for (int i=1;i<=Q;i++)
{
int t=Bin(a[x[i]]);
cnt[t]--;
if (!cnt[t])
{
next=upper_bound(sx+1,sx+icnt+1,sx[t]+L)-sx;
LCT.Change(pos[t],0),LCT.Cut(pos[next],pos[t]),LCT.Link(pos[t],pos[t+1]);
}
t=Bin(a[x[i]]=y[i]);
cnt[t]++;
if (cnt[t]==1)
{
next=upper_bound(sx+1,sx+icnt+1,sx[t]+L)-sx;
LCT.Change(pos[t],1),LCT.Cut(pos[t+1],pos[t]),LCT.Link(pos[t],pos[next]);
}
ans=LCT.Query(pos[1],pos[icnt]);
printf("%d\n",ans);
}
return 0;
}