"Link" I am the link, point me:)
Test instructions
Test instructions is the equivalent of asking you to change a position.
The maximum value is scanned from left to right. How many times this maximum value changes.
Exercises
Suppose we change the position of I, the following a[i] are changed to Q after the a[i]
We can figure out outright violence how many times the maximum on the left will change and how many times the maximum on the right will change.
So how to find it?
First in 1.. I-1 find the maximum value a[idx1]
This can be pretreated with the St table.
And then if a[i] This position is one of the updates to the maximum value of ans++.
Obviously A[i] to meet >A[IDX1]
If not, then a[i] is not the update of the maximum value of one time, ans does not change as good as still 0
Then we can preprocess it through a single scan to reach a[idx1] how many times the maximum value needs to be updated.
Then we can use the St table. Find out the nearest and bigger number A[IDX2]
(Just look at L..) Mid This section of the maximum value (obtained with St table) is not greater than key, is the word r = mid-1 otherwise L = Mid +1, so write a second point can find out A[IDX2]
The key here, if A[I]>A[IDX1], then key = A[i] Otherwise key = a[idx1];(because the nature of the increment is to be satisfied.
(Be careful not to think that Upper_bound....upper_bound is looking for the smallest and bigger number than the key a[idx2] ... For example, the result of a={2 4 3}|,set.upper_bound (2) is 3 rather than 4 closer to him ....
Then only ask for the A[IDX2] start to update how many times the maximum value is OK.
This can be maintained from the back to a monotonous queue. The length of the monotonic queue is the number of times the maximum value is updated.
ans+= the number of times the idx1 update was reached +idx2 to the last update.
Output ans is good.
Code
#include <bits/stdc++.h>using namespace Std;const int N = 1e5;int N,m,a[n+10],_size[n+10];int dl[n+10],h,t,step[n +10],dp[n+10][20];int MA (int i,int j) {if (a[i]>=a[j]) return i; return J;} int get_ma (int l,int r) {if (l>r) return 0; int len = log2 (r-l+1); Return Ma (dp[l][len],dp[r-(1<<len) +1][len]);} int Get_first_bigger (int q,int l) {int L = L,r = N,temp =-1; while (L <= r) {int mid = (l+r) >>1; if (A[get_ma (l,mid)]>q) {temp = mid; R = mid-1; }else L = mid + 1; } return temp;} int main () {//freopen ("D:\\rush.txt", "R", stdin); Freopen ("D:\\rush_out.txt", "w", stdout); int T; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&m); for (int i = 1;i <= n;i++) scanf ("%d", &a[i]); H = 1,t = 0; for (int i = N;i >= 1;i--) {while (h<=t && A[i]>=a[dl[t]]) t--; Dl[++t] = i; _size[i] = t-h+1; } step[1] = 1; int cur = 1; for (int i = 2;i <= n;i++) if (A[i]>a[cur]) {step[i] = step[cur]+1; cur = i; } for (int i = 0;i <= n;i++) dp[i][0] = i; for (int l = 1;l <= 17;l++) {for (int i = 1;i <= n;i++) {if ((i+ (1<<l)-1) >n) Brea K Dp[i][l] = MA (dp[i][l-1],dp[i+ (1<< (L-1))][l-1]); }} for (int i = 1;i <= m;i++) {int p,q; scanf ("%d%d", &p,&q); int idx = GET_MA (1,p-1); int key = Q,ans = 0; if (Q<=a[idx]) {key = A[idx]; ans =-1; } int idx2 = Get_first_bigger (key,p+1); if (Idx2==-1) {ans + = step[idx]+1; }else{ans + = step[idx]+1+_size[idx2]; } printf ("%d\n", ans); }} return 0;}
"Hdu 6406" Taotao picks Apples