Surprise
Title Description
Input Format
Output Format
Sample
Data range and tips
This problem is the road cancer, cancer of the tumor can not, you can come up with various O (n- . 5 ), O (n- . 4 ), O (n- . 3 ) so that O (n- 2 ) of the algorithm, but you will be T, cards and cards often not in the past, and positive solution is O (nlogn) of
About algorithm overtime I actually still want to talk about, because the exam when I zz, so it is best also played a n 4 , n fact, this question of 3 good fight, is the starting point and the size of the enumeration block this complexity is at most T27 bar
Then the n- 2 , then you need to carefully read the title, I did not read the question properly, I am not a man , I note that bold marked red line (when exams Why no one gave me bold .....) he said that on a per row, then we put the checkered how to shoot flat, we put it into an one-dimensional from two-dimensional, which is expressed as a [x] = y form, then think about, for an interval between the [L, R], LR is the range of the abscissa, i.e. Hiroko graticule, and a [L] to a [R] max and min is the ordinate range, i.e. FIG subsquares length, and that as long as the max-min = RL, this range is a valid range, so the cycle enumeration two L and R to reach the n- 2 , will be a pleasant T67, according to some Dalao say the n- 2 card a card T91
Then there is a positive solution, need to use just shoot flat squares of ideas, but also with two points, monotonic and barrel
If we think of binary words, ans [L, R] = ans [L, mid] + ans [mid + 1, R] + legitimate programs across the mid portion, about the first two, to count no recursion issue, the focus needs to be addressed is the third, we have to think about this across the mid contain what circumstances
1.min max are left
2.min max are on the right
3.min the left to the right max
4.min on the left to the right max
We look at four of the reduction scheme becomes a most value on the same side two. Value most in the opposite side
First we need to record at most intermediate values and mid + 1 to R interval within the interval values from the most to the mid intermediate L
A most value on the same side (the left side in each Example)
If most values are on the left, then you need to meet the max [l] -min [l] = rl, this and the n- 2 idea is the same, we move about the item, you get r = max [l] -min [l] + l, then we can enumerate the l, to determine each corresponding r, point of view is not eligible
If you are on the right side, the same lines enumerate the right point on it
Two most opposite side value (minimum value to maximum value on the left in the right Example)
I'm talking about before mentioned monotonic positive solution, then what is monotone it? This interval max min monotonic, since the left most value of the mid section are all started to push, and that from the mid to L, min does not strictly increase, max strictly does not drop, it is clear that this thing, that the same token, from the mid + 1 to strict Rmin does not rise, max strictly not fall, we assume that the left is the interval [L, mid], that what is lawful and right sections of it? We set two pointers r1, r2, to their initial positions placed at the mid + 1, we think before moving the pointer to what should be satisfied before the right may be referred interval method, ei, not that min [r] > min [l] max [r]> max [l] Well, for this we can find the first max [l] is larger than max, since this monotony, and after the first is greater than max max [L ], we can find a last [l] large, this also due to a monotonic min after the last less than min [l], into an illegal range, which than two min so we use r1, r2 pointer find the first and last one, which is the middle legitimate program, he also needs to satisfy a condition of what was it? max [r] -min [l] = rl, or transposition, it can be deformed max [r] -r = min [l] -l, so both sides of the equation becomes two separate parts, we call this separate parts thrown into the bucket and let him carry out simple calculations of operation we can get the correct number of intervals, of course, we initially set the big left interval [L, mid], we now need to shrink it, that r1 and r2 are two pointers will then move it
If the maximum value on the left, the minimum value on the right, is the same idea, to find legitimate range on the left, move the right intervals on it
Of course, this problem can be reversed, that is the reverse, the code becomes shorter, the complexity of the case more logn
Note : Remember the tub empty, min [l] -l may be less than 0, the n-plus
1 #include<iostream> 2 #include<cstdio> 3 #define maxn 50010 4 #define ll long long 5 using namespace std; 6 int n,maxx,minn; 7 ll ans; 8 int a[maxn],mi[maxn],ma[maxn],tong[maxn*3]; 9 inline int read() 10 { 11 int e=0,f=1; char ch=getchar(); 12 while(ch<'0'||ch>'9') 13 { 14 if(ch=='-') f=-1; 15 ch=getchar(); 16 } 17 while(ch>='0'&&ch<='9') {e=(e<<3)+(e<<1)+(ch-48); ch=getchar();} 18 return e*f; 19 } 20 ll fz(int l,int r) 21 { 22 if(l==r) return 1; 23 ll da=0,sum=0; int mid=(l+r)>>1; 24 da=fz(l,mid)+fz(mid+1,r); 25 ma[mid]=mi[mid]=a[mid]; 26 for(int i=mid-1;i>=l;--i) {ma[i]=max(ma[i+1],a[i]); mi[i]=min(mi[i+1],a[i]);} 27 ma[mid+1]=mi[mid+1]=a[mid+1]; 28 for(int i=mid+2;i<=r;++i) {ma[i]=max(ma[i-1],a[i]); mi[i]=min(mi[i-1],a[i]);} 29 for(int i=mid;i>=l;--i)//都在左侧 30 { 31 int R=ma[i]-mi[i]+i; 32 if(R<=mid||R>r) continue; 33 if(ma[R]<ma[i]&&mi[R]>mi[i]) sum++; 34 } 35 for(int i=mid+1;i<=r;++i)//都在右侧 36 { 37 intL = I-mA [I] + mi The [I]; 38 is IF (L> = MID + . 1 || L <L) Continue ; 39 IF (mA [L] <mA [I] && mi The [L]> mi The [I ]) SUM ++ ; 40 } 41 is int R1 = MID + . 1 , R2 = MID + . 1 ; // minimum on the left and maximum on the right 42 is the while (R1 <= R & lt && mi the [R1]> mi the [L]) {Tong [n-+ mA [R1] -r1] ++; R1 ++ ;} 43 is the while (R2 <= R & lt && mA [R2] <mA [L]) {Tong [n-+ mA [R2] -R2] -; R2 ++ ;} 44 is for ( int L = L; L <= MID; ++ L) 45 { 46 is the while(r1>mid+1&&mi[r1-1]<mi[L]) {r1--; tong[n+ma[r1]-r1]--;} 47 while(r2>mid+1&&ma[r2-1]>ma[L]) {r2--; tong[n+ma[r2]-r2]++;} 48 if(tong[n+mi[L]-L]>0) sum+=tong[n+mi[L]-L]; 49 } 50 for(int i=mid+1;i<=r;++i) tong[n+ma[i]-i]=0; 51 r1=mid; r2=mid;//最大值在坐,最小值在右 52 while(r1>=l&&mi[r1]>mi[r]) {tong[n+ma[r1]+r1]++; r1--;} 53 while(r2>=l&&ma[r2]<ma[r]) {tong[n+ma[r2]+r2]--; r2--;} 54 for(int R=r;R>=mid+1;--R) 55 { 56 while(r1<mid&&mi[r1+1]<mi[R]) {r1++; tong[n+ma[r1]+r1]--;} 57 while(r2<mid&&ma[r2+1]>ma[R]) {r2++; tong[n+ma[r2]+r2]++;} 58 if(tong[n+mi[R]+R]>0) sum+=tong[n+mi[R]+R]; 59 } 60 for(int i=l;i<=mid;++i) tong[n+ma[i]+i]=0; 61 da+=sum; 62 //cout<<"from"<<l<<"to"<<r<<"sum="<<sum<<endl; 63 return da; 64 } 65 int main() 66 { 67 n=read(); 68 for(int i=1;i<=n;++i) {int o=read(),p=read(); a[o]=p;} 69 ans=fz(1,n); 70 printf("%lld\n",ans); 71 return 0; 72 }