The main topic: to you n person, everyone has an ID, there is M inquiry, each time asked an interval [l,r], ask the interval inside how many ID is continuous (alone is also considered a)
Idea: do so many offline + tree-like array of topics, feel this thing is a template, 23333, anyway is defined right interval.
The key difficulty of this problem is how to define the ID is continuous. Each time we put a value in the interval to add (POS, 1), that is, all the relationship between the Pos~n is +1. Then add (Pos[id-1], 1) (same as id+1) if the id-1 appears before the POS, which means pos[ Id-1]~n started, the back of all and he is a team relationship, the next is simple.
//see if it explodes. int! array will not be one dimension less! //You must be careful about the conditions of the winning.#include <bits/stdc++.h>using namespacestd;#defineLL Long Long#defineAll (a) A.begin (), A.end ()#definePB Push_back#defineMk Make_pair#defineFi first#defineSe SecondConst intMAXN = 1e5 +5; Vector<pair<int,int> >Q[MAXN];intPOS[MAXN], A[MAXN], TREE[MAXN], ANS[MAXN];intN, M;inlineintLowbit (intx) {returnX &-x;}voidUpdateintXintval) { for(inti = x; I <= N; i + =lowbit (i)) {Tree[i]+=Val; }}intSumintx) { intAns =0; for(inti = x; i >0; I-=lowbit (i)) {ans+=Tree[i]; } returnans;}intMain () {intT CIN >>T; while(t--) {scanf ("%d%d", &n, &m); memset (POS,0,sizeof(POS)); for(inti =1; I <= N; i++) {scanf ("%d", A +i); Pos[a[i]]=i; Q[i].clear (); } for(inti =1; I <= m; i++){ intL, R; scanf"%d%d", &l, &R); Q[R].PB (MK (L, i)); } memset (Tree,0,sizeof(tree)); for(inti =1; I <= N; i++) {Update (I,1); if(A[i] +1<= n && pos[a[i] +1] < i) update (Pos[a[i] +1], -1); if(A[i]-1>=1&& Pos[a[i]-1] < i) update (Pos[a[i]-1], -1); intLen =q[i].size (); for(intj =0; J < Len; J + +) {pair<int,int> P =Q[i][j]; Ans[p.second]= SUM (i)-SUM (P.first-1); } } for(inti =1; I <= m; i++) {printf ("%d\n", Ans[i]); } } return 0;}View Code
The count of the interval relationship HDU 4638 offline + Tree array