orz ljss……
我们发现,以i为两个端点中较小的那个的好的点对最多有两个(以i为左端点一个,右端点一个)
单调栈预处理所有好的点对然后二维数点即可
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
using namespace std;
#define MAXN 300010
#define MAXM 12000010
#define INF 1000000000
#define MOD 1000000007
#define eps 1e-8
#define ll long long
struct pt{
int r;
int l;
friend bool operator <(pt x,pt y){
return x.r<y.r;
}
};
int n,m,typ,la;
int a[MAXN];
int st[MAXN],tp;
int rt[MAXN];
int son[MAXM][2],siz[MAXM];
pt p[MAXN<<1];
int tot,TOT;
void change(int &x,int xx,int y,int z,int p){
x=++TOT;
memcpy(son[x],son[xx],sizeof(son[x]));
siz[x]=siz[xx]+1;
if(y==z){
return ;
}
int mid=y+z>>1;
if(p<=mid){
change(son[x][0],son[xx][0],y,mid,p);
}else{
change(son[x][1],son[xx][1],mid+1,z,p);
}
}
int ask(int x,int y,int z,int l,int r){
if(!x){
return 0;
}
if(y==l&&z==r){
return siz[x];
}
int mid=y+z>>1;
if(r<=mid){
return ask(son[x][0],y,mid,l,r);
}else if(l>mid){
return ask(son[x][1],mid+1,z,l,r);
}else{
return ask(son[x][0],y,mid,l,mid)+ask(son[x][1],mid+1,z,mid+1,r);
}
}
int main(){
int i,x,y;
scanf("%d%d%d",&n,&m,&typ);
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++){
while(tp&&a[st[tp]]<a[i]){
tp--;
}
if(tp&&a[st[tp]]>a[i]){
p[++tot].r=i;
p[tot].l=st[tp];
}
st[++tp]=i;
}
tp=0;
for(i=n;i;i--){
while(tp&&a[st[tp]]<a[i]){
tp--;
}
if(tp&&a[st[tp]]>=a[i]){
p[++tot].r=st[tp];
p[tot].l=i;
}
st[++tp]=i;
}
sort(p+1,p+tot+1);
int wzh=1;
for(i=1;i<=n;i++){
rt[i]=rt[i-1];
while(wzh<=tot&&p[wzh].r==i){
change(rt[i],rt[i],1,n,p[wzh].l);
wzh++;
}
}
while(m--){
scanf("%d%d",&x,&y);
if(typ){
x=(x+la-1)%n+1;
y=(y+la-1)%n+1;
if(x>y){
swap(x,y);
}
}
printf("%d\n",la=ask(rt[y],1,n,x,n));
}
return 0;
}
/*
3 3 0
3 3 2
2 2
1 3
3 3
*/