简单题,想法比较巧妙,除了题目的英语实在看不懂...
题意:给出N个数,M个询问,问询问内有至少有几个连续段,连续段的定义是 I,I+1,I+2...即两两至多差1,。
范围: N。M<=10W
解法: 一开始考虑了莫队,果断TLE,发现可以离线+树状数组维护下增减序列即可NlogN求解。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
char c; int sgn; T bit=0.1;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
if(c==' '||c=='\n'){ ret*=sgn; return 1; }
while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
ret*=sgn;
return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int NN=100100;
int n,m;
int a[NN],c[NN],zh[NN];
bool vis[NN];
#define lowbit(x) (x&(-x))
void add(int i,int val){
for(;i<=n;i+=lowbit(i))c[i]+=val;
}
int getsum(int i){
int sum=0;
for(;i>0;i-=lowbit(i))sum+=c[i];
return sum;
}
struct query{
int l,r,idx;
bool operator < (const query x)const{
return l<x.l;
}
}q[NN];
int ans[NN];
void dele(int pos){
if(pos<=0)return;
int x=a[pos];
vis[x]=0;
add(pos,-1);
if(vis[x-1])add(zh[x-1],1);
if(vis[x+1])add(zh[x+1],1);
}
int main(){
tdata{
scanff(n);
scanff(m);
rep(i,1,n)vis[i]=0,c[i]=0,scanff(a[i]),zh[a[i]]=i;
rep(i,1,n){
int x=a[i];
vis[x]=1;
if(vis[x-1]&&vis[x+1])add(i,-1);
if(!vis[x-1]&&!vis[x+1])add(i,1);
}
rep(i,1,m)scanff(q[i].l),scanff(q[i].r),q[i].idx=i;
sort(q+1,q+1+m);
int ptr=1;
rep(i,1,n){
dele(i-1);
while(q[ptr].l==i)ans[q[ptr].idx]=getsum(q[ptr].r),ptr++;
}
rep(i,1,m)printf("%d\n",ans[i]);
}
return 0;
}