After so long, I finally made an offline line segment tree .... I personally think that if the inquiry will affect each other, I will use the so-called "offline...
This question is considered to add numbers one by one from left to right. If a [I]-1 or a [I] + 1 is added, the total number of segments remains unchanged. If both ends are merged, the number of segments is reduced by one. If none are added, adding a [I] is equivalent to adding a new segment.
Process all requests from the right endpoint from small to large (a large range, the added elements will affect the small range); process the Edge Addition.
include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<fstream> #include<sstream> #include<bitset> #include<vector> #include<string> #include<cstdio> #include<cmath> #include<stack> #include<queue> #include<stack> #include<map> #include<set> #define FF(i, a, b) for(int i=a; i<b; i++) #define FD(i, a, b) for(int i=a; i>=b; i--) #define REP(i, n) for(int i=0; i<n; i++) #define CLR(a, b) memset(a, b, sizeof(a)) #define debug puts("**debug**") #define LL long long #define PB push_back using namespace std; const int maxn = 100001; int a[maxn], c[maxn], pos[maxn], ans[maxn]; int n, m; struct Q { int l, r, id; bool operator < (const Q rhs) const { return r < rhs.r; } }q[maxn]; int low(int x) {return x & (-x);} void add(int x, int v) { while(x <= n) { c[x] += v; x += low(x); } } int getsum(int x) { int ret = 0; while(x > 0) { ret += c[x]; x -= low(x); } return ret; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); FF(i, 1, n+1) scanf("%d", &a[i]), pos[a[i]] = i, c[i] = 0; REP(i, m) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i; sort(q, q+m); int j = 1; for(int i=0; i<m; ) { if(q[i].r < j) { ans[q[i].id] = getsum(q[i].r) - getsum(q[i].l-1); i++; } else { add(j, 1); if(a[j] > 1 && pos[a[j]-1] < j) add(pos[a[j]-1], -1); if(a[j] < n && pos[a[j]+1] < j) add(pos[a[j]+1], -1); j++; } } REP(i, m) printf("%d\n", ans[i]); } return 0; } #include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<fstream>#include<sstream>#include<bitset>#include<vector>#include<string>#include<cstdio>#include<cmath>#include<stack>#include<queue>#include<stack>#include<map>#include<set>#define FF(i, a, b) for(int i=a; i<b; i++)#define FD(i, a, b) for(int i=a; i>=b; i--)#define REP(i, n) for(int i=0; i<n; i++)#define CLR(a, b) memset(a, b, sizeof(a))#define debug puts("**debug**")#define LL long long#define PB push_backusing namespace std;const int maxn = 100001;int a[maxn], c[maxn], pos[maxn], ans[maxn];int n, m;struct Q{ int l, r, id; bool operator < (const Q rhs) const { return r < rhs.r; }}q[maxn];int low(int x) {return x & (-x);}void add(int x, int v){ while(x <= n) { c[x] += v; x += low(x); }}int getsum(int x){ int ret = 0; while(x > 0) { ret += c[x]; x -= low(x); } return ret;}int main(){ int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); FF(i, 1, n+1) scanf("%d", &a[i]), pos[a[i]] = i, c[i] = 0; REP(i, m) scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i; sort(q, q+m); int j = 1; for(int i=0; i<m; ) { if(q[i].r < j) { ans[q[i].id] = getsum(q[i].r) - getsum(q[i].l-1); i++; } else { add(j, 1); if(a[j] > 1 && pos[a[j]-1] < j) add(pos[a[j]-1], -1); if(a[j] < n && pos[a[j]+1] < j) add(pos[a[j]+1], -1); j++; } } REP(i, m) printf("%d\n", ans[i]); } return 0;}