# HNOI2018D1T2 转盘

## 题解

ans=min(i+max(A[j]))+n-1,i=1~n,j>=i

## 贴代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

using namespace std;

const int maxn=2e5+5;

int tree[maxn*3],f[maxn*3],mi[maxn*3];
int a[maxn];
int i,j,k,l,m,n,o,x,y,p,an,ans;

void fd(int v,int l,int r,int x){
if (l==r){
if (tree[v]>=x) p=l;
} else{
int mid=(l+r)/2;
if (x<tree[v*2+1]) fd(v*2+1,mid+1,r,x); else fd(v*2,l,mid,x);
}
}
void find(int v,int l,int r,int x){
if (l==r){
if (tree[v]<x) an=min(an,l+x);
} else{
int mid=(l+r)/2;
if (x<tree[v*2+1]){
an=min(an,f[v]);
find(v*2+1,mid+1,r,x);
} else find(v*2,l,mid,x);
}
}
void maketree(int v,int l,int r){
if (l==r){
tree[v]=a[l]; mi[v]=a[l]+l;
} else{
int mid=(l+r)/2;
maketree(v*2,l,mid); maketree(v*2+1,mid+1,r);
an=1e6; find(v*2,l,mid,tree[v*2+1]);
p=l-1;
fd(v*2,l,mid,tree[v*2+1]);
f[v]=min(an,p+1+tree[v*2+1]);
mi[v]=min(f[v],mi[v*2+1]);
tree[v]=max(tree[v*2],tree[v*2+1]);
}
}
void change(int v,int l,int r,int x,int y){
if (l==r){
tree[v]=y; mi[v]=y+l;
} else{
int mid=(l+r)/2;
if (x<=mid) change(v*2,l,mid,x,y); else change(v*2+1,mid+1,r,x,y);
an=1e6; find(v*2,l,mid,tree[v*2+1]);
p=l-1;
fd(v*2,l,mid,tree[v*2+1]);
f[v]=min(an,p+1+tree[v*2+1]);
mi[v]=min(f[v],mi[v*2+1]);
tree[v]=max(tree[v*2],tree[v*2+1]);
}
}
int main(){
freopen("circle.in","r",stdin);
freopen("circle.out","w",stdout);
scanf("%d%d%d",&n,&m,&o);
fo(i,1,n){
scanf("%d",&a[i]);
a[i]=a[i]-i; a[i+n]=a[i]-n;
}
maketree(1,1,2*n);
ans=f[1]+n-1;
printf("%d\n",ans);
while (m--){
scanf("%d%d",&x,&y);
if (o==1){
x^=ans; y^=ans;
}
y=y-x;
change(1,1,2*n,x,y);
change(1,1,2*n,x+n,y-n);
ans=f[1]+n-1;
printf("%d\n",ans);
}
return 0;
}